73be7ccfe1f22b48b7388b5d738cef864f261485
[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, 2003, 2004
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
32   (require 'cl)
33   (defvar tool-bar-map))
34
35 (require 'gnus)
36 (require 'gnus-group)
37 (require 'gnus-spec)
38 (require 'gnus-range)
39 (require 'gnus-int)
40 (require 'gnus-undo)
41 (require 'gnus-util)
42 (require 'nnoo)
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 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
61 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
62 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
63
64 (defcustom gnus-kill-summary-on-exit t
65   "*If non-nil, kill the summary buffer when you exit from it.
66 If nil, the summary will become a \"*Dead Summary*\" buffer, and
67 it will be killed sometime later."
68   :group 'gnus-summary-exit
69   :type 'boolean)
70
71 (defcustom gnus-fetch-old-headers nil
72   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
73 If an unread article in the group refers to an older, already read (or
74 just marked as read) article, the old article will not normally be
75 displayed in the Summary buffer.  If this variable is non-nil, Gnus
76 will attempt to grab the headers to the old articles, and thereby
77 build complete threads.  If it has the value `some', only enough
78 headers to connect otherwise loose threads will be displayed.  This
79 variable can also be a number.  In that case, no more than that number
80 of old headers will be fetched.  If it has the value `invisible', all
81 old headers will be fetched, but none will be displayed.
82
83 The server has to support NOV for any of this to work."
84   :group 'gnus-thread
85   :type '(choice (const :tag "off" nil)
86                  (const some)
87                  number
88                  (sexp :menu-tag "other" t)))
89
90 (defcustom gnus-refer-thread-limit 200
91   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
92 If t, fetch all the available old headers."
93   :group 'gnus-thread
94   :type '(choice number
95                  (sexp :menu-tag "other" t)))
96
97 (defcustom gnus-summary-make-false-root 'adopt
98   "*nil means that Gnus won't gather loose threads.
99 If the root of a thread has expired or been read in a previous
100 session, the information necessary to build a complete thread has been
101 lost.  Instead of having many small sub-threads from this original thread
102 scattered all over the summary buffer, Gnus can gather them.
103
104 If non-nil, Gnus will try to gather all loose sub-threads from an
105 original thread into one large thread.
106
107 If this variable is non-nil, it should be one of `none', `adopt',
108 `dummy' or `empty'.
109
110 If this variable is `none', Gnus will not make a false root, but just
111 present the sub-threads after another.
112 If this variable is `dummy', Gnus will create a dummy root that will
113 have all the sub-threads as children.
114 If this variable is `adopt', Gnus will make one of the \"children\"
115 the parent and mark all the step-children as such.
116 If this variable is `empty', the \"children\" are printed with empty
117 subject fields.  (Or rather, they will be printed with a string
118 given by the `gnus-summary-same-subject' variable.)"
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const none)
122                  (const dummy)
123                  (const adopt)
124                  (const empty)))
125
126 (defcustom gnus-summary-make-false-root-always nil
127   "Always make a false dummy root."
128   :group 'gnus-thread
129   :type 'boolean)
130
131 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
132   "*A regexp to match subjects to be excluded from loose thread gathering.
133 As loose thread gathering is done on subjects only, that means that
134 there can be many false gatherings performed.  By rooting out certain
135 common subjects, gathering might become saner."
136   :group 'gnus-thread
137   :type 'regexp)
138
139 (defcustom gnus-summary-gather-subject-limit nil
140   "*Maximum length of subject comparisons when gathering loose threads.
141 Use nil to compare full subjects.  Setting this variable to a low
142 number will help gather threads that have been corrupted by
143 newsreaders chopping off subject lines, but it might also mean that
144 unrelated articles that have subject that happen to begin with the
145 same few characters will be incorrectly gathered.
146
147 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
148 comparing subjects."
149   :group 'gnus-thread
150   :type '(choice (const :tag "off" nil)
151                  (const fuzzy)
152                  (sexp :menu-tag "on" t)))
153
154 (defcustom gnus-simplify-subject-functions nil
155   "List of functions taking a string argument that simplify subjects.
156 The functions are applied recursively.
157
158 Useful functions to put in this list include:
159 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
160 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
161   :group 'gnus-thread
162   :type '(repeat function))
163
164 (defcustom gnus-simplify-ignored-prefixes nil
165   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  regexp))
169
170 (defcustom gnus-build-sparse-threads nil
171   "*If non-nil, fill in the gaps in threads.
172 If `some', only fill in the gaps that are needed to tie loose threads
173 together.  If `more', fill in all leaf nodes that Gnus can find.  If
174 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
175   :group 'gnus-thread
176   :type '(choice (const :tag "off" nil)
177                  (const some)
178                  (const more)
179                  (sexp :menu-tag "all" t)))
180
181 (defcustom gnus-summary-thread-gathering-function
182   'gnus-gather-threads-by-subject
183   "*Function used for gathering loose threads.
184 There are two pre-defined functions: `gnus-gather-threads-by-subject',
185 which only takes Subjects into consideration; and
186 `gnus-gather-threads-by-references', which compared the References
187 headers of the articles to find matches."
188   :group 'gnus-thread
189   :type '(radio (function-item gnus-gather-threads-by-subject)
190                 (function-item gnus-gather-threads-by-references)
191                 (function :tag "other")))
192
193 (defcustom gnus-summary-same-subject ""
194   "*String indicating that the current article has the same subject as the previous.
195 This variable will only be used if the value of
196 `gnus-summary-make-false-root' is `empty'."
197   :group 'gnus-summary-format
198   :type 'string)
199
200 (defcustom gnus-summary-goto-unread t
201   "*If t, many commands will go to the next unread article.
202 This applies to marking commands as well as other commands that
203 \"naturally\" select the next article, like, for instance, `SPC' at
204 the end of an article.
205
206 If nil, the marking commands do NOT go to the next unread article
207 \(they go to the next article instead).  If `never', commands that
208 usually go to the next unread article, will go to the next article,
209 whether it is read or not."
210   :group 'gnus-summary-marks
211   :link '(custom-manual "(gnus)Setting Marks")
212   :type '(choice (const :tag "off" nil)
213                  (const never)
214                  (sexp :menu-tag "on" t)))
215
216 (defcustom gnus-summary-default-score 0
217   "*Default article score level.
218 All scores generated by the score files will be added to this score.
219 If this variable is nil, scoring will be disabled."
220   :group 'gnus-score-default
221   :type '(choice (const :tag "disable")
222                  integer))
223
224 (defcustom gnus-summary-default-high-score 0
225   "*Default threshold for a high scored article.
226 An article will be highlighted as high scored if its score is greater
227 than this score."
228   :group 'gnus-score-default
229   :type 'integer)
230
231 (defcustom gnus-summary-default-low-score 0
232   "*Default threshold for a low scored article.
233 An article will be highlighted as low scored if its score is smaller
234 than this score."
235   :group 'gnus-score-default
236   :type 'integer)
237
238 (defcustom gnus-summary-zcore-fuzz 0
239   "*Fuzziness factor for the zcore in the summary buffer.
240 Articles with scores closer than this to `gnus-summary-default-score'
241 will not be marked."
242   :group 'gnus-summary-format
243   :type 'integer)
244
245 (defcustom gnus-simplify-subject-fuzzy-regexp nil
246   "*Strings to be removed when doing fuzzy matches.
247 This can either be a regular expression or list of regular expressions
248 that will be removed from subject strings if fuzzy subject
249 simplification is selected."
250   :group 'gnus-thread
251   :type '(repeat regexp))
252
253 (defcustom gnus-show-threads t
254   "*If non-nil, display threads in summary mode."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-subtree nil
259   "*If non-nil, hide all threads initially.
260 This can be a predicate specifier which says which threads to hide.
261 If threads are hidden, you have to run the command
262 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
263 to expose hidden threads."
264   :group 'gnus-thread
265   :type '(radio (sexp :format "Non-nil\n"
266                       :match (lambda (widget value)
267                                (not (or (consp value) (functionp value))))
268                       :value t)
269                 (const nil)
270                 (sexp :tag "Predicate specifier" :size 0)))
271
272 (defcustom gnus-thread-hide-killed t
273   "*If non-nil, hide killed threads automatically."
274   :group 'gnus-thread
275   :type 'boolean)
276
277 (defcustom gnus-thread-ignore-subject t
278   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
279 If nil, articles that have different subjects from their parents will
280 start separate threads."
281   :group 'gnus-thread
282   :type 'boolean)
283
284 (defcustom gnus-thread-operation-ignore-subject t
285   "*If non-nil, subjects will be ignored when doing thread commands.
286 This affects commands like `gnus-summary-kill-thread' and
287 `gnus-summary-lower-thread'.
288
289 If this variable is nil, articles in the same thread with different
290 subjects will not be included in the operation in question.  If this
291 variable is `fuzzy', only articles that have subjects that are fuzzily
292 equal will be included."
293   :group 'gnus-thread
294   :type '(choice (const :tag "off" nil)
295                  (const fuzzy)
296                  (sexp :tag "on" t)))
297
298 (defcustom gnus-thread-indent-level 4
299   "*Number that says how much each sub-thread should be indented."
300   :group 'gnus-thread
301   :type 'integer)
302
303 (defcustom gnus-auto-extend-newsgroup t
304   "*If non-nil, extend newsgroup forward and backward when requested."
305   :group 'gnus-summary-choose
306   :type 'boolean)
307
308 (defcustom gnus-auto-select-first t
309   "*If non-nil, select the article under point.
310 Which article this is is controlled by the `gnus-auto-select-subject'
311 variable.
312
313 If you want to prevent automatic selection of articles in some
314 newsgroups, set the variable to nil in `gnus-select-group-hook'."
315   :group 'gnus-group-select
316   :type '(choice (const :tag "none" nil)
317                  (sexp :menu-tag "first" t)))
318
319 (defcustom gnus-auto-select-subject 'unread
320   "*Says what subject to place under point when entering a group.
321
322 This variable can either be the symbols `first' (place point on the
323 first subject), `unread' (place point on the subject line of the first
324 unread article), `best' (place point on the subject line of the
325 higest-scored article), `unseen' (place point on the subject line of
326 the first unseen article), 'unseen-or-unread' (place point on the subject
327 line of the first unseen article or, if all article have been seen, on the
328 subject line of the first unread article), or a function to be called to
329 place point on some subject line."
330   :group 'gnus-group-select
331   :type '(choice (const best)
332                  (const unread)
333                  (const first)
334                  (const unseen)
335                  (const unseen-or-unread)))
336
337 (defcustom gnus-dont-select-after-jump-to-other-group nil
338   "If non-nil, don't select the first unread article after entering the
339 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
340 it is depend on the value of `gnus-auto-select-first' whether to select
341 or not."
342   :group 'gnus-group-select
343   :type 'boolean)
344
345 (defcustom gnus-auto-select-next t
346   "*If non-nil, offer to go to the next group from the end of the previous.
347 If the value is t and the next newsgroup is empty, Gnus will exit
348 summary mode and go back to group mode.  If the value is neither nil
349 nor t, Gnus will select the following unread newsgroup.  In
350 particular, if the value is the symbol `quietly', the next unread
351 newsgroup will be selected without any confirmation, and if it is
352 `almost-quietly', the next group will be selected without any
353 confirmation if you are located on the last article in the group.
354 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
355 will go to the next group without confirmation."
356   :group 'gnus-summary-maneuvering
357   :type '(choice (const :tag "off" nil)
358                  (const quietly)
359                  (const almost-quietly)
360                  (const slightly-quietly)
361                  (sexp :menu-tag "on" t)))
362
363 (defcustom gnus-auto-select-same nil
364   "*If non-nil, select the next article with the same subject.
365 If there are no more articles with the same subject, go to
366 the first unread article."
367   :group 'gnus-summary-maneuvering
368   :type 'boolean)
369
370 (defcustom gnus-auto-goto-ignores 'unfetched
371   "*Says how to handle unfetched articles when maneuvering.
372
373 This variable can either be the symbols nil (maneuver to any
374 article), `undownloaded' (maneuvering while unplugged ignores articles
375 that have not been fetched), `always-undownloaded' (maneuvering always
376 ignores articles that have not been fetched), `unfetched' (maneuvering
377 ignores articles whose headers have not been fetched).
378
379 NOTE: The list of unfetched articles will always be nil when plugged
380 and, when unplugged, a subset of the undownloaded article list."
381   :group 'gnus-summary-maneuvering
382   :type '(choice (const :tag "None" nil)
383                  (const :tag "Undownloaded when unplugged" undownloaded)
384                  (const :tag "Undownloaded" always-undownloaded)
385                  (const :tag "Unfetched" unfetched)))
386
387 (defcustom gnus-summary-check-current nil
388   "*If non-nil, consider the current article when moving.
389 The \"unread\" movement commands will stay on the same line if the
390 current article is unread."
391   :group 'gnus-summary-maneuvering
392   :type 'boolean)
393
394 (defcustom gnus-auto-center-summary t
395   "*If non-nil, always center the current summary buffer.
396 In particular, if `vertical' do only vertical recentering.  If non-nil
397 and non-`vertical', do both horizontal and vertical recentering."
398   :group 'gnus-summary-maneuvering
399   :type '(choice (const :tag "none" nil)
400                  (const vertical)
401                  (integer :tag "height")
402                  (sexp :menu-tag "both" t)))
403
404 (defvar gnus-auto-center-group t
405   "*If non-nil, always center the group buffer.")
406
407 (defcustom gnus-show-all-headers nil
408   "*If non-nil, don't hide any headers."
409   :group 'gnus-article-hiding
410   :group 'gnus-article-headers
411   :type 'boolean)
412
413 (defcustom gnus-summary-ignore-duplicates nil
414   "*If non-nil, ignore articles with identical Message-ID headers."
415   :group 'gnus-summary
416   :type 'boolean)
417
418 (defcustom gnus-single-article-buffer t
419   "*If non-nil, display all articles in the same buffer.
420 If nil, each group will get its own article buffer."
421   :group 'gnus-article-various
422   :type 'boolean)
423
424 (defcustom gnus-break-pages t
425   "*If non-nil, do page breaking on articles.
426 The page delimiter is specified by the `gnus-page-delimiter'
427 variable."
428   :group 'gnus-article-various
429   :type 'boolean)
430
431 (defcustom gnus-show-mime t
432   "*If non-nil, do mime processing of articles.
433 The articles will simply be fed to the function given by
434 `gnus-article-display-method-for-mime'."
435   :group 'gnus-article-mime
436   :type 'boolean)
437
438 (defcustom gnus-move-split-methods nil
439   "*Variable used to suggest where articles are to be moved to.
440 It uses the same syntax as the `gnus-split-methods' variable.
441 However, whereas `gnus-split-methods' specifies file names as targets,
442 this variable specifies group names."
443   :group 'gnus-summary-mail
444   :type '(repeat (choice (list :value (fun) function)
445                          (cons :value ("" "") regexp (repeat string))
446                          (sexp :value nil))))
447
448 (defcustom gnus-unread-mark ?\ ;;;Whitespace
449   "*Mark used for unread articles."
450   :group 'gnus-summary-marks
451   :type 'character)
452
453 (defcustom gnus-ticked-mark ?!
454   "*Mark used for ticked articles."
455   :group 'gnus-summary-marks
456   :type 'character)
457
458 (defcustom gnus-dormant-mark ??
459   "*Mark used for dormant articles."
460   :group 'gnus-summary-marks
461   :type 'character)
462
463 (defcustom gnus-del-mark ?r
464   "*Mark used for del'd articles."
465   :group 'gnus-summary-marks
466   :type 'character)
467
468 (defcustom gnus-read-mark ?R
469   "*Mark used for read articles."
470   :group 'gnus-summary-marks
471   :type 'character)
472
473 (defcustom gnus-expirable-mark ?E
474   "*Mark used for expirable articles."
475   :group 'gnus-summary-marks
476   :type 'character)
477
478 (defcustom gnus-killed-mark ?K
479   "*Mark used for killed articles."
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-spam-mark ?$
484   "*Mark used for spam articles."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-souped-mark ?F
489   "*Mark used for souped articles."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-kill-file-mark ?X
494   "*Mark used for articles killed by kill files."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-low-score-mark ?Y
499   "*Mark used for articles with a low score."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-catchup-mark ?C
504   "*Mark used for articles that are caught up."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-replied-mark ?A
509   "*Mark used for articles that have been replied to."
510   :group 'gnus-summary-marks
511   :type 'character)
512
513 (defcustom gnus-forwarded-mark ?F
514   "*Mark used for articles that have been forwarded."
515   :group 'gnus-summary-marks
516   :type 'character)
517
518 (defcustom gnus-recent-mark ?N
519   "*Mark used for articles that are recent."
520   :group 'gnus-summary-marks
521   :type 'character)
522
523 (defcustom gnus-cached-mark ?*
524   "*Mark used for articles that are in the cache."
525   :group 'gnus-summary-marks
526   :type 'character)
527
528 (defcustom gnus-saved-mark ?S
529   "*Mark used for articles that have been saved."
530   :group 'gnus-summary-marks
531   :type 'character)
532
533 (defcustom gnus-unseen-mark ?.
534   "*Mark used for articles that haven't been seen."
535   :group 'gnus-summary-marks
536   :type 'character)
537
538 (defcustom gnus-no-mark ?\ ;;;Whitespace
539   "*Mark used for articles that have no other secondary mark."
540   :group 'gnus-summary-marks
541   :type 'character)
542
543 (defcustom gnus-ancient-mark ?O
544   "*Mark used for ancient articles."
545   :group 'gnus-summary-marks
546   :type 'character)
547
548 (defcustom gnus-sparse-mark ?Q
549   "*Mark used for sparsely reffed articles."
550   :group 'gnus-summary-marks
551   :type 'character)
552
553 (defcustom gnus-canceled-mark ?G
554   "*Mark used for canceled articles."
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-duplicate-mark ?M
559   "*Mark used for duplicate articles."
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-undownloaded-mark ?-
564   "*Mark used for articles that weren't downloaded."
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-downloaded-mark ?+
569   "*Mark used for articles that were downloaded."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloadable-mark ?%
574   "*Mark used for articles that are to be downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-unsendable-mark ?=
579   "*Mark used for articles that won't be sent."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-score-over-mark ?+
584   "*Score mark used for articles with high scores."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-below-mark ?-
589   "*Score mark used for articles with low scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
594   "*There is no thread under the article."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-not-empty-thread-mark ?=
599   "*There is a thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-view-pseudo-asynchronously nil
604   "*If non-nil, Gnus will view pseudo-articles asynchronously."
605   :group 'gnus-extract-view
606   :type 'boolean)
607
608 (defcustom gnus-auto-expirable-marks
609   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
610         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
611         gnus-souped-mark gnus-duplicate-mark)
612   "*The list of marks converted into expiration if a group is auto-expirable."
613   :version "21.1"
614   :group 'gnus-summary
615   :type '(repeat character))
616
617 (defcustom gnus-inhibit-user-auto-expire t
618   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
619   :version "21.1"
620   :group 'gnus-summary
621   :type 'boolean)
622
623 (defcustom gnus-view-pseudos nil
624   "*If `automatic', pseudo-articles will be viewed automatically.
625 If `not-confirm', pseudos will be viewed automatically, and the user
626 will not be asked to confirm the command."
627   :group 'gnus-extract-view
628   :type '(choice (const :tag "off" nil)
629                  (const automatic)
630                  (const not-confirm)))
631
632 (defcustom gnus-view-pseudos-separately t
633   "*If non-nil, one pseudo-article will be created for each file to be viewed.
634 If nil, all files that use the same viewing command will be given as a
635 list of parameters to that command."
636   :group 'gnus-extract-view
637   :type 'boolean)
638
639 (defcustom gnus-insert-pseudo-articles t
640   "*If non-nil, insert pseudo-articles when decoding articles."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-summary-dummy-line-format
645   "   %(:                             :%) %S\n"
646   "*The format specification for the dummy roots in the summary buffer.
647 It works along the same lines as a normal formatting string,
648 with some simple extensions.
649
650 %S  The subject
651
652 General format specifiers can also be used.
653 See `(gnus)Formatting Variables'."
654   :link '(custom-manual "(gnus)Formatting Variables")
655   :group 'gnus-threading
656   :type 'string)
657
658 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
659   "*The format specification for the summary mode line.
660 It works along the same lines as a normal formatting string,
661 with some simple extensions:
662
663 %G  Group name
664 %p  Unprefixed group name
665 %A  Current article number
666 %z  Current article score
667 %V  Gnus version
668 %U  Number of unread articles in the group
669 %e  Number of unselected articles in the group
670 %Z  A string with unread/unselected article counts
671 %g  Shortish group name
672 %S  Subject of the current article
673 %u  User-defined spec
674 %s  Current score file name
675 %d  Number of dormant articles
676 %r  Number of articles that have been marked as read in this session
677 %E  Number of articles expunged by the score files"
678   :group 'gnus-summary-format
679   :type 'string)
680
681 (defcustom gnus-list-identifiers nil
682   "Regexp that matches list identifiers to be removed from subject.
683 This can also be a list of regexps."
684   :version "21.1"
685   :group 'gnus-summary-format
686   :group 'gnus-article-hiding
687   :type '(choice (const :tag "none" nil)
688                  (regexp :value ".*")
689                  (repeat :value (".*") regexp)))
690
691 (defcustom gnus-summary-mark-below 0
692   "*Mark all articles with a score below this variable as read.
693 This variable is local to each summary buffer and usually set by the
694 score file."
695   :group 'gnus-score-default
696   :type 'integer)
697
698 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
699   "*List of functions used for sorting articles in the summary buffer.
700
701 Each function takes two articles and returns non-nil if the first
702 article should be sorted before the other.  If you use more than one
703 function, the primary sort function should be the last.  You should
704 probably always include `gnus-article-sort-by-number' in the list of
705 sorting functions -- preferably first.  Also note that sorting by date
706 is often much slower than sorting by number, and the sorting order is
707 very similar.  (Sorting by date means sorting by the time the message
708 was sent, sorting by number means sorting by arrival time.)
709
710 Ready-made functions include `gnus-article-sort-by-number',
711 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
712 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
713 and `gnus-article-sort-by-score'.
714
715 When threading is turned on, the variable `gnus-thread-sort-functions'
716 controls how articles are sorted."
717   :group 'gnus-summary-sort
718   :type '(repeat (choice (function-item gnus-article-sort-by-number)
719                          (function-item gnus-article-sort-by-author)
720                          (function-item gnus-article-sort-by-subject)
721                          (function-item gnus-article-sort-by-date)
722                          (function-item gnus-article-sort-by-score)
723                          (function-item gnus-article-sort-by-random)
724                          (function :tag "other"))))
725
726 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
727   "*List of functions used for sorting threads in the summary buffer.
728 By default, threads are sorted by article number.
729
730 Each function takes two threads and returns non-nil if the first
731 thread should be sorted before the other.  If you use more than one
732 function, the primary sort function should be the last.  You should
733 probably always include `gnus-thread-sort-by-number' in the list of
734 sorting functions -- preferably first.  Also note that sorting by date
735 is often much slower than sorting by number, and the sorting order is
736 very similar.  (Sorting by date means sorting by the time the message
737 was sent, sorting by number means sorting by arrival time.)
738
739 Ready-made functions include `gnus-thread-sort-by-number',
740 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
741 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
742 `gnus-thread-sort-by-most-recent-number',
743 `gnus-thread-sort-by-most-recent-date',
744 `gnus-thread-sort-by-random', and
745 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
746
747 When threading is turned off, the variable
748 `gnus-article-sort-functions' controls how articles are sorted."
749   :group 'gnus-summary-sort
750   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
751                          (function-item gnus-thread-sort-by-author)
752                          (function-item gnus-thread-sort-by-subject)
753                          (function-item gnus-thread-sort-by-date)
754                          (function-item gnus-thread-sort-by-score)
755                          (function-item gnus-thread-sort-by-total-score)
756                          (function-item gnus-thread-sort-by-random)
757                          (function :tag "other"))))
758
759 (defcustom gnus-thread-score-function '+
760   "*Function used for calculating the total score of a thread.
761
762 The function is called with the scores of the article and each
763 subthread and should then return the score of the thread.
764
765 Some functions you can use are `+', `max', or `min'."
766   :group 'gnus-summary-sort
767   :type 'function)
768
769 (defcustom gnus-summary-expunge-below nil
770   "All articles that have a score less than this variable will be expunged.
771 This variable is local to the summary buffers."
772   :group 'gnus-score-default
773   :type '(choice (const :tag "off" nil)
774                  integer))
775
776 (defcustom gnus-thread-expunge-below nil
777   "All threads that have a total score less than this variable will be expunged.
778 See `gnus-thread-score-function' for en explanation of what a
779 \"thread score\" is.
780
781 This variable is local to the summary buffers."
782   :group 'gnus-threading
783   :group 'gnus-score-default
784   :type '(choice (const :tag "off" nil)
785                  integer))
786
787 (defcustom gnus-summary-mode-hook nil
788   "*A hook for Gnus summary mode.
789 This hook is run before any variables are set in the summary buffer."
790   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
791   :group 'gnus-summary-various
792   :type 'hook)
793
794 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
795 (when (featurep 'xemacs)
796   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
797   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
798   (add-hook 'gnus-summary-mode-hook
799             'gnus-xmas-switch-horizontal-scrollbar-off))
800
801 (defcustom gnus-summary-menu-hook nil
802   "*Hook run after the creation of the summary mode menu."
803   :group 'gnus-summary-visual
804   :type 'hook)
805
806 (defcustom gnus-summary-exit-hook nil
807   "*A hook called on exit from the summary buffer.
808 It will be called with point in the group buffer."
809   :group 'gnus-summary-exit
810   :type 'hook)
811
812 (defcustom gnus-summary-prepare-hook nil
813   "*A hook called after the summary buffer has been generated.
814 If you want to modify the summary buffer, you can use this hook."
815   :group 'gnus-summary-various
816   :type 'hook)
817
818 (defcustom gnus-summary-prepared-hook nil
819   "*A hook called as the last thing after the summary buffer has been generated."
820   :group 'gnus-summary-various
821   :type 'hook)
822
823 (defcustom gnus-summary-generate-hook nil
824   "*A hook run just before generating the summary buffer.
825 This hook is commonly used to customize threading variables and the
826 like."
827   :group 'gnus-summary-various
828   :type 'hook)
829
830 (defcustom gnus-select-group-hook nil
831   "*A hook called when a newsgroup is selected.
832
833 If you'd like to simplify subjects like the
834 `gnus-summary-next-same-subject' command does, you can use the
835 following hook:
836
837  (add-hook gnus-select-group-hook
838            (lambda ()
839              (mapcar (lambda (header)
840                        (mail-header-set-subject
841                         header
842                         (gnus-simplify-subject
843                          (mail-header-subject header) 're-only)))
844                      gnus-newsgroup-headers)))"
845   :group 'gnus-group-select
846   :type 'hook)
847
848 (defcustom gnus-select-article-hook nil
849   "*A hook called when an article is selected."
850   :group 'gnus-summary-choose
851   :options '(gnus-agent-fetch-selected-article)
852   :type 'hook)
853
854 (defcustom gnus-visual-mark-article-hook
855   (list 'gnus-highlight-selected-summary)
856   "*Hook run after selecting an article in the summary buffer.
857 It is meant to be used for highlighting the article in some way.  It
858 is not run if `gnus-visual' is nil."
859   :group 'gnus-summary-visual
860   :type 'hook)
861
862 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
863   "*A hook called before parsing the headers."
864   :group 'gnus-various
865   :type 'hook)
866
867 (defcustom gnus-exit-group-hook nil
868   "*A hook called when exiting summary mode.
869 This hook is not called from the non-updating exit commands like `Q'."
870   :group 'gnus-various
871   :type 'hook)
872
873 (defcustom gnus-summary-update-hook
874   (list 'gnus-summary-highlight-line)
875   "*A hook called when a summary line is changed.
876 The hook will not be called if `gnus-visual' is nil.
877
878 The default function `gnus-summary-highlight-line' will
879 highlight the line according to the `gnus-summary-highlight'
880 variable."
881   :group 'gnus-summary-visual
882   :type 'hook)
883
884 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
885   "*A hook called when an article is selected for the first time.
886 The hook is intended to mark an article as read (or unread)
887 automatically when it is selected."
888   :group 'gnus-summary-choose
889   :type 'hook)
890
891 (defcustom gnus-group-no-more-groups-hook nil
892   "*A hook run when returning to group mode having no more (unread) groups."
893   :group 'gnus-group-select
894   :type 'hook)
895
896 (defcustom gnus-ps-print-hook nil
897   "*A hook run before ps-printing something from Gnus."
898   :group 'gnus-summary
899   :type 'hook)
900
901 (defcustom gnus-summary-article-move-hook nil
902   "*A hook called after an article is moved, copied, respooled, or crossposted."
903   :group 'gnus-summary
904   :type 'hook)
905
906 (defcustom gnus-summary-article-delete-hook nil
907   "*A hook called after an article is deleted."
908   :group 'gnus-summary
909   :type 'hook)
910
911 (defcustom gnus-summary-article-expire-hook nil
912   "*A hook called after an article is expired."
913   :group 'gnus-summary
914   :type 'hook)
915
916 (defcustom gnus-summary-display-arrow
917   (and (fboundp 'display-graphic-p)
918        (display-graphic-p))
919   "*If non-nil, display an arrow highlighting the current article."
920   :version "21.1"
921   :group 'gnus-summary
922   :type 'boolean)
923
924 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
925   "Face used for highlighting the current article in the summary buffer."
926   :group 'gnus-summary-visual
927   :type 'face)
928
929 (defvar gnus-tmp-downloaded nil)
930
931 (defcustom gnus-summary-highlight
932   '(((eq mark gnus-canceled-mark)
933      . gnus-summary-cancelled-face)
934     ((and uncached (> score default-high))
935      . gnus-summary-high-undownloaded-face)
936     ((and uncached (< score default-low))
937      . gnus-summary-low-undownloaded-face)
938     (uncached
939      . gnus-summary-normal-undownloaded-face)
940     ((and (> score default-high)
941           (or (eq mark gnus-dormant-mark)
942               (eq mark gnus-ticked-mark)))
943      . gnus-summary-high-ticked-face)
944     ((and (< score default-low)
945           (or (eq mark gnus-dormant-mark)
946               (eq mark gnus-ticked-mark)))
947      . gnus-summary-low-ticked-face)
948     ((or (eq mark gnus-dormant-mark)
949          (eq mark gnus-ticked-mark))
950      . gnus-summary-normal-ticked-face)
951     ((and (> score default-high) (eq mark gnus-ancient-mark))
952      . gnus-summary-high-ancient-face)
953     ((and (< score default-low) (eq mark gnus-ancient-mark))
954      . gnus-summary-low-ancient-face)
955     ((eq mark gnus-ancient-mark)
956      . gnus-summary-normal-ancient-face)
957     ((and (> score default-high) (eq mark gnus-unread-mark))
958      . gnus-summary-high-unread-face)
959     ((and (< score default-low) (eq mark gnus-unread-mark))
960      . gnus-summary-low-unread-face)
961     ((eq mark gnus-unread-mark)
962      . gnus-summary-normal-unread-face)
963     ((> score default-high)
964      . gnus-summary-high-read-face)
965     ((< score default-low)
966      . gnus-summary-low-read-face)
967     (t
968      . gnus-summary-normal-read-face))
969   "*Controls the highlighting of summary buffer lines.
970
971 A list of (FORM . FACE) pairs.  When deciding how a a particular
972 summary line should be displayed, each form is evaluated.  The content
973 of the face field after the first true form is used.  You can change
974 how those summary lines are displayed, by editing the face field.
975
976 You can use the following variables in the FORM field.
977
978 score:        The article's score
979 default:      The default article score.
980 default-high: The default score for high scored articles.
981 default-low:  The default score for low scored articles.
982 below:        The score below which articles are automatically marked as read.
983 mark:         The article's mark.
984 uncached:     Non-nil if the article is uncached."
985   :group 'gnus-summary-visual
986   :type '(repeat (cons (sexp :tag "Form" nil)
987                        face)))
988
989 (defcustom gnus-alter-header-function nil
990   "Function called to allow alteration of article header structures.
991 The function is called with one parameter, the article header vector,
992 which it may alter in any way."
993   :type '(choice (const :tag "None" nil)
994                  function)
995   :group 'gnus-summary)
996
997 (defvar gnus-decode-encoded-word-function
998   (mime-find-field-decoder 'From 'nov)
999   "Variable that says which function should be used to decode a string with encoded words.")
1000
1001 (defcustom gnus-extra-headers '(To Newsgroups)
1002   "*Extra headers to parse."
1003   :version "21.1"
1004   :group 'gnus-summary
1005   :type '(repeat symbol))
1006
1007 (defcustom gnus-ignored-from-addresses
1008   (and user-mail-address (regexp-quote user-mail-address))
1009   "*Regexp of From headers that may be suppressed in favor of To headers."
1010   :version "21.1"
1011   :group 'gnus-summary
1012   :type 'regexp)
1013
1014 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1015   "List of charsets that should be ignored.
1016 When these charsets are used in the \"charset\" parameter, the
1017 default charset will be used instead."
1018   :version "21.1"
1019   :type '(repeat symbol)
1020   :group 'gnus-charset)
1021
1022 (gnus-define-group-parameter
1023  ignored-charsets
1024  :type list
1025  :function-document
1026  "Return the ignored charsets of GROUP."
1027  :variable gnus-group-ignored-charsets-alist
1028  :variable-default
1029  '(("alt\\.chinese\\.text" iso-8859-1))
1030  :variable-document
1031  "Alist of regexps (to match group names) and charsets that should be ignored.
1032 When these charsets are used in the \"charset\" parameter, the
1033 default charset will be used instead."
1034  :variable-group gnus-charset
1035  :variable-type '(repeat (cons (regexp :tag "Group")
1036                                (repeat symbol)))
1037  :parameter-type '(choice :tag "Ignored charsets"
1038                           :value nil
1039                           (repeat (symbol)))
1040  :parameter-document       "\
1041 List of charsets that should be ignored.
1042
1043 When these charsets are used in the \"charset\" parameter, the
1044 default charset will be used instead.")
1045
1046 (defcustom gnus-group-highlight-words-alist nil
1047   "Alist of group regexps and highlight regexps.
1048 This variable uses the same syntax as `gnus-emphasis-alist'."
1049   :version "21.1"
1050   :type '(repeat (cons (regexp :tag "Group")
1051                        (repeat (list (regexp :tag "Highlight regexp")
1052                                      (number :tag "Group for entire word" 0)
1053                                      (number :tag "Group for displayed part" 0)
1054                                      (symbol :tag "Face"
1055                                              gnus-emphasis-highlight-words)))))
1056   :group 'gnus-summary-visual)
1057
1058 (defcustom gnus-use-wheel nil
1059   "Use Intelli-mouse on summary movement"
1060   :type 'boolean
1061   :group 'gnus-summary-maneuvering)
1062
1063 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1064   "Amount to scroll messages by spinning the mouse wheel.
1065 This is actually a cons cell, where the first item is the amount to scroll
1066 on a normal wheel event, and the second is the amount to scroll when the
1067 wheel is moved with the shift key depressed."
1068   :type '(cons (integer :tag "Shift") integer)
1069   :group 'gnus-summary-maneuvering)
1070
1071 (defcustom gnus-wheel-edge-resistance 2
1072   "How hard it should be to change the current article
1073 by moving the mouse over the edge of the article window."
1074   :type 'integer
1075   :group 'gnus-summary-maneuvering)
1076
1077 (defcustom gnus-summary-show-article-charset-alist
1078   nil
1079   "Alist of number and charset.
1080 The article will be shown with the charset corresponding to the
1081 numbered argument.
1082 For example: ((1 . cn-gb-2312) (2 . big5))."
1083   :version "21.1"
1084   :type '(repeat (cons (number :tag "Argument" 1)
1085                        (symbol :tag "Charset")))
1086   :group 'gnus-charset)
1087
1088 (defcustom gnus-preserve-marks t
1089   "Whether marks are preserved when moving, copying and respooling messages."
1090   :version "21.1"
1091   :type 'boolean
1092   :group 'gnus-summary-marks)
1093
1094 (defcustom gnus-alter-articles-to-read-function nil
1095   "Function to be called to alter the list of articles to be selected."
1096   :type '(choice (const nil) function)
1097   :group 'gnus-summary)
1098
1099 (defcustom gnus-orphan-score nil
1100   "*All orphans get this score added.  Set in the score file."
1101   :group 'gnus-score-default
1102   :type '(choice (const nil)
1103                  integer))
1104
1105 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1106   "*A regexp to match MIME parts when saving multiple parts of a
1107 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1108 This regexp will be used by default when prompting the user for which
1109 type of files to save."
1110   :group 'gnus-summary
1111   :type 'regexp)
1112
1113 (defcustom gnus-read-all-available-headers nil
1114   "Whether Gnus should parse all headers made available to it.
1115 This is mostly relevant for slow back ends where the user may
1116 wish to widen the summary buffer to include all headers
1117 that were fetched.  Say, for nnultimate groups."
1118   :group 'gnus-summary
1119   :type '(choice boolean regexp))
1120
1121 (defcustom gnus-summary-muttprint-program "muttprint"
1122   "Command (and optional arguments) used to run Muttprint."
1123   :version "21.3"
1124   :group 'gnus-summary
1125   :type 'string)
1126
1127 (defcustom gnus-article-loose-mime nil
1128   "If non-nil, don't require MIME-Version header.
1129 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1130 supply the MIME-Version header or deliberately strip it From the mail.
1131 Set it to non-nil, Gnus will treat some articles as MIME even if
1132 the MIME-Version header is missed."
1133   :version "21.3"
1134   :type 'boolean
1135   :group 'gnus-article-mime)
1136
1137 (defcustom gnus-article-emulate-mime t
1138   "If non-nil, use MIME emulation for uuencode and the like.
1139 This means that Gnus will search message bodies for text that look
1140 like uuencoded bits, yEncoded bits, and so on, and present that using
1141 the normal Gnus MIME machinery."
1142   :type 'boolean
1143   :group 'gnus-article-mime)
1144
1145 ;;; Internal variables
1146
1147 (defvar gnus-summary-display-cache nil)
1148 (defvar gnus-article-mime-handles nil)
1149 (defvar gnus-article-decoded-p nil)
1150 (defvar gnus-article-charset nil)
1151 (defvar gnus-article-ignored-charsets nil)
1152 (defvar gnus-scores-exclude-files nil)
1153 (defvar gnus-page-broken nil)
1154
1155 (defvar gnus-original-article nil)
1156 (defvar gnus-article-internal-prepare-hook nil)
1157 (defvar gnus-newsgroup-process-stack nil)
1158
1159 (defvar gnus-thread-indent-array nil)
1160 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1161 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1162   "Function called to sort the articles within a thread after it has been gathered together.")
1163
1164 (defvar gnus-summary-save-parts-type-history nil)
1165 (defvar gnus-summary-save-parts-last-directory nil)
1166
1167 ;; Avoid highlighting in kill files.
1168 (defvar gnus-summary-inhibit-highlight nil)
1169 (defvar gnus-newsgroup-selected-overlay nil)
1170 (defvar gnus-inhibit-limiting nil)
1171 (defvar gnus-newsgroup-adaptive-score-file nil)
1172 (defvar gnus-current-score-file nil)
1173 (defvar gnus-current-move-group nil)
1174 (defvar gnus-current-copy-group nil)
1175 (defvar gnus-current-crosspost-group nil)
1176 (defvar gnus-newsgroup-display nil)
1177
1178 (defvar gnus-newsgroup-dependencies nil)
1179 (defvar gnus-newsgroup-adaptive nil)
1180 (defvar gnus-summary-display-article-function nil)
1181 (defvar gnus-summary-highlight-line-function nil
1182   "Function called after highlighting a summary line.")
1183
1184 (defvar gnus-summary-line-format-alist
1185   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1186     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1187     (?s gnus-tmp-subject-or-nil ?s)
1188     (?n gnus-tmp-name ?s)
1189     (?A (std11-address-string
1190          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1191     (?a (or (std11-full-name-string
1192              (car (mime-entity-read-field gnus-tmp-header 'From)))
1193             gnus-tmp-from) ?s)
1194     (?F gnus-tmp-from ?s)
1195     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1196     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1197     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1198     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1199     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1200     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1201     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1202     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1203     (?L gnus-tmp-lines ?s)
1204     (?O gnus-tmp-downloaded ?c)
1205     (?I gnus-tmp-indentation ?s)
1206     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1207     (?R gnus-tmp-replied ?c)
1208     (?\[ gnus-tmp-opening-bracket ?c)
1209     (?\] gnus-tmp-closing-bracket ?c)
1210     (?\> (make-string gnus-tmp-level ? ) ?s)
1211     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1212     (?i gnus-tmp-score ?d)
1213     (?z gnus-tmp-score-char ?c)
1214     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1215     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1216     (?U gnus-tmp-unread ?c)
1217     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1218         ?s)
1219     (?t (gnus-summary-number-of-articles-in-thread
1220          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1221         ?d)
1222     (?e (gnus-summary-number-of-articles-in-thread
1223          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1224         ?c)
1225     (?u gnus-tmp-user-defined ?s)
1226     (?P (gnus-pick-line-number) ?d)
1227     (?B gnus-tmp-thread-tree-header-string ?s)
1228     (user-date (gnus-user-date
1229                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1230   "An alist of format specifications that can appear in summary lines.
1231 These are paired with what variables they correspond with, along with
1232 the type of the variable (string, integer, character, etc).")
1233
1234 (defvar gnus-summary-dummy-line-format-alist
1235   `((?S gnus-tmp-subject ?s)
1236     (?N gnus-tmp-number ?d)
1237     (?u gnus-tmp-user-defined ?s)))
1238
1239 (defvar gnus-summary-mode-line-format-alist
1240   `((?G gnus-tmp-group-name ?s)
1241     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1242     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1243     (?A gnus-tmp-article-number ?d)
1244     (?Z gnus-tmp-unread-and-unselected ?s)
1245     (?V gnus-version ?s)
1246     (?U gnus-tmp-unread-and-unticked ?d)
1247     (?S gnus-tmp-subject ?s)
1248     (?e gnus-tmp-unselected ?d)
1249     (?u gnus-tmp-user-defined ?s)
1250     (?d (length gnus-newsgroup-dormant) ?d)
1251     (?t (length gnus-newsgroup-marked) ?d)
1252     (?h (length gnus-newsgroup-spam-marked) ?d)
1253     (?r (length gnus-newsgroup-reads) ?d)
1254     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1255     (?E gnus-newsgroup-expunged-tally ?d)
1256     (?s (gnus-current-score-file-nondirectory) ?s)))
1257
1258 (defvar gnus-last-search-regexp nil
1259   "Default regexp for article search command.")
1260
1261 (defvar gnus-summary-search-article-matched-data nil
1262   "Last matched data of article search command.  It is the local variable
1263 in `gnus-article-buffer' which consists of the list of start position,
1264 end position and text.")
1265
1266 (defvar gnus-last-shell-command nil
1267   "Default shell command on article.")
1268
1269 (defvar gnus-newsgroup-agentized nil
1270   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1271 (defvar gnus-newsgroup-begin nil)
1272 (defvar gnus-newsgroup-end nil)
1273 (defvar gnus-newsgroup-last-rmail nil)
1274 (defvar gnus-newsgroup-last-mail nil)
1275 (defvar gnus-newsgroup-last-folder nil)
1276 (defvar gnus-newsgroup-last-file nil)
1277 (defvar gnus-newsgroup-auto-expire nil)
1278 (defvar gnus-newsgroup-active nil)
1279
1280 (defvar gnus-newsgroup-data nil)
1281 (defvar gnus-newsgroup-data-reverse nil)
1282 (defvar gnus-newsgroup-limit nil)
1283 (defvar gnus-newsgroup-limits nil)
1284 (defvar gnus-summary-use-undownloaded-faces nil)
1285
1286 (defvar gnus-newsgroup-unreads nil
1287   "Sorted list of unread articles in the current newsgroup.")
1288
1289 (defvar gnus-newsgroup-unselected nil
1290   "Sorted list of unselected unread articles in the current newsgroup.")
1291
1292 (defvar gnus-newsgroup-reads nil
1293   "Alist of read articles and article marks in the current newsgroup.")
1294
1295 (defvar gnus-newsgroup-expunged-tally nil)
1296
1297 (defvar gnus-newsgroup-marked nil
1298   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1299
1300 (defvar gnus-newsgroup-spam-marked nil
1301   "List of ranges of articles that have been marked as spam.")
1302
1303 (defvar gnus-newsgroup-killed nil
1304   "List of ranges of articles that have been through the scoring process.")
1305
1306 (defvar gnus-newsgroup-cached nil
1307   "Sorted list of articles that come from the article cache.")
1308
1309 (defvar gnus-newsgroup-saved nil
1310   "List of articles that have been saved.")
1311
1312 (defvar gnus-newsgroup-kill-headers nil)
1313
1314 (defvar gnus-newsgroup-replied nil
1315   "List of articles that have been replied to in the current newsgroup.")
1316
1317 (defvar gnus-newsgroup-forwarded nil
1318   "List of articles that have been forwarded in the current newsgroup.")
1319
1320 (defvar gnus-newsgroup-recent nil
1321   "List of articles that have are recent in the current newsgroup.")
1322
1323 (defvar gnus-newsgroup-expirable nil
1324   "Sorted list of articles in the current newsgroup that can be expired.")
1325
1326 (defvar gnus-newsgroup-processable nil
1327   "List of articles in the current newsgroup that can be processed.")
1328
1329 (defvar gnus-newsgroup-downloadable nil
1330   "Sorted list of articles in the current newsgroup that can be processed.")
1331
1332 (defvar gnus-newsgroup-unfetched nil
1333   "Sorted list of articles in the current newsgroup whose headers have
1334 not been fetched into the agent.
1335
1336 This list will always be a subset of gnus-newsgroup-undownloaded.")
1337
1338 (defvar gnus-newsgroup-undownloaded nil
1339   "List of articles in the current newsgroup that haven't been downloaded.")
1340
1341 (defvar gnus-newsgroup-unsendable nil
1342   "List of articles in the current newsgroup that won't be sent.")
1343
1344 (defvar gnus-newsgroup-bookmarks nil
1345   "List of articles in the current newsgroup that have bookmarks.")
1346
1347 (defvar gnus-newsgroup-dormant nil
1348   "Sorted list of dormant articles in the current newsgroup.")
1349
1350 (defvar gnus-newsgroup-unseen nil
1351   "List of unseen articles in the current newsgroup.")
1352
1353 (defvar gnus-newsgroup-seen nil
1354   "Range of seen articles in the current newsgroup.")
1355
1356 (defvar gnus-newsgroup-articles nil
1357   "List of articles in the current newsgroup.")
1358
1359 (defvar gnus-newsgroup-scored nil
1360   "List of scored articles in the current newsgroup.")
1361
1362 (defvar gnus-newsgroup-incorporated nil
1363   "List of incorporated articles in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-headers nil
1366   "List of article headers in the current newsgroup.")
1367
1368 (defvar gnus-newsgroup-threads nil)
1369
1370 (defvar gnus-newsgroup-prepared nil
1371   "Whether the current group has been prepared properly.")
1372
1373 (defvar gnus-newsgroup-ancient nil
1374   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1375
1376 (defvar gnus-newsgroup-sparse nil)
1377
1378 (defvar gnus-current-article nil)
1379 (defvar gnus-article-current nil)
1380 (defvar gnus-current-headers nil)
1381 (defvar gnus-have-all-headers nil)
1382 (defvar gnus-last-article nil)
1383 (defvar gnus-newsgroup-history nil)
1384 (defvar gnus-newsgroup-charset nil)
1385 (defvar gnus-newsgroup-ephemeral-charset nil)
1386 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1387
1388 (defvar gnus-article-before-search nil)
1389
1390 (defvar gnus-summary-local-variables
1391   '(gnus-newsgroup-name
1392     gnus-newsgroup-begin gnus-newsgroup-end
1393     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1394     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1395     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1396     gnus-newsgroup-unselected gnus-newsgroup-marked
1397     gnus-newsgroup-spam-marked
1398     gnus-newsgroup-reads gnus-newsgroup-saved
1399     gnus-newsgroup-replied gnus-newsgroup-forwarded
1400     gnus-newsgroup-recent
1401     gnus-newsgroup-expirable
1402     gnus-newsgroup-processable gnus-newsgroup-killed
1403     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1404     gnus-newsgroup-unfetched
1405     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1406     gnus-newsgroup-seen gnus-newsgroup-articles
1407     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1408     gnus-newsgroup-headers gnus-newsgroup-threads
1409     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1410     gnus-current-article gnus-current-headers gnus-have-all-headers
1411     gnus-last-article gnus-article-internal-prepare-hook
1412     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1413     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1414     gnus-thread-expunge-below
1415     gnus-score-alist gnus-current-score-file
1416     (gnus-summary-expunge-below . global)
1417     (gnus-summary-mark-below . global)
1418     (gnus-orphan-score . global)
1419     gnus-newsgroup-active gnus-scores-exclude-files
1420     gnus-newsgroup-history gnus-newsgroup-ancient
1421     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1422     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1423     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1424     (gnus-newsgroup-expunged-tally . 0)
1425     gnus-cache-removable-articles gnus-newsgroup-cached
1426     gnus-newsgroup-data gnus-newsgroup-data-reverse
1427     gnus-newsgroup-limit gnus-newsgroup-limits
1428     gnus-newsgroup-charset gnus-newsgroup-display
1429     gnus-summary-use-undownloaded-faces
1430     gnus-newsgroup-incorporated)
1431   "Variables that are buffer-local to the summary buffers.")
1432
1433 (defvar gnus-newsgroup-variables nil
1434   "A list of variables that have separate values in different newsgroups.
1435 A list of newsgroup (summary buffer) local variables, or cons of
1436 variables and their default values (when the default values are not
1437 nil), that should be made global while the summary buffer is active.
1438 These variables can be used to set variables in the group parameters
1439 while still allowing them to affect operations done in other
1440 buffers. For example:
1441
1442 \(setq gnus-newsgroup-variables
1443      '(message-use-followup-to
1444        (gnus-visible-headers .
1445          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1446 ")
1447
1448 ;; Byte-compiler warning.
1449 (eval-when-compile
1450   ;; Bind features so that require will believe that gnus-sum has
1451   ;; already been loaded (avoids infinite recursion)
1452   (let ((features (cons 'gnus-sum features)))
1453     ;; Several of the declarations in gnus-sum are needed to load the
1454     ;; following files. Right now, these definitions have been
1455     ;; compiled but not defined (evaluated).  We could either do a
1456     ;; eval-and-compile about all of the declarations or evaluate the
1457     ;; source file.
1458     (if (boundp 'gnus-newsgroup-variables)
1459         nil
1460       (load "gnus-sum.el" t t t))
1461     (require 'gnus)
1462     (require 'gnus-agent)
1463     (require 'gnus-art)))
1464
1465 ;; Subject simplification.
1466
1467 (defun gnus-simplify-whitespace (str)
1468   "Remove excessive whitespace from STR."
1469   ;; Multiple spaces.
1470   (while (string-match "[ \t][ \t]+" str)
1471     (setq str (concat (substring str 0 (match-beginning 0))
1472                         " "
1473                         (substring str (match-end 0)))))
1474   ;; Leading spaces.
1475   (when (string-match "^[ \t]+" str)
1476     (setq str (substring str (match-end 0))))
1477   ;; Trailing spaces.
1478   (when (string-match "[ \t]+$" str)
1479     (setq str (substring str 0 (match-beginning 0))))
1480   str)
1481
1482 (defun gnus-simplify-all-whitespace (str)
1483   "Remove all whitespace from STR."
1484   (while (string-match "[ \t\n]+" str)
1485     (setq str (replace-match "" nil nil str)))
1486   str)
1487
1488 (defsubst gnus-simplify-subject-re (subject)
1489   "Remove \"Re:\" from subject lines."
1490   (if (string-match message-subject-re-regexp subject)
1491       (substring subject (match-end 0))
1492     subject))
1493
1494 (defun gnus-simplify-subject (subject &optional re-only)
1495   "Remove `Re:' and words in parentheses.
1496 If RE-ONLY is non-nil, strip leading `Re:'s only."
1497   (let ((case-fold-search t))           ;Ignore case.
1498     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1499     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1500       (setq subject (substring subject (match-end 0))))
1501     ;; Remove uninteresting prefixes.
1502     (when (and (not re-only)
1503                gnus-simplify-ignored-prefixes
1504                (string-match gnus-simplify-ignored-prefixes subject))
1505       (setq subject (substring subject (match-end 0))))
1506     ;; Remove words in parentheses from end.
1507     (unless re-only
1508       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1509         (setq subject (substring subject 0 (match-beginning 0)))))
1510     ;; Return subject string.
1511     subject))
1512
1513 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1514 ;; all whitespace.
1515 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1516   (goto-char (point-min))
1517   (while (re-search-forward regexp nil t)
1518     (replace-match (or newtext ""))))
1519
1520 (defun gnus-simplify-buffer-fuzzy ()
1521   "Simplify string in the buffer fuzzily.
1522 The string in the accessible portion of the current buffer is simplified.
1523 It is assumed to be a single-line subject.
1524 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1525 matter is removed.  Additional things can be deleted by setting
1526 `gnus-simplify-subject-fuzzy-regexp'."
1527   (let ((case-fold-search t)
1528         (modified-tick))
1529     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1530
1531     (while (not (eq modified-tick (buffer-modified-tick)))
1532       (setq modified-tick (buffer-modified-tick))
1533       (cond
1534        ((listp gnus-simplify-subject-fuzzy-regexp)
1535         (mapcar 'gnus-simplify-buffer-fuzzy-step
1536                 gnus-simplify-subject-fuzzy-regexp))
1537        (gnus-simplify-subject-fuzzy-regexp
1538         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1539       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1540       (gnus-simplify-buffer-fuzzy-step
1541        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1542       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1543
1544     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1545     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1546     (gnus-simplify-buffer-fuzzy-step " $")
1547     (gnus-simplify-buffer-fuzzy-step "^ +")))
1548
1549 (defun gnus-simplify-subject-fuzzy (subject)
1550   "Simplify a subject string fuzzily.
1551 See `gnus-simplify-buffer-fuzzy' for details."
1552   (save-excursion
1553     (gnus-set-work-buffer)
1554     (let ((case-fold-search t))
1555       ;; Remove uninteresting prefixes.
1556       (when (and gnus-simplify-ignored-prefixes
1557                  (string-match gnus-simplify-ignored-prefixes subject))
1558         (setq subject (substring subject (match-end 0))))
1559       (insert subject)
1560       (inline (gnus-simplify-buffer-fuzzy))
1561       (buffer-string))))
1562
1563 (defsubst gnus-simplify-subject-fully (subject)
1564   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1565   (cond
1566    (gnus-simplify-subject-functions
1567     (gnus-map-function gnus-simplify-subject-functions subject))
1568    ((null gnus-summary-gather-subject-limit)
1569     (gnus-simplify-subject-re subject))
1570    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1571     (gnus-simplify-subject-fuzzy subject))
1572    ((numberp gnus-summary-gather-subject-limit)
1573     (gnus-limit-string (gnus-simplify-subject-re subject)
1574                        gnus-summary-gather-subject-limit))
1575    (t
1576     subject)))
1577
1578 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1579   "Check whether two subjects are equal.
1580 If optional argument SIMPLE-FIRST is t, first argument is already
1581 simplified."
1582   (cond
1583    ((null simple-first)
1584     (equal (gnus-simplify-subject-fully s1)
1585            (gnus-simplify-subject-fully s2)))
1586    (t
1587     (equal s1
1588            (gnus-simplify-subject-fully s2)))))
1589
1590 (defun gnus-summary-bubble-group ()
1591   "Increase the score of the current group.
1592 This is a handy function to add to `gnus-summary-exit-hook' to
1593 increase the score of each group you read."
1594   (gnus-group-add-score gnus-newsgroup-name))
1595
1596 \f
1597 ;;;
1598 ;;; Gnus summary mode
1599 ;;;
1600
1601 (put 'gnus-summary-mode 'mode-class 'special)
1602
1603 (defvar gnus-article-commands-menu)
1604
1605 ;; Non-orthogonal keys
1606
1607 (gnus-define-keys gnus-summary-mode-map
1608   " " gnus-summary-next-page
1609   "\177" gnus-summary-prev-page
1610   [delete] gnus-summary-prev-page
1611   [backspace] gnus-summary-prev-page
1612   "\r" gnus-summary-scroll-up
1613   "\M-\r" gnus-summary-scroll-down
1614   "n" gnus-summary-next-unread-article
1615   "p" gnus-summary-prev-unread-article
1616   "N" gnus-summary-next-article
1617   "P" gnus-summary-prev-article
1618   "\M-\C-n" gnus-summary-next-same-subject
1619   "\M-\C-p" gnus-summary-prev-same-subject
1620   "\M-n" gnus-summary-next-unread-subject
1621   "\M-p" gnus-summary-prev-unread-subject
1622   "." gnus-summary-first-unread-article
1623   "," gnus-summary-best-unread-article
1624   "\M-s" gnus-summary-search-article-forward
1625   "\M-r" gnus-summary-search-article-backward
1626   "<" gnus-summary-beginning-of-article
1627   ">" gnus-summary-end-of-article
1628   "j" gnus-summary-goto-article
1629   "^" gnus-summary-refer-parent-article
1630   "\M-^" gnus-summary-refer-article
1631   "u" gnus-summary-tick-article-forward
1632   "!" gnus-summary-tick-article-forward
1633   "U" gnus-summary-tick-article-backward
1634   "d" gnus-summary-mark-as-read-forward
1635   "D" gnus-summary-mark-as-read-backward
1636   "E" gnus-summary-mark-as-expirable
1637   "\M-u" gnus-summary-clear-mark-forward
1638   "\M-U" gnus-summary-clear-mark-backward
1639   "k" gnus-summary-kill-same-subject-and-select
1640   "\C-k" gnus-summary-kill-same-subject
1641   "\M-\C-k" gnus-summary-kill-thread
1642   "\M-\C-l" gnus-summary-lower-thread
1643   "e" gnus-summary-edit-article
1644   "#" gnus-summary-mark-as-processable
1645   "\M-#" gnus-summary-unmark-as-processable
1646   "\M-\C-t" gnus-summary-toggle-threads
1647   "\M-\C-s" gnus-summary-show-thread
1648   "\M-\C-h" gnus-summary-hide-thread
1649   "\M-\C-f" gnus-summary-next-thread
1650   "\M-\C-b" gnus-summary-prev-thread
1651   [(meta down)] gnus-summary-next-thread
1652   [(meta up)] gnus-summary-prev-thread
1653   "\M-\C-u" gnus-summary-up-thread
1654   "\M-\C-d" gnus-summary-down-thread
1655   "&" gnus-summary-execute-command
1656   "c" gnus-summary-catchup-and-exit
1657   "\C-w" gnus-summary-mark-region-as-read
1658   "\C-t" gnus-summary-toggle-truncation
1659   "?" gnus-summary-mark-as-dormant
1660   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1661   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1662   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1663   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1664   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1665   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1666   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1667   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1668   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1669   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1670   "=" gnus-summary-expand-window
1671   "\C-x\C-s" gnus-summary-reselect-current-group
1672   "\M-g" gnus-summary-rescan-group
1673   "w" gnus-summary-stop-page-breaking
1674   "\C-c\C-r" gnus-summary-caesar-message
1675   "\M-t" gnus-summary-toggle-mime
1676   "f" gnus-summary-followup
1677   "F" gnus-summary-followup-with-original
1678   "C" gnus-summary-cancel-article
1679   "r" gnus-summary-reply
1680   "R" gnus-summary-reply-with-original
1681   "\C-c\C-f" gnus-summary-mail-forward
1682   "o" gnus-summary-save-article
1683   "\C-o" gnus-summary-save-article-mail
1684   "|" gnus-summary-pipe-output
1685   "\M-k" gnus-summary-edit-local-kill
1686   "\M-K" gnus-summary-edit-global-kill
1687   ;; "V" gnus-version
1688   "\C-c\C-d" gnus-summary-describe-group
1689   "q" gnus-summary-exit
1690   "Q" gnus-summary-exit-no-update
1691   "\C-c\C-i" gnus-info-find-node
1692   gnus-mouse-2 gnus-mouse-pick-article
1693   "m" gnus-summary-mail-other-window
1694   "a" gnus-summary-post-news
1695   "i" gnus-summary-news-other-window
1696   "x" gnus-summary-limit-to-unread
1697   "s" gnus-summary-isearch-article
1698   "t" gnus-summary-toggle-header
1699   "g" gnus-summary-show-article
1700   "l" gnus-summary-goto-last-article
1701   "v" gnus-summary-preview-mime-message
1702   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1703   "\C-d" gnus-summary-enter-digest-group
1704   "\M-\C-d" gnus-summary-read-document
1705   "\M-\C-e" gnus-summary-edit-parameters
1706   "\M-\C-a" gnus-summary-customize-parameters
1707   "\C-c\C-b" gnus-bug
1708   "\C-c\C-n" gnus-namazu-search
1709   "*" gnus-cache-enter-article
1710   "\M-*" gnus-cache-remove-article
1711   "\M-&" gnus-summary-universal-argument
1712   "\C-l" gnus-recenter
1713   "I" gnus-summary-increase-score
1714   "L" gnus-summary-lower-score
1715   "\M-i" gnus-symbolic-argument
1716   "h" gnus-summary-select-article-buffer
1717
1718   "V" gnus-summary-score-map
1719   "X" gnus-uu-extract-map
1720   "S" gnus-summary-send-map)
1721
1722   ;; Sort of orthogonal keymap
1723 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1724   "t" gnus-summary-tick-article-forward
1725   "!" gnus-summary-tick-article-forward
1726   "d" gnus-summary-mark-as-read-forward
1727   "r" gnus-summary-mark-as-read-forward
1728   "c" gnus-summary-clear-mark-forward
1729   " " gnus-summary-clear-mark-forward
1730   "e" gnus-summary-mark-as-expirable
1731   "x" gnus-summary-mark-as-expirable
1732   "?" gnus-summary-mark-as-dormant
1733   "b" gnus-summary-set-bookmark
1734   "B" gnus-summary-remove-bookmark
1735   "#" gnus-summary-mark-as-processable
1736   "\M-#" gnus-summary-unmark-as-processable
1737   "S" gnus-summary-limit-include-expunged
1738   "C" gnus-summary-catchup
1739   "H" gnus-summary-catchup-to-here
1740   "h" gnus-summary-catchup-from-here
1741   "\C-c" gnus-summary-catchup-all
1742   "k" gnus-summary-kill-same-subject-and-select
1743   "K" gnus-summary-kill-same-subject
1744   "P" gnus-uu-mark-map)
1745
1746 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1747   "c" gnus-summary-clear-above
1748   "u" gnus-summary-tick-above
1749   "m" gnus-summary-mark-above
1750   "k" gnus-summary-kill-below)
1751
1752 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1753   "/" gnus-summary-limit-to-subject
1754   "n" gnus-summary-limit-to-articles
1755   "w" gnus-summary-pop-limit
1756   "s" gnus-summary-limit-to-subject
1757   "a" gnus-summary-limit-to-author
1758   "u" gnus-summary-limit-to-unread
1759   "m" gnus-summary-limit-to-marks
1760   "M" gnus-summary-limit-exclude-marks
1761   "v" gnus-summary-limit-to-score
1762   "*" gnus-summary-limit-include-cached
1763   "D" gnus-summary-limit-include-dormant
1764   "T" gnus-summary-limit-include-thread
1765   "d" gnus-summary-limit-exclude-dormant
1766   "t" gnus-summary-limit-to-age
1767   "." gnus-summary-limit-to-unseen
1768   "x" gnus-summary-limit-to-extra
1769   "p" gnus-summary-limit-to-display-predicate
1770   "E" gnus-summary-limit-include-expunged
1771   "c" gnus-summary-limit-exclude-childless-dormant
1772   "C" gnus-summary-limit-mark-excluded-as-read
1773   "o" gnus-summary-insert-old-articles
1774   "N" gnus-summary-insert-new-articles)
1775
1776 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1777   "n" gnus-summary-next-unread-article
1778   "p" gnus-summary-prev-unread-article
1779   "N" gnus-summary-next-article
1780   "P" gnus-summary-prev-article
1781   "\C-n" gnus-summary-next-same-subject
1782   "\C-p" gnus-summary-prev-same-subject
1783   "\M-n" gnus-summary-next-unread-subject
1784   "\M-p" gnus-summary-prev-unread-subject
1785   "f" gnus-summary-first-unread-article
1786   "b" gnus-summary-best-unread-article
1787   "j" gnus-summary-goto-article
1788   "g" gnus-summary-goto-subject
1789   "l" gnus-summary-goto-last-article
1790   "o" gnus-summary-pop-article)
1791
1792 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1793   "k" gnus-summary-kill-thread
1794   "l" gnus-summary-lower-thread
1795   "i" gnus-summary-raise-thread
1796   "T" gnus-summary-toggle-threads
1797   "t" gnus-summary-rethread-current
1798   "^" gnus-summary-reparent-thread
1799   "s" gnus-summary-show-thread
1800   "S" gnus-summary-show-all-threads
1801   "h" gnus-summary-hide-thread
1802   "H" gnus-summary-hide-all-threads
1803   "n" gnus-summary-next-thread
1804   "p" gnus-summary-prev-thread
1805   "u" gnus-summary-up-thread
1806   "o" gnus-summary-top-thread
1807   "d" gnus-summary-down-thread
1808   "#" gnus-uu-mark-thread
1809   "\M-#" gnus-uu-unmark-thread)
1810
1811 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1812   "g" gnus-summary-prepare
1813   "c" gnus-summary-insert-cached-articles
1814   "d" gnus-summary-insert-dormant-articles)
1815
1816 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1817   "c" gnus-summary-catchup-and-exit
1818   "C" gnus-summary-catchup-all-and-exit
1819   "E" gnus-summary-exit-no-update
1820   "J" gnus-summary-jump-to-other-group
1821   "Q" gnus-summary-exit
1822   "Z" gnus-summary-exit
1823   "n" gnus-summary-catchup-and-goto-next-group
1824   "R" gnus-summary-reselect-current-group
1825   "G" gnus-summary-rescan-group
1826   "N" gnus-summary-next-group
1827   "s" gnus-summary-save-newsrc
1828   "P" gnus-summary-prev-group)
1829
1830 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1831   " " gnus-summary-next-page
1832   "n" gnus-summary-next-page
1833   "\177" gnus-summary-prev-page
1834   [delete] gnus-summary-prev-page
1835   "p" gnus-summary-prev-page
1836   "\r" gnus-summary-scroll-up
1837   "\M-\r" gnus-summary-scroll-down
1838   "<" gnus-summary-beginning-of-article
1839   ">" gnus-summary-end-of-article
1840   "b" gnus-summary-beginning-of-article
1841   "e" gnus-summary-end-of-article
1842   "^" gnus-summary-refer-parent-article
1843   "r" gnus-summary-refer-parent-article
1844   "D" gnus-summary-enter-digest-group
1845   "R" gnus-summary-refer-references
1846   "T" gnus-summary-refer-thread
1847   "g" gnus-summary-show-article
1848   "s" gnus-summary-isearch-article
1849   "P" gnus-summary-print-article
1850   "M" gnus-mailing-list-insinuate
1851   "t" gnus-article-babel)
1852
1853 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1854   "b" gnus-article-add-buttons
1855   "B" gnus-article-add-buttons-to-head
1856   "o" gnus-article-treat-overstrike
1857   "e" gnus-article-emphasize
1858   "w" gnus-article-fill-cited-article
1859   "Q" gnus-article-fill-long-lines
1860   "C" gnus-article-capitalize-sentences
1861   "c" gnus-article-remove-cr
1862   "Z" gnus-article-decode-HZ
1863   "A" gnus-article-treat-ansi-sequences
1864   "h" gnus-article-wash-html
1865   "u" gnus-article-unsplit-urls
1866   "f" gnus-article-display-x-face
1867   "l" gnus-summary-stop-page-breaking
1868   "r" gnus-summary-caesar-message
1869   "m" gnus-summary-morse-message
1870   "t" gnus-summary-toggle-header
1871   "g" gnus-treat-smiley
1872   "v" gnus-summary-verbose-headers
1873   "m" gnus-summary-toggle-mime
1874   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1875   "p" gnus-article-verify-x-pgp-sig
1876   "d" gnus-article-treat-dumbquotes)
1877
1878 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1879   ;; mnemonic: deuglif*Y*
1880   "u" gnus-article-outlook-unwrap-lines
1881   "a" gnus-article-outlook-repair-attribution
1882   "c" gnus-article-outlook-rearrange-citation
1883   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1884
1885 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1886   "a" gnus-article-hide
1887   "h" gnus-article-hide-headers
1888   "b" gnus-article-hide-boring-headers
1889   "s" gnus-article-hide-signature
1890   "c" gnus-article-hide-citation
1891   "C" gnus-article-hide-citation-in-followups
1892   "l" gnus-article-hide-list-identifiers
1893   "B" gnus-article-strip-banner
1894   "P" gnus-article-hide-pem
1895   "\C-c" gnus-article-hide-citation-maybe)
1896
1897 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1898   "a" gnus-article-highlight
1899   "h" gnus-article-highlight-headers
1900   "c" gnus-article-highlight-citation
1901   "s" gnus-article-highlight-signature)
1902
1903 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1904   "f" gnus-article-treat-fold-headers
1905   "u" gnus-article-treat-unfold-headers
1906   "n" gnus-article-treat-fold-newsgroups)
1907
1908 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1909   "x" gnus-article-display-x-face
1910   "d" gnus-article-display-face
1911   "s" gnus-treat-smiley
1912   "D" gnus-article-remove-images
1913   "f" gnus-treat-from-picon
1914   "m" gnus-treat-mail-picon
1915   "n" gnus-treat-newsgroups-picon)
1916
1917 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1918   "z" gnus-article-date-ut
1919   "u" gnus-article-date-ut
1920   "l" gnus-article-date-local
1921   "p" gnus-article-date-english
1922   "e" gnus-article-date-lapsed
1923   "o" gnus-article-date-original
1924   "i" gnus-article-date-iso8601
1925   "s" gnus-article-date-user)
1926
1927 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1928   "t" gnus-article-remove-trailing-blank-lines
1929   "l" gnus-article-strip-leading-blank-lines
1930   "m" gnus-article-strip-multiple-blank-lines
1931   "a" gnus-article-strip-blank-lines
1932   "A" gnus-article-strip-all-blank-lines
1933   "s" gnus-article-strip-leading-space
1934   "e" gnus-article-strip-trailing-space
1935   "w" gnus-article-remove-leading-whitespace)
1936
1937 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1938   "v" gnus-version
1939   "f" gnus-summary-fetch-faq
1940   "d" gnus-summary-describe-group
1941   "h" gnus-summary-describe-briefly
1942   "i" gnus-info-find-node
1943   "c" gnus-group-fetch-charter
1944   "C" gnus-group-fetch-control)
1945
1946 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1947   "e" gnus-summary-expire-articles
1948   "\M-\C-e" gnus-summary-expire-articles-now
1949   "\177" gnus-summary-delete-article
1950   [delete] gnus-summary-delete-article
1951   [backspace] gnus-summary-delete-article
1952   "m" gnus-summary-move-article
1953   "r" gnus-summary-respool-article
1954   "w" gnus-summary-edit-article
1955   "c" gnus-summary-copy-article
1956   "B" gnus-summary-crosspost-article
1957   "q" gnus-summary-respool-query
1958   "t" gnus-summary-respool-trace
1959   "i" gnus-summary-import-article
1960   "I" gnus-summary-create-article
1961   "p" gnus-summary-article-posted-p)
1962
1963 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1964   "o" gnus-summary-save-article
1965   "m" gnus-summary-save-article-mail
1966   "F" gnus-summary-write-article-file
1967   "r" gnus-summary-save-article-rmail
1968   "f" gnus-summary-save-article-file
1969   "b" gnus-summary-save-article-body-file
1970   "h" gnus-summary-save-article-folder
1971   "v" gnus-summary-save-article-vm
1972   "p" gnus-summary-pipe-output
1973   "P" gnus-summary-muttprint
1974   "s" gnus-soup-add-article)
1975
1976 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1977   "b" gnus-summary-display-buttonized
1978   "m" gnus-summary-repair-multipart
1979   "v" gnus-article-view-part
1980   "o" gnus-article-save-part
1981   "c" gnus-article-copy-part
1982   "C" gnus-article-view-part-as-charset
1983   "e" gnus-article-view-part-externally
1984   "E" gnus-article-encrypt-body
1985   "i" gnus-article-inline-part
1986   "|" gnus-article-pipe-part)
1987
1988 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1989   "p" gnus-summary-mark-as-processable
1990   "u" gnus-summary-unmark-as-processable
1991   "U" gnus-summary-unmark-all-processable
1992   "v" gnus-uu-mark-over
1993   "s" gnus-uu-mark-series
1994   "r" gnus-uu-mark-region
1995   "g" gnus-uu-unmark-region
1996   "R" gnus-uu-mark-by-regexp
1997   "G" gnus-uu-unmark-by-regexp
1998   "t" gnus-uu-mark-thread
1999   "T" gnus-uu-unmark-thread
2000   "a" gnus-uu-mark-all
2001   "b" gnus-uu-mark-buffer
2002   "S" gnus-uu-mark-sparse
2003   "k" gnus-summary-kill-process-mark
2004   "y" gnus-summary-yank-process-mark
2005   "w" gnus-summary-save-process-mark
2006   "i" gnus-uu-invert-processable)
2007
2008 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2009   ;;"x" gnus-uu-extract-any
2010   "m" gnus-summary-save-parts
2011   "u" gnus-uu-decode-uu
2012   "U" gnus-uu-decode-uu-and-save
2013   "s" gnus-uu-decode-unshar
2014   "S" gnus-uu-decode-unshar-and-save
2015   "o" gnus-uu-decode-save
2016   "O" gnus-uu-decode-save
2017   "b" gnus-uu-decode-binhex
2018   "B" gnus-uu-decode-binhex
2019   "p" gnus-uu-decode-postscript
2020   "P" gnus-uu-decode-postscript-and-save)
2021
2022 (gnus-define-keys
2023     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2024   "u" gnus-uu-decode-uu-view
2025   "U" gnus-uu-decode-uu-and-save-view
2026   "s" gnus-uu-decode-unshar-view
2027   "S" gnus-uu-decode-unshar-and-save-view
2028   "o" gnus-uu-decode-save-view
2029   "O" gnus-uu-decode-save-view
2030   "b" gnus-uu-decode-binhex-view
2031   "B" gnus-uu-decode-binhex-view
2032   "p" gnus-uu-decode-postscript-view
2033   "P" gnus-uu-decode-postscript-and-save-view)
2034
2035 (defvar gnus-article-post-menu nil)
2036
2037 (defconst gnus-summary-menu-maxlen 20)
2038
2039 (defun gnus-summary-menu-split (menu)
2040   ;; If we have lots of elements, divide them into groups of 20
2041   ;; and make a pane (or submenu) for each one.
2042   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2043       (let ((menu menu) sublists next
2044             (i 1))
2045         (while menu
2046           ;; Pull off the next gnus-summary-menu-maxlen elements
2047           ;; and make them the next element of sublist.
2048           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2049           (if next
2050               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2051                       nil))
2052           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2053                                              (aref (car (last menu)) 0)) menu)
2054                                sublists))
2055           (setq i (1+ i))
2056           (setq menu next))
2057         (nreverse sublists))
2058     ;; Few elements--put them all in one pane.
2059     menu))
2060
2061 (defun gnus-summary-make-menu-bar ()
2062   (gnus-turn-off-edit-menu 'summary)
2063
2064   (unless (boundp 'gnus-summary-misc-menu)
2065
2066     (easy-menu-define
2067      gnus-summary-kill-menu gnus-summary-mode-map ""
2068      (cons
2069       "Score"
2070       (nconc
2071        (list
2072         ["Customize" gnus-score-customize t])
2073        (gnus-make-score-map 'increase)
2074        (gnus-make-score-map 'lower)
2075        '(("Mark"
2076           ["Kill below" gnus-summary-kill-below t]
2077           ["Mark above" gnus-summary-mark-above t]
2078           ["Tick above" gnus-summary-tick-above t]
2079           ["Clear above" gnus-summary-clear-above t])
2080          ["Current score" gnus-summary-current-score t]
2081          ["Set score" gnus-summary-set-score t]
2082          ["Switch current score file..." gnus-score-change-score-file t]
2083          ["Set mark below..." gnus-score-set-mark-below t]
2084          ["Set expunge below..." gnus-score-set-expunge-below t]
2085          ["Edit current score file" gnus-score-edit-current-scores t]
2086          ["Edit score file" gnus-score-edit-file t]
2087          ["Trace score" gnus-score-find-trace t]
2088          ["Find words" gnus-score-find-favourite-words t]
2089          ["Rescore buffer" gnus-summary-rescore t]
2090          ["Increase score..." gnus-summary-increase-score t]
2091          ["Lower score..." gnus-summary-lower-score t]))))
2092
2093     ;; Define both the Article menu in the summary buffer and the
2094     ;; equivalent Commands menu in the article buffer here for
2095     ;; consistency.
2096     (let ((innards
2097            `(("Hide"
2098               ["All" gnus-article-hide t]
2099               ["Headers" gnus-article-hide-headers t]
2100               ["Signature" gnus-article-hide-signature t]
2101               ["Citation" gnus-article-hide-citation t]
2102               ["List identifiers" gnus-article-hide-list-identifiers t]
2103               ["Banner" gnus-article-strip-banner t]
2104               ["Boring headers" gnus-article-hide-boring-headers t])
2105              ("Highlight"
2106               ["All" gnus-article-highlight t]
2107               ["Headers" gnus-article-highlight-headers t]
2108               ["Signature" gnus-article-highlight-signature t]
2109               ["Citation" gnus-article-highlight-citation t])
2110              ("Date"
2111               ["Local" gnus-article-date-local t]
2112               ["ISO8601" gnus-article-date-iso8601 t]
2113               ["UT" gnus-article-date-ut t]
2114               ["Original" gnus-article-date-original t]
2115               ["Lapsed" gnus-article-date-lapsed t]
2116               ["User-defined" gnus-article-date-user t])
2117              ("Display"
2118               ["Remove images" gnus-article-remove-images t]
2119               ["Toggle smiley" gnus-treat-smiley t]
2120               ["Show X-Face" gnus-article-display-x-face t]
2121               ["Show picons in From" gnus-treat-from-picon t]
2122               ["Show picons in mail headers" gnus-treat-mail-picon t]
2123               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2124               ("View as different encoding"
2125                ,@(gnus-summary-menu-split
2126                   (mapcar
2127                    (lambda (cs)
2128                      ;; Since easymenu under Emacs doesn't allow
2129                      ;; lambda forms for menu commands, we should
2130                      ;; provide intern'ed function symbols.
2131                      (let ((command (intern (format "\
2132 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2133                        (fset command
2134                              `(lambda ()
2135                                 (interactive)
2136                                 (let ((gnus-summary-show-article-charset-alist
2137                                        '((1 . ,cs))))
2138                                   (gnus-summary-show-article 1))))
2139                        `[,(symbol-name cs) ,command t]))
2140                    (sort (if (fboundp 'coding-system-list)
2141                              (coding-system-list)
2142                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2143                            )
2144                          'string<)))))
2145              ("Washing"
2146               ("Remove Blanks"
2147                ["Leading" gnus-article-strip-leading-blank-lines t]
2148                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2149                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2150                ["All of the above" gnus-article-strip-blank-lines t]
2151                ["All" gnus-article-strip-all-blank-lines t]
2152                ["Leading space" gnus-article-strip-leading-space t]
2153                ["Trailing space" gnus-article-strip-trailing-space t]
2154                ["Leading space in headers"
2155                 gnus-article-remove-leading-whitespace t])
2156               ["Overstrike" gnus-article-treat-overstrike t]
2157               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2158               ["Emphasis" gnus-article-emphasize t]
2159               ["Word wrap" gnus-article-fill-cited-article t]
2160               ["Fill long lines" gnus-article-fill-long-lines t]
2161               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2162               ["Remove CR" gnus-article-remove-cr t]
2163               ["Rot 13" gnus-summary-caesar-message
2164                ,@(if (featurep 'xemacs) '(t)
2165                    '(:help "\"Caesar rotate\" article by 13"))]
2166               ["Morse decode" gnus-summary-morse-message t]
2167               ["Unix pipe..." gnus-summary-pipe-message t]
2168               ["Add buttons" gnus-article-add-buttons t]
2169               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2170               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2171               ["Toggle MIME" gnus-summary-toggle-mime t]
2172               ["Verbose header" gnus-summary-verbose-headers t]
2173               ["Toggle header" gnus-summary-toggle-header t]
2174               ["Unfold headers" gnus-article-treat-unfold-headers t]
2175               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2176               ["Html" gnus-article-wash-html t]
2177               ["Unsplit URLs" gnus-article-unsplit-urls t]
2178               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2179               ["Decode HZ" gnus-article-decode-HZ t]
2180               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2181               ("(Outlook) Deuglify"
2182                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2183                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2184                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2185                ["Full (Outlook) deuglify"
2186                 gnus-article-outlook-deuglify-article t])
2187               )
2188              ("Output"
2189               ["Save in default format..." gnus-summary-save-article
2190                ,@(if (featurep 'xemacs) '(t)
2191                    '(:help "Save article using default method"))]
2192               ["Save in file..." gnus-summary-save-article-file
2193                ,@(if (featurep 'xemacs) '(t)
2194                    '(:help "Save article in file"))]
2195               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2196               ["Save in MH folder..." gnus-summary-save-article-folder t]
2197               ["Save in VM folder..." gnus-summary-save-article-vm t]
2198               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2199               ["Save body in file..." gnus-summary-save-article-body-file t]
2200               ["Pipe through a filter..." gnus-summary-pipe-output t]
2201               ["Add to SOUP packet" gnus-soup-add-article t]
2202               ["Print with Muttprint..." gnus-summary-muttprint t]
2203               ["Print" gnus-summary-print-article t])
2204              ("Backend"
2205               ["Respool article..." gnus-summary-respool-article t]
2206               ["Move article..." gnus-summary-move-article
2207                (gnus-check-backend-function
2208                 'request-move-article gnus-newsgroup-name)]
2209               ["Copy article..." gnus-summary-copy-article t]
2210               ["Crosspost article..." gnus-summary-crosspost-article
2211                (gnus-check-backend-function
2212                 'request-replace-article gnus-newsgroup-name)]
2213               ["Import file..." gnus-summary-import-article
2214                (gnus-check-backend-function
2215                 'request-accept-article gnus-newsgroup-name)]
2216               ["Create article..." gnus-summary-create-article
2217                (gnus-check-backend-function
2218                 'request-accept-article gnus-newsgroup-name)]
2219               ["Check if posted" gnus-summary-article-posted-p t]
2220               ["Edit article" gnus-summary-edit-article
2221                (not (gnus-group-read-only-p))]
2222               ["Delete article" gnus-summary-delete-article
2223                (gnus-check-backend-function
2224                 'request-expire-articles gnus-newsgroup-name)]
2225               ["Query respool" gnus-summary-respool-query t]
2226               ["Trace respool" gnus-summary-respool-trace t]
2227               ["Delete expirable articles" gnus-summary-expire-articles-now
2228                (gnus-check-backend-function
2229                 'request-expire-articles gnus-newsgroup-name)])
2230              ("Extract"
2231               ["Uudecode" gnus-uu-decode-uu
2232                ,@(if (featurep 'xemacs) '(t)
2233                    '(:help "Decode uuencoded article(s)"))]
2234               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2235               ["Unshar" gnus-uu-decode-unshar t]
2236               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2237               ["Save" gnus-uu-decode-save t]
2238               ["Binhex" gnus-uu-decode-binhex t]
2239               ["Postscript" gnus-uu-decode-postscript t])
2240              ("Cache"
2241               ["Enter article" gnus-cache-enter-article t]
2242               ["Remove article" gnus-cache-remove-article t])
2243              ["Translate" gnus-article-babel t]
2244              ["Select article buffer" gnus-summary-select-article-buffer t]
2245              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2246              ["Isearch article..." gnus-summary-isearch-article t]
2247              ["Beginning of the article" gnus-summary-beginning-of-article t]
2248              ["End of the article" gnus-summary-end-of-article t]
2249              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2250              ["Fetch referenced articles" gnus-summary-refer-references t]
2251              ["Fetch current thread" gnus-summary-refer-thread t]
2252              ["Fetch article with id..." gnus-summary-refer-article t]
2253              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2254              ["Redisplay" gnus-summary-show-article t]
2255              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2256       (easy-menu-define
2257        gnus-summary-article-menu gnus-summary-mode-map ""
2258        (cons "Article" innards))
2259
2260       (if (not (keymapp gnus-summary-article-menu))
2261           (easy-menu-define
2262            gnus-article-commands-menu gnus-article-mode-map ""
2263            (cons "Commands" innards))
2264         ;; in Emacs, don't share menu.
2265         (setq gnus-article-commands-menu
2266               (copy-keymap gnus-summary-article-menu))
2267         (define-key gnus-article-mode-map [menu-bar commands]
2268           (cons "Commands" gnus-article-commands-menu))))
2269
2270     (easy-menu-define
2271      gnus-summary-thread-menu gnus-summary-mode-map ""
2272      '("Threads"
2273        ["Find all messages in thread" gnus-summary-refer-thread t]
2274        ["Toggle threading" gnus-summary-toggle-threads t]
2275        ["Hide threads" gnus-summary-hide-all-threads t]
2276        ["Show threads" gnus-summary-show-all-threads t]
2277        ["Hide thread" gnus-summary-hide-thread t]
2278        ["Show thread" gnus-summary-show-thread t]
2279        ["Go to next thread" gnus-summary-next-thread t]
2280        ["Go to previous thread" gnus-summary-prev-thread t]
2281        ["Go down thread" gnus-summary-down-thread t]
2282        ["Go up thread" gnus-summary-up-thread t]
2283        ["Top of thread" gnus-summary-top-thread t]
2284        ["Mark thread as read" gnus-summary-kill-thread t]
2285        ["Lower thread score" gnus-summary-lower-thread t]
2286        ["Raise thread score" gnus-summary-raise-thread t]
2287        ["Rethread current" gnus-summary-rethread-current t]))
2288
2289     (easy-menu-define
2290      gnus-summary-post-menu gnus-summary-mode-map ""
2291      `("Post"
2292        ["Send a message (mail or news)" gnus-summary-post-news
2293         ,@(if (featurep 'xemacs) '(t)
2294             '(:help "Post an article"))]
2295        ["Followup" gnus-summary-followup
2296         ,@(if (featurep 'xemacs) '(t)
2297             '(:help "Post followup to this article"))]
2298        ["Followup and yank" gnus-summary-followup-with-original
2299         ,@(if (featurep 'xemacs) '(t)
2300             '(:help "Post followup to this article, quoting its contents"))]
2301        ["Supersede article" gnus-summary-supersede-article t]
2302        ["Cancel article" gnus-summary-cancel-article
2303         ,@(if (featurep 'xemacs) '(t)
2304             '(:help "Cancel an article you posted"))]
2305        ["Reply" gnus-summary-reply t]
2306        ["Reply and yank" gnus-summary-reply-with-original t]
2307        ["Wide reply" gnus-summary-wide-reply t]
2308        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2309         ,@(if (featurep 'xemacs) '(t)
2310             '(:help "Mail a reply, quoting this article"))]
2311        ["Very wide reply" gnus-summary-very-wide-reply t]
2312        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2313         ,@(if (featurep 'xemacs) '(t)
2314             '(:help "Mail a very wide reply, quoting this article"))]
2315        ["Mail forward" gnus-summary-mail-forward t]
2316        ["Post forward" gnus-summary-post-forward t]
2317        ["Digest and mail" gnus-summary-digest-mail-forward t]
2318        ["Digest and post" gnus-summary-digest-post-forward t]
2319        ["Resend message" gnus-summary-resend-message t]
2320        ["Resend message edit" gnus-summary-resend-message-edit t]
2321        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2322        ["Send a mail" gnus-summary-mail-other-window t]
2323        ["Create a local message" gnus-summary-news-other-window t]
2324        ["Uuencode and post" gnus-uu-post-news
2325         ,@(if (featurep 'xemacs) '(t)
2326             '(:help "Post a uuencoded article"))]
2327        ["Followup via news" gnus-summary-followup-to-mail t]
2328        ["Followup via news and yank"
2329         gnus-summary-followup-to-mail-with-original t]
2330        ;;("Draft"
2331        ;;["Send" gnus-summary-send-draft t]
2332        ;;["Send bounced" gnus-resend-bounced-mail t])
2333        ))
2334
2335     (cond
2336      ((not (keymapp gnus-summary-post-menu))
2337       (setq gnus-article-post-menu gnus-summary-post-menu))
2338      ((not gnus-article-post-menu)
2339       ;; Don't share post menu.
2340       (setq gnus-article-post-menu
2341             (copy-keymap gnus-summary-post-menu))))
2342     (define-key gnus-article-mode-map [menu-bar post]
2343       (cons "Post" gnus-article-post-menu))
2344
2345     (easy-menu-define
2346      gnus-summary-misc-menu gnus-summary-mode-map ""
2347      `("Gnus"
2348        ("Mark Read"
2349         ["Mark as read" gnus-summary-mark-as-read-forward t]
2350         ["Mark same subject and select"
2351          gnus-summary-kill-same-subject-and-select t]
2352         ["Mark same subject" gnus-summary-kill-same-subject t]
2353         ["Catchup" gnus-summary-catchup
2354          ,@(if (featurep 'xemacs) '(t)
2355              '(:help "Mark unread articles in this group as read"))]
2356         ["Catchup all" gnus-summary-catchup-all t]
2357         ["Catchup to here" gnus-summary-catchup-to-here t]
2358         ["Catchup from here" gnus-summary-catchup-from-here t]
2359         ["Catchup region" gnus-summary-mark-region-as-read
2360          (gnus-mark-active-p)]
2361         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2362        ("Mark Various"
2363         ["Tick" gnus-summary-tick-article-forward t]
2364         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2365         ["Remove marks" gnus-summary-clear-mark-forward t]
2366         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2367         ["Set bookmark" gnus-summary-set-bookmark t]
2368         ["Remove bookmark" gnus-summary-remove-bookmark t])
2369        ("Limit to"
2370         ["Marks..." gnus-summary-limit-to-marks t]
2371         ["Subject..." gnus-summary-limit-to-subject t]
2372         ["Author..." gnus-summary-limit-to-author t]
2373         ["Age..." gnus-summary-limit-to-age t]
2374         ["Extra..." gnus-summary-limit-to-extra t]
2375         ["Score..." gnus-summary-limit-to-score t]
2376         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2377         ["Unread" gnus-summary-limit-to-unread t]
2378         ["Unseen" gnus-summary-limit-to-unseen t]
2379         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2380         ["Next articles" gnus-summary-limit-to-articles t]
2381         ["Pop limit" gnus-summary-pop-limit t]
2382         ["Show dormant" gnus-summary-limit-include-dormant t]
2383         ["Hide childless dormant"
2384          gnus-summary-limit-exclude-childless-dormant t]
2385         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2386         ["Hide marked" gnus-summary-limit-exclude-marks t]
2387         ["Show expunged" gnus-summary-limit-include-expunged t])
2388        ("Process Mark"
2389         ["Set mark" gnus-summary-mark-as-processable t]
2390         ["Remove mark" gnus-summary-unmark-as-processable t]
2391         ["Remove all marks" gnus-summary-unmark-all-processable t]
2392         ["Mark above" gnus-uu-mark-over t]
2393         ["Mark series" gnus-uu-mark-series t]
2394         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2395         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2396         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2397         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2398         ["Mark all" gnus-uu-mark-all t]
2399         ["Mark buffer" gnus-uu-mark-buffer t]
2400         ["Mark sparse" gnus-uu-mark-sparse t]
2401         ["Mark thread" gnus-uu-mark-thread t]
2402         ["Unmark thread" gnus-uu-unmark-thread t]
2403         ("Process Mark Sets"
2404          ["Kill" gnus-summary-kill-process-mark t]
2405          ["Yank" gnus-summary-yank-process-mark
2406           gnus-newsgroup-process-stack]
2407          ["Save" gnus-summary-save-process-mark t]
2408          ["Run command on marked..." gnus-summary-universal-argument t]))
2409        ("Scroll article"
2410         ["Page forward" gnus-summary-next-page
2411          ,@(if (featurep 'xemacs) '(t)
2412              '(:help "Show next page of article"))]
2413         ["Page backward" gnus-summary-prev-page
2414          ,@(if (featurep 'xemacs) '(t)
2415              '(:help "Show previous page of article"))]
2416         ["Line forward" gnus-summary-scroll-up t])
2417        ("Move"
2418         ["Next unread article" gnus-summary-next-unread-article t]
2419         ["Previous unread article" gnus-summary-prev-unread-article t]
2420         ["Next article" gnus-summary-next-article t]
2421         ["Previous article" gnus-summary-prev-article t]
2422         ["Next unread subject" gnus-summary-next-unread-subject t]
2423         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2424         ["Next article same subject" gnus-summary-next-same-subject t]
2425         ["Previous article same subject" gnus-summary-prev-same-subject t]
2426         ["First unread article" gnus-summary-first-unread-article t]
2427         ["Best unread article" gnus-summary-best-unread-article t]
2428         ["Go to subject number..." gnus-summary-goto-subject t]
2429         ["Go to article number..." gnus-summary-goto-article t]
2430         ["Go to the last article" gnus-summary-goto-last-article t]
2431         ["Pop article off history" gnus-summary-pop-article t])
2432        ("Sort"
2433         ["Sort by number" gnus-summary-sort-by-number t]
2434         ["Sort by author" gnus-summary-sort-by-author t]
2435         ["Sort by subject" gnus-summary-sort-by-subject t]
2436         ["Sort by date" gnus-summary-sort-by-date t]
2437         ["Sort by score" gnus-summary-sort-by-score t]
2438         ["Sort by lines" gnus-summary-sort-by-lines t]
2439         ["Sort by characters" gnus-summary-sort-by-chars t]
2440         ["Randomize" gnus-summary-sort-by-random t]
2441         ["Original sort" gnus-summary-sort-by-original t])
2442        ("Help"
2443         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2444         ["Describe group" gnus-summary-describe-group t]
2445         ["Fetch charter" gnus-group-fetch-charter
2446          ,@(if (featurep 'xemacs) nil
2447              '(:help "Display the charter of the current group"))]
2448         ["Fetch control message" gnus-group-fetch-control
2449          ,@(if (featurep 'xemacs) nil
2450              '(:help "Display the archived control message for the current group"))]
2451         ["Read manual" gnus-info-find-node t])
2452        ("Modes"
2453         ["Pick and read" gnus-pick-mode t]
2454         ["Binary" gnus-binary-mode t])
2455        ("Regeneration"
2456         ["Regenerate" gnus-summary-prepare t]
2457         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2458         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2459         ["Toggle threading" gnus-summary-toggle-threads t])
2460        ["See old articles" gnus-summary-insert-old-articles t]
2461        ["See new articles" gnus-summary-insert-new-articles t]
2462        ["Filter articles..." gnus-summary-execute-command t]
2463        ["Run command on articles..." gnus-summary-universal-argument t]
2464        ["Search articles forward..." gnus-summary-search-article-forward t]
2465        ["Search articles backward..." gnus-summary-search-article-backward t]
2466        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2467        ["Expand window" gnus-summary-expand-window t]
2468        ["Expire expirable articles" gnus-summary-expire-articles
2469         (gnus-check-backend-function
2470          'request-expire-articles gnus-newsgroup-name)]
2471        ["Edit local kill file" gnus-summary-edit-local-kill t]
2472        ["Edit main kill file" gnus-summary-edit-global-kill t]
2473        ["Edit group parameters" gnus-summary-edit-parameters t]
2474        ["Customize group parameters" gnus-summary-customize-parameters t]
2475        ["Send a bug report" gnus-bug t]
2476        ("Exit"
2477         ["Catchup and exit" gnus-summary-catchup-and-exit
2478          ,@(if (featurep 'xemacs) '(t)
2479              '(:help "Mark unread articles in this group as read, then exit"))]
2480         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2481         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2482         ["Exit group" gnus-summary-exit
2483          ,@(if (featurep 'xemacs) '(t)
2484              '(:help "Exit current group, return to group selection mode"))]
2485         ["Exit group without updating" gnus-summary-exit-no-update t]
2486         ["Exit and goto next group" gnus-summary-next-group t]
2487         ["Exit and goto prev group" gnus-summary-prev-group t]
2488         ["Reselect group" gnus-summary-reselect-current-group t]
2489         ["Rescan group" gnus-summary-rescan-group t]
2490         ["Update dribble" gnus-summary-save-newsrc t])))
2491
2492     (gnus-run-hooks 'gnus-summary-menu-hook)))
2493
2494 (defvar gnus-summary-tool-bar-map nil)
2495
2496 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2497 (defun gnus-summary-make-tool-bar ()
2498   (if (and (fboundp 'tool-bar-add-item-from-menu)
2499            (default-value 'tool-bar-mode)
2500            (not gnus-summary-tool-bar-map))
2501       (setq gnus-summary-tool-bar-map
2502             (let ((tool-bar-map (make-sparse-keymap))
2503                   (load-path (mm-image-load-path)))
2504               (tool-bar-add-item-from-menu
2505                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2506               (tool-bar-add-item-from-menu
2507                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2508               (tool-bar-add-item-from-menu
2509                'gnus-summary-post-news "post" gnus-summary-mode-map)
2510               (tool-bar-add-item-from-menu
2511                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2512               (tool-bar-add-item-from-menu
2513                'gnus-summary-followup "followup" gnus-summary-mode-map)
2514               (tool-bar-add-item-from-menu
2515                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2516               (tool-bar-add-item-from-menu
2517                'gnus-summary-reply "reply" gnus-summary-mode-map)
2518               (tool-bar-add-item-from-menu
2519                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2520               (tool-bar-add-item-from-menu
2521                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2522               (tool-bar-add-item-from-menu
2523                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2524               (tool-bar-add-item-from-menu
2525                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2526               (tool-bar-add-item-from-menu
2527                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2528               (tool-bar-add-item-from-menu
2529                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2530               (tool-bar-add-item-from-menu
2531                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2532               (tool-bar-add-item-from-menu
2533                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2534               tool-bar-map)))
2535   (if gnus-summary-tool-bar-map
2536       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2537
2538 (defun gnus-score-set-default (var value)
2539   "A version of set that updates the GNU Emacs menu-bar."
2540   (set var value)
2541   ;; It is the message that forces the active status to be updated.
2542   (message ""))
2543
2544 (defun gnus-make-score-map (type)
2545   "Make a summary score map of type TYPE."
2546   (if t
2547       nil
2548     (let ((headers '(("author" "from" string)
2549                      ("subject" "subject" string)
2550                      ("article body" "body" string)
2551                      ("article head" "head" string)
2552                      ("xref" "xref" string)
2553                      ("extra header" "extra" string)
2554                      ("lines" "lines" number)
2555                      ("followups to author" "followup" string)))
2556           (types '((number ("less than" <)
2557                            ("greater than" >)
2558                            ("equal" =))
2559                    (string ("substring" s)
2560                            ("exact string" e)
2561                            ("fuzzy string" f)
2562                            ("regexp" r))))
2563           (perms '(("temporary" (current-time-string))
2564                    ("permanent" nil)
2565                    ("immediate" now)))
2566           header)
2567       (list
2568        (apply
2569         'nconc
2570         (list
2571          (if (eq type 'lower)
2572              "Lower score"
2573            "Increase score"))
2574         (let (outh)
2575           (while headers
2576             (setq header (car headers))
2577             (setq outh
2578                   (cons
2579                    (apply
2580                     'nconc
2581                     (list (car header))
2582                     (let ((ts (cdr (assoc (nth 2 header) types)))
2583                           outt)
2584                       (while ts
2585                         (setq outt
2586                               (cons
2587                                (apply
2588                                 'nconc
2589                                 (list (caar ts))
2590                                 (let ((ps perms)
2591                                       outp)
2592                                   (while ps
2593                                     (setq outp
2594                                           (cons
2595                                            (vector
2596                                             (caar ps)
2597                                             (list
2598                                              'gnus-summary-score-entry
2599                                              (nth 1 header)
2600                                              (if (or (string= (nth 1 header)
2601                                                               "head")
2602                                                      (string= (nth 1 header)
2603                                                               "body"))
2604                                                  ""
2605                                                (list 'gnus-summary-header
2606                                                      (nth 1 header)))
2607                                              (list 'quote (nth 1 (car ts)))
2608                                              (list 'gnus-score-delta-default
2609                                                    nil)
2610                                              (nth 1 (car ps))
2611                                              t)
2612                                             t)
2613                                            outp))
2614                                     (setq ps (cdr ps)))
2615                                   (list (nreverse outp))))
2616                                outt))
2617                         (setq ts (cdr ts)))
2618                       (list (nreverse outt))))
2619                    outh))
2620             (setq headers (cdr headers)))
2621           (list (nreverse outh))))))))
2622
2623 \f
2624
2625 (defun gnus-summary-mode (&optional group)
2626   "Major mode for reading articles.
2627
2628 All normal editing commands are switched off.
2629 \\<gnus-summary-mode-map>
2630 Each line in this buffer represents one article.  To read an
2631 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2632 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2633 respectively.
2634
2635 You can also post articles and send mail from this buffer.  To
2636 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2637 of an article, type `\\[gnus-summary-reply]'.
2638
2639 There are approx. one gazillion commands you can execute in this
2640 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2641
2642 The following commands are available:
2643
2644 \\{gnus-summary-mode-map}"
2645   (interactive)
2646   (kill-all-local-variables)
2647   (when (gnus-visual-p 'summary-menu 'menu)
2648     (gnus-summary-make-menu-bar)
2649     (gnus-summary-make-tool-bar))
2650   (gnus-summary-make-local-variables)
2651   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2652     (gnus-summary-make-local-variables))
2653   (gnus-make-thread-indent-array)
2654   (gnus-simplify-mode-line)
2655   (setq major-mode 'gnus-summary-mode)
2656   (setq mode-name "Summary")
2657   (make-local-variable 'minor-mode-alist)
2658   (use-local-map gnus-summary-mode-map)
2659   (buffer-disable-undo)
2660   (setq buffer-read-only t)             ;Disable modification
2661   (setq truncate-lines t)
2662   (setq selective-display t)
2663   (setq selective-display-ellipses t)   ;Display `...'
2664   (gnus-summary-set-display-table)
2665   (gnus-set-default-directory)
2666   (setq gnus-newsgroup-name group)
2667   (unless (gnus-news-group-p group)
2668     (setq gnus-newsgroup-incorporated
2669           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2670   (make-local-variable 'gnus-summary-line-format)
2671   (make-local-variable 'gnus-summary-line-format-spec)
2672   (make-local-variable 'gnus-summary-dummy-line-format)
2673   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2674   (make-local-variable 'gnus-summary-mark-positions)
2675   (gnus-make-local-hook 'pre-command-hook)
2676   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2677   (gnus-run-hooks 'gnus-summary-mode-hook)
2678   (turn-on-gnus-mailing-list-mode)
2679   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2680   (gnus-update-summary-mark-positions))
2681
2682 (defun gnus-summary-make-local-variables ()
2683   "Make all the local summary buffer variables."
2684   (let (global)
2685     (dolist (local gnus-summary-local-variables)
2686       (if (consp local)
2687           (progn
2688             (if (eq (cdr local) 'global)
2689                 ;; Copy the global value of the variable.
2690                 (setq global (symbol-value (car local)))
2691               ;; Use the value from the list.
2692               (setq global (eval (cdr local))))
2693             (set (make-local-variable (car local)) global))
2694         ;; Simple nil-valued local variable.
2695         (set (make-local-variable local) nil)))))
2696
2697 (defun gnus-summary-clear-local-variables ()
2698   (let ((locals gnus-summary-local-variables))
2699     (while locals
2700       (if (consp (car locals))
2701           (and (vectorp (caar locals))
2702                (set (caar locals) nil))
2703         (and (vectorp (car locals))
2704              (set (car locals) nil)))
2705       (setq locals (cdr locals)))))
2706
2707 ;; Summary data functions.
2708
2709 (defmacro gnus-data-number (data)
2710   `(car ,data))
2711
2712 (defmacro gnus-data-set-number (data number)
2713   `(setcar ,data ,number))
2714
2715 (defmacro gnus-data-mark (data)
2716   `(nth 1 ,data))
2717
2718 (defmacro gnus-data-set-mark (data mark)
2719   `(setcar (nthcdr 1 ,data) ,mark))
2720
2721 (defmacro gnus-data-pos (data)
2722   `(nth 2 ,data))
2723
2724 (defmacro gnus-data-set-pos (data pos)
2725   `(setcar (nthcdr 2 ,data) ,pos))
2726
2727 (defmacro gnus-data-header (data)
2728   `(nth 3 ,data))
2729
2730 (defmacro gnus-data-set-header (data header)
2731   `(setcar (nthcdr 3 ,data) ,header))
2732
2733 (defmacro gnus-data-level (data)
2734   `(nth 4 ,data))
2735
2736 (defmacro gnus-data-unread-p (data)
2737   `(= (nth 1 ,data) gnus-unread-mark))
2738
2739 (defmacro gnus-data-read-p (data)
2740   `(/= (nth 1 ,data) gnus-unread-mark))
2741
2742 (defmacro gnus-data-pseudo-p (data)
2743   `(consp (nth 3 ,data)))
2744
2745 (defmacro gnus-data-find (number)
2746   `(assq ,number gnus-newsgroup-data))
2747
2748 (defmacro gnus-data-find-list (number &optional data)
2749   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2750      (memq (assq ,number bdata)
2751            bdata)))
2752
2753 (defmacro gnus-data-make (number mark pos header level)
2754   `(list ,number ,mark ,pos ,header ,level))
2755
2756 (defun gnus-data-enter (after-article number mark pos header level offset)
2757   (let ((data (gnus-data-find-list after-article)))
2758     (unless data
2759       (error "No such article: %d" after-article))
2760     (setcdr data (cons (gnus-data-make number mark pos header level)
2761                        (cdr data)))
2762     (setq gnus-newsgroup-data-reverse nil)
2763     (gnus-data-update-list (cddr data) offset)))
2764
2765 (defun gnus-data-enter-list (after-article list &optional offset)
2766   (when list
2767     (let ((data (and after-article (gnus-data-find-list after-article)))
2768           (ilist list))
2769       (if (not (or data
2770                    after-article))
2771           (let ((odata gnus-newsgroup-data))
2772             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2773             (when offset
2774               (gnus-data-update-list odata offset)))
2775         ;; Find the last element in the list to be spliced into the main
2776         ;; list.
2777         (while (cdr list)
2778           (setq list (cdr list)))
2779         (if (not data)
2780             (progn
2781               (setcdr list gnus-newsgroup-data)
2782               (setq gnus-newsgroup-data ilist)
2783               (when offset
2784                 (gnus-data-update-list (cdr list) offset)))
2785           (setcdr list (cdr data))
2786           (setcdr data ilist)
2787           (when offset
2788             (gnus-data-update-list (cdr list) offset))))
2789       (setq gnus-newsgroup-data-reverse nil))))
2790
2791 (defun gnus-data-remove (article &optional offset)
2792   (let ((data gnus-newsgroup-data))
2793     (if (= (gnus-data-number (car data)) article)
2794         (progn
2795           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2796                 gnus-newsgroup-data-reverse nil)
2797           (when offset
2798             (gnus-data-update-list gnus-newsgroup-data offset)))
2799       (while (cdr data)
2800         (when (= (gnus-data-number (cadr data)) article)
2801           (setcdr data (cddr data))
2802           (when offset
2803             (gnus-data-update-list (cdr data) offset))
2804           (setq data nil
2805                 gnus-newsgroup-data-reverse nil))
2806         (setq data (cdr data))))))
2807
2808 (defmacro gnus-data-list (backward)
2809   `(if ,backward
2810        (or gnus-newsgroup-data-reverse
2811            (setq gnus-newsgroup-data-reverse
2812                  (reverse gnus-newsgroup-data)))
2813      gnus-newsgroup-data))
2814
2815 (defun gnus-data-update-list (data offset)
2816   "Add OFFSET to the POS of all data entries in DATA."
2817   (setq gnus-newsgroup-data-reverse nil)
2818   (while data
2819     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2820     (setq data (cdr data))))
2821
2822 (defun gnus-summary-article-pseudo-p (article)
2823   "Say whether this article is a pseudo article or not."
2824   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2825
2826 (defmacro gnus-summary-article-sparse-p (article)
2827   "Say whether this article is a sparse article or not."
2828   `(memq ,article gnus-newsgroup-sparse))
2829
2830 (defmacro gnus-summary-article-ancient-p (article)
2831   "Say whether this article is a sparse article or not."
2832   `(memq ,article gnus-newsgroup-ancient))
2833
2834 (defun gnus-article-parent-p (number)
2835   "Say whether this article is a parent or not."
2836   (let ((data (gnus-data-find-list number)))
2837     (and (cdr data)                     ; There has to be an article after...
2838          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2839             (gnus-data-level (nth 1 data))))))
2840
2841 (defun gnus-article-children (number)
2842   "Return a list of all children to NUMBER."
2843   (let* ((data (gnus-data-find-list number))
2844          (level (gnus-data-level (car data)))
2845          children)
2846     (setq data (cdr data))
2847     (while (and data
2848                 (= (gnus-data-level (car data)) (1+ level)))
2849       (push (gnus-data-number (car data)) children)
2850       (setq data (cdr data)))
2851     children))
2852
2853 (defmacro gnus-summary-skip-intangible ()
2854   "If the current article is intangible, then jump to a different article."
2855   '(let ((to (get-text-property (point) 'gnus-intangible)))
2856      (and to (gnus-summary-goto-subject to))))
2857
2858 (defmacro gnus-summary-article-intangible-p ()
2859   "Say whether this article is intangible or not."
2860   '(get-text-property (point) 'gnus-intangible))
2861
2862 (defun gnus-article-read-p (article)
2863   "Say whether ARTICLE is read or not."
2864   (not (or (memq article gnus-newsgroup-marked)
2865            (memq article gnus-newsgroup-spam-marked)
2866            (memq article gnus-newsgroup-unreads)
2867            (memq article gnus-newsgroup-unselected)
2868            (memq article gnus-newsgroup-dormant))))
2869
2870 ;; Some summary mode macros.
2871
2872 (defmacro gnus-summary-article-number ()
2873   "The article number of the article on the current line.
2874 If there isn't an article number here, then we return the current
2875 article number."
2876   '(progn
2877      (gnus-summary-skip-intangible)
2878      (or (get-text-property (point) 'gnus-number)
2879          (gnus-summary-last-subject))))
2880
2881 (defmacro gnus-summary-article-header (&optional number)
2882   "Return the header of article NUMBER."
2883   `(gnus-data-header (gnus-data-find
2884                       ,(or number '(gnus-summary-article-number)))))
2885
2886 (defmacro gnus-summary-thread-level (&optional number)
2887   "Return the level of thread that starts with article NUMBER."
2888   `(if (and (eq gnus-summary-make-false-root 'dummy)
2889             (get-text-property (point) 'gnus-intangible))
2890        0
2891      (gnus-data-level (gnus-data-find
2892                        ,(or number '(gnus-summary-article-number))))))
2893
2894 (defmacro gnus-summary-article-mark (&optional number)
2895   "Return the mark of article NUMBER."
2896   `(gnus-data-mark (gnus-data-find
2897                     ,(or number '(gnus-summary-article-number)))))
2898
2899 (defmacro gnus-summary-article-pos (&optional number)
2900   "Return the position of the line of article NUMBER."
2901   `(gnus-data-pos (gnus-data-find
2902                    ,(or number '(gnus-summary-article-number)))))
2903
2904 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2905 (defmacro gnus-summary-article-subject (&optional number)
2906   "Return current subject string or nil if nothing."
2907   `(let ((headers
2908           ,(if number
2909                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2910              '(gnus-data-header (assq (gnus-summary-article-number)
2911                                       gnus-newsgroup-data)))))
2912      (and headers
2913           (vectorp headers)
2914           (mail-header-subject headers))))
2915
2916 (defmacro gnus-summary-article-score (&optional number)
2917   "Return current article score."
2918   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2919                   gnus-newsgroup-scored))
2920        gnus-summary-default-score 0))
2921
2922 (defun gnus-summary-article-children (&optional number)
2923   "Return a list of article numbers that are children of article NUMBER."
2924   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2925          (level (gnus-data-level (car data)))
2926          l children)
2927     (while (and (setq data (cdr data))
2928                 (> (setq l (gnus-data-level (car data))) level))
2929       (and (= (1+ level) l)
2930            (push (gnus-data-number (car data))
2931                  children)))
2932     (nreverse children)))
2933
2934 (defun gnus-summary-article-parent (&optional number)
2935   "Return the article number of the parent of article NUMBER."
2936   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2937                                     (gnus-data-list t)))
2938          (level (gnus-data-level (car data))))
2939     (if (zerop level)
2940         ()                              ; This is a root.
2941       ;; We search until we find an article with a level less than
2942       ;; this one.  That function has to be the parent.
2943       (while (and (setq data (cdr data))
2944                   (not (< (gnus-data-level (car data)) level))))
2945       (and data (gnus-data-number (car data))))))
2946
2947 (defun gnus-unread-mark-p (mark)
2948   "Say whether MARK is the unread mark."
2949   (= mark gnus-unread-mark))
2950
2951 (defun gnus-read-mark-p (mark)
2952   "Say whether MARK is one of the marks that mark as read.
2953 This is all marks except unread, ticked, dormant, and expirable."
2954   (not (or (= mark gnus-unread-mark)
2955            (= mark gnus-ticked-mark)
2956            (= mark gnus-spam-mark)
2957            (= mark gnus-dormant-mark)
2958            (= mark gnus-expirable-mark))))
2959
2960 (defmacro gnus-article-mark (number)
2961   "Return the MARK of article NUMBER.
2962 This macro should only be used when computing the mark the \"first\"
2963 time; i.e., when generating the summary lines.  After that,
2964 `gnus-summary-article-mark' should be used to examine the
2965 marks of articles."
2966   `(cond
2967     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2968     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2969     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2970     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2971     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2972     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2973     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2974     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2975            gnus-ancient-mark))))
2976
2977 ;; Saving hidden threads.
2978
2979 (defmacro gnus-save-hidden-threads (&rest forms)
2980   "Save hidden threads, eval FORMS, and restore the hidden threads."
2981   (let ((config (make-symbol "config")))
2982     `(let ((,config (gnus-hidden-threads-configuration)))
2983        (unwind-protect
2984            (save-excursion
2985              ,@forms)
2986          (gnus-restore-hidden-threads-configuration ,config)))))
2987 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2988 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2989
2990 (defun gnus-data-compute-positions ()
2991   "Compute the positions of all articles."
2992   (setq gnus-newsgroup-data-reverse nil)
2993   (let ((data gnus-newsgroup-data))
2994     (save-excursion
2995       (gnus-save-hidden-threads
2996         (gnus-summary-show-all-threads)
2997         (goto-char (point-min))
2998         (while data
2999           (while (get-text-property (point) 'gnus-intangible)
3000             (forward-line 1))
3001           (gnus-data-set-pos (car data) (+ (point) 3))
3002           (setq data (cdr data))
3003           (forward-line 1))))))
3004
3005 (defun gnus-hidden-threads-configuration ()
3006   "Return the current hidden threads configuration."
3007   (save-excursion
3008     (let (config)
3009       (goto-char (point-min))
3010       (while (search-forward "\r" nil t)
3011         (push (1- (point)) config))
3012       config)))
3013
3014 (defun gnus-restore-hidden-threads-configuration (config)
3015   "Restore hidden threads configuration from CONFIG."
3016   (save-excursion
3017     (let (point buffer-read-only)
3018       (while (setq point (pop config))
3019         (when (and (< point (point-max))
3020                    (goto-char point)
3021                    (eq (char-after) ?\n))
3022           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3023
3024 ;; Various summary mode internalish functions.
3025
3026 (defun gnus-mouse-pick-article (e)
3027   (interactive "e")
3028   (mouse-set-point e)
3029   (gnus-summary-next-page nil t))
3030
3031 (defun gnus-summary-set-display-table ()
3032   "Change the display table.
3033 Odd characters have a tendency to mess
3034 up nicely formatted displays - we make all possible glyphs
3035 display only a single character."
3036
3037   ;; We start from the standard display table, if any.
3038   (let ((table (or (copy-sequence standard-display-table)
3039                    (make-display-table)))
3040         (i 32))
3041     ;; Nix out all the control chars...
3042     (while (>= (setq i (1- i)) 0)
3043       (aset table i [??]))
3044     ;; ... but not newline and cr, of course.  (cr is necessary for the
3045     ;; selective display).
3046     (aset table ?\n nil)
3047     (aset table ?\r nil)
3048     ;; We keep TAB as well.
3049     (aset table ?\t nil)
3050     ;; We nix out any glyphs over 126 that are not set already.
3051     (let ((i 256))
3052       (while (>= (setq i (1- i)) 127)
3053         ;; Only modify if the entry is nil.
3054         (unless (aref table i)
3055           (aset table i [??]))))
3056     (setq buffer-display-table table)))
3057
3058 (defun gnus-summary-set-article-display-arrow (pos)
3059   "Update the overlay arrow to point to line at position POS."
3060   (when (and gnus-summary-display-arrow
3061              (boundp 'overlay-arrow-position)
3062              (boundp 'overlay-arrow-string))
3063     (save-excursion
3064       (goto-char pos)
3065       (beginning-of-line)
3066       (unless overlay-arrow-position
3067         (setq overlay-arrow-position (make-marker)))
3068       (setq overlay-arrow-string "=>"
3069             overlay-arrow-position (set-marker overlay-arrow-position
3070                                                (point)
3071                                                (current-buffer))))))
3072
3073 (defun gnus-summary-setup-buffer (group)
3074   "Initialize summary buffer."
3075   (let ((buffer (gnus-summary-buffer-name group))
3076         (dead-name (concat "*Dead Summary "
3077                            (gnus-group-decoded-name group) "*")))
3078     ;; If a dead summary buffer exists, we kill it.
3079     (when (gnus-buffer-live-p dead-name)
3080       (gnus-kill-buffer dead-name))
3081     (if (get-buffer buffer)
3082         (progn
3083           (set-buffer buffer)
3084           (setq gnus-summary-buffer (current-buffer))
3085           (not gnus-newsgroup-prepared))
3086       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3087       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3088       (gnus-summary-mode group)
3089       (when gnus-carpal
3090         (gnus-carpal-setup-buffer 'summary))
3091       (unless gnus-single-article-buffer
3092         (make-local-variable 'gnus-article-buffer)
3093         (make-local-variable 'gnus-article-current)
3094         (make-local-variable 'gnus-original-article-buffer))
3095       (setq gnus-newsgroup-name group)
3096       ;; Set any local variables in the group parameters.
3097       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3098       t)))
3099
3100 (defun gnus-set-global-variables ()
3101   "Set the global equivalents of the buffer-local variables.
3102 They are set to the latest values they had.  These reflect the summary
3103 buffer that was in action when the last article was fetched."
3104   (when (eq major-mode 'gnus-summary-mode)
3105     (setq gnus-summary-buffer (current-buffer))
3106     (let ((name gnus-newsgroup-name)
3107           (marked gnus-newsgroup-marked)
3108           (spam gnus-newsgroup-spam-marked)
3109           (unread gnus-newsgroup-unreads)
3110           (headers gnus-current-headers)
3111           (data gnus-newsgroup-data)
3112           (summary gnus-summary-buffer)
3113           (article-buffer gnus-article-buffer)
3114           (original gnus-original-article-buffer)
3115           (gac gnus-article-current)
3116           (reffed gnus-reffed-article-number)
3117           (score-file gnus-current-score-file)
3118           (default-charset gnus-newsgroup-charset)
3119           vlist)
3120       (let ((locals gnus-newsgroup-variables))
3121         (while locals
3122           (if (consp (car locals))
3123               (push (eval (caar locals)) vlist)
3124             (push (eval (car locals)) vlist))
3125           (setq locals (cdr locals)))
3126         (setq vlist (nreverse vlist)))
3127       (save-excursion
3128         (set-buffer gnus-group-buffer)
3129         (setq gnus-newsgroup-name name
3130               gnus-newsgroup-marked marked
3131               gnus-newsgroup-spam-marked spam
3132               gnus-newsgroup-unreads unread
3133               gnus-current-headers headers
3134               gnus-newsgroup-data data
3135               gnus-article-current gac
3136               gnus-summary-buffer summary
3137               gnus-article-buffer article-buffer
3138               gnus-original-article-buffer original
3139               gnus-reffed-article-number reffed
3140               gnus-current-score-file score-file
3141               gnus-newsgroup-charset default-charset)
3142         (let ((locals gnus-newsgroup-variables))
3143           (while locals
3144             (if (consp (car locals))
3145                 (set (caar locals) (pop vlist))
3146               (set (car locals) (pop vlist)))
3147             (setq locals (cdr locals))))
3148         ;; The article buffer also has local variables.
3149         (when (gnus-buffer-live-p gnus-article-buffer)
3150           (set-buffer gnus-article-buffer)
3151           (setq gnus-summary-buffer summary))))))
3152
3153 (defun gnus-summary-article-unread-p (article)
3154   "Say whether ARTICLE is unread or not."
3155   (memq article gnus-newsgroup-unreads))
3156
3157 (defun gnus-summary-first-article-p (&optional article)
3158   "Return whether ARTICLE is the first article in the buffer."
3159   (if (not (setq article (or article (gnus-summary-article-number))))
3160       nil
3161     (eq article (caar gnus-newsgroup-data))))
3162
3163 (defun gnus-summary-last-article-p (&optional article)
3164   "Return whether ARTICLE is the last article in the buffer."
3165   (if (not (setq article (or article (gnus-summary-article-number))))
3166       ;; All non-existent numbers are the last article.  :-)
3167       t
3168     (not (cdr (gnus-data-find-list article)))))
3169
3170 (defun gnus-make-thread-indent-array ()
3171   (let ((n 200))
3172     (unless (and gnus-thread-indent-array
3173                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3174       (setq gnus-thread-indent-array (make-vector 201 "")
3175             gnus-thread-indent-array-level gnus-thread-indent-level)
3176       (while (>= n 0)
3177         (aset gnus-thread-indent-array n
3178               (make-string (* n gnus-thread-indent-level) ? ))
3179         (setq n (1- n))))))
3180
3181 (defun gnus-update-summary-mark-positions ()
3182   "Compute where the summary marks are to go."
3183   (save-excursion
3184     (when (gnus-buffer-exists-p gnus-summary-buffer)
3185       (set-buffer gnus-summary-buffer))
3186     (let ((gnus-replied-mark 129)
3187           (gnus-score-below-mark 130)
3188           (gnus-score-over-mark 130)
3189           (gnus-undownloaded-mark 131)
3190           (spec gnus-summary-line-format-spec)
3191           gnus-visual pos)
3192       (save-excursion
3193         (gnus-set-work-buffer)
3194         (let ((gnus-summary-line-format-spec spec)
3195               (gnus-newsgroup-downloadable '(0)))
3196           (gnus-summary-insert-line
3197            (make-full-mail-header 0 "" "nobody"
3198                                   "05 Apr 2001 23:33:09 +0400"
3199                                   "" "" 0 0 "" nil)
3200            0 nil t 128 t nil "" nil 1)
3201           (goto-char (point-min))
3202           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3203                                              (- (point) (point-min) 1)))))
3204           (goto-char (point-min))
3205           (push (cons 'replied (and (search-forward "\201" nil t)
3206                                     (- (point) (point-min) 1)))
3207                 pos)
3208           (goto-char (point-min))
3209           (push (cons 'score (and (search-forward "\202" nil t)
3210                                   (- (point) (point-min) 1)))
3211                 pos)
3212           (goto-char (point-min))
3213           (push (cons 'download
3214                       (and (search-forward "\203" nil t)
3215                            (- (point) (point-min) 1)))
3216                 pos)))
3217       (setq gnus-summary-mark-positions pos))))
3218
3219 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3220   "Insert a dummy root in the summary buffer."
3221   (beginning-of-line)
3222   (gnus-add-text-properties
3223    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3224    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3225
3226 (defun gnus-summary-extract-address-component (from)
3227   (or (car (funcall gnus-extract-address-components from))
3228       from))
3229
3230 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3231   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3232                                 default-mime-charset)))
3233     ;; Is it really necessary to do this next part for each summary line?
3234     ;; Luckily, doesn't seem to slow things down much.
3235     (or
3236      (and gnus-ignored-from-addresses
3237           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3238           (let ((extra-headers (mail-header-extra header))
3239                 to
3240                 newsgroups)
3241             (cond
3242              ((setq to (cdr (assq 'To extra-headers)))
3243               (concat "-> "
3244                       (inline
3245                         (gnus-summary-extract-address-component
3246                          (funcall gnus-decode-encoded-word-function to)))))
3247              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3248               (concat "=> " newsgroups)))))
3249      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3250
3251 (defun gnus-summary-insert-line (gnus-tmp-header
3252                                  gnus-tmp-level gnus-tmp-current
3253                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3254                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3255                                  &optional gnus-tmp-dummy gnus-tmp-score
3256                                  gnus-tmp-process)
3257   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3258          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3259          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3260          (gnus-tmp-score-char
3261           (if (or (null gnus-summary-default-score)
3262                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3263                       gnus-summary-zcore-fuzz))
3264               ?\ ;;;Whitespace
3265             (if (< gnus-tmp-score gnus-summary-default-score)
3266                 gnus-score-below-mark gnus-score-over-mark)))
3267          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3268          (gnus-tmp-replied
3269           (cond (gnus-tmp-process gnus-process-mark)
3270                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3271                  gnus-cached-mark)
3272                 (gnus-tmp-replied gnus-replied-mark)
3273                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3274                  gnus-forwarded-mark)
3275                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3276                  gnus-saved-mark)
3277                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3278                  gnus-recent-mark)
3279                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3280                  gnus-unseen-mark)
3281                 (t gnus-no-mark)))
3282          (gnus-tmp-downloaded
3283           (cond (undownloaded
3284                  gnus-undownloaded-mark)
3285                 (gnus-newsgroup-agentized
3286                  gnus-downloaded-mark)
3287                 (t
3288                  gnus-no-mark)))
3289          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3290          (gnus-tmp-name
3291           (cond
3292            ((string-match "<[^>]+> *$" gnus-tmp-from)
3293             (let ((beg (match-beginning 0)))
3294               (or (and (string-match "^\".+\"" gnus-tmp-from)
3295                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3296                   (substring gnus-tmp-from 0 beg))))
3297            ((string-match "(.+)" gnus-tmp-from)
3298             (substring gnus-tmp-from
3299                        (1+ (match-beginning 0)) (1- (match-end 0))))
3300            (t gnus-tmp-from)))
3301          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3302          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3303          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3304          (buffer-read-only nil))
3305     (when (string= gnus-tmp-name "")
3306       (setq gnus-tmp-name gnus-tmp-from))
3307     (unless (numberp gnus-tmp-lines)
3308       (setq gnus-tmp-lines -1))
3309     (if (= gnus-tmp-lines -1)
3310         (setq gnus-tmp-lines "?")
3311       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3312     (gnus-put-text-property-excluding-characters-with-faces
3313      (point)
3314      (progn (eval gnus-summary-line-format-spec) (point))
3315      'gnus-number gnus-tmp-number)
3316     (when (gnus-visual-p 'summary-highlight 'highlight)
3317       (forward-line -1)
3318       (gnus-run-hooks 'gnus-summary-update-hook)
3319       (forward-line 1))))
3320
3321 (defun gnus-summary-update-line (&optional dont-update)
3322   "Update summary line after change."
3323   (when (and gnus-summary-default-score
3324              (not gnus-summary-inhibit-highlight))
3325     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3326            (article (gnus-summary-article-number))
3327            (score (gnus-summary-article-score article)))
3328       (unless dont-update
3329         (if (and gnus-summary-mark-below
3330                  (< (gnus-summary-article-score)
3331                     gnus-summary-mark-below))
3332             ;; This article has a low score, so we mark it as read.
3333             (when (memq article gnus-newsgroup-unreads)
3334               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3335           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3336             ;; This article was previously marked as read on account
3337             ;; of a low score, but now it has risen, so we mark it as
3338             ;; unread.
3339             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3340         (gnus-summary-update-mark
3341          (if (or (null gnus-summary-default-score)
3342                  (<= (abs (- score gnus-summary-default-score))
3343                      gnus-summary-zcore-fuzz))
3344              ?\ ;;;Whitespace
3345            (if (< score gnus-summary-default-score)
3346                gnus-score-below-mark gnus-score-over-mark))
3347          'score))
3348       ;; Do visual highlighting.
3349       (when (gnus-visual-p 'summary-highlight 'highlight)
3350         (gnus-run-hooks 'gnus-summary-update-hook)))))
3351
3352 (defvar gnus-tmp-new-adopts nil)
3353
3354 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3355   "Return the number of articles in THREAD.
3356 This may be 0 in some cases -- if none of the articles in
3357 the thread are to be displayed."
3358   (let* ((number
3359           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3360           (cond
3361            ((not (listp thread))
3362             1)
3363            ((and (consp thread) (cdr thread))
3364             (apply
3365              '+ 1 (mapcar
3366                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3367            ((null thread)
3368             1)
3369            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3370             1)
3371            (t 0))))
3372     (when (and level (zerop level) gnus-tmp-new-adopts)
3373       (incf number
3374             (apply '+ (mapcar
3375                        'gnus-summary-number-of-articles-in-thread
3376                        gnus-tmp-new-adopts))))
3377     (if char
3378         (if (> number 1) gnus-not-empty-thread-mark
3379           gnus-empty-thread-mark)
3380       number)))
3381
3382 (defsubst gnus-summary-line-message-size (head)
3383   "Return pretty-printed version of message size.
3384 This function is intended to be used in
3385 `gnus-summary-line-format-alist'."
3386   (let ((c (or (mail-header-chars head) -1)))
3387     (cond ((< c 0) "n/a")               ; chars not available
3388           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3389           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3390           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3391           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3392
3393
3394 (defun gnus-summary-set-local-parameters (group)
3395   "Go through the local params of GROUP and set all variable specs in that list."
3396   (let ((params (gnus-group-find-parameter group))
3397         (vars '(quit-config))           ; Ignore quit-config.
3398         elem)
3399     (while params
3400       (setq elem (car params)
3401             params (cdr params))
3402       (and (consp elem)                 ; Has to be a cons.
3403            (consp (cdr elem))           ; The cdr has to be a list.
3404            (symbolp (car elem))         ; Has to be a symbol in there.
3405            (not (memq (car elem) vars))
3406            (ignore-errors               ; So we set it.
3407              (push (car elem) vars)
3408              (make-local-variable (car elem))
3409              (set (car elem) (eval (nth 1 elem))))))))
3410
3411 (defun gnus-summary-read-group (group &optional show-all no-article
3412                                       kill-buffer no-display backward
3413                                       select-articles)
3414   "Start reading news in newsgroup GROUP.
3415 If SHOW-ALL is non-nil, already read articles are also listed.
3416 If NO-ARTICLE is non-nil, no article is selected initially.
3417 If NO-DISPLAY, don't generate a summary buffer."
3418   (let (result)
3419     (while (and group
3420                 (null (setq result
3421                             (let ((gnus-auto-select-next nil))
3422                               (or (gnus-summary-read-group-1
3423                                    group show-all no-article
3424                                    kill-buffer no-display
3425                                    select-articles)
3426                                   (setq show-all nil
3427                                         select-articles nil)))))
3428                 (eq gnus-auto-select-next 'quietly))
3429       (set-buffer gnus-group-buffer)
3430       ;; The entry function called above goes to the next
3431       ;; group automatically, so we go two groups back
3432       ;; if we are searching for the previous group.
3433       (when backward
3434         (gnus-group-prev-unread-group 2))
3435       (if (not (equal group (gnus-group-group-name)))
3436           (setq group (gnus-group-group-name))
3437         (setq group nil)))
3438     result))
3439
3440 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3441   "Directly jump to the other GROUP from summary buffer.
3442 If SHOW-ALL is non-nil, already read articles are also listed."
3443   (interactive
3444    (if (eq gnus-summary-buffer (current-buffer))
3445        (list (completing-read
3446               "Group: " gnus-active-hashtb nil t
3447               (when (and gnus-newsgroup-name
3448                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3449                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3450               'gnus-group-history)
3451              current-prefix-arg)
3452      (error "%s must be invoked from a gnus summary buffer." this-command)))
3453   (unless (or (zerop (length group))
3454               (and gnus-newsgroup-name
3455                    (string-equal gnus-newsgroup-name group)))
3456     (gnus-summary-exit)
3457     (gnus-summary-read-group group show-all
3458                              gnus-dont-select-after-jump-to-other-group)))
3459
3460 (defun gnus-summary-read-group-1 (group show-all no-article
3461                                         kill-buffer no-display
3462                                         &optional select-articles)
3463   ;; Killed foreign groups can't be entered.
3464   ;;  (when (and (not (gnus-group-native-p group))
3465   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3466   ;;    (error "Dead non-native groups can't be entered"))
3467   (gnus-message 5 "Retrieving newsgroup: %s..."
3468                 (gnus-group-decoded-name group))
3469   (let* ((new-group (gnus-summary-setup-buffer group))
3470          (quit-config (gnus-group-quit-config group))
3471          (did-select (and new-group (gnus-select-newsgroup
3472                                      group show-all select-articles))))
3473     (cond
3474      ;; This summary buffer exists already, so we just select it.
3475      ((not new-group)
3476       (gnus-set-global-variables)
3477       (when kill-buffer
3478         (gnus-kill-or-deaden-summary kill-buffer))
3479       (gnus-configure-windows 'summary 'force)
3480       (gnus-set-mode-line 'summary)
3481       (gnus-summary-position-point)
3482       (message "")
3483       t)
3484      ;; We couldn't select this group.
3485      ((null did-select)
3486       (when (and (eq major-mode 'gnus-summary-mode)
3487                  (not (equal (current-buffer) kill-buffer)))
3488         (kill-buffer (current-buffer))
3489         (if (not quit-config)
3490             (progn
3491               ;; Update the info -- marks might need to be removed,
3492               ;; for instance.
3493               (gnus-summary-update-info)
3494               (set-buffer gnus-group-buffer)
3495               (gnus-group-jump-to-group group)
3496               (gnus-group-next-unread-group 1))
3497           (gnus-handle-ephemeral-exit quit-config)))
3498       (let ((grpinfo (gnus-get-info group)))
3499         (if (null (gnus-info-read grpinfo))
3500             (gnus-message 3 "Group %s contains no messages"
3501                           (gnus-group-decoded-name group))
3502           (gnus-message 3 "Can't select group")))
3503       nil)
3504      ;; The user did a `C-g' while prompting for number of articles,
3505      ;; so we exit this group.
3506      ((eq did-select 'quit)
3507       (and (eq major-mode 'gnus-summary-mode)
3508            (not (equal (current-buffer) kill-buffer))
3509            (kill-buffer (current-buffer)))
3510       (when kill-buffer
3511         (gnus-kill-or-deaden-summary kill-buffer))
3512       (if (not quit-config)
3513           (progn
3514             (set-buffer gnus-group-buffer)
3515             (gnus-group-jump-to-group group)
3516             (gnus-group-next-unread-group 1)
3517             (gnus-configure-windows 'group 'force))
3518         (gnus-handle-ephemeral-exit quit-config))
3519       ;; Finally signal the quit.
3520       (signal 'quit nil))
3521      ;; The group was successfully selected.
3522      (t
3523       (gnus-set-global-variables)
3524       ;; Save the active value in effect when the group was entered.
3525       (setq gnus-newsgroup-active
3526             (gnus-copy-sequence
3527              (gnus-active gnus-newsgroup-name)))
3528       ;; You can change the summary buffer in some way with this hook.
3529       (gnus-run-hooks 'gnus-select-group-hook)
3530       (gnus-update-format-specifications
3531        nil 'summary 'summary-mode 'summary-dummy)
3532       (gnus-update-summary-mark-positions)
3533       ;; Do score processing.
3534       (when gnus-use-scoring
3535         (gnus-possibly-score-headers))
3536       ;; Check whether to fill in the gaps in the threads.
3537       (when gnus-build-sparse-threads
3538         (gnus-build-sparse-threads))
3539       ;; Find the initial limit.
3540       (if gnus-show-threads
3541           (if show-all
3542               (let ((gnus-newsgroup-dormant nil))
3543                 (gnus-summary-initial-limit show-all))
3544             (gnus-summary-initial-limit show-all))
3545         ;; When unthreaded, all articles are always shown.
3546         (setq gnus-newsgroup-limit
3547               (mapcar
3548                (lambda (header) (mail-header-number header))
3549                gnus-newsgroup-headers)))
3550       ;; Generate the summary buffer.
3551       (unless no-display
3552         (gnus-summary-prepare))
3553       (when gnus-use-trees
3554         (gnus-tree-open group)
3555         (setq gnus-summary-highlight-line-function
3556               'gnus-tree-highlight-article))
3557       ;; If the summary buffer is empty, but there are some low-scored
3558       ;; articles or some excluded dormants, we include these in the
3559       ;; buffer.
3560       (when (and (zerop (buffer-size))
3561                  (not no-display))
3562         (cond (gnus-newsgroup-dormant
3563                (gnus-summary-limit-include-dormant))
3564               ((and gnus-newsgroup-scored show-all)
3565                (gnus-summary-limit-include-expunged t))))
3566       ;; Function `gnus-apply-kill-file' must be called in this hook.
3567       (gnus-run-hooks 'gnus-apply-kill-hook)
3568       (if (and (zerop (buffer-size))
3569                (not no-display))
3570           (progn
3571             ;; This newsgroup is empty.
3572             (gnus-summary-catchup-and-exit nil t)
3573             (gnus-message 6 "No unread news")
3574             (when kill-buffer
3575               (gnus-kill-or-deaden-summary kill-buffer))
3576             ;; Return nil from this function.
3577             nil)
3578         ;; Hide conversation thread subtrees.  We cannot do this in
3579         ;; gnus-summary-prepare-hook since kill processing may not
3580         ;; work with hidden articles.
3581         (gnus-summary-maybe-hide-threads)
3582         (when kill-buffer
3583           (gnus-kill-or-deaden-summary kill-buffer))
3584         (gnus-summary-auto-select-subject)
3585         ;; Show first unread article if requested.
3586         (if (and (not no-article)
3587                  (not no-display)
3588                  gnus-newsgroup-unreads
3589                  gnus-auto-select-first)
3590             (progn
3591               (gnus-configure-windows 'summary)
3592               (let ((art (gnus-summary-article-number)))
3593                 (unless (and (not gnus-plugged)
3594                              (or (memq art gnus-newsgroup-undownloaded)
3595                                  (memq art gnus-newsgroup-downloadable)))
3596                   (gnus-summary-goto-article art))))
3597           ;; Don't select any articles.
3598           (gnus-summary-position-point)
3599           (gnus-configure-windows 'summary 'force)
3600           (gnus-set-mode-line 'summary))
3601         (when (and gnus-auto-center-group
3602                    (get-buffer-window gnus-group-buffer t))
3603           ;; Gotta use windows, because recenter does weird stuff if
3604           ;; the current buffer ain't the displayed window.
3605           (let ((owin (selected-window)))
3606             (select-window (get-buffer-window gnus-group-buffer t))
3607             (when (gnus-group-goto-group group)
3608               (recenter))
3609             (select-window owin)))
3610         ;; Mark this buffer as "prepared".
3611         (setq gnus-newsgroup-prepared t)
3612         (gnus-run-hooks 'gnus-summary-prepared-hook)
3613         (unless (gnus-ephemeral-group-p group)
3614           (gnus-group-update-group group))
3615         t)))))
3616
3617 (defun gnus-summary-auto-select-subject ()
3618   "Select the subject line on initial group entry."
3619   (goto-char (point-min))
3620   (cond
3621    ((eq gnus-auto-select-subject 'best)
3622     (gnus-summary-best-unread-subject))
3623    ((eq gnus-auto-select-subject 'unread)
3624     (gnus-summary-first-unread-subject))
3625    ((eq gnus-auto-select-subject 'unseen)
3626     (gnus-summary-first-unseen-subject))
3627    ((eq gnus-auto-select-subject 'unseen-or-unread)
3628     (gnus-summary-first-unseen-or-unread-subject))
3629    ((eq gnus-auto-select-subject 'first)
3630     ;; Do nothing.
3631     )
3632    ((functionp gnus-auto-select-subject)
3633     (funcall gnus-auto-select-subject))))
3634
3635 (defun gnus-summary-prepare ()
3636   "Generate the summary buffer."
3637   (interactive)
3638   (let ((buffer-read-only nil))
3639     (erase-buffer)
3640     (setq gnus-newsgroup-data nil
3641           gnus-newsgroup-data-reverse nil)
3642     (gnus-run-hooks 'gnus-summary-generate-hook)
3643     ;; Generate the buffer, either with threads or without.
3644     (when gnus-newsgroup-headers
3645       (gnus-summary-prepare-threads
3646        (if gnus-show-threads
3647            (gnus-sort-gathered-threads
3648             (funcall gnus-summary-thread-gathering-function
3649                      (gnus-sort-threads
3650                       (gnus-cut-threads (gnus-make-threads)))))
3651          ;; Unthreaded display.
3652          (gnus-sort-articles gnus-newsgroup-headers))))
3653     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3654     ;; Call hooks for modifying summary buffer.
3655     (goto-char (point-min))
3656     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3657
3658 (defsubst gnus-general-simplify-subject (subject)
3659   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3660   (setq subject
3661         (cond
3662          ;; Truncate the subject.
3663          (gnus-simplify-subject-functions
3664           (gnus-map-function gnus-simplify-subject-functions subject))
3665          ((numberp gnus-summary-gather-subject-limit)
3666           (setq subject (gnus-simplify-subject-re subject))
3667           (if (> (length subject) gnus-summary-gather-subject-limit)
3668               (substring subject 0 gnus-summary-gather-subject-limit)
3669             subject))
3670          ;; Fuzzily simplify it.
3671          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3672           (gnus-simplify-subject-fuzzy subject))
3673          ;; Just remove the leading "Re:".
3674          (t
3675           (gnus-simplify-subject-re subject))))
3676
3677   (if (and gnus-summary-gather-exclude-subject
3678            (string-match gnus-summary-gather-exclude-subject subject))
3679       nil                               ; This article shouldn't be gathered
3680     subject))
3681
3682 (defun gnus-summary-simplify-subject-query ()
3683   "Query where the respool algorithm would put this article."
3684   (interactive)
3685   (gnus-summary-select-article)
3686   (message "%s"
3687            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3688
3689 (defun gnus-gather-threads-by-subject (threads)
3690   "Gather threads by looking at Subject headers."
3691   (if (not gnus-summary-make-false-root)
3692       threads
3693     (let ((hashtb (gnus-make-hashtable 1024))
3694           (prev threads)
3695           (result threads)
3696           subject hthread whole-subject)
3697       (while threads
3698         (setq subject (gnus-general-simplify-subject
3699                        (setq whole-subject (mail-header-subject
3700                                             (caar threads)))))
3701         (when subject
3702           (if (setq hthread (gnus-gethash subject hashtb))
3703               (progn
3704                 ;; We enter a dummy root into the thread, if we
3705                 ;; haven't done that already.
3706                 (unless (stringp (caar hthread))
3707                   (setcar hthread (list whole-subject (car hthread))))
3708                 ;; We add this new gathered thread to this gathered
3709                 ;; thread.
3710                 (setcdr (car hthread)
3711                         (nconc (cdar hthread) (list (car threads))))
3712                 ;; Remove it from the list of threads.
3713                 (setcdr prev (cdr threads))
3714                 (setq threads prev))
3715             ;; Enter this thread into the hash table.
3716             (gnus-sethash subject
3717                           (if gnus-summary-make-false-root-always
3718                               (progn
3719                                 ;; If you want a dummy root above all
3720                                 ;; threads...
3721                                 (setcar threads (list whole-subject
3722                                                       (car threads)))
3723                                 threads)
3724                             threads)
3725                           hashtb)))
3726         (setq prev threads)
3727         (setq threads (cdr threads)))
3728       result)))
3729
3730 (defun gnus-gather-threads-by-references (threads)
3731   "Gather threads by looking at References headers."
3732   (let ((idhashtb (gnus-make-hashtable 1024))
3733         (thhashtb (gnus-make-hashtable 1024))
3734         (prev threads)
3735         (result threads)
3736         ids references id gthread gid entered ref)
3737     (while threads
3738       (when (setq references (mail-header-references (caar threads)))
3739         (setq id (mail-header-id (caar threads))
3740               ids (inline (gnus-split-references references))
3741               entered nil)
3742         (while (setq ref (pop ids))
3743           (setq ids (delete ref ids))
3744           (if (not (setq gid (gnus-gethash ref idhashtb)))
3745               (progn
3746                 (gnus-sethash ref id idhashtb)
3747                 (gnus-sethash id threads thhashtb))
3748             (setq gthread (gnus-gethash gid thhashtb))
3749             (unless entered
3750               ;; We enter a dummy root into the thread, if we
3751               ;; haven't done that already.
3752               (unless (stringp (caar gthread))
3753                 (setcar gthread (list (mail-header-subject (caar gthread))
3754                                       (car gthread))))
3755               ;; We add this new gathered thread to this gathered
3756               ;; thread.
3757               (setcdr (car gthread)
3758                       (nconc (cdar gthread) (list (car threads)))))
3759             ;; Add it into the thread hash table.
3760             (gnus-sethash id gthread thhashtb)
3761             (setq entered t)
3762             ;; Remove it from the list of threads.
3763             (setcdr prev (cdr threads))
3764             (setq threads prev))))
3765       (setq prev threads)
3766       (setq threads (cdr threads)))
3767     result))
3768
3769 (defun gnus-sort-gathered-threads (threads)
3770   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3771   (let ((result threads))
3772     (while threads
3773       (when (stringp (caar threads))
3774         (setcdr (car threads)
3775                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3776       (setq threads (cdr threads)))
3777     result))
3778
3779 (defun gnus-thread-loop-p (root thread)
3780   "Say whether ROOT is in THREAD."
3781   (let ((stack (list thread))
3782         (infloop 0)
3783         th)
3784     (while (setq thread (pop stack))
3785       (setq th (cdr thread))
3786       (while (and th
3787                   (not (eq (caar th) root)))
3788         (pop th))
3789       (if th
3790           ;; We have found a loop.
3791           (let (ref-dep)
3792             (setcdr thread (delq (car th) (cdr thread)))
3793             (if (boundp (setq ref-dep (intern "none"
3794                                               gnus-newsgroup-dependencies)))
3795                 (setcdr (symbol-value ref-dep)
3796                         (nconc (cdr (symbol-value ref-dep))
3797                                (list (car th))))
3798               (set ref-dep (list nil (car th))))
3799             (setq infloop 1
3800                   stack nil))
3801         ;; Push all the subthreads onto the stack.
3802         (push (cdr thread) stack)))
3803     infloop))
3804
3805 (defun gnus-make-threads ()
3806   "Go through the dependency hashtb and find the roots.  Return all threads."
3807   (let (threads)
3808     (while (catch 'infloop
3809              (mapatoms
3810               (lambda (refs)
3811                 ;; Deal with self-referencing References loops.
3812                 (when (and (car (symbol-value refs))
3813                            (not (zerop
3814                                  (apply
3815                                   '+
3816                                   (mapcar
3817                                    (lambda (thread)
3818                                      (gnus-thread-loop-p
3819                                       (car (symbol-value refs)) thread))
3820                                    (cdr (symbol-value refs)))))))
3821                   (setq threads nil)
3822                   (throw 'infloop t))
3823                 (unless (car (symbol-value refs))
3824                   ;; These threads do not refer back to any other articles,
3825                   ;; so they're roots.
3826                   (setq threads (append (cdr (symbol-value refs)) threads))))
3827               gnus-newsgroup-dependencies)))
3828     threads))
3829
3830 ;; Build the thread tree.
3831 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3832   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3833
3834 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3835 if it was already present.
3836
3837 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3838 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3839 Message-IDs will be renamed to a unique Message-ID before being
3840 entered.
3841
3842 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3843   (let* ((id (mail-header-id header))
3844          (id-dep (and id (intern id dependencies)))
3845          parent-id ref ref-dep ref-header replaced)
3846     ;; Enter this `header' in the `dependencies' table.
3847     (cond
3848      ((not id-dep)
3849       (setq header nil))
3850      ;; The first two cases do the normal part: enter a new `header'
3851      ;; in the `dependencies' table.
3852      ((not (boundp id-dep))
3853       (set id-dep (list header)))
3854      ((null (car (symbol-value id-dep)))
3855       (setcar (symbol-value id-dep) header))
3856
3857      ;; From here the `header' was already present in the
3858      ;; `dependencies' table.
3859      (force-new
3860       ;; Overrides an existing entry;
3861       ;; just set the header part of the entry.
3862       (setcar (symbol-value id-dep) header)
3863       (setq replaced t))
3864
3865      ;; Renames the existing `header' to a unique Message-ID.
3866      ((not gnus-summary-ignore-duplicates)
3867       ;; An article with this Message-ID has already been seen.
3868       ;; We rename the Message-ID.
3869       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3870            (list header))
3871       (mail-header-set-id header id))
3872
3873      ;; The last case ignores an existing entry, except it adds any
3874      ;; additional Xrefs (in case the two articles came from different
3875      ;; servers.
3876      ;; Also sets `header' to `nil' meaning that the `dependencies'
3877      ;; table was *not* modified.
3878      (t
3879       (mail-header-set-xref
3880        (car (symbol-value id-dep))
3881        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3882                    "")
3883                (or (mail-header-xref header) "")))
3884       (setq header nil)))
3885
3886     (when (and header (not replaced))
3887       ;; First check that we are not creating a References loop.
3888       (setq parent-id (gnus-parent-id (mail-header-references header)))
3889       (setq ref parent-id)
3890       (while (and ref
3891                   (setq ref-dep (intern-soft ref dependencies))
3892                   (boundp ref-dep)
3893                   (setq ref-header (car (symbol-value ref-dep))))
3894         (if (string= id ref)
3895             ;; Yuk!  This is a reference loop.  Make the article be a
3896             ;; root article.
3897             (progn
3898               (mail-header-set-references (car (symbol-value id-dep)) "none")
3899               (setq ref nil)
3900               (setq parent-id nil))
3901           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3902       (setq ref-dep (intern (or parent-id "none") dependencies))
3903       (if (boundp ref-dep)
3904           (setcdr (symbol-value ref-dep)
3905                   (nconc (cdr (symbol-value ref-dep))
3906                          (list (symbol-value id-dep))))
3907         (set ref-dep (list nil (symbol-value id-dep)))))
3908     header))
3909
3910 (defun gnus-extract-message-id-from-in-reply-to (string)
3911   (if (string-match "<[^>]+>" string)
3912       (substring string (match-beginning 0) (match-end 0))
3913     nil))
3914
3915 (defun gnus-build-sparse-threads ()
3916   (let ((headers gnus-newsgroup-headers)
3917         (mail-parse-charset gnus-newsgroup-charset)
3918         (gnus-summary-ignore-duplicates t)
3919         header references generation relations
3920         subject child end new-child date)
3921     ;; First we create an alist of generations/relations, where
3922     ;; generations is how much we trust the relation, and the relation
3923     ;; is parent/child.
3924     (gnus-message 7 "Making sparse threads...")
3925     (save-excursion
3926       (nnheader-set-temp-buffer " *gnus sparse threads*")
3927       (while (setq header (pop headers))
3928         (when (and (setq references (mail-header-references header))
3929                    (not (string= references "")))
3930           (insert references)
3931           (setq child (mail-header-id header)
3932                 subject (mail-header-subject header)
3933                 date (mail-header-date header)
3934                 generation 0)
3935           (while (search-backward ">" nil t)
3936             (setq end (1+ (point)))
3937             (when (search-backward "<" nil t)
3938               (setq new-child (buffer-substring (point) end))
3939               (push (list (incf generation)
3940                           child (setq child new-child)
3941                           subject date)
3942                     relations)))
3943           (when child
3944             (push (list (1+ generation) child nil subject) relations))
3945           (erase-buffer)))
3946       (kill-buffer (current-buffer)))
3947     ;; Sort over trustworthiness.
3948     (mapcar
3949      (lambda (relation)
3950        (when (gnus-dependencies-add-header
3951               (make-full-mail-header-from-decoded-header
3952                gnus-reffed-article-number
3953                (nth 3 relation) "" (or (nth 4 relation) "")
3954                (nth 1 relation)
3955                (or (nth 2 relation) "") 0 0 "")
3956               gnus-newsgroup-dependencies nil)
3957          (push gnus-reffed-article-number gnus-newsgroup-limit)
3958          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3959          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3960                gnus-newsgroup-reads)
3961          (decf gnus-reffed-article-number)))
3962      (sort relations 'car-less-than-car))
3963     (gnus-message 7 "Making sparse threads...done")))
3964
3965 (defun gnus-build-old-threads ()
3966   ;; Look at all the articles that refer back to old articles, and
3967   ;; fetch the headers for the articles that aren't there.  This will
3968   ;; build complete threads - if the roots haven't been expired by the
3969   ;; server, that is.
3970   (let ((mail-parse-charset gnus-newsgroup-charset)
3971         id heads)
3972     (mapatoms
3973      (lambda (refs)
3974        (when (not (car (symbol-value refs)))
3975          (setq heads (cdr (symbol-value refs)))
3976          (while heads
3977            (if (memq (mail-header-number (caar heads))
3978                      gnus-newsgroup-dormant)
3979                (setq heads (cdr heads))
3980              (setq id (symbol-name refs))
3981              (while (and (setq id (gnus-build-get-header id))
3982                          (not (car (gnus-id-to-thread id)))))
3983              (setq heads nil)))))
3984      gnus-newsgroup-dependencies)))
3985
3986 (defsubst gnus-remove-odd-characters (string)
3987   "Translate STRING into something that doesn't contain weird characters."
3988   (mm-subst-char-in-string
3989    ?\r ?\-
3990    (mm-subst-char-in-string
3991     ?\n ?\- string)))
3992
3993 ;; This function has to be called with point after the article number
3994 ;; on the beginning of the line.
3995 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3996   (let ((eol (point-at-eol))
3997         (buffer (current-buffer))
3998         header references in-reply-to)
3999
4000     ;; overview: [num subject from date id refs chars lines misc]
4001     (unwind-protect
4002         (progn
4003           (narrow-to-region (point) eol)
4004           (unless (eobp)
4005             (forward-char))
4006
4007           (setq header
4008                 (make-full-mail-header
4009                  number                         ; number
4010                  (nnheader-nov-field)           ; subject
4011                  (nnheader-nov-field)           ; from
4012                  (nnheader-nov-field)           ; date
4013                  (nnheader-nov-read-message-id) ; id
4014                  (nnheader-nov-field)           ; refs
4015                  (nnheader-nov-read-integer)    ; chars
4016                  (nnheader-nov-read-integer)    ; lines
4017                  (unless (eobp)
4018                    (if (looking-at "Xref: ")
4019                        (goto-char (match-end 0)))
4020                    (nnheader-nov-field))        ; Xref
4021                  (nnheader-nov-parse-extra))))  ; extra
4022
4023       (widen))
4024
4025     (when (and (string= references "")
4026                (setq in-reply-to (mail-header-extra header))
4027                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4028       (mail-header-set-references
4029        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4030
4031     (when gnus-alter-header-function
4032       (funcall gnus-alter-header-function header))
4033     (gnus-dependencies-add-header header dependencies force-new)))
4034
4035 (defun gnus-build-get-header (id)
4036   "Look through the buffer of NOV lines and find the header to ID.
4037 Enter this line into the dependencies hash table, and return
4038 the id of the parent article (if any)."
4039   (let ((deps gnus-newsgroup-dependencies)
4040         found header)
4041     (prog1
4042         (save-excursion
4043           (set-buffer nntp-server-buffer)
4044           (let ((case-fold-search nil))
4045             (goto-char (point-min))
4046             (while (and (not found)
4047                         (search-forward id nil t))
4048               (beginning-of-line)
4049               (setq found (looking-at
4050                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4051                                    (regexp-quote id))))
4052               (or found (beginning-of-line 2)))
4053             (when found
4054               (beginning-of-line)
4055               (and
4056                (setq header (gnus-nov-parse-line
4057                              (read (current-buffer)) deps))
4058                (gnus-parent-id (mail-header-references header))))))
4059       (when header
4060         (let ((number (mail-header-number header)))
4061           (push number gnus-newsgroup-limit)
4062           (push header gnus-newsgroup-headers)
4063           (if (memq number gnus-newsgroup-unselected)
4064               (progn
4065                 (setq gnus-newsgroup-unreads
4066                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4067                                                number))
4068                 (setq gnus-newsgroup-unselected
4069                       (delq number gnus-newsgroup-unselected)))
4070             (push number gnus-newsgroup-ancient)))))))
4071
4072 (defun gnus-build-all-threads ()
4073   "Read all the headers."
4074   (let ((gnus-summary-ignore-duplicates t)
4075         (mail-parse-charset gnus-newsgroup-charset)
4076         (dependencies gnus-newsgroup-dependencies)
4077         header article)
4078     (save-excursion
4079       (set-buffer nntp-server-buffer)
4080       (let ((case-fold-search nil))
4081         (goto-char (point-min))
4082         (while (not (eobp))
4083           (ignore-errors
4084             (setq article (read (current-buffer))
4085                   header (gnus-nov-parse-line article dependencies)))
4086           (when header
4087             (save-excursion
4088               (set-buffer gnus-summary-buffer)
4089               (push header gnus-newsgroup-headers)
4090               (if (memq (setq article (mail-header-number header))
4091                         gnus-newsgroup-unselected)
4092                   (progn
4093                     (setq gnus-newsgroup-unreads
4094                           (gnus-add-to-sorted-list
4095                            gnus-newsgroup-unreads article))
4096                     (setq gnus-newsgroup-unselected
4097                           (delq article gnus-newsgroup-unselected)))
4098                 (push article gnus-newsgroup-ancient)))
4099             (forward-line 1)))))))
4100
4101 (defun gnus-summary-update-article-line (article header)
4102   "Update the line for ARTICLE using HEADER."
4103   (let* ((id (mail-header-id header))
4104          (thread (gnus-id-to-thread id)))
4105     (unless thread
4106       (error "Article in no thread"))
4107     ;; Update the thread.
4108     (setcar thread header)
4109     (gnus-summary-goto-subject article)
4110     (let* ((datal (gnus-data-find-list article))
4111            (data (car datal))
4112            (buffer-read-only nil)
4113            (level (gnus-summary-thread-level)))
4114       (gnus-delete-line)
4115       (let ((inserted (- (point)
4116                          (progn
4117                            (gnus-summary-insert-line
4118                             header level nil
4119                             (memq article gnus-newsgroup-undownloaded)
4120                             (gnus-article-mark article)
4121                             (memq article gnus-newsgroup-replied)
4122                             (memq article gnus-newsgroup-expirable)
4123                             ;; Only insert the Subject string when it's different
4124                             ;; from the previous Subject string.
4125                             (if (and
4126                                  gnus-show-threads
4127                                  (gnus-subject-equal
4128                                   (condition-case ()
4129                                       (mail-header-subject
4130                                        (gnus-data-header
4131                                         (cadr
4132                                          (gnus-data-find-list
4133                                           article
4134                                           (gnus-data-list t)))))
4135                                     ;; Error on the side of excessive subjects.
4136                                     (error ""))
4137                                   (mail-header-subject header)))
4138                                 ""
4139                               (mail-header-subject header))
4140                             nil (cdr (assq article gnus-newsgroup-scored))
4141                             (memq article gnus-newsgroup-processable))
4142                            (point)))))
4143         (when (cdr datal)
4144           (gnus-data-update-list
4145            (cdr datal)
4146            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4147
4148 (defun gnus-summary-update-article (article &optional iheader)
4149   "Update ARTICLE in the summary buffer."
4150   (set-buffer gnus-summary-buffer)
4151   (let* ((header (gnus-summary-article-header article))
4152          (id (mail-header-id header))
4153          (data (gnus-data-find article))
4154          (thread (gnus-id-to-thread id))
4155          (references (mail-header-references header))
4156          (parent
4157           (gnus-id-to-thread
4158            (or (gnus-parent-id
4159                 (when (and references
4160                            (not (equal "" references)))
4161                   references))
4162                "none")))
4163          (buffer-read-only nil)
4164          (old (car thread)))
4165     (when thread
4166       (unless iheader
4167         (setcar thread nil)
4168         (when parent
4169           (delq thread parent)))
4170       (if (gnus-summary-insert-subject id header)
4171           ;; Set the (possibly) new article number in the data structure.
4172           (gnus-data-set-number data (gnus-id-to-article id))
4173         (setcar thread old)
4174         nil))))
4175
4176 (defun gnus-rebuild-thread (id &optional line)
4177   "Rebuild the thread containing ID.
4178 If LINE, insert the rebuilt thread starting on line LINE."
4179   (let ((buffer-read-only nil)
4180         old-pos current thread data)
4181     (if (not gnus-show-threads)
4182         (setq thread (list (car (gnus-id-to-thread id))))
4183       ;; Get the thread this article is part of.
4184       (setq thread (gnus-remove-thread id)))
4185     (setq old-pos (point-at-bol))
4186     (setq current (save-excursion
4187                     (and (re-search-backward "[\r\n]" nil t)
4188                          (gnus-summary-article-number))))
4189     ;; If this is a gathered thread, we have to go some re-gathering.
4190     (when (stringp (car thread))
4191       (let ((subject (car thread))
4192             roots thr)
4193         (setq thread (cdr thread))
4194         (while thread
4195           (unless (memq (setq thr (gnus-id-to-thread
4196                                    (gnus-root-id
4197                                     (mail-header-id (caar thread)))))
4198                         roots)
4199             (push thr roots))
4200           (setq thread (cdr thread)))
4201         ;; We now have all (unique) roots.
4202         (if (= (length roots) 1)
4203             ;; All the loose roots are now one solid root.
4204             (setq thread (car roots))
4205           (setq thread (cons subject (gnus-sort-threads roots))))))
4206     (let (threads)
4207       ;; We then insert this thread into the summary buffer.
4208       (when line
4209         (goto-char (point-min))
4210         (forward-line (1- line)))
4211       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4212         (if gnus-show-threads
4213             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4214           (gnus-summary-prepare-unthreaded thread))
4215         (setq data (nreverse gnus-newsgroup-data))
4216         (setq threads gnus-newsgroup-threads))
4217       ;; We splice the new data into the data structure.
4218       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4219       ;;!!! then we want to insert at the beginning of the buffer.
4220       ;;!!! That happens to be true with Gnus now, but that may
4221       ;;!!! change in the future.  Perhaps.
4222       (gnus-data-enter-list
4223        (if line nil current) data (- (point) old-pos))
4224       (setq gnus-newsgroup-threads
4225             (nconc threads gnus-newsgroup-threads))
4226       (gnus-data-compute-positions))))
4227
4228 (defun gnus-number-to-header (number)
4229   "Return the header for article NUMBER."
4230   (let ((headers gnus-newsgroup-headers))
4231     (while (and headers
4232                 (not (= number (mail-header-number (car headers)))))
4233       (pop headers))
4234     (when headers
4235       (car headers))))
4236
4237 (defun gnus-parent-headers (in-headers &optional generation)
4238   "Return the headers of the GENERATIONeth parent of HEADERS."
4239   (unless generation
4240     (setq generation 1))
4241   (let ((parent t)
4242         (headers in-headers)
4243         references)
4244     (while (and parent
4245                 (not (zerop generation))
4246                 (setq references (mail-header-references headers)))
4247       (setq headers (if (and references
4248                              (setq parent (gnus-parent-id references)))
4249                         (car (gnus-id-to-thread parent))
4250                       nil))
4251       (decf generation))
4252     (and (not (eq headers in-headers))
4253          headers)))
4254
4255 (defun gnus-id-to-thread (id)
4256   "Return the (sub-)thread where ID appears."
4257   (gnus-gethash id gnus-newsgroup-dependencies))
4258
4259 (defun gnus-id-to-article (id)
4260   "Return the article number of ID."
4261   (let ((thread (gnus-id-to-thread id)))
4262     (when (and thread
4263                (car thread))
4264       (mail-header-number (car thread)))))
4265
4266 (defun gnus-id-to-header (id)
4267   "Return the article headers of ID."
4268   (car (gnus-id-to-thread id)))
4269
4270 (defun gnus-article-displayed-root-p (article)
4271   "Say whether ARTICLE is a root(ish) article."
4272   (let ((level (gnus-summary-thread-level article))
4273         (refs (mail-header-references  (gnus-summary-article-header article)))
4274         particle)
4275     (cond
4276      ((null level) nil)
4277      ((zerop level) t)
4278      ((null refs) t)
4279      ((null (gnus-parent-id refs)) t)
4280      ((and (= 1 level)
4281            (null (setq particle (gnus-id-to-article
4282                                  (gnus-parent-id refs))))
4283            (null (gnus-summary-thread-level particle)))))))
4284
4285 (defun gnus-root-id (id)
4286   "Return the id of the root of the thread where ID appears."
4287   (let (last-id prev)
4288     (while (and id (setq prev (car (gnus-id-to-thread id))))
4289       (setq last-id id
4290             id (gnus-parent-id (mail-header-references prev))))
4291     last-id))
4292
4293 (defun gnus-articles-in-thread (thread)
4294   "Return the list of articles in THREAD."
4295   (cons (mail-header-number (car thread))
4296         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4297
4298 (defun gnus-remove-thread (id &optional dont-remove)
4299   "Remove the thread that has ID in it."
4300   (let (headers thread last-id)
4301     ;; First go up in this thread until we find the root.
4302     (setq last-id (gnus-root-id id)
4303           headers (message-flatten-list (gnus-id-to-thread last-id)))
4304     ;; We have now found the real root of this thread.  It might have
4305     ;; been gathered into some loose thread, so we have to search
4306     ;; through the threads to find the thread we wanted.
4307     (let ((threads gnus-newsgroup-threads)
4308           sub)
4309       (while threads
4310         (setq sub (car threads))
4311         (if (stringp (car sub))
4312             ;; This is a gathered thread, so we look at the roots
4313             ;; below it to find whether this article is in this
4314             ;; gathered root.
4315             (progn
4316               (setq sub (cdr sub))
4317               (while sub
4318                 (when (member (caar sub) headers)
4319                   (setq thread (car threads)
4320                         threads nil
4321                         sub nil))
4322                 (setq sub (cdr sub))))
4323           ;; It's an ordinary thread, so we check it.
4324           (when (eq (car sub) (car headers))
4325             (setq thread sub
4326                   threads nil)))
4327         (setq threads (cdr threads)))
4328       ;; If this article is in no thread, then it's a root.
4329       (if thread
4330           (unless dont-remove
4331             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4332         (setq thread (gnus-id-to-thread last-id)))
4333       (when thread
4334         (prog1
4335             thread                      ; We return this thread.
4336           (unless dont-remove
4337             (if (stringp (car thread))
4338                 (progn
4339                   ;; If we use dummy roots, then we have to remove the
4340                   ;; dummy root as well.
4341                   (when (eq gnus-summary-make-false-root 'dummy)
4342                     ;; We go to the dummy root by going to
4343                     ;; the first sub-"thread", and then one line up.
4344                     (gnus-summary-goto-article
4345                      (mail-header-number (caadr thread)))
4346                     (forward-line -1)
4347                     (gnus-delete-line)
4348                     (gnus-data-compute-positions))
4349                   (setq thread (cdr thread))
4350                   (while thread
4351                     (gnus-remove-thread-1 (car thread))
4352                     (setq thread (cdr thread))))
4353               (gnus-remove-thread-1 thread))))))))
4354
4355 (defun gnus-remove-thread-1 (thread)
4356   "Remove the thread THREAD recursively."
4357   (let ((number (mail-header-number (pop thread)))
4358         d)
4359     (setq thread (reverse thread))
4360     (while thread
4361       (gnus-remove-thread-1 (pop thread)))
4362     (when (setq d (gnus-data-find number))
4363       (goto-char (gnus-data-pos d))
4364       (gnus-summary-show-thread)
4365       (gnus-data-remove
4366        number
4367        (- (point-at-bol)
4368           (prog1
4369               (1+ (point-at-eol))
4370             (gnus-delete-line)))))))
4371
4372 (defun gnus-sort-threads-1 (threads func)
4373   (sort (mapcar (lambda (thread)
4374                   (cons (car thread)
4375                         (and (cdr thread)
4376                              (gnus-sort-threads-1 (cdr thread) func))))
4377                 threads) func))
4378
4379 (defun gnus-sort-threads (threads)
4380   "Sort THREADS."
4381   (if (not gnus-thread-sort-functions)
4382       threads
4383     (gnus-message 8 "Sorting threads...")
4384     (let ((max-lisp-eval-depth 5000))
4385       (prog1 (gnus-sort-threads-1
4386          threads
4387          (gnus-make-sort-function gnus-thread-sort-functions))
4388         (gnus-message 8 "Sorting threads...done")))))
4389
4390 (defun gnus-sort-articles (articles)
4391   "Sort ARTICLES."
4392   (when gnus-article-sort-functions
4393     (gnus-message 7 "Sorting articles...")
4394     (prog1
4395         (setq gnus-newsgroup-headers
4396               (sort articles (gnus-make-sort-function
4397                               gnus-article-sort-functions)))
4398       (gnus-message 7 "Sorting articles...done"))))
4399
4400 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4401 (defmacro gnus-thread-header (thread)
4402   "Return header of first article in THREAD.
4403 Note that THREAD must never, ever be anything else than a variable -
4404 using some other form will lead to serious barfage."
4405   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4406   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4407   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4408         (vector thread) 2))
4409
4410 (defsubst gnus-article-sort-by-number (h1 h2)
4411   "Sort articles by article number."
4412   (< (mail-header-number h1)
4413      (mail-header-number h2)))
4414
4415 (defun gnus-thread-sort-by-number (h1 h2)
4416   "Sort threads by root article number."
4417   (gnus-article-sort-by-number
4418    (gnus-thread-header h1) (gnus-thread-header h2)))
4419
4420 (defsubst gnus-article-sort-by-random (h1 h2)
4421   "Sort articles by article number."
4422   (zerop (random 2)))
4423
4424 (defun gnus-thread-sort-by-random (h1 h2)
4425   "Sort threads by root article number."
4426   (gnus-article-sort-by-random
4427    (gnus-thread-header h1) (gnus-thread-header h2)))
4428
4429 (defsubst gnus-article-sort-by-lines (h1 h2)
4430   "Sort articles by article Lines header."
4431   (< (mail-header-lines h1)
4432      (mail-header-lines h2)))
4433
4434 (defun gnus-thread-sort-by-lines (h1 h2)
4435   "Sort threads by root article Lines header."
4436   (gnus-article-sort-by-lines
4437    (gnus-thread-header h1) (gnus-thread-header h2)))
4438
4439 (defsubst gnus-article-sort-by-chars (h1 h2)
4440   "Sort articles by octet length."
4441   (< (mail-header-chars h1)
4442      (mail-header-chars h2)))
4443
4444 (defun gnus-thread-sort-by-chars (h1 h2)
4445   "Sort threads by root article octet length."
4446   (gnus-article-sort-by-chars
4447    (gnus-thread-header h1) (gnus-thread-header h2)))
4448
4449 (defsubst gnus-article-sort-by-author (h1 h2)
4450   "Sort articles by root author."
4451   (string-lessp
4452    (let ((addr (car (mime-entity-read-field h1 'From))))
4453      (or (std11-full-name-string addr)
4454          (std11-address-string addr)
4455          ""))
4456    (let ((addr (car (mime-entity-read-field h2 'From))))
4457      (or (std11-full-name-string addr)
4458          (std11-address-string addr)
4459          ""))
4460    ))
4461
4462 (defun gnus-thread-sort-by-author (h1 h2)
4463   "Sort threads by root author."
4464   (gnus-article-sort-by-author
4465    (gnus-thread-header h1)  (gnus-thread-header h2)))
4466
4467 (defsubst gnus-article-sort-by-subject (h1 h2)
4468   "Sort articles by root subject."
4469   (string-lessp
4470    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4471    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4472
4473 (defun gnus-thread-sort-by-subject (h1 h2)
4474   "Sort threads by root subject."
4475   (gnus-article-sort-by-subject
4476    (gnus-thread-header h1) (gnus-thread-header h2)))
4477
4478 (defsubst gnus-article-sort-by-date (h1 h2)
4479   "Sort articles by root article date."
4480   (time-less-p
4481    (gnus-date-get-time (mail-header-date h1))
4482    (gnus-date-get-time (mail-header-date h2))))
4483
4484 (defun gnus-thread-sort-by-date (h1 h2)
4485   "Sort threads by root article date."
4486   (gnus-article-sort-by-date
4487    (gnus-thread-header h1) (gnus-thread-header h2)))
4488
4489 (defsubst gnus-article-sort-by-score (h1 h2)
4490   "Sort articles by root article score.
4491 Unscored articles will be counted as having a score of zero."
4492   (> (or (cdr (assq (mail-header-number h1)
4493                     gnus-newsgroup-scored))
4494          gnus-summary-default-score 0)
4495      (or (cdr (assq (mail-header-number h2)
4496                     gnus-newsgroup-scored))
4497          gnus-summary-default-score 0)))
4498
4499 (defun gnus-thread-sort-by-score (h1 h2)
4500   "Sort threads by root article score."
4501   (gnus-article-sort-by-score
4502    (gnus-thread-header h1) (gnus-thread-header h2)))
4503
4504 (defun gnus-thread-sort-by-total-score (h1 h2)
4505   "Sort threads by the sum of all scores in the thread.
4506 Unscored articles will be counted as having a score of zero."
4507   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4508
4509 (defun gnus-thread-total-score (thread)
4510   ;; This function find the total score of THREAD.
4511   (cond
4512    ((null thread)
4513     0)
4514    ((consp thread)
4515     (if (stringp (car thread))
4516         (apply gnus-thread-score-function 0
4517                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4518       (gnus-thread-total-score-1 thread)))
4519    (t
4520     (gnus-thread-total-score-1 (list thread)))))
4521
4522 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4523   "Sort threads such that the thread with the most recently arrived article comes first."
4524   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4525
4526 (defun gnus-thread-highest-number (thread)
4527   "Return the highest article number in THREAD."
4528   (apply 'max (mapcar (lambda (header)
4529                         (mail-header-number header))
4530                       (message-flatten-list thread))))
4531
4532 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4533   "Sort threads such that the thread with the most recently dated article comes first."
4534   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4535
4536 (defun gnus-thread-latest-date (thread)
4537   "Return the highest article date in THREAD."
4538   (let ((previous-time 0))
4539     (apply 'max
4540            (mapcar
4541             (lambda (header)
4542               (setq previous-time
4543                     (condition-case ()
4544                         (time-to-seconds (mail-header-parse-date
4545                                           (mail-header-date header)))
4546                       (error previous-time))))
4547             (sort
4548              (message-flatten-list thread)
4549              (lambda (h1 h2)
4550                (< (mail-header-number h1)
4551                   (mail-header-number h2))))))))
4552
4553 (defun gnus-thread-total-score-1 (root)
4554   ;; This function find the total score of the thread below ROOT.
4555   (setq root (car root))
4556   (apply gnus-thread-score-function
4557          (or (append
4558               (mapcar 'gnus-thread-total-score
4559                       (cdr (gnus-id-to-thread (mail-header-id root))))
4560               (when (> (mail-header-number root) 0)
4561                 (list (or (cdr (assq (mail-header-number root)
4562                                      gnus-newsgroup-scored))
4563                           gnus-summary-default-score 0))))
4564              (list gnus-summary-default-score)
4565              '(0))))
4566
4567 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4568 (defvar gnus-tmp-prev-subject nil)
4569 (defvar gnus-tmp-false-parent nil)
4570 (defvar gnus-tmp-root-expunged nil)
4571 (defvar gnus-tmp-dummy-line nil)
4572
4573 (eval-when-compile (defvar gnus-tmp-header))
4574 (defun gnus-extra-header (type &optional header)
4575   "Return the extra header of TYPE."
4576   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4577       ""))
4578
4579 (defvar gnus-tmp-thread-tree-header-string "")
4580
4581 (defcustom gnus-sum-thread-tree-root "> "
4582   "With %B spec, used for the root of a thread.
4583 If nil, use subject instead."
4584   :type '(radio (const :format "%v  " nil) (string :size 0))
4585   :group 'gnus-thread)
4586 (defcustom gnus-sum-thread-tree-false-root "> "
4587   "With %B spec, used for a false root of a thread.
4588 If nil, use subject instead."
4589   :type '(radio (const :format "%v  " nil) (string :size 0))
4590   :group 'gnus-thread)
4591 (defcustom gnus-sum-thread-tree-single-indent ""
4592   "With %B spec, used for a thread with just one message.
4593 If nil, use subject instead."
4594   :type '(radio (const :format "%v  " nil) (string :size 0))
4595   :group 'gnus-thread)
4596 (defcustom gnus-sum-thread-tree-vertical "| "
4597   "With %B spec, used for drawing a vertical line."
4598   :type 'string
4599   :group 'gnus-thread)
4600 (defcustom gnus-sum-thread-tree-indent "  "
4601   "With %B spec, used for indenting."
4602   :type 'string
4603   :group 'gnus-thread)
4604 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4605   "With %B spec, used for a leaf with brothers."
4606   :type 'string
4607   :group 'gnus-thread)
4608 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4609   "With %B spec, used for a leaf without brothers."
4610   :type 'string
4611   :group 'gnus-thread)
4612
4613 (defun gnus-summary-prepare-threads (threads)
4614   "Prepare summary buffer from THREADS and indentation LEVEL.
4615 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4616 or a straight list of headers."
4617   (gnus-message 7 "Generating summary...")
4618
4619   (setq gnus-newsgroup-threads threads)
4620   (beginning-of-line)
4621
4622   (let ((gnus-tmp-level 0)
4623         (default-score (or gnus-summary-default-score 0))
4624         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4625         (building-line-count gnus-summary-display-while-building)
4626         (building-count (integerp gnus-summary-display-while-building))
4627         thread number subject stack state gnus-tmp-gathered beg-match
4628         new-roots gnus-tmp-new-adopts thread-end simp-subject
4629         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4630         gnus-tmp-replied gnus-tmp-subject-or-nil
4631         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4632         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4633         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4634         tree-stack)
4635
4636     (setq gnus-tmp-prev-subject nil
4637           gnus-tmp-thread-tree-header-string "")
4638
4639     (if (vectorp (car threads))
4640         ;; If this is a straight (sic) list of headers, then a
4641         ;; threaded summary display isn't required, so we just create
4642         ;; an unthreaded one.
4643         (gnus-summary-prepare-unthreaded threads)
4644
4645       ;; Do the threaded display.
4646
4647       (if gnus-summary-display-while-building
4648           (switch-to-buffer (buffer-name)))
4649       (while (or threads stack gnus-tmp-new-adopts new-roots)
4650
4651         (if (and (= gnus-tmp-level 0)
4652                  (or (not stack)
4653                      (= (caar stack) 0))
4654                  (not gnus-tmp-false-parent)
4655                  (or gnus-tmp-new-adopts new-roots))
4656             (if gnus-tmp-new-adopts
4657                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4658                       thread (list (car gnus-tmp-new-adopts))
4659                       gnus-tmp-header (caar thread)
4660                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4661               (when new-roots
4662                 (setq thread (list (car new-roots))
4663                       gnus-tmp-header (caar thread)
4664                       new-roots (cdr new-roots))))
4665
4666           (if threads
4667               ;; If there are some threads, we do them before the
4668               ;; threads on the stack.
4669               (setq thread threads
4670                     gnus-tmp-header (caar thread))
4671             ;; There were no current threads, so we pop something off
4672             ;; the stack.
4673             (setq state (car stack)
4674                   gnus-tmp-level (car state)
4675                   tree-stack (cadr state)
4676                   thread (caddr state)
4677                   stack (cdr stack)
4678                   gnus-tmp-header (caar thread))))
4679
4680         (setq gnus-tmp-false-parent nil)
4681         (setq gnus-tmp-root-expunged nil)
4682         (setq thread-end nil)
4683
4684         (if (stringp gnus-tmp-header)
4685             ;; The header is a dummy root.
4686             (cond
4687              ((eq gnus-summary-make-false-root 'adopt)
4688               ;; We let the first article adopt the rest.
4689               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4690                                                (cddar thread)))
4691               (setq gnus-tmp-gathered
4692                     (nconc (mapcar
4693                             (lambda (h) (mail-header-number (car h)))
4694                             (cddar thread))
4695                            gnus-tmp-gathered))
4696               (setq thread (cons (list (caar thread)
4697                                        (cadar thread))
4698                                  (cdr thread)))
4699               (setq gnus-tmp-level -1
4700                     gnus-tmp-false-parent t))
4701              ((eq gnus-summary-make-false-root 'empty)
4702               ;; We print adopted articles with empty subject fields.
4703               (setq gnus-tmp-gathered
4704                     (nconc (mapcar
4705                             (lambda (h) (mail-header-number (car h)))
4706                             (cddar thread))
4707                            gnus-tmp-gathered))
4708               (setq gnus-tmp-level -1))
4709              ((eq gnus-summary-make-false-root 'dummy)
4710               ;; We remember that we probably want to output a dummy
4711               ;; root.
4712               (setq gnus-tmp-dummy-line gnus-tmp-header)
4713               (setq gnus-tmp-prev-subject gnus-tmp-header))
4714              (t
4715               ;; We do not make a root for the gathered
4716               ;; sub-threads at all.
4717               (setq gnus-tmp-level -1)))
4718
4719           (setq number (mail-header-number gnus-tmp-header)
4720                 subject (mail-header-subject gnus-tmp-header)
4721                 simp-subject (gnus-simplify-subject-fully subject))
4722
4723           (cond
4724            ;; If the thread has changed subject, we might want to make
4725            ;; this subthread into a root.
4726            ((and (null gnus-thread-ignore-subject)
4727                  (not (zerop gnus-tmp-level))
4728                  gnus-tmp-prev-subject
4729                  (not (string= gnus-tmp-prev-subject simp-subject)))
4730             (setq new-roots (nconc new-roots (list (car thread)))
4731                   thread-end t
4732                   gnus-tmp-header nil))
4733            ;; If the article lies outside the current limit,
4734            ;; then we do not display it.
4735            ((not (memq number gnus-newsgroup-limit))
4736             (setq gnus-tmp-gathered
4737                   (nconc (mapcar
4738                           (lambda (h) (mail-header-number (car h)))
4739                           (cdar thread))
4740                          gnus-tmp-gathered))
4741             (setq gnus-tmp-new-adopts (if (cdar thread)
4742                                           (append gnus-tmp-new-adopts
4743                                                   (cdar thread))
4744                                         gnus-tmp-new-adopts)
4745                   thread-end t
4746                   gnus-tmp-header nil)
4747             (when (zerop gnus-tmp-level)
4748               (setq gnus-tmp-root-expunged t)))
4749            ;; Perhaps this article is to be marked as read?
4750            ((and gnus-summary-mark-below
4751                  (< (or (cdr (assq number gnus-newsgroup-scored))
4752                         default-score)
4753                     gnus-summary-mark-below)
4754                  ;; Don't touch sparse articles.
4755                  (not (gnus-summary-article-sparse-p number))
4756                  (not (gnus-summary-article-ancient-p number)))
4757             (setq gnus-newsgroup-unreads
4758                   (delq number gnus-newsgroup-unreads))
4759             (if gnus-newsgroup-auto-expire
4760                 (setq gnus-newsgroup-expirable
4761                       (gnus-add-to-sorted-list
4762                        gnus-newsgroup-expirable number))
4763               (push (cons number gnus-low-score-mark)
4764                     gnus-newsgroup-reads))))
4765
4766           (when gnus-tmp-header
4767             ;; We may have an old dummy line to output before this
4768             ;; article.
4769             (when (and gnus-tmp-dummy-line
4770                        (gnus-subject-equal
4771                         gnus-tmp-dummy-line
4772                         (mail-header-subject gnus-tmp-header)))
4773               (gnus-summary-insert-dummy-line
4774                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4775               (setq gnus-tmp-dummy-line nil))
4776
4777             ;; Compute the mark.
4778             (setq gnus-tmp-unread (gnus-article-mark number))
4779
4780             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4781                                   gnus-tmp-header gnus-tmp-level)
4782                   gnus-newsgroup-data)
4783
4784             ;; Actually insert the line.
4785             (setq
4786              gnus-tmp-subject-or-nil
4787              (cond
4788               ((and gnus-thread-ignore-subject
4789                     gnus-tmp-prev-subject
4790                     (not (string= gnus-tmp-prev-subject simp-subject)))
4791                subject)
4792               ((zerop gnus-tmp-level)
4793                (if (and (eq gnus-summary-make-false-root 'empty)
4794                         (memq number gnus-tmp-gathered)
4795                         gnus-tmp-prev-subject
4796                         (string= gnus-tmp-prev-subject simp-subject))
4797                    gnus-summary-same-subject
4798                  subject))
4799               (t gnus-summary-same-subject)))
4800             (if (and (eq gnus-summary-make-false-root 'adopt)
4801                      (= gnus-tmp-level 1)
4802                      (memq number gnus-tmp-gathered))
4803                 (setq gnus-tmp-opening-bracket ?\<
4804                       gnus-tmp-closing-bracket ?\>)
4805               (setq gnus-tmp-opening-bracket ?\[
4806                     gnus-tmp-closing-bracket ?\]))
4807             (setq
4808              gnus-tmp-indentation
4809              (aref gnus-thread-indent-array gnus-tmp-level)
4810              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4811              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4812                                 gnus-summary-default-score 0)
4813              gnus-tmp-score-char
4814              (if (or (null gnus-summary-default-score)
4815                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4816                          gnus-summary-zcore-fuzz))
4817                  ?\ ;;;Whitespace
4818                (if (< gnus-tmp-score gnus-summary-default-score)
4819                    gnus-score-below-mark gnus-score-over-mark))
4820              gnus-tmp-replied
4821              (cond ((memq number gnus-newsgroup-processable)
4822                     gnus-process-mark)
4823                    ((memq number gnus-newsgroup-cached)
4824                     gnus-cached-mark)
4825                    ((memq number gnus-newsgroup-replied)
4826                     gnus-replied-mark)
4827                    ((memq number gnus-newsgroup-forwarded)
4828                     gnus-forwarded-mark)
4829                    ((memq number gnus-newsgroup-saved)
4830                     gnus-saved-mark)
4831                    ((memq number gnus-newsgroup-recent)
4832                     gnus-recent-mark)
4833                    ((memq number gnus-newsgroup-unseen)
4834                     gnus-unseen-mark)
4835                    (t gnus-no-mark))
4836              gnus-tmp-downloaded
4837              (cond ((memq number gnus-newsgroup-undownloaded)
4838                     gnus-undownloaded-mark)
4839                    (gnus-newsgroup-agentized
4840                     gnus-downloaded-mark)
4841                    (t
4842                     gnus-no-mark))
4843              gnus-tmp-from (mail-header-from gnus-tmp-header)
4844              gnus-tmp-name
4845              (cond
4846               ((string-match "<[^>]+> *$" gnus-tmp-from)
4847                (setq beg-match (match-beginning 0))
4848                (or (and (string-match "^\".+\"" gnus-tmp-from)
4849                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4850                    (substring gnus-tmp-from 0 beg-match)))
4851               ((string-match "(.+)" gnus-tmp-from)
4852                (substring gnus-tmp-from
4853                           (1+ (match-beginning 0)) (1- (match-end 0))))
4854               (t gnus-tmp-from))
4855
4856              ;; Do the %B string
4857              gnus-tmp-thread-tree-header-string
4858              (cond
4859               ((not gnus-show-threads) "")
4860               ((zerop gnus-tmp-level)
4861                (cond ((cdar thread)
4862                       (or gnus-sum-thread-tree-root subject))
4863                      (gnus-tmp-new-adopts
4864                       (or gnus-sum-thread-tree-false-root subject))
4865                      (t
4866                       (or gnus-sum-thread-tree-single-indent subject))))
4867               (t
4868                (concat (apply 'concat
4869                               (mapcar (lambda (item)
4870                                         (if (= item 1)
4871                                             gnus-sum-thread-tree-vertical
4872                                           gnus-sum-thread-tree-indent))
4873                                       (cdr (reverse tree-stack))))
4874                        (if (nth 1 thread)
4875                            gnus-sum-thread-tree-leaf-with-other
4876                          gnus-sum-thread-tree-single-leaf)))))
4877             (when (string= gnus-tmp-name "")
4878               (setq gnus-tmp-name gnus-tmp-from))
4879             (unless (numberp gnus-tmp-lines)
4880               (setq gnus-tmp-lines -1))
4881             (if (= gnus-tmp-lines -1)
4882                 (setq gnus-tmp-lines "?")
4883               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4884               (gnus-put-text-property
4885              (point)
4886              (progn (eval gnus-summary-line-format-spec) (point))
4887                'gnus-number number)
4888             (when gnus-visual-p
4889               (forward-line -1)
4890               (gnus-run-hooks 'gnus-summary-update-hook)
4891               (forward-line 1))
4892
4893             (setq gnus-tmp-prev-subject simp-subject)))
4894
4895         (when (nth 1 thread)
4896           (push (list (max 0 gnus-tmp-level)
4897                       (copy-sequence tree-stack)
4898                       (nthcdr 1 thread))
4899                 stack))
4900         (push (if (nth 1 thread) 1 0) tree-stack)
4901         (incf gnus-tmp-level)
4902         (setq threads (if thread-end nil (cdar thread)))
4903         (if gnus-summary-display-while-building
4904             (if building-count
4905                 (progn
4906                   ;; use a set frequency
4907                   (setq building-line-count (1- building-line-count))
4908                   (when (= building-line-count 0)
4909                     (sit-for 0)
4910                     (setq building-line-count
4911                           gnus-summary-display-while-building)))
4912               ;; always
4913               (sit-for 0)))
4914         (unless threads
4915           (setq gnus-tmp-level 0)))))
4916   (gnus-message 7 "Generating summary...done"))
4917
4918 (defun gnus-summary-prepare-unthreaded (headers)
4919   "Generate an unthreaded summary buffer based on HEADERS."
4920   (let (header number mark)
4921
4922     (beginning-of-line)
4923
4924     (while headers
4925       ;; We may have to root out some bad articles...
4926       (when (memq (setq number (mail-header-number
4927                                 (setq header (pop headers))))
4928                   gnus-newsgroup-limit)
4929         ;; Mark article as read when it has a low score.
4930         (when (and gnus-summary-mark-below
4931                    (< (or (cdr (assq number gnus-newsgroup-scored))
4932                           gnus-summary-default-score 0)
4933                       gnus-summary-mark-below)
4934                    (not (gnus-summary-article-ancient-p number)))
4935           (setq gnus-newsgroup-unreads
4936                 (delq number gnus-newsgroup-unreads))
4937           (if gnus-newsgroup-auto-expire
4938               (push number gnus-newsgroup-expirable)
4939             (push (cons number gnus-low-score-mark)
4940                   gnus-newsgroup-reads)))
4941
4942         (setq mark (gnus-article-mark number))
4943         (push (gnus-data-make number mark (1+ (point)) header 0)
4944               gnus-newsgroup-data)
4945         (gnus-summary-insert-line
4946          header 0 number
4947          (memq number gnus-newsgroup-undownloaded)
4948          mark (memq number gnus-newsgroup-replied)
4949          (memq number gnus-newsgroup-expirable)
4950          (mail-header-subject header) nil
4951          (cdr (assq number gnus-newsgroup-scored))
4952          (memq number gnus-newsgroup-processable))))))
4953
4954 (defun gnus-summary-remove-list-identifiers ()
4955   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4956   (let ((regexp (if (consp gnus-list-identifiers)
4957                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4958                   gnus-list-identifiers))
4959         changed subject)
4960     (when regexp
4961       (dolist (header gnus-newsgroup-headers)
4962         (setq subject (mail-header-subject header)
4963               changed nil)
4964         (while (string-match
4965                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4966                 subject)
4967           (setq subject
4968                 (concat (substring subject 0 (match-beginning 2))
4969                         (substring subject (match-end 0)))
4970                 changed t))
4971         (when (and changed
4972                    (string-match
4973                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4974           (setq subject
4975                 (concat (substring subject 0 (match-beginning 1))
4976                         (substring subject (match-end 1)))))
4977         (when changed
4978           (mail-header-set-subject header subject))))))
4979
4980 (defun gnus-fetch-headers (articles)
4981   "Fetch headers of ARTICLES."
4982   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4983     (gnus-message 5 "Fetching headers for %s..." name)
4984     (prog1
4985         (if (eq 'nov
4986                 (setq gnus-headers-retrieved-by
4987                       (gnus-retrieve-headers
4988                        articles gnus-newsgroup-name
4989                        ;; We might want to fetch old headers, but
4990                        ;; not if there is only 1 article.
4991                        (and (or (and
4992                                  (not (eq gnus-fetch-old-headers 'some))
4993                                  (not (numberp gnus-fetch-old-headers)))
4994                                 (> (length articles) 1))
4995                             gnus-fetch-old-headers))))
4996             (gnus-get-newsgroup-headers-xover
4997              articles nil nil gnus-newsgroup-name t)
4998           (gnus-get-newsgroup-headers))
4999       (gnus-message 5 "Fetching headers for %s...done" name))))
5000
5001 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5002   "Select newsgroup GROUP.
5003 If READ-ALL is non-nil, all articles in the group are selected.
5004 If SELECT-ARTICLES, only select those articles from GROUP."
5005   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5006          ;;!!! Dirty hack; should be removed.
5007          (gnus-summary-ignore-duplicates
5008           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5009               t
5010             gnus-summary-ignore-duplicates))
5011          (info (nth 2 entry))
5012          articles fetched-articles cached)
5013
5014     (unless (gnus-check-server
5015              (set (make-local-variable 'gnus-current-select-method)
5016                   (gnus-find-method-for-group group)))
5017       (error "Couldn't open server"))
5018
5019     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5020         (gnus-activate-group group)     ; Or we can activate it...
5021         (progn                          ; Or we bug out.
5022           (when (equal major-mode 'gnus-summary-mode)
5023             (gnus-kill-buffer (current-buffer)))
5024           (error "Couldn't activate group %s: %s"
5025                  group (gnus-status-message group))))
5026
5027     (unless (gnus-request-group group t)
5028       (when (equal major-mode 'gnus-summary-mode)
5029         (gnus-kill-buffer (current-buffer)))
5030       (error "Couldn't request group %s: %s"
5031              group (gnus-status-message group)))
5032
5033     (when gnus-agent
5034       ;; The agent may be storing articles that are no longer in the
5035       ;; server's active range.  If that is the case, the active range
5036       ;; needs to be expanded such that the agent's articles can be
5037       ;; included in the summary.
5038       (let* ((gnus-command-method (gnus-find-method-for-group group))
5039              (alist (gnus-agent-load-alist group))
5040              (active (gnus-active group)))
5041         (if (and (car alist)
5042                  (< (caar alist) (car active)))
5043             (gnus-set-active group (cons (caar alist) (cdr active)))))
5044
5045       (setq gnus-summary-use-undownloaded-faces
5046             (gnus-agent-find-parameter
5047              group
5048              'agent-enable-undownloaded-faces)))
5049
5050     (setq gnus-newsgroup-name group
5051           gnus-newsgroup-unselected nil
5052           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5053
5054     (let ((display (gnus-group-find-parameter group 'display)))
5055       (setq gnus-newsgroup-display
5056             (cond
5057              ((not (zerop (or (car-safe read-all) 0)))
5058               ;; The user entered the group with C-u SPC/RET, let's show
5059               ;; all articles.
5060               'gnus-not-ignore)
5061              ((eq display 'all)
5062               'gnus-not-ignore)
5063              ((arrayp display)
5064               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5065              ((numberp display)
5066               ;; The following is probably the "correct" solution, but
5067               ;; it makes Gnus fetch all headers and then limit the
5068               ;; articles (which is slow), so instead we hack the
5069               ;; select-articles parameter instead. -- Simon Josefsson
5070               ;; <jas@kth.se>
5071               ;;
5072               ;; (gnus-byte-compile
5073               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5074               ;;                         display)))))
5075               (setq select-articles
5076                     (gnus-uncompress-range
5077                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5078                              (if (> tmp 0)
5079                                  tmp
5080                                1))
5081                            (cdr (gnus-active group)))))
5082               nil)
5083              (t
5084               nil))))
5085
5086     (gnus-summary-setup-default-charset)
5087
5088     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5089     (when (gnus-virtual-group-p group)
5090       (setq cached gnus-newsgroup-cached))
5091
5092     (setq gnus-newsgroup-unreads
5093           (gnus-sorted-ndifference
5094            (gnus-sorted-ndifference gnus-newsgroup-unreads
5095                                     gnus-newsgroup-marked)
5096            gnus-newsgroup-dormant))
5097
5098     (setq gnus-newsgroup-processable nil)
5099
5100     (gnus-update-read-articles group gnus-newsgroup-unreads)
5101
5102     ;; Adjust and set lists of article marks.
5103     (when info
5104       (gnus-adjust-marked-articles info))
5105     (if (setq articles select-articles)
5106         (setq gnus-newsgroup-unselected
5107               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5108       (setq articles (gnus-articles-to-read group read-all)))
5109
5110     (cond
5111      ((null articles)
5112       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5113       'quit)
5114      ((eq articles 0) nil)
5115      (t
5116       ;; Init the dependencies hash table.
5117       (setq gnus-newsgroup-dependencies
5118             (gnus-make-hashtable (length articles)))
5119       (gnus-set-global-variables)
5120       ;; Retrieve the headers and read them in.
5121
5122       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5123
5124       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5125       (when cached
5126         (setq gnus-newsgroup-cached cached))
5127
5128       ;; Suppress duplicates?
5129       (when gnus-suppress-duplicates
5130         (gnus-dup-suppress-articles))
5131
5132       ;; Set the initial limit.
5133       (setq gnus-newsgroup-limit (copy-sequence articles))
5134       ;; Remove canceled articles from the list of unread articles.
5135       (setq fetched-articles
5136             (mapcar (lambda (headers) (mail-header-number headers))
5137                     gnus-newsgroup-headers))
5138       (setq gnus-newsgroup-articles fetched-articles)
5139       (setq gnus-newsgroup-unreads
5140             (gnus-sorted-nintersection
5141              gnus-newsgroup-unreads fetched-articles))
5142       (gnus-compute-unseen-list)
5143
5144       ;; Removed marked articles that do not exist.
5145       (gnus-update-missing-marks
5146        (gnus-sorted-difference articles fetched-articles))
5147       ;; We might want to build some more threads first.
5148       (when (and gnus-fetch-old-headers
5149                  (eq gnus-headers-retrieved-by 'nov))
5150         (if (eq gnus-fetch-old-headers 'invisible)
5151             (gnus-build-all-threads)
5152           (gnus-build-old-threads)))
5153       ;; Let the Gnus agent mark articles as read.
5154       (when gnus-agent
5155         (gnus-agent-get-undownloaded-list))
5156       ;; Remove list identifiers from subject
5157       (when gnus-list-identifiers
5158         (gnus-summary-remove-list-identifiers))
5159       ;; Check whether auto-expire is to be done in this group.
5160       (setq gnus-newsgroup-auto-expire
5161             (gnus-group-auto-expirable-p group))
5162       ;; Set up the article buffer now, if necessary.
5163       (unless gnus-single-article-buffer
5164         (gnus-article-setup-buffer))
5165       ;; First and last article in this newsgroup.
5166       (when gnus-newsgroup-headers
5167         (setq gnus-newsgroup-begin
5168               (mail-header-number (car gnus-newsgroup-headers))
5169               gnus-newsgroup-end
5170               (mail-header-number
5171                (gnus-last-element gnus-newsgroup-headers))))
5172       ;; GROUP is successfully selected.
5173       (or gnus-newsgroup-headers t)))))
5174
5175 (defun gnus-compute-unseen-list ()
5176   ;; The `seen' marks are treated specially.
5177   (if (not gnus-newsgroup-seen)
5178       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5179     (setq gnus-newsgroup-unseen
5180           (gnus-inverse-list-range-intersection
5181            gnus-newsgroup-articles gnus-newsgroup-seen))))
5182
5183 (defun gnus-summary-display-make-predicate (display)
5184   (require 'gnus-agent)
5185   (when (= (length display) 1)
5186     (setq display (car display)))
5187   (unless gnus-summary-display-cache
5188     (dolist (elem (append '((unread . unread)
5189                             (read . read)
5190                             (unseen . unseen))
5191                           gnus-article-mark-lists))
5192       (push (cons (cdr elem)
5193                   (gnus-byte-compile
5194                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5195             gnus-summary-display-cache)))
5196   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5197         (gnus-category-predicate-cache gnus-summary-display-cache))
5198     (gnus-get-predicate display)))
5199
5200 ;; Uses the dynamically bound `number' variable.
5201 (eval-when-compile
5202   (defvar number))
5203 (defun gnus-article-marked-p (type &optional article)
5204   (let ((article (or article number)))
5205     (cond
5206      ((eq type 'tick)
5207       (memq article gnus-newsgroup-marked))
5208      ((eq type 'spam)
5209       (memq article gnus-newsgroup-spam-marked))
5210      ((eq type 'unsend)
5211       (memq article gnus-newsgroup-unsendable))
5212      ((eq type 'undownload)
5213       (memq article gnus-newsgroup-undownloaded))
5214      ((eq type 'download)
5215       (memq article gnus-newsgroup-downloadable))
5216      ((eq type 'unread)
5217       (memq article gnus-newsgroup-unreads))
5218      ((eq type 'read)
5219       (memq article gnus-newsgroup-reads))
5220      ((eq type 'dormant)
5221       (memq article gnus-newsgroup-dormant) )
5222      ((eq type 'expire)
5223       (memq article gnus-newsgroup-expirable))
5224      ((eq type 'reply)
5225       (memq article gnus-newsgroup-replied))
5226      ((eq type 'killed)
5227       (memq article gnus-newsgroup-killed))
5228      ((eq type 'bookmark)
5229       (assq article gnus-newsgroup-bookmarks))
5230      ((eq type 'score)
5231       (assq article gnus-newsgroup-scored))
5232      ((eq type 'save)
5233       (memq article gnus-newsgroup-saved))
5234      ((eq type 'cache)
5235       (memq article gnus-newsgroup-cached))
5236      ((eq type 'forward)
5237       (memq article gnus-newsgroup-forwarded))
5238      ((eq type 'seen)
5239       (not (memq article gnus-newsgroup-unseen)))
5240      ((eq type 'recent)
5241       (memq article gnus-newsgroup-recent))
5242      (t t))))
5243
5244 (defun gnus-articles-to-read (group &optional read-all)
5245   "Find out what articles the user wants to read."
5246   (let* ((display (gnus-group-find-parameter group 'display))
5247          (articles
5248           ;; Select all articles if `read-all' is non-nil, or if there
5249           ;; are no unread articles.
5250           (if (or read-all
5251                   (and (zerop (length gnus-newsgroup-marked))
5252                        (zerop (length gnus-newsgroup-unreads)))
5253                   ;; Fetch all if the predicate is non-nil.
5254                   gnus-newsgroup-display)
5255               ;; We want to select the headers for all the articles in
5256               ;; the group, so we select either all the active
5257               ;; articles in the group, or (if that's nil), the
5258               ;; articles in the cache.
5259               (or
5260                (gnus-uncompress-range (gnus-active group))
5261                (gnus-cache-articles-in-group group))
5262             ;; Select only the "normal" subset of articles.
5263             (gnus-sorted-nunion
5264              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5265              gnus-newsgroup-unreads)))
5266          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5267          (scored (length scored-list))
5268          (number (length articles))
5269          (marked (+ (length gnus-newsgroup-marked)
5270                     (length gnus-newsgroup-dormant)))
5271          (select
5272           (cond
5273            ((numberp read-all)
5274             read-all)
5275            ((numberp gnus-newsgroup-display)
5276             gnus-newsgroup-display)
5277            (t
5278             (condition-case ()
5279                 (cond
5280                  ((and (or (<= scored marked) (= scored number))
5281                        (numberp gnus-large-newsgroup)
5282                        (> number gnus-large-newsgroup))
5283                   (let* ((cursor-in-echo-area nil)
5284                          (initial (gnus-parameter-large-newsgroup-initial
5285                                    gnus-newsgroup-name))
5286                          (input
5287                           (read-string
5288                            (format
5289                             "How many articles from %s (%s %d): "
5290                             (gnus-limit-string
5291                              (gnus-group-decoded-name gnus-newsgroup-name)
5292                              35)
5293                             (if initial "max" "default")
5294                             number)
5295                            (if initial
5296                                (cons (number-to-string initial)
5297                                      0)))))
5298                     (if (string-match "^[ \t]*$" input) number input)))
5299                  ((and (> scored marked) (< scored number)
5300                        (> (- scored number) 20))
5301                   (let ((input
5302                          (read-string
5303                           (format "%s %s (%d scored, %d total): "
5304                                   "How many articles from"
5305                                   (gnus-group-decoded-name group)
5306                                   scored number))))
5307                     (if (string-match "^[ \t]*$" input)
5308                         number input)))
5309                  (t number))
5310               (quit
5311                (message "Quit getting the articles to read")
5312                nil))))))
5313     (setq select (if (stringp select) (string-to-number select) select))
5314     (if (or (null select) (zerop select))
5315         select
5316       (if (and (not (zerop scored)) (<= (abs select) scored))
5317           (progn
5318             (setq articles (sort scored-list '<))
5319             (setq number (length articles)))
5320         (setq articles (copy-sequence articles)))
5321
5322       (when (< (abs select) number)
5323         (if (< select 0)
5324             ;; Select the N oldest articles.
5325             (setcdr (nthcdr (1- (abs select)) articles) nil)
5326           ;; Select the N most recent articles.
5327           (setq articles (nthcdr (- number select) articles))))
5328       (setq gnus-newsgroup-unselected
5329             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5330       (when gnus-alter-articles-to-read-function
5331         (setq articles
5332               (sort
5333                (funcall gnus-alter-articles-to-read-function
5334                         gnus-newsgroup-name articles)
5335                '<)))
5336       articles)))
5337
5338 (defun gnus-killed-articles (killed articles)
5339   (let (out)
5340     (while articles
5341       (when (inline (gnus-member-of-range (car articles) killed))
5342         (push (car articles) out))
5343       (setq articles (cdr articles)))
5344     out))
5345
5346 (defun gnus-uncompress-marks (marks)
5347   "Uncompress the mark ranges in MARKS."
5348   (let ((uncompressed '(score bookmark))
5349         out)
5350     (while marks
5351       (if (memq (caar marks) uncompressed)
5352           (push (car marks) out)
5353         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5354       (setq marks (cdr marks)))
5355     out))
5356
5357 (defun gnus-article-mark-to-type (mark)
5358   "Return the type of MARK."
5359   (or (cadr (assq mark gnus-article-special-mark-lists))
5360       'list))
5361
5362 (defun gnus-article-unpropagatable-p (mark)
5363   "Return whether MARK should be propagated to back end."
5364   (memq mark gnus-article-unpropagated-mark-lists))
5365
5366 (defun gnus-adjust-marked-articles (info)
5367   "Set all article lists and remove all marks that are no longer valid."
5368   (let* ((marked-lists (gnus-info-marks info))
5369          (active (gnus-active (gnus-info-group info)))
5370          (min (car active))
5371          (max (cdr active))
5372          (types gnus-article-mark-lists)
5373          marks var articles article mark mark-type)
5374
5375     (dolist (marks marked-lists)
5376       (setq mark (car marks)
5377             mark-type (gnus-article-mark-to-type mark)
5378             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5379
5380       ;; We set the variable according to the type of the marks list,
5381       ;; and then adjust the marks to a subset of the active articles.
5382       (cond
5383        ;; Adjust "simple" lists.
5384        ((eq mark-type 'list)
5385         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5386         (when (memq mark '(tick dormant expire reply save))
5387           (while articles
5388             (when (or (< (setq article (pop articles)) min) (> article max))
5389               (set var (delq article (symbol-value var)))))))
5390        ;; Adjust assocs.
5391        ((eq mark-type 'tuple)
5392         (set var (setq articles (cdr marks)))
5393         (when (not (listp (cdr (symbol-value var))))
5394           (set var (list (symbol-value var))))
5395         (when (not (listp (cdr articles)))
5396           (setq articles (list articles)))
5397         (while articles
5398           (when (or (not (consp (setq article (pop articles))))
5399                     (< (car article) min)
5400                     (> (car article) max))
5401             (set var (delq article (symbol-value var))))))
5402        ;; Adjust ranges (sloppily).
5403        ((eq mark-type 'range)
5404         (cond
5405          ((eq mark 'seen)
5406           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5407           ;; It should be (seen (NUM1 . NUM2)).
5408           (when (numberp (cddr marks))
5409             (setcdr marks (list (cdr marks))))
5410           (setq articles (cdr marks))
5411           (while (and articles
5412                       (or (and (consp (car articles))
5413                                (> min (cdar articles)))
5414                           (and (numberp (car articles))
5415                                (> min (car articles)))))
5416             (pop articles))
5417           (set var articles))))))))
5418
5419 (defun gnus-update-missing-marks (missing)
5420   "Go through the list of MISSING articles and remove them from the mark lists."
5421   (when missing
5422     (let (var m)
5423       ;; Go through all types.
5424       (dolist (elem gnus-article-mark-lists)
5425         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5426           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5427           (when (symbol-value var)
5428             ;; This list has articles.  So we delete all missing
5429             ;; articles from it.
5430             (setq m missing)
5431             (while m
5432               (set var (delq (pop m) (symbol-value var))))))))))
5433
5434 (defun gnus-update-marks ()
5435   "Enter the various lists of marked articles into the newsgroup info list."
5436   (let ((types gnus-article-mark-lists)
5437         (info (gnus-get-info gnus-newsgroup-name))
5438         type list newmarked symbol delta-marks)
5439     (when info
5440       ;; Add all marks lists to the list of marks lists.
5441       (while (setq type (pop types))
5442         (setq list (symbol-value
5443                     (setq symbol
5444                           (intern (format "gnus-newsgroup-%s" (car type))))))
5445
5446         (when list
5447           ;; Get rid of the entries of the articles that have the
5448           ;; default score.
5449           (when (and (eq (cdr type) 'score)
5450                      gnus-save-score
5451                      list)
5452             (let* ((arts list)
5453                    (prev (cons nil list))
5454                    (all prev))
5455               (while arts
5456                 (if (or (not (consp (car arts)))
5457                         (= (cdar arts) gnus-summary-default-score))
5458                     (setcdr prev (cdr arts))
5459                   (setq prev arts))
5460                 (setq arts (cdr arts)))
5461               (setq list (cdr all)))))
5462
5463         (when (eq (cdr type) 'seen)
5464           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5465
5466         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5467           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5468
5469         (when (and (gnus-check-backend-function
5470                     'request-set-mark gnus-newsgroup-name)
5471                    (not (gnus-article-unpropagatable-p (cdr type))))
5472           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5473                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5474                  (add (gnus-remove-from-range
5475                        (gnus-copy-sequence list) old)))
5476             (when add
5477               (push (list add 'add (list (cdr type))) delta-marks))
5478             (when del
5479               (push (list del 'del (list (cdr type))) delta-marks))))
5480
5481         (when list
5482           (push (cons (cdr type) list) newmarked)))
5483
5484       (when delta-marks
5485         (unless (gnus-check-group gnus-newsgroup-name)
5486           (error "Can't open server for %s" gnus-newsgroup-name))
5487         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5488
5489       ;; Enter these new marks into the info of the group.
5490       (if (nthcdr 3 info)
5491           (setcar (nthcdr 3 info) newmarked)
5492         ;; Add the marks lists to the end of the info.
5493         (when newmarked
5494           (setcdr (nthcdr 2 info) (list newmarked))))
5495
5496       ;; Cut off the end of the info if there's nothing else there.
5497       (let ((i 5))
5498         (while (and (> i 2)
5499                     (not (nth i info)))
5500           (when (nthcdr (decf i) info)
5501             (setcdr (nthcdr i info) nil)))))))
5502
5503 (defun gnus-set-mode-line (where)
5504   "Set the mode line of the article or summary buffers.
5505 If WHERE is `summary', the summary mode line format will be used."
5506   ;; Is this mode line one we keep updated?
5507   (when (and (memq where gnus-updated-mode-lines)
5508              (symbol-value
5509               (intern (format "gnus-%s-mode-line-format-spec" where))))
5510     (let (mode-string)
5511       (save-excursion
5512         ;; We evaluate this in the summary buffer since these
5513         ;; variables are buffer-local to that buffer.
5514         (set-buffer gnus-summary-buffer)
5515         ;; We bind all these variables that are used in the `eval' form
5516         ;; below.
5517         (let* ((mformat (symbol-value
5518                          (intern
5519                           (format "gnus-%s-mode-line-format-spec" where))))
5520                (gnus-tmp-group-name (gnus-group-decoded-name
5521                                      gnus-newsgroup-name))
5522                (gnus-tmp-article-number (or gnus-current-article 0))
5523                (gnus-tmp-unread gnus-newsgroup-unreads)
5524                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5525                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5526                (gnus-tmp-unread-and-unselected
5527                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5528                             (zerop gnus-tmp-unselected))
5529                        "")
5530                       ((zerop gnus-tmp-unselected)
5531                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5532                       (t (format "{%d(+%d) more}"
5533                                  gnus-tmp-unread-and-unticked
5534                                  gnus-tmp-unselected))))
5535                (gnus-tmp-subject
5536                 (if (and gnus-current-headers
5537                          (vectorp gnus-current-headers))
5538                     (gnus-mode-string-quote
5539                      (mail-header-subject gnus-current-headers))
5540                   ""))
5541                bufname-length max-len
5542                gnus-tmp-header);; passed as argument to any user-format-funcs
5543           (setq mode-string (eval mformat))
5544           (setq bufname-length (if (string-match "%b" mode-string)
5545                                    (- (length
5546                                        (buffer-name
5547                                         (if (eq where 'summary)
5548                                             nil
5549                                           (get-buffer gnus-article-buffer))))
5550                                       2)
5551                                  0))
5552           (setq max-len (max 4 (if gnus-mode-non-string-length
5553                                    (- (window-width)
5554                                       gnus-mode-non-string-length
5555                                       bufname-length)
5556                                  (length mode-string))))
5557           ;; We might have to chop a bit of the string off...
5558           (when (> (length mode-string) max-len)
5559             (setq mode-string
5560                   (concat (gnus-truncate-string mode-string (- max-len 3))
5561                           "...")))
5562           ;; Pad the mode string a bit.
5563           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5564       ;; Update the mode line.
5565       (setq mode-line-buffer-identification
5566             (gnus-mode-line-buffer-identification (list mode-string)))
5567       (set-buffer-modified-p t))))
5568
5569 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5570   "Go through the HEADERS list and add all Xrefs to a hash table.
5571 The resulting hash table is returned, or nil if no Xrefs were found."
5572   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5573          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5574          (xref-hashtb (gnus-make-hashtable))
5575          start group entry number xrefs header)
5576     (while headers
5577       (setq header (pop headers))
5578       (when (and (setq xrefs (mail-header-xref header))
5579                  (not (memq (setq number (mail-header-number header))
5580                             unreads)))
5581         (setq start 0)
5582         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5583           (setq start (match-end 0))
5584           (setq group (if prefix
5585                           (concat prefix (substring xrefs (match-beginning 1)
5586                                                     (match-end 1)))
5587                         (substring xrefs (match-beginning 1) (match-end 1))))
5588           (setq number
5589                 (string-to-int (substring xrefs (match-beginning 2)
5590                                           (match-end 2))))
5591           (if (setq entry (gnus-gethash group xref-hashtb))
5592               (setcdr entry (cons number (cdr entry)))
5593             (gnus-sethash group (cons number nil) xref-hashtb)))))
5594     (and start xref-hashtb)))
5595
5596 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5597   "Look through all the headers and mark the Xrefs as read."
5598   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5599         name entry info xref-hashtb idlist method nth4)
5600     (save-excursion
5601       (set-buffer gnus-group-buffer)
5602       (when (setq xref-hashtb
5603                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5604         (mapatoms
5605          (lambda (group)
5606            (unless (string= from-newsgroup (setq name (symbol-name group)))
5607              (setq idlist (symbol-value group))
5608              ;; Dead groups are not updated.
5609              (and (prog1
5610                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5611                             info (nth 2 entry))
5612                     (when (stringp (setq nth4 (gnus-info-method info)))
5613                       (setq nth4 (gnus-server-to-method nth4))))
5614                   ;; Only do the xrefs if the group has the same
5615                   ;; select method as the group we have just read.
5616                   (or (gnus-methods-equal-p
5617                        nth4 (gnus-find-method-for-group from-newsgroup))
5618                       virtual
5619                       (equal nth4 (setq method (gnus-find-method-for-group
5620                                                 from-newsgroup)))
5621                       (and (equal (car nth4) (car method))
5622                            (equal (nth 1 nth4) (nth 1 method))))
5623                   gnus-use-cross-reference
5624                   (or (not (eq gnus-use-cross-reference t))
5625                       virtual
5626                       ;; Only do cross-references on subscribed
5627                       ;; groups, if that is what is wanted.
5628                       (<= (gnus-info-level info) gnus-level-subscribed))
5629                   (gnus-group-make-articles-read name idlist))))
5630          xref-hashtb)))))
5631
5632 (defun gnus-compute-read-articles (group articles)
5633   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5634          (info (nth 2 entry))
5635          (active (gnus-active group))
5636          ninfo)
5637     (when entry
5638       ;; First peel off all invalid article numbers.
5639       (when active
5640         (let ((ids articles)
5641               id first)
5642           (while (setq id (pop ids))
5643             (when (and first (> id (cdr active)))
5644               ;; We'll end up in this situation in one particular
5645               ;; obscure situation.  If you re-scan a group and get
5646               ;; a new article that is cross-posted to a different
5647               ;; group that has not been re-scanned, you might get
5648               ;; crossposted article that has a higher number than
5649               ;; Gnus believes possible.  So we re-activate this
5650               ;; group as well.  This might mean doing the
5651               ;; crossposting thingy will *increase* the number
5652               ;; of articles in some groups.  Tsk, tsk.
5653               (setq active (or (gnus-activate-group group) active)))
5654             (when (or (> id (cdr active))
5655                       (< id (car active)))
5656               (setq articles (delq id articles))))))
5657       ;; If the read list is nil, we init it.
5658       (if (and active
5659                (null (gnus-info-read info))
5660                (> (car active) 1))
5661           (setq ninfo (cons 1 (1- (car active))))
5662         (setq ninfo (gnus-info-read info)))
5663       ;; Then we add the read articles to the range.
5664       (gnus-add-to-range
5665        ninfo (setq articles (sort articles '<))))))
5666
5667 (defun gnus-group-make-articles-read (group articles)
5668   "Update the info of GROUP to say that ARTICLES are read."
5669   (let* ((num 0)
5670          (entry (gnus-gethash group gnus-newsrc-hashtb))
5671          (info (nth 2 entry))
5672          (active (gnus-active group))
5673          range)
5674     (when entry
5675       (setq range (gnus-compute-read-articles group articles))
5676       (save-excursion
5677         (set-buffer gnus-group-buffer)
5678         (gnus-undo-register
5679           `(progn
5680              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5681              (gnus-info-set-read ',info ',(gnus-info-read info))
5682              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5683              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5684              (gnus-group-update-group ,group t))))
5685       ;; Add the read articles to the range.
5686       (gnus-info-set-read info range)
5687       (gnus-request-set-mark group (list (list range 'add '(read))))
5688       ;; Then we have to re-compute how many unread
5689       ;; articles there are in this group.
5690       (when active
5691         (cond
5692          ((not range)
5693           (setq num (- (1+ (cdr active)) (car active))))
5694          ((not (listp (cdr range)))
5695           (setq num (- (cdr active) (- (1+ (cdr range))
5696                                        (car range)))))
5697          (t
5698           (while range
5699             (if (numberp (car range))
5700                 (setq num (1+ num))
5701               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5702             (setq range (cdr range)))
5703           (setq num (- (cdr active) num))))
5704         ;; Update the number of unread articles.
5705         (setcar entry num)
5706         ;; Update the group buffer.
5707         (unless (gnus-ephemeral-group-p group)
5708           (gnus-group-update-group group t))))))
5709
5710 (defvar gnus-newsgroup-none-id 0)
5711
5712 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5713   (let ((cur nntp-server-buffer)
5714         (dependencies
5715          (or dependencies
5716              (save-excursion (set-buffer gnus-summary-buffer)
5717                              gnus-newsgroup-dependencies)))
5718         headers id end ref
5719         (mail-parse-charset gnus-newsgroup-charset)
5720         (mail-parse-ignored-charsets
5721          (save-excursion (condition-case nil
5722                              (set-buffer gnus-summary-buffer)
5723                            (error))
5724                          gnus-newsgroup-ignored-charsets)))
5725     (save-excursion
5726       (set-buffer nntp-server-buffer)
5727       ;; Translate all TAB characters into SPACE characters.
5728       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5729       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5730       (ietf-drums-unfold-fws)
5731       (gnus-run-hooks 'gnus-parse-headers-hook)
5732       (let ((case-fold-search t)
5733             in-reply-to header p lines chars ctype)
5734         (goto-char (point-min))
5735         ;; Search to the beginning of the next header.  Error messages
5736         ;; do not begin with 2 or 3.
5737         (while (re-search-forward "^[23][0-9]+ " nil t)
5738           (setq id nil
5739                 ref nil)
5740           ;; This implementation of this function, with nine
5741           ;; search-forwards instead of the one re-search-forward and
5742           ;; a case (which basically was the old function) is actually
5743           ;; about twice as fast, even though it looks messier.  You
5744           ;; can't have everything, I guess.  Speed and elegance
5745           ;; doesn't always go hand in hand.
5746           (setq
5747            header
5748            (make-full-mail-header
5749             ;; Number.
5750             (prog1
5751                 (read cur)
5752               (end-of-line)
5753               (setq p (point))
5754               (narrow-to-region (point)
5755                                 (or (and (search-forward "\n.\n" nil t)
5756                                          (- (point) 2))
5757                                     (point))))
5758             ;; Subject.
5759             (progn
5760               (goto-char p)
5761               (if (search-forward "\nsubject:" nil t)
5762                   (nnheader-header-value)
5763                 "(none)"))
5764             ;; From.
5765             (progn
5766               (goto-char p)
5767               (if (search-forward "\nfrom:" nil t)
5768                   (nnheader-header-value)
5769                 "(nobody)"))
5770             ;; Date.
5771             (progn
5772               (goto-char p)
5773               (if (search-forward "\ndate:" nil t)
5774                   (nnheader-header-value) ""))
5775             ;; Message-ID.
5776             (progn
5777               (goto-char p)
5778               (setq id (if (re-search-forward
5779                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5780                            ;; We do it this way to make sure the Message-ID
5781                            ;; is (somewhat) syntactically valid.
5782                            (buffer-substring (match-beginning 1)
5783                                              (match-end 1))
5784                          ;; If there was no message-id, we just fake one
5785                          ;; to make subsequent routines simpler.
5786                          (nnheader-generate-fake-message-id))))
5787             ;; References.
5788             (progn
5789               (goto-char p)
5790               (if (search-forward "\nreferences:" nil t)
5791                   (progn
5792                     (setq end (point))
5793                     (prog1
5794                         (nnheader-header-value)
5795                       (setq ref
5796                             (buffer-substring
5797                              (progn
5798                                ;; (end-of-line)
5799                                (search-backward ">" end t)
5800                                (1+ (point)))
5801                              (progn
5802                                (search-backward "<" end t)
5803                                (point))))))
5804                 ;; Get the references from the in-reply-to header if there
5805                 ;; were no references and the in-reply-to header looks
5806                 ;; promising.
5807                 (if (and (search-forward "\nin-reply-to:" nil t)
5808                          (setq in-reply-to (nnheader-header-value))
5809                          (string-match "<[^>]+>" in-reply-to))
5810                     (let (ref2)
5811                       (setq ref (substring in-reply-to (match-beginning 0)
5812                                            (match-end 0)))
5813                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5814                         (setq ref2 (substring in-reply-to (match-beginning 0)
5815                                               (match-end 0)))
5816                         (when (> (length ref2) (length ref))
5817                           (setq ref ref2)))
5818                       ref)
5819                   (setq ref nil))))
5820             ;; Chars.
5821             (progn
5822               (goto-char p)
5823               (if (search-forward "\nchars: " nil t)
5824                   (if (numberp (setq chars (ignore-errors (read cur))))
5825                       chars -1)
5826                 -1))
5827             ;; Lines.
5828             (progn
5829               (goto-char p)
5830               (if (search-forward "\nlines: " nil t)
5831                   (if (numberp (setq lines (ignore-errors (read cur))))
5832                       lines -1)
5833                 -1))
5834             ;; Xref.
5835             (progn
5836               (goto-char p)
5837               (and (search-forward "\nxref:" nil t)
5838                    (nnheader-header-value)))
5839             ;; Extra.
5840             (when gnus-extra-headers
5841               (let ((extra gnus-extra-headers)
5842                     out)
5843                 (while extra
5844                   (goto-char p)
5845                   (when (search-forward
5846                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5847                     (push (cons (car extra) (nnheader-header-value)) out))
5848                   (pop extra))
5849                 out))))
5850           (goto-char p)
5851           (if (and (search-forward "\ncontent-type: " nil t)
5852                    (setq ctype (nnheader-header-value)))
5853               (mime-entity-set-content-type-internal
5854                header (mime-parse-Content-Type ctype)))
5855           (when (equal id ref)
5856             (setq ref nil))
5857
5858           (when gnus-alter-header-function
5859             (funcall gnus-alter-header-function header)
5860             (setq id (mail-header-id header)
5861                   ref (gnus-parent-id (mail-header-references header))))
5862
5863           (when (setq header
5864                       (gnus-dependencies-add-header
5865                        header dependencies force-new))
5866             (push header headers))
5867           (goto-char (point-max))
5868           (widen))
5869         (nreverse headers)))))
5870
5871 ;; Goes through the xover lines and returns a list of vectors
5872 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5873                                                   force-new dependencies
5874                                                   group also-fetch-heads)
5875   "Parse the news overview data in the server buffer.
5876 Return a list of headers that match SEQUENCE (see
5877 `nntp-retrieve-headers')."
5878   ;; Get the Xref when the users reads the articles since most/some
5879   ;; NNTP servers do not include Xrefs when using XOVER.
5880   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5881   (let ((mail-parse-charset gnus-newsgroup-charset)
5882         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5883         (cur nntp-server-buffer)
5884         (dependencies (or dependencies gnus-newsgroup-dependencies))
5885         (allp (cond
5886                ((eq gnus-read-all-available-headers t)
5887                 t)
5888                ((stringp gnus-read-all-available-headers)
5889                 (string-match gnus-read-all-available-headers group))
5890                (t
5891                 nil)))
5892         number headers header)
5893     (save-excursion
5894       (set-buffer nntp-server-buffer)
5895       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5896       ;; Allow the user to mangle the headers before parsing them.
5897       (gnus-run-hooks 'gnus-parse-headers-hook)
5898       (goto-char (point-min))
5899       (gnus-parse-without-error
5900         (while (and (or sequence allp)
5901                     (not (eobp)))
5902           (setq number (read cur))
5903           (when (not allp)
5904             (while (and sequence
5905                         (< (car sequence) number))
5906               (setq sequence (cdr sequence))))
5907           (when (and (or allp
5908                          (and sequence
5909                               (eq number (car sequence))))
5910                      (progn
5911                        (setq sequence (cdr sequence))
5912                        (setq header (inline
5913                                       (gnus-nov-parse-line
5914                                        number dependencies force-new)))))
5915             (push header headers))
5916           (forward-line 1)))
5917       ;; A common bug in inn is that if you have posted an article and
5918       ;; then retrieves the active file, it will answer correctly --
5919       ;; the new article is included.  However, a NOV entry for the
5920       ;; article may not have been generated yet, so this may fail.
5921       ;; We work around this problem by retrieving the last few
5922       ;; headers using HEAD.
5923       (if (or (not also-fetch-heads)
5924               (not sequence))
5925           ;; We (probably) got all the headers.
5926           (nreverse headers)
5927         (let ((gnus-nov-is-evil t))
5928           (nconc
5929            (nreverse headers)
5930            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5931              (gnus-get-newsgroup-headers))))))))
5932
5933 (defun gnus-article-get-xrefs ()
5934   "Fill in the Xref value in `gnus-current-headers', if necessary.
5935 This is meant to be called in `gnus-article-internal-prepare-hook'."
5936   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5937                                  gnus-current-headers)))
5938     (or (not gnus-use-cross-reference)
5939         (not headers)
5940         (and (mail-header-xref headers)
5941              (not (string= (mail-header-xref headers) "")))
5942         (let ((case-fold-search t)
5943               xref)
5944           (save-restriction
5945             (nnheader-narrow-to-headers)
5946             (goto-char (point-min))
5947             (when (or (and (not (eobp))
5948                            (eq (downcase (char-after)) ?x)
5949                            (looking-at "Xref:"))
5950                       (search-forward "\nXref:" nil t))
5951               (goto-char (1+ (match-end 0)))
5952               (setq xref (buffer-substring (point) (point-at-eol)))
5953               (mail-header-set-xref headers xref)))))))
5954
5955 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5956   "Find article ID and insert the summary line for that article.
5957 OLD-HEADER can either be a header or a line number to insert
5958 the subject line on."
5959   (let* ((line (and (numberp old-header) old-header))
5960          (old-header (and (vectorp old-header) old-header))
5961          (header (cond ((and old-header use-old-header)
5962                         old-header)
5963                        ((and (numberp id)
5964                              (gnus-number-to-header id))
5965                         (gnus-number-to-header id))
5966                        (t
5967                         (gnus-read-header id))))
5968          (number (and (numberp id) id))
5969          d)
5970     (when header
5971       ;; Rebuild the thread that this article is part of and go to the
5972       ;; article we have fetched.
5973       (when (and (not gnus-show-threads)
5974                  old-header)
5975         (when (and number
5976                    (setq d (gnus-data-find (mail-header-number old-header))))
5977           (goto-char (gnus-data-pos d))
5978           (gnus-data-remove
5979            number
5980            (- (point-at-bol)
5981               (prog1
5982                   (1+ (point-at-eol))
5983                 (gnus-delete-line))))))
5984       (when old-header
5985         (mail-header-set-number header (mail-header-number old-header)))
5986       (setq gnus-newsgroup-sparse
5987             (delq (setq number (mail-header-number header))
5988                   gnus-newsgroup-sparse))
5989       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5990       (push number gnus-newsgroup-limit)
5991       (gnus-rebuild-thread (mail-header-id header) line)
5992       (gnus-summary-goto-subject number nil t))
5993     (when (and (numberp number)
5994                (> number 0))
5995       ;; We have to update the boundaries even if we can't fetch the
5996       ;; article if ID is a number -- so that the next `P' or `N'
5997       ;; command will fetch the previous (or next) article even
5998       ;; if the one we tried to fetch this time has been canceled.
5999       (when (> number gnus-newsgroup-end)
6000         (setq gnus-newsgroup-end number))
6001       (when (< number gnus-newsgroup-begin)
6002         (setq gnus-newsgroup-begin number))
6003       (setq gnus-newsgroup-unselected
6004             (delq number gnus-newsgroup-unselected)))
6005     ;; Report back a success?
6006     (and header (mail-header-number header))))
6007
6008 ;;; Process/prefix in the summary buffer
6009
6010 (defun gnus-summary-work-articles (n)
6011   "Return a list of articles to be worked upon.
6012 The prefix argument, the list of process marked articles, and the
6013 current article will be taken into consideration."
6014   (save-excursion
6015     (set-buffer gnus-summary-buffer)
6016     (cond
6017      (n
6018       ;; A numerical prefix has been given.
6019       (setq n (prefix-numeric-value n))
6020       (let ((backward (< n 0))
6021             (n (abs (prefix-numeric-value n)))
6022             articles article)
6023         (save-excursion
6024           (while
6025               (and (> n 0)
6026                    (push (setq article (gnus-summary-article-number))
6027                          articles)
6028                    (if backward
6029                        (gnus-summary-find-prev nil article)
6030                      (gnus-summary-find-next nil article)))
6031             (decf n)))
6032         (nreverse articles)))
6033      ((and (gnus-region-active-p) (mark))
6034       (message "region active")
6035       ;; Work on the region between point and mark.
6036       (let ((max (max (point) (mark)))
6037             articles article)
6038         (save-excursion
6039           (goto-char (min (point) (mark)))
6040           (while
6041               (and
6042                (push (setq article (gnus-summary-article-number)) articles)
6043                (gnus-summary-find-next nil article)
6044                (< (point) max)))
6045           (nreverse articles))))
6046      (gnus-newsgroup-processable
6047       ;; There are process-marked articles present.
6048       ;; Save current state.
6049       (gnus-summary-save-process-mark)
6050       ;; Return the list.
6051       (reverse gnus-newsgroup-processable))
6052      (t
6053       ;; Just return the current article.
6054       (list (gnus-summary-article-number))))))
6055
6056 (defmacro gnus-summary-iterate (arg &rest forms)
6057   "Iterate over the process/prefixed articles and do FORMS.
6058 ARG is the interactive prefix given to the command.  FORMS will be
6059 executed with point over the summary line of the articles."
6060   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6061     `(let ((,articles (gnus-summary-work-articles ,arg)))
6062        (while ,articles
6063          (gnus-summary-goto-subject (car ,articles))
6064          ,@forms
6065          (pop ,articles)))))
6066
6067 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6068 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6069
6070 (defun gnus-summary-save-process-mark ()
6071   "Push the current set of process marked articles on the stack."
6072   (interactive)
6073   (push (copy-sequence gnus-newsgroup-processable)
6074         gnus-newsgroup-process-stack))
6075
6076 (defun gnus-summary-kill-process-mark ()
6077   "Push the current set of process marked articles on the stack and unmark."
6078   (interactive)
6079   (gnus-summary-save-process-mark)
6080   (gnus-summary-unmark-all-processable))
6081
6082 (defun gnus-summary-yank-process-mark ()
6083   "Pop the last process mark state off the stack and restore it."
6084   (interactive)
6085   (unless gnus-newsgroup-process-stack
6086     (error "Empty mark stack"))
6087   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6088
6089 (defun gnus-summary-process-mark-set (set)
6090   "Make SET into the current process marked articles."
6091   (gnus-summary-unmark-all-processable)
6092   (while set
6093     (gnus-summary-set-process-mark (pop set))))
6094
6095 ;;; Searching and stuff
6096
6097 (defun gnus-summary-search-group (&optional backward use-level)
6098   "Search for next unread newsgroup.
6099 If optional argument BACKWARD is non-nil, search backward instead."
6100   (save-excursion
6101     (set-buffer gnus-group-buffer)
6102     (when (gnus-group-search-forward
6103            backward nil (if use-level (gnus-group-group-level) nil))
6104       (gnus-group-group-name))))
6105
6106 (defun gnus-summary-best-group (&optional exclude-group)
6107   "Find the name of the best unread group.
6108 If EXCLUDE-GROUP, do not go to this group."
6109   (save-excursion
6110     (set-buffer gnus-group-buffer)
6111     (save-excursion
6112       (gnus-group-best-unread-group exclude-group))))
6113
6114 (defun gnus-summary-find-next (&optional unread article backward)
6115   (if backward
6116       (gnus-summary-find-prev unread article)
6117     (let* ((dummy (gnus-summary-article-intangible-p))
6118            (article (or article (gnus-summary-article-number)))
6119            (data (gnus-data-find-list article))
6120            result)
6121       (when (and (not dummy)
6122                  (or (not gnus-summary-check-current)
6123                      (not unread)
6124                      (not (gnus-data-unread-p (car data)))))
6125         (setq data (cdr data)))
6126       (when (setq result
6127                   (if unread
6128                       (progn
6129                         (while data
6130                           (unless (memq (gnus-data-number (car data))
6131                                         (cond
6132                                          ((eq gnus-auto-goto-ignores
6133                                               'always-undownloaded)
6134                                           gnus-newsgroup-undownloaded)
6135                                          (gnus-plugged
6136                                           nil)
6137                                          ((eq gnus-auto-goto-ignores
6138                                               'unfetched)
6139                                           gnus-newsgroup-unfetched)
6140                                          ((eq gnus-auto-goto-ignores
6141                                               'undownloaded)
6142                                           gnus-newsgroup-undownloaded)))
6143                             (when (gnus-data-unread-p (car data))
6144                               (setq result (car data)
6145                                     data nil)))
6146                           (setq data (cdr data)))
6147                         result)
6148                     (car data)))
6149         (goto-char (gnus-data-pos result))
6150         (gnus-data-number result)))))
6151
6152 (defun gnus-summary-find-prev (&optional unread article)
6153   (let* ((eobp (eobp))
6154          (article (or article (gnus-summary-article-number)))
6155          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6156          result)
6157     (when (and (not eobp)
6158                (or (not gnus-summary-check-current)
6159                    (not unread)
6160                    (not (gnus-data-unread-p (car data)))))
6161       (setq data (cdr data)))
6162     (when (setq result
6163                 (if unread
6164                     (progn
6165                       (while data
6166                         (unless (memq (gnus-data-number (car data))
6167                                       (cond
6168                                        ((eq gnus-auto-goto-ignores
6169                                             'always-undownloaded)
6170                                         gnus-newsgroup-undownloaded)
6171                                        (gnus-plugged
6172                                         nil)
6173                                        ((eq gnus-auto-goto-ignores
6174                                             'unfetched)
6175                                         gnus-newsgroup-unfetched)
6176                                        ((eq gnus-auto-goto-ignores
6177                                             'undownloaded)
6178                                         gnus-newsgroup-undownloaded)))
6179                           (when (gnus-data-unread-p (car data))
6180                             (setq result (car data)
6181                                   data nil)))
6182                         (setq data (cdr data)))
6183                       result)
6184                   (car data)))
6185       (goto-char (gnus-data-pos result))
6186       (gnus-data-number result))))
6187
6188 (defun gnus-summary-find-subject (subject &optional unread backward article)
6189   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6190          (article (or article (gnus-summary-article-number)))
6191          (articles (gnus-data-list backward))
6192          (arts (gnus-data-find-list article articles))
6193          result)
6194     (when (or (not gnus-summary-check-current)
6195               (not unread)
6196               (not (gnus-data-unread-p (car arts))))
6197       (setq arts (cdr arts)))
6198     (while arts
6199       (and (or (not unread)
6200                (gnus-data-unread-p (car arts)))
6201            (vectorp (gnus-data-header (car arts)))
6202            (gnus-subject-equal
6203             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6204            (setq result (car arts)
6205                  arts nil))
6206       (setq arts (cdr arts)))
6207     (and result
6208          (goto-char (gnus-data-pos result))
6209          (gnus-data-number result))))
6210
6211 (defun gnus-summary-search-forward (&optional unread subject backward)
6212   "Search forward for an article.
6213 If UNREAD, look for unread articles.  If SUBJECT, look for
6214 articles with that subject.  If BACKWARD, search backward instead."
6215   (cond (subject (gnus-summary-find-subject subject unread backward))
6216         (backward (gnus-summary-find-prev unread))
6217         (t (gnus-summary-find-next unread))))
6218
6219 (defun gnus-recenter (&optional n)
6220   "Center point in window and redisplay frame.
6221 Also do horizontal recentering."
6222   (interactive "P")
6223   (when (and gnus-auto-center-summary
6224              (not (eq gnus-auto-center-summary 'vertical)))
6225     (gnus-horizontal-recenter))
6226   (recenter n))
6227
6228 (defun gnus-summary-recenter ()
6229   "Center point in the summary window.
6230 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6231 displayed, no centering will be performed."
6232   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6233   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6234   (interactive)
6235   ;; The user has to want it.
6236   (when gnus-auto-center-summary
6237     (let* ((top (cond ((< (window-height) 4) 0)
6238                       ((< (window-height) 7) 1)
6239                       (t (if (numberp gnus-auto-center-summary)
6240                              gnus-auto-center-summary
6241                            2))))
6242            (height (1- (window-height)))
6243            (bottom (save-excursion (goto-char (point-max))
6244                                    (forward-line (- height))
6245                                    (point)))
6246            (window (get-buffer-window (current-buffer))))
6247       (when (get-buffer-window gnus-article-buffer)
6248         ;; Only do recentering when the article buffer is displayed,
6249         ;; Set the window start to either `bottom', which is the biggest
6250         ;; possible valid number, or the second line from the top,
6251         ;; whichever is the least.
6252         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6253           (if (> bottom top-pos)
6254               ;; Keep the second line from the top visible
6255               (set-window-start window top-pos t)
6256             ;; Try to keep the bottom line visible; if it's partially
6257             ;; obscured, either scroll one more line to make it fully
6258             ;; visible, or revert to using TOP-POS.
6259             (save-excursion
6260               (goto-char (point-max))
6261               (forward-line -1)
6262               (let ((last-line-start (point)))
6263                 (goto-char bottom)
6264                 (set-window-start window (point) t)
6265                 (when (not (pos-visible-in-window-p last-line-start window))
6266                   (forward-line 1)
6267                   (set-window-start window (min (point) top-pos) t)))))))
6268       ;; Do horizontal recentering while we're at it.
6269       (when (and (get-buffer-window (current-buffer) t)
6270                  (not (eq gnus-auto-center-summary 'vertical)))
6271         (let ((selected (selected-window)))
6272           (select-window (get-buffer-window (current-buffer) t))
6273           (gnus-summary-position-point)
6274           (gnus-horizontal-recenter)
6275           (select-window selected))))))
6276
6277 (defun gnus-summary-jump-to-group (newsgroup)
6278   "Move point to NEWSGROUP in group mode buffer."
6279   ;; Keep update point of group mode buffer if visible.
6280   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6281       (save-window-excursion
6282         ;; Take care of tree window mode.
6283         (when (get-buffer-window gnus-group-buffer)
6284           (pop-to-buffer gnus-group-buffer))
6285         (gnus-group-jump-to-group newsgroup))
6286     (save-excursion
6287       ;; Take care of tree window mode.
6288       (if (get-buffer-window gnus-group-buffer)
6289           (pop-to-buffer gnus-group-buffer)
6290         (set-buffer gnus-group-buffer))
6291       (gnus-group-jump-to-group newsgroup))))
6292
6293 ;; This function returns a list of article numbers based on the
6294 ;; difference between the ranges of read articles in this group and
6295 ;; the range of active articles.
6296 (defun gnus-list-of-unread-articles (group)
6297   (let* ((read (gnus-info-read (gnus-get-info group)))
6298          (active (or (gnus-active group) (gnus-activate-group group)))
6299          (last (cdr active))
6300          first nlast unread)
6301     ;; If none are read, then all are unread.
6302     (if (not read)
6303         (setq first (car active))
6304       ;; If the range of read articles is a single range, then the
6305       ;; first unread article is the article after the last read
6306       ;; article.  Sounds logical, doesn't it?
6307       (if (and (not (listp (cdr read)))
6308                (or (< (car read) (car active))
6309                    (progn (setq read (list read))
6310                           nil)))
6311           (setq first (max (car active) (1+ (cdr read))))
6312         ;; `read' is a list of ranges.
6313         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6314                                   (caar read)))
6315                   1)
6316           (setq first (car active)))
6317         (while read
6318           (when first
6319             (while (< first nlast)
6320               (push first unread)
6321               (setq first (1+ first))))
6322           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6323           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6324           (setq read (cdr read)))))
6325     ;; And add the last unread articles.
6326     (while (<= first last)
6327       (push first unread)
6328       (setq first (1+ first)))
6329     ;; Return the list of unread articles.
6330     (delq 0 (nreverse unread))))
6331
6332 (defun gnus-list-of-read-articles (group)
6333   "Return a list of unread, unticked and non-dormant articles."
6334   (let* ((info (gnus-get-info group))
6335          (marked (gnus-info-marks info))
6336          (active (gnus-active group)))
6337     (and info active
6338          (gnus-list-range-difference
6339           (gnus-list-range-difference
6340            (gnus-sorted-complement
6341             (gnus-uncompress-range active)
6342             (gnus-list-of-unread-articles group))
6343            (cdr (assq 'dormant marked)))
6344           (cdr (assq 'tick marked))))))
6345
6346 ;; Various summary commands
6347
6348 (defun gnus-summary-select-article-buffer ()
6349   "Reconfigure windows to show article buffer."
6350   (interactive)
6351   (if (not (gnus-buffer-live-p gnus-article-buffer))
6352       (error "There is no article buffer for this summary buffer")
6353     (gnus-configure-windows 'article)
6354     (select-window (get-buffer-window gnus-article-buffer))))
6355
6356 (defun gnus-summary-universal-argument (arg)
6357   "Perform any operation on all articles that are process/prefixed."
6358   (interactive "P")
6359   (let ((articles (gnus-summary-work-articles arg))
6360         func article)
6361     (if (eq
6362          (setq
6363           func
6364           (key-binding
6365            (read-key-sequence
6366             (substitute-command-keys
6367              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6368          'undefined)
6369         (gnus-error 1 "Undefined key")
6370       (save-excursion
6371         (while articles
6372           (gnus-summary-goto-subject (setq article (pop articles)))
6373           (let (gnus-newsgroup-processable)
6374             (command-execute func))
6375           (gnus-summary-remove-process-mark article)))))
6376   (gnus-summary-position-point))
6377
6378 (defun gnus-summary-toggle-truncation (&optional arg)
6379   "Toggle truncation of summary lines.
6380 With ARG, turn line truncation on if ARG is positive."
6381   (interactive "P")
6382   (setq truncate-lines
6383         (if (null arg) (not truncate-lines)
6384           (> (prefix-numeric-value arg) 0)))
6385   (redraw-display))
6386
6387 (defun gnus-summary-find-for-reselect ()
6388   "Return the number of an article to stay on across a reselect.
6389 The current article is considered, then following articles, then previous
6390 articles.  An article is sought which is not cancelled and isn't a temporary
6391 insertion from another group.  If there's no such then return a dummy 0."
6392   (let (found)
6393     (dolist (rev '(nil t))
6394       (unless found      ; don't demand the reverse list if we don't need it
6395         (let ((data (gnus-data-find-list
6396                      (gnus-summary-article-number) (gnus-data-list rev))))
6397           (while (and data (not found))
6398             (if (and (< 0 (gnus-data-number (car data)))
6399                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6400                 (setq found (gnus-data-number (car data))))
6401             (setq data (cdr data))))))
6402     (or found 0)))
6403
6404 (defun gnus-summary-reselect-current-group (&optional all rescan)
6405   "Exit and then reselect the current newsgroup.
6406 The prefix argument ALL means to select all articles."
6407   (interactive "P")
6408   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6409     (error "Ephemeral groups can't be reselected"))
6410   (let ((current-subject (gnus-summary-find-for-reselect))
6411         (group gnus-newsgroup-name))
6412     (setq gnus-newsgroup-begin nil)
6413     (gnus-summary-exit nil 'leave-hidden)
6414     ;; We have to adjust the point of group mode buffer because
6415     ;; point was moved to the next unread newsgroup by exiting.
6416     (gnus-summary-jump-to-group group)
6417     (when rescan
6418       (save-excursion
6419         (save-window-excursion
6420           ;; Don't show group contents.
6421           (set-window-start (selected-window) (point-max))
6422           (gnus-group-get-new-news-this-group 1))))
6423     (gnus-group-read-group all t)
6424     (gnus-summary-goto-subject current-subject nil t)))
6425
6426 (defun gnus-summary-rescan-group (&optional all)
6427   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6428   (interactive "P")
6429   (gnus-summary-reselect-current-group all t))
6430
6431 (defun gnus-summary-update-info (&optional non-destructive)
6432   (save-excursion
6433     (let ((group gnus-newsgroup-name))
6434       (when group
6435         (when gnus-newsgroup-kill-headers
6436           (setq gnus-newsgroup-killed
6437                 (gnus-compress-sequence
6438                  (gnus-sorted-union
6439                   (gnus-list-range-intersection
6440                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6441                   gnus-newsgroup-unreads)
6442                  t)))
6443         (unless (listp (cdr gnus-newsgroup-killed))
6444           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6445         (let ((headers gnus-newsgroup-headers))
6446           ;; Set the new ranges of read articles.
6447           (save-excursion
6448             (set-buffer gnus-group-buffer)
6449             (gnus-undo-force-boundary))
6450           (gnus-update-read-articles
6451            group (gnus-sorted-union
6452                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6453           ;; Set the current article marks.
6454           (let ((gnus-newsgroup-scored
6455                  (if (and (not gnus-save-score)
6456                           (not non-destructive))
6457                      nil
6458                    gnus-newsgroup-scored)))
6459             (save-excursion
6460               (gnus-update-marks)))
6461           ;; Do the cross-ref thing.
6462           (when gnus-use-cross-reference
6463             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6464           ;; Do not switch windows but change the buffer to work.
6465           (set-buffer gnus-group-buffer)
6466           (unless (gnus-ephemeral-group-p group)
6467             (gnus-group-update-group group)))))))
6468
6469 (defun gnus-summary-save-newsrc (&optional force)
6470   "Save the current number of read/marked articles in the dribble buffer.
6471 The dribble buffer will then be saved.
6472 If FORCE (the prefix), also save the .newsrc file(s)."
6473   (interactive "P")
6474   (gnus-summary-update-info t)
6475   (if force
6476       (gnus-save-newsrc-file)
6477     (gnus-dribble-save)))
6478
6479 (defun gnus-summary-exit (&optional temporary leave-hidden)
6480   "Exit reading current newsgroup, and then return to group selection mode.
6481 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6482   (interactive)
6483   (gnus-set-global-variables)
6484   (gnus-kill-save-kill-buffer)
6485   (gnus-async-halt-prefetch)
6486   (let* ((group gnus-newsgroup-name)
6487          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6488          (gnus-group-is-exiting-p t)
6489          (mode major-mode)
6490          (group-point nil)
6491          (buf (current-buffer)))
6492     (unless quit-config
6493       ;; Do adaptive scoring, and possibly save score files.
6494       (when gnus-newsgroup-adaptive
6495         (gnus-score-adaptive))
6496       (when gnus-use-scoring
6497         (gnus-score-save)))
6498     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6499     ;; If we have several article buffers, we kill them at exit.
6500     (unless gnus-single-article-buffer
6501       (gnus-kill-buffer gnus-original-article-buffer)
6502       (setq gnus-article-current nil))
6503     (when gnus-use-cache
6504       (gnus-cache-possibly-remove-articles)
6505       (gnus-cache-save-buffers))
6506     (gnus-async-prefetch-remove-group group)
6507     (when gnus-suppress-duplicates
6508       (gnus-dup-enter-articles))
6509     (when gnus-use-trees
6510       (gnus-tree-close group))
6511     (when gnus-use-cache
6512       (gnus-cache-write-active))
6513     ;; Remove entries for this group.
6514     (nnmail-purge-split-history (gnus-group-real-name group))
6515     ;; Make all changes in this group permanent.
6516     (unless quit-config
6517       (gnus-run-hooks 'gnus-exit-group-hook)
6518       (gnus-summary-update-info))
6519     (gnus-close-group group)
6520     ;; Make sure where we were, and go to next newsgroup.
6521     (set-buffer gnus-group-buffer)
6522     (unless quit-config
6523       (gnus-group-jump-to-group group))
6524     (gnus-run-hooks 'gnus-summary-exit-hook)
6525     (unless (or quit-config
6526                 ;; If this group has disappeared from the summary
6527                 ;; buffer, don't skip forwards.
6528                 (not (string= group (gnus-group-group-name))))
6529       (gnus-group-next-unread-group 1))
6530     (setq group-point (point))
6531     (if temporary
6532         nil                             ;Nothing to do.
6533       ;; If we have several article buffers, we kill them at exit.
6534       (unless gnus-single-article-buffer
6535         (gnus-kill-buffer gnus-article-buffer)
6536         (gnus-kill-buffer gnus-original-article-buffer)
6537         (setq gnus-article-current nil))
6538       (set-buffer buf)
6539       (if (not gnus-kill-summary-on-exit)
6540           (progn
6541             (gnus-deaden-summary)
6542             (setq mode nil))
6543         ;; We set all buffer-local variables to nil.  It is unclear why
6544         ;; this is needed, but if we don't, buffer-local variables are
6545         ;; not garbage-collected, it seems.  This would the lead to en
6546         ;; ever-growing Emacs.
6547         (gnus-summary-clear-local-variables)
6548         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6549           (gnus-summary-clear-local-variables))
6550         (when (get-buffer gnus-article-buffer)
6551           (bury-buffer gnus-article-buffer))
6552         ;; We clear the global counterparts of the buffer-local
6553         ;; variables as well, just to be on the safe side.
6554         (set-buffer gnus-group-buffer)
6555         (gnus-summary-clear-local-variables)
6556         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6557           (gnus-summary-clear-local-variables))
6558         ;; Return to group mode buffer.
6559         (when (eq mode 'gnus-summary-mode)
6560           (gnus-kill-buffer buf)))
6561       (setq gnus-current-select-method gnus-select-method)
6562       (if leave-hidden
6563           (set-buffer gnus-group-buffer)
6564         (pop-to-buffer gnus-group-buffer))
6565       (if (not quit-config)
6566           (progn
6567             (goto-char group-point)
6568             (unless leave-hidden
6569               (gnus-configure-windows 'group 'force))
6570             (unless (pos-visible-in-window-p)
6571               (forward-line (/ (static-if (featurep 'xemacs)
6572                                    (window-displayed-height)
6573                                  (1- (window-height)))
6574                                -2))
6575               (set-window-start (selected-window) (point))
6576               (goto-char group-point)))
6577         (gnus-handle-ephemeral-exit quit-config))
6578       ;; Clear the current group name.
6579       (unless quit-config
6580         (setq gnus-newsgroup-name nil)))))
6581
6582 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6583 (defun gnus-summary-exit-no-update (&optional no-questions)
6584   "Quit reading current newsgroup without updating read article info."
6585   (interactive)
6586   (let* ((group gnus-newsgroup-name)
6587          (gnus-group-is-exiting-p t)
6588          (gnus-group-is-exiting-without-update-p t)
6589          (quit-config (gnus-group-quit-config group)))
6590     (when (or no-questions
6591               gnus-expert-user
6592               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6593       (gnus-async-halt-prefetch)
6594       (run-hooks 'gnus-summary-prepare-exit-hook)
6595       ;; If we have several article buffers, we kill them at exit.
6596       (unless gnus-single-article-buffer
6597         (gnus-kill-buffer gnus-article-buffer)
6598         (gnus-kill-buffer gnus-original-article-buffer)
6599         (setq gnus-article-current nil))
6600       (if (not gnus-kill-summary-on-exit)
6601           (gnus-deaden-summary)
6602         (gnus-close-group group)
6603         (gnus-summary-clear-local-variables)
6604         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6605           (gnus-summary-clear-local-variables))
6606         (set-buffer gnus-group-buffer)
6607         (gnus-summary-clear-local-variables)
6608         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6609           (gnus-summary-clear-local-variables))
6610         (gnus-kill-buffer gnus-summary-buffer))
6611       (unless gnus-single-article-buffer
6612         (setq gnus-article-current nil))
6613       (when gnus-use-trees
6614         (gnus-tree-close group))
6615       (gnus-async-prefetch-remove-group group)
6616       (when (get-buffer gnus-article-buffer)
6617         (bury-buffer gnus-article-buffer))
6618       ;; Return to the group buffer.
6619       (gnus-configure-windows 'group 'force)
6620       ;; Clear the current group name.
6621       (setq gnus-newsgroup-name nil)
6622       (unless (gnus-ephemeral-group-p group)
6623         (gnus-group-update-group group))
6624       (when (equal (gnus-group-group-name) group)
6625         (gnus-group-next-unread-group 1))
6626       (when quit-config
6627         (gnus-handle-ephemeral-exit quit-config)))))
6628
6629 (defun gnus-handle-ephemeral-exit (quit-config)
6630   "Handle movement when leaving an ephemeral group.
6631 The state which existed when entering the ephemeral is reset."
6632   (if (not (buffer-name (car quit-config)))
6633       (gnus-configure-windows 'group 'force)
6634     (set-buffer (car quit-config))
6635     (cond ((eq major-mode 'gnus-summary-mode)
6636            (gnus-set-global-variables))
6637           ((eq major-mode 'gnus-article-mode)
6638            (save-excursion
6639              ;; The `gnus-summary-buffer' variable may point
6640              ;; to the old summary buffer when using a single
6641              ;; article buffer.
6642              (unless (gnus-buffer-live-p gnus-summary-buffer)
6643                (set-buffer gnus-group-buffer))
6644              (set-buffer gnus-summary-buffer)
6645              (gnus-set-global-variables))))
6646     (if (or (eq (cdr quit-config) 'article)
6647             (eq (cdr quit-config) 'pick))
6648         (progn
6649           ;; The current article may be from the ephemeral group
6650           ;; thus it is best that we reload this article
6651           ;;
6652           ;; If we're exiting from a large digest, this can be
6653           ;; extremely slow.  So, it's better not to reload it. -- jh.
6654           ;;(gnus-summary-show-article)
6655           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6656               (gnus-configure-windows 'pick 'force)
6657             (gnus-configure-windows (cdr quit-config) 'force)))
6658       (gnus-configure-windows (cdr quit-config) 'force))
6659     (when (eq major-mode 'gnus-summary-mode)
6660       (gnus-summary-next-subject 1 nil t)
6661       (gnus-summary-recenter)
6662       (gnus-summary-position-point))))
6663
6664 (defun gnus-summary-preview-mime-message ()
6665   "MIME decode and play this message."
6666   (interactive)
6667   (let ((gnus-break-pages nil)
6668         (gnus-show-mime t))
6669     (gnus-summary-select-article gnus-show-all-headers t))
6670   (let ((w (get-buffer-window gnus-article-buffer)))
6671     (when w
6672       (select-window (get-buffer-window gnus-article-buffer)))))
6673
6674 ;;; Dead summaries.
6675
6676 (defvar gnus-dead-summary-mode-map nil)
6677
6678 (unless gnus-dead-summary-mode-map
6679   (setq gnus-dead-summary-mode-map (make-keymap))
6680   (suppress-keymap gnus-dead-summary-mode-map)
6681   (substitute-key-definition
6682    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6683   (dolist (key '("\C-d" "\r" "\177" [delete]))
6684     (define-key gnus-dead-summary-mode-map
6685       key 'gnus-summary-wake-up-the-dead))
6686   (dolist (key '("q" "Q"))
6687     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6688
6689 (defvar gnus-dead-summary-mode nil
6690   "Minor mode for Gnus summary buffers.")
6691
6692 (defun gnus-dead-summary-mode (&optional arg)
6693   "Minor mode for Gnus summary buffers."
6694   (interactive "P")
6695   (when (eq major-mode 'gnus-summary-mode)
6696     (make-local-variable 'gnus-dead-summary-mode)
6697     (setq gnus-dead-summary-mode
6698           (if (null arg) (not gnus-dead-summary-mode)
6699             (> (prefix-numeric-value arg) 0)))
6700     (when gnus-dead-summary-mode
6701       (add-minor-mode
6702        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6703
6704 (defun gnus-deaden-summary ()
6705   "Make the current summary buffer into a dead summary buffer."
6706   ;; Kill any previous dead summary buffer.
6707   (when (and gnus-dead-summary
6708              (buffer-name gnus-dead-summary))
6709     (save-excursion
6710       (set-buffer gnus-dead-summary)
6711       (when gnus-dead-summary-mode
6712         (kill-buffer (current-buffer)))))
6713   ;; Make this the current dead summary.
6714   (setq gnus-dead-summary (current-buffer))
6715   (gnus-dead-summary-mode 1)
6716   (let ((name (buffer-name)))
6717     (when (string-match "Summary" name)
6718       (rename-buffer
6719        (concat (substring name 0 (match-beginning 0)) "Dead "
6720                (substring name (match-beginning 0)))
6721        t)
6722       (bury-buffer))))
6723
6724 (defun gnus-kill-or-deaden-summary (buffer)
6725   "Kill or deaden the summary BUFFER."
6726   (save-excursion
6727     (when (and (buffer-name buffer)
6728                (not gnus-single-article-buffer))
6729       (save-excursion
6730         (set-buffer buffer)
6731         (gnus-kill-buffer gnus-article-buffer)
6732         (gnus-kill-buffer gnus-original-article-buffer)))
6733     (cond
6734      ;; Kill the buffer.
6735      (gnus-kill-summary-on-exit
6736       (when (and gnus-use-trees
6737                  (gnus-buffer-exists-p buffer))
6738         (save-excursion
6739           (set-buffer buffer)
6740           (gnus-tree-close gnus-newsgroup-name)))
6741       (gnus-kill-buffer buffer))
6742      ;; Deaden the buffer.
6743      ((gnus-buffer-exists-p buffer)
6744       (save-excursion
6745         (set-buffer buffer)
6746         (gnus-deaden-summary))))))
6747
6748 (defun gnus-summary-wake-up-the-dead (&rest args)
6749   "Wake up the dead summary buffer."
6750   (interactive)
6751   (gnus-dead-summary-mode -1)
6752   (let ((name (buffer-name)))
6753     (when (string-match "Dead " name)
6754       (rename-buffer
6755        (concat (substring name 0 (match-beginning 0))
6756                (substring name (match-end 0)))
6757        t)))
6758   (gnus-message 3 "This dead summary is now alive again"))
6759
6760 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6761 (defun gnus-summary-fetch-faq (&optional faq-dir)
6762   "Fetch the FAQ for the current group.
6763 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6764 in."
6765   (interactive
6766    (list
6767     (when current-prefix-arg
6768       (completing-read
6769        "FAQ dir: " (and (listp gnus-group-faq-directory)
6770                         (mapcar (lambda (file) (list file))
6771                                 gnus-group-faq-directory))))))
6772   (let (gnus-faq-buffer)
6773     (when (setq gnus-faq-buffer
6774                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6775       (gnus-configure-windows 'summary-faq))))
6776
6777 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6778 (defun gnus-summary-describe-group (&optional force)
6779   "Describe the current newsgroup."
6780   (interactive "P")
6781   (gnus-group-describe-group force gnus-newsgroup-name))
6782
6783 (defun gnus-summary-describe-briefly ()
6784   "Describe summary mode commands briefly."
6785   (interactive)
6786   (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")))
6787
6788 ;; Walking around group mode buffer from summary mode.
6789
6790 (defun gnus-summary-next-group (&optional no-article target-group backward)
6791   "Exit current newsgroup and then select next unread newsgroup.
6792 If prefix argument NO-ARTICLE is non-nil, no article is selected
6793 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6794 previous group instead."
6795   (interactive "P")
6796   ;; Stop pre-fetching.
6797   (gnus-async-halt-prefetch)
6798   (let ((current-group gnus-newsgroup-name)
6799         (current-buffer (current-buffer))
6800         entered)
6801     ;; First we semi-exit this group to update Xrefs and all variables.
6802     ;; We can't do a real exit, because the window conf must remain
6803     ;; the same in case the user is prompted for info, and we don't
6804     ;; want the window conf to change before that...
6805     (gnus-summary-exit t)
6806     (while (not entered)
6807       ;; Then we find what group we are supposed to enter.
6808       (set-buffer gnus-group-buffer)
6809       (gnus-group-jump-to-group current-group)
6810       (setq target-group
6811             (or target-group
6812                 (if (eq gnus-keep-same-level 'best)
6813                     (gnus-summary-best-group gnus-newsgroup-name)
6814                   (gnus-summary-search-group backward gnus-keep-same-level))))
6815       (if (not target-group)
6816           ;; There are no further groups, so we return to the group
6817           ;; buffer.
6818           (progn
6819             (gnus-message 5 "Returning to the group buffer")
6820             (setq entered t)
6821             (when (gnus-buffer-live-p current-buffer)
6822               (set-buffer current-buffer)
6823               (gnus-summary-exit))
6824             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6825         ;; We try to enter the target group.
6826         (gnus-group-jump-to-group target-group)
6827         (let ((unreads (gnus-group-group-unread)))
6828           (if (and (or (eq t unreads)
6829                        (and unreads (not (zerop unreads))))
6830                    (gnus-summary-read-group
6831                     target-group nil no-article
6832                     (and (buffer-name current-buffer) current-buffer)
6833                     nil backward))
6834               (setq entered t)
6835             (setq current-group target-group
6836                   target-group nil)))))))
6837
6838 (defun gnus-summary-prev-group (&optional no-article)
6839   "Exit current newsgroup and then select previous unread newsgroup.
6840 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6841   (interactive "P")
6842   (gnus-summary-next-group no-article nil t))
6843
6844 ;; Walking around summary lines.
6845
6846 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6847   "Go to the first subject satisfying any non-nil constraint.
6848 If UNREAD is non-nil, the article should be unread.
6849 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6850 If UNSEEN is non-nil, the article should be unseen.
6851 Returns the article selected or nil if there are no matching articles."
6852   (interactive "P")
6853   (cond
6854    ;; Empty summary.
6855    ((null gnus-newsgroup-data)
6856     (gnus-message 3 "No articles in the group")
6857     nil)
6858    ;; Pick the first article.
6859    ((not (or unread undownloaded unseen))
6860     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6861     (gnus-data-number (car gnus-newsgroup-data)))
6862    ;; Find the first unread article.
6863    (t
6864     (let ((data gnus-newsgroup-data))
6865       (while (and data
6866                   (let ((num (gnus-data-number (car data))))
6867                     (or (memq num gnus-newsgroup-unfetched)
6868                         (not (or (and unread
6869                                       (memq num gnus-newsgroup-unreads))
6870                                  (and undownloaded
6871                                       (memq num gnus-newsgroup-undownloaded))
6872                                  (and unseen
6873                                       (memq num gnus-newsgroup-unseen)))))))
6874         (setq data (cdr data)))
6875       (prog1
6876           (if data
6877               (progn
6878                 (goto-char (gnus-data-pos (car data)))
6879                 (gnus-data-number (car data)))
6880             (gnus-message 3 "No more%s articles"
6881                           (let* ((r (when unread " unread"))
6882                                  (d (when undownloaded " undownloaded"))
6883                                  (s (when unseen " unseen"))
6884                                  (l (delq nil (list r d s))))
6885                             (cond ((= 3 (length l))
6886                                    (concat r "," d ", or" s))
6887                                   ((= 2 (length l))
6888                                    (concat (car l) ", or" (cadr l)))
6889                                   ((= 1 (length l))
6890                                    (car l))
6891                                   (t
6892                                    ""))))
6893             nil
6894             )
6895         (gnus-summary-position-point))))))
6896
6897 (defun gnus-summary-next-subject (n &optional unread dont-display)
6898   "Go to next N'th summary line.
6899 If N is negative, go to the previous N'th subject line.
6900 If UNREAD is non-nil, only unread articles are selected.
6901 The difference between N and the actual number of steps taken is
6902 returned."
6903   (interactive "p")
6904   (let ((backward (< n 0))
6905         (n (abs n)))
6906     (while (and (> n 0)
6907                 (if backward
6908                     (gnus-summary-find-prev unread)
6909                   (gnus-summary-find-next unread)))
6910       (unless (zerop (setq n (1- n)))
6911         (gnus-summary-show-thread)))
6912     (when (/= 0 n)
6913       (gnus-message 7 "No more%s articles"
6914                     (if unread " unread" "")))
6915     (unless dont-display
6916       (gnus-summary-recenter)
6917       (gnus-summary-position-point))
6918     n))
6919
6920 (defun gnus-summary-next-unread-subject (n)
6921   "Go to next N'th unread summary line."
6922   (interactive "p")
6923   (gnus-summary-next-subject n t))
6924
6925 (defun gnus-summary-prev-subject (n &optional unread)
6926   "Go to previous N'th summary line.
6927 If optional argument UNREAD is non-nil, only unread article is selected."
6928   (interactive "p")
6929   (gnus-summary-next-subject (- n) unread))
6930
6931 (defun gnus-summary-prev-unread-subject (n)
6932   "Go to previous N'th unread summary line."
6933   (interactive "p")
6934   (gnus-summary-next-subject (- n) t))
6935
6936 (defun gnus-summary-goto-subjects (articles)
6937   "Insert the subject header for ARTICLES in the current buffer."
6938   (save-excursion
6939     (dolist (article articles)
6940       (gnus-summary-goto-subject article t)))
6941   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6942   (gnus-summary-position-point))
6943  
6944 (defun gnus-summary-goto-subject (article &optional force silent)
6945   "Go the subject line of ARTICLE.
6946 If FORCE, also allow jumping to articles not currently shown."
6947   (interactive "nArticle number: ")
6948   (unless (numberp article)
6949     (error "Article %s is not a number" article))
6950   (let ((b (point))
6951         (data (gnus-data-find article)))
6952     ;; We read in the article if we have to.
6953     (and (not data)
6954          force
6955          (gnus-summary-insert-subject
6956           article
6957           (if (or (numberp force) (vectorp force)) force)
6958           t)
6959          (setq data (gnus-data-find article)))
6960     (goto-char b)
6961     (if (not data)
6962         (progn
6963           (unless silent
6964             (gnus-message 3 "Can't find article %d" article))
6965           nil)
6966       (let ((pt (gnus-data-pos data)))
6967         (goto-char pt)
6968         (gnus-summary-set-article-display-arrow pt))
6969       (gnus-summary-position-point)
6970       article)))
6971
6972 ;; Walking around summary lines with displaying articles.
6973
6974 (defun gnus-summary-expand-window (&optional arg)
6975   "Make the summary buffer take up the entire Emacs frame.
6976 Given a prefix, will force an `article' buffer configuration."
6977   (interactive "P")
6978   (if arg
6979       (gnus-configure-windows 'article 'force)
6980     (gnus-configure-windows 'summary 'force)))
6981
6982 (defun gnus-summary-display-article (article &optional all-header)
6983   "Display ARTICLE in article buffer."
6984   (when (gnus-buffer-live-p gnus-article-buffer)
6985     (with-current-buffer gnus-article-buffer
6986       (set-buffer-multibyte t)))
6987   (gnus-set-global-variables)
6988   (when (gnus-buffer-live-p gnus-article-buffer)
6989     (with-current-buffer gnus-article-buffer
6990       (setq gnus-article-charset gnus-newsgroup-charset)
6991       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6992       (set-buffer-multibyte t)))
6993   (if (null article)
6994       nil
6995     (prog1
6996         (if gnus-summary-display-article-function
6997             (funcall gnus-summary-display-article-function article all-header)
6998           (gnus-article-prepare article all-header))
6999       (with-current-buffer gnus-article-buffer
7000         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7001              nil))
7002       (gnus-run-hooks 'gnus-select-article-hook)
7003       (when (and gnus-current-article
7004                  (not (zerop gnus-current-article)))
7005         (gnus-summary-goto-subject gnus-current-article))
7006       (gnus-summary-recenter)
7007       (when (and gnus-use-trees gnus-show-threads)
7008         (gnus-possibly-generate-tree article)
7009         (gnus-highlight-selected-tree article))
7010       ;; Successfully display article.
7011       (gnus-article-set-window-start
7012        (cdr (assq article gnus-newsgroup-bookmarks))))))
7013
7014 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7015   "Select the current article.
7016 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7017 non-nil, the article will be re-fetched even if it already present in
7018 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7019 be displayed."
7020   ;; Make sure we are in the summary buffer to work around bbdb bug.
7021   (unless (eq major-mode 'gnus-summary-mode)
7022     (set-buffer gnus-summary-buffer))
7023   (let ((article (or article (gnus-summary-article-number)))
7024         (all-headers (not (not all-headers))) ;Must be T or NIL.
7025         gnus-summary-display-article-function)
7026     (and (not pseudo)
7027          (gnus-summary-article-pseudo-p article)
7028          (error "This is a pseudo-article"))
7029     (save-excursion
7030       (set-buffer gnus-summary-buffer)
7031       (if (or (and gnus-single-article-buffer
7032                    (or (null gnus-current-article)
7033                        (null gnus-article-current)
7034                        (null (get-buffer gnus-article-buffer))
7035                        (not (eq article (cdr gnus-article-current)))
7036                        (not (equal (car gnus-article-current)
7037                                    gnus-newsgroup-name))))
7038               (and (not gnus-single-article-buffer)
7039                    (or (null gnus-current-article)
7040                        (not (eq gnus-current-article article))))
7041               force)
7042           ;; The requested article is different from the current article.
7043           (progn
7044             (gnus-summary-display-article article all-headers)
7045             (gnus-article-set-window-start
7046              (cdr (assq article gnus-newsgroup-bookmarks)))
7047             article)
7048         'old))))
7049
7050 (defun gnus-summary-force-verify-and-decrypt ()
7051   "Display buttons for signed/encrypted parts and verify/decrypt them."
7052   (interactive)
7053   (let ((mm-verify-option 'known)
7054         (mm-decrypt-option 'known)
7055         (gnus-article-emulate-mime t)
7056         (gnus-buttonized-mime-types (append (list "multipart/signed"
7057                                                   "multipart/encrypted")
7058                                             gnus-buttonized-mime-types)))
7059     (gnus-summary-select-article nil 'force)))
7060
7061 (defun gnus-summary-set-current-mark (&optional current-mark)
7062   "Obsolete function."
7063   nil)
7064
7065 (defun gnus-summary-next-article (&optional unread subject backward push)
7066   "Select the next article.
7067 If UNREAD, only unread articles are selected.
7068 If SUBJECT, only articles with SUBJECT are selected.
7069 If BACKWARD, the previous article is selected instead of the next."
7070   (interactive "P")
7071   (cond
7072    ;; Is there such an article?
7073    ((and (gnus-summary-search-forward unread subject backward)
7074          (or (gnus-summary-display-article (gnus-summary-article-number))
7075              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7076     (gnus-summary-position-point))
7077    ;; If not, we try the first unread, if that is wanted.
7078    ((and subject
7079          gnus-auto-select-same
7080          (gnus-summary-first-unread-article))
7081     (gnus-summary-position-point)
7082     (gnus-message 6 "Wrapped"))
7083    ;; Try to get next/previous article not displayed in this group.
7084    ((and gnus-auto-extend-newsgroup
7085          (not unread) (not subject))
7086     (gnus-summary-goto-article
7087      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7088      nil (count-lines (point-min) (point))))
7089    ;; Go to next/previous group.
7090    (t
7091     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7092       (gnus-summary-jump-to-group gnus-newsgroup-name))
7093     (let ((cmd last-command-char)
7094           (point
7095            (save-excursion
7096              (set-buffer gnus-group-buffer)
7097              (point)))
7098           (group
7099            (if (eq gnus-keep-same-level 'best)
7100                (gnus-summary-best-group gnus-newsgroup-name)
7101              (gnus-summary-search-group backward gnus-keep-same-level))))
7102       ;; For some reason, the group window gets selected.  We change
7103       ;; it back.
7104       (select-window (get-buffer-window (current-buffer)))
7105       ;; Select next unread newsgroup automagically.
7106       (cond
7107        ((or (not gnus-auto-select-next)
7108             (not cmd))
7109         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7110        ((or (eq gnus-auto-select-next 'quietly)
7111             (and (eq gnus-auto-select-next 'slightly-quietly)
7112                  push)
7113             (and (eq gnus-auto-select-next 'almost-quietly)
7114                  (gnus-summary-last-article-p)))
7115         ;; Select quietly.
7116         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7117             (gnus-summary-exit)
7118           (gnus-message 7 "No more%s articles (%s)..."
7119                         (if unread " unread" "")
7120                         (if group (concat "selecting " group)
7121                           "exiting"))
7122           (gnus-summary-next-group nil group backward)))
7123        (t
7124         (when (gnus-key-press-event-p last-input-event)
7125           (gnus-summary-walk-group-buffer
7126            gnus-newsgroup-name cmd unread backward point))))))))
7127
7128 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7129   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7130                       (?\C-p (gnus-group-prev-unread-group 1))))
7131         (cursor-in-echo-area t)
7132         keve key group ended prompt)
7133     (save-excursion
7134       (set-buffer gnus-group-buffer)
7135       (goto-char start)
7136       (setq group
7137             (if (eq gnus-keep-same-level 'best)
7138                 (gnus-summary-best-group gnus-newsgroup-name)
7139               (gnus-summary-search-group backward gnus-keep-same-level))))
7140     (while (not ended)
7141       (setq prompt
7142             (format
7143              "No more%s articles%s " (if unread " unread" "")
7144              (if (and group
7145                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7146                  (format " (Type %s for %s [%s])"
7147                          (single-key-description cmd) group
7148                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7149                (format " (Type %s to exit %s)"
7150                        (single-key-description cmd)
7151                        gnus-newsgroup-name))))
7152       ;; Confirm auto selection.
7153       (setq key (car (setq keve (gnus-read-event-char prompt)))
7154             ended t)
7155       (cond
7156        ((assq key keystrokes)
7157         (let ((obuf (current-buffer)))
7158           (switch-to-buffer gnus-group-buffer)
7159           (when group
7160             (gnus-group-jump-to-group group))
7161           (eval (cadr (assq key keystrokes)))
7162           (setq group (gnus-group-group-name))
7163           (switch-to-buffer obuf))
7164         (setq ended nil))
7165        ((equal key cmd)
7166         (if (or (not group)
7167                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7168             (gnus-summary-exit)
7169           (gnus-summary-next-group nil group backward)))
7170        (t
7171         (push (cdr keve) unread-command-events))))))
7172
7173 (defun gnus-summary-next-unread-article ()
7174   "Select unread article after current one."
7175   (interactive)
7176   (gnus-summary-next-article
7177    (or (not (eq gnus-summary-goto-unread 'never))
7178        (gnus-summary-last-article-p (gnus-summary-article-number)))
7179    (and gnus-auto-select-same
7180         (gnus-summary-article-subject))))
7181
7182 (defun gnus-summary-prev-article (&optional unread subject)
7183   "Select the article after the current one.
7184 If UNREAD is non-nil, only unread articles are selected."
7185   (interactive "P")
7186   (gnus-summary-next-article unread subject t))
7187
7188 (defun gnus-summary-prev-unread-article ()
7189   "Select unread article before current one."
7190   (interactive)
7191   (gnus-summary-prev-article
7192    (or (not (eq gnus-summary-goto-unread 'never))
7193        (gnus-summary-first-article-p (gnus-summary-article-number)))
7194    (and gnus-auto-select-same
7195         (gnus-summary-article-subject))))
7196
7197 (defun gnus-summary-next-page (&optional lines circular stop)
7198   "Show next page of the selected article.
7199 If at the end of the current article, select the next article.
7200 LINES says how many lines should be scrolled up.
7201
7202 If CIRCULAR is non-nil, go to the start of the article instead of
7203 selecting the next article when reaching the end of the current
7204 article.
7205
7206 If STOP is non-nil, just stop when reaching the end of the message.
7207
7208 Also see the variable `gnus-article-skip-boring'."
7209   (interactive "P")
7210   (setq gnus-summary-buffer (current-buffer))
7211   (gnus-set-global-variables)
7212   (let ((article (gnus-summary-article-number))
7213         (article-window (get-buffer-window gnus-article-buffer t))
7214         endp)
7215     ;; If the buffer is empty, we have no article.
7216     (unless article
7217       (error "No article to select"))
7218     (gnus-configure-windows 'article)
7219     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7220         (if (and (eq gnus-summary-goto-unread 'never)
7221                  (not (gnus-summary-last-article-p article)))
7222             (gnus-summary-next-article)
7223           (gnus-summary-next-unread-article))
7224       (if (or (null gnus-current-article)
7225               (null gnus-article-current)
7226               (/= article (cdr gnus-article-current))
7227               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7228           ;; Selected subject is different from current article's.
7229           (gnus-summary-display-article article)
7230         (when article-window
7231           (gnus-eval-in-buffer-window gnus-article-buffer
7232             (setq endp (or (gnus-article-next-page lines)
7233                            (gnus-article-only-boring-p))))
7234           (when endp
7235             (cond (stop
7236                    (gnus-message 3 "End of message"))
7237                   (circular
7238                    (gnus-summary-beginning-of-article))
7239                   (lines
7240                    (gnus-message 3 "End of message"))
7241                   ((null lines)
7242                    (if (and (eq gnus-summary-goto-unread 'never)
7243                             (not (gnus-summary-last-article-p article)))
7244                        (gnus-summary-next-article)
7245                      (gnus-summary-next-unread-article))))))))
7246     (gnus-summary-recenter)
7247     (gnus-summary-position-point)))
7248
7249 (defun gnus-summary-prev-page (&optional lines move)
7250   "Show previous page of selected article.
7251 Argument LINES specifies lines to be scrolled down.
7252 If MOVE, move to the previous unread article if point is at
7253 the beginning of the buffer."
7254   (interactive "P")
7255   (let ((article (gnus-summary-article-number))
7256         (article-window (get-buffer-window gnus-article-buffer t))
7257         endp)
7258     (gnus-configure-windows 'article)
7259     (if (or (null gnus-current-article)
7260             (null gnus-article-current)
7261             (/= article (cdr gnus-article-current))
7262             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7263         ;; Selected subject is different from current article's.
7264         (gnus-summary-display-article article)
7265       (gnus-summary-recenter)
7266       (when article-window
7267         (gnus-eval-in-buffer-window gnus-article-buffer
7268           (setq endp (gnus-article-prev-page lines)))
7269         (when (and move endp)
7270           (cond (lines
7271                  (gnus-message 3 "Beginning of message"))
7272                 ((null lines)
7273                  (if (and (eq gnus-summary-goto-unread 'never)
7274                           (not (gnus-summary-first-article-p article)))
7275                      (gnus-summary-prev-article)
7276                    (gnus-summary-prev-unread-article))))))))
7277   (gnus-summary-position-point))
7278
7279 (defun gnus-summary-prev-page-or-article (&optional lines)
7280   "Show previous page of selected article.
7281 Argument LINES specifies lines to be scrolled down.
7282 If at the beginning of the article, go to the next article."
7283   (interactive "P")
7284   (gnus-summary-prev-page lines t))
7285
7286 (defun gnus-summary-scroll-up (lines)
7287   "Scroll up (or down) one line current article.
7288 Argument LINES specifies lines to be scrolled up (or down if negative)."
7289   (interactive "p")
7290   (gnus-configure-windows 'article)
7291   (gnus-summary-show-thread)
7292   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7293     (gnus-eval-in-buffer-window gnus-article-buffer
7294       (cond ((> lines 0)
7295              (when (gnus-article-next-page lines)
7296                (gnus-message 3 "End of message")))
7297             ((< lines 0)
7298              (gnus-article-prev-page (- lines))))))
7299   (gnus-summary-recenter)
7300   (gnus-summary-position-point))
7301
7302 (defun gnus-summary-scroll-down (lines)
7303   "Scroll down (or up) one line current article.
7304 Argument LINES specifies lines to be scrolled down (or up if negative)."
7305   (interactive "p")
7306   (gnus-summary-scroll-up (- lines)))
7307
7308 (defun gnus-summary-next-same-subject ()
7309   "Select next article which has the same subject as current one."
7310   (interactive)
7311   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7312
7313 (defun gnus-summary-prev-same-subject ()
7314   "Select previous article which has the same subject as current one."
7315   (interactive)
7316   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7317
7318 (defun gnus-summary-next-unread-same-subject ()
7319   "Select next unread article which has the same subject as current one."
7320   (interactive)
7321   (gnus-summary-next-article t (gnus-summary-article-subject)))
7322
7323 (defun gnus-summary-prev-unread-same-subject ()
7324   "Select previous unread article which has the same subject as current one."
7325   (interactive)
7326   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7327
7328 (defun gnus-summary-first-unread-article ()
7329   "Select the first unread article.
7330 Return nil if there are no unread articles."
7331   (interactive)
7332   (prog1
7333       (when (gnus-summary-first-subject t)
7334         (gnus-summary-show-thread)
7335         (gnus-summary-first-subject t)
7336         (gnus-summary-display-article (gnus-summary-article-number)))
7337     (gnus-summary-position-point)))
7338
7339 (defun gnus-summary-first-unread-subject ()
7340   "Place the point on the subject line of the first unread article.
7341 Return nil if there are no unread articles."
7342   (interactive)
7343   (prog1
7344       (when (gnus-summary-first-subject t)
7345         (gnus-summary-show-thread)
7346         (gnus-summary-first-subject t))
7347     (gnus-summary-position-point)))
7348
7349 (defun gnus-summary-first-unseen-subject ()
7350   "Place the point on the subject line of the first unseen article.
7351 Return nil if there are no unseen articles."
7352   (interactive)
7353   (prog1
7354       (when (gnus-summary-first-subject nil nil t)
7355         (gnus-summary-show-thread)
7356         (gnus-summary-first-subject nil nil t))
7357     (gnus-summary-position-point)))
7358
7359 (defun gnus-summary-first-unseen-or-unread-subject ()
7360   "Place the point on the subject line of the first unseen article or,
7361 if all article have been seen, on the subject line of the first unread
7362 article."
7363   (interactive)
7364   (prog1
7365       (unless (when (gnus-summary-first-subject nil nil t)
7366                 (gnus-summary-show-thread)
7367                 (gnus-summary-first-subject nil nil t))
7368         (when (gnus-summary-first-subject t)
7369           (gnus-summary-show-thread)
7370           (gnus-summary-first-subject t)))
7371     (gnus-summary-position-point)))
7372
7373 (defun gnus-summary-first-article ()
7374   "Select the first article.
7375 Return nil if there are no articles."
7376   (interactive)
7377   (prog1
7378       (when (gnus-summary-first-subject)
7379         (gnus-summary-show-thread)
7380         (gnus-summary-first-subject)
7381         (gnus-summary-display-article (gnus-summary-article-number)))
7382     (gnus-summary-position-point)))
7383
7384 (defun gnus-summary-best-unread-article (&optional arg)
7385   "Select the unread article with the highest score.
7386 If given a prefix argument, select the next unread article that has a
7387 score higher than the default score."
7388   (interactive "P")
7389   (let ((article (if arg
7390                      (gnus-summary-better-unread-subject)
7391                    (gnus-summary-best-unread-subject))))
7392     (if article
7393         (gnus-summary-goto-article article)
7394       (error "No unread articles"))))
7395
7396 (defun gnus-summary-best-unread-subject ()
7397   "Select the unread subject with the highest score."
7398   (interactive)
7399   (let ((best -1000000)
7400         (data gnus-newsgroup-data)
7401         article score)
7402     (while data
7403       (and (gnus-data-unread-p (car data))
7404            (> (setq score
7405                     (gnus-summary-article-score (gnus-data-number (car data))))
7406               best)
7407            (setq best score
7408                  article (gnus-data-number (car data))))
7409       (setq data (cdr data)))
7410     (when article
7411       (gnus-summary-goto-subject article))
7412     (gnus-summary-position-point)
7413     article))
7414
7415 (defun gnus-summary-better-unread-subject ()
7416   "Select the first unread subject that has a score over the default score."
7417   (interactive)
7418   (let ((data gnus-newsgroup-data)
7419         article score)
7420     (while (and (setq article (gnus-data-number (car data)))
7421                 (or (gnus-data-read-p (car data))
7422                     (not (> (gnus-summary-article-score article)
7423                             gnus-summary-default-score))))
7424       (setq data (cdr data)))
7425     (when article
7426       (gnus-summary-goto-subject article))
7427     (gnus-summary-position-point)
7428     article))
7429
7430 (defun gnus-summary-last-subject ()
7431   "Go to the last displayed subject line in the group."
7432   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7433     (when article
7434       (gnus-summary-goto-subject article))))
7435
7436 (defun gnus-summary-goto-article (article &optional all-headers force)
7437   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7438 If ALL-HEADERS is non-nil, no header lines are hidden.
7439 If FORCE, go to the article even if it isn't displayed.  If FORCE
7440 is a number, it is the line the article is to be displayed on."
7441   (interactive
7442    (list
7443     (completing-read
7444      "Article number or Message-ID: "
7445      (mapcar (lambda (number) (list (int-to-string number)))
7446              gnus-newsgroup-limit))
7447     current-prefix-arg
7448     t))
7449   (prog1
7450       (if (and (stringp article)
7451                (string-match "@\\|%40" article))
7452           (gnus-summary-refer-article article)
7453         (when (stringp article)
7454           (setq article (string-to-number article)))
7455         (if (gnus-summary-goto-subject article force)
7456             (gnus-summary-display-article article all-headers)
7457           (gnus-message 4 "Couldn't go to article %s" article) nil))
7458     (gnus-summary-position-point)))
7459
7460 (defun gnus-summary-goto-last-article ()
7461   "Go to the previously read article."
7462   (interactive)
7463   (prog1
7464       (when gnus-last-article
7465         (gnus-summary-goto-article gnus-last-article nil t))
7466     (gnus-summary-position-point)))
7467
7468 (defun gnus-summary-pop-article (number)
7469   "Pop one article off the history and go to the previous.
7470 NUMBER articles will be popped off."
7471   (interactive "p")
7472   (let (to)
7473     (setq gnus-newsgroup-history
7474           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7475     (if to
7476         (gnus-summary-goto-article (car to) nil t)
7477       (error "Article history empty")))
7478   (gnus-summary-position-point))
7479
7480 ;; Summary commands and functions for limiting the summary buffer.
7481
7482 (defun gnus-summary-limit-to-articles (n)
7483   "Limit the summary buffer to the next N articles.
7484 If not given a prefix, use the process marked articles instead."
7485   (interactive "P")
7486   (prog1
7487       (let ((articles (gnus-summary-work-articles n)))
7488         (setq gnus-newsgroup-processable nil)
7489         (gnus-summary-limit articles))
7490     (gnus-summary-position-point)))
7491
7492 (defun gnus-summary-pop-limit (&optional total)
7493   "Restore the previous limit.
7494 If given a prefix, remove all limits."
7495   (interactive "P")
7496   (when total
7497     (setq gnus-newsgroup-limits
7498           (list (mapcar (lambda (h) (mail-header-number h))
7499                         gnus-newsgroup-headers))))
7500   (unless gnus-newsgroup-limits
7501     (error "No limit to pop"))
7502   (prog1
7503       (gnus-summary-limit nil 'pop)
7504     (gnus-summary-position-point)))
7505
7506 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7507   "Limit the summary buffer to articles that have subjects that match a regexp.
7508 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7509   (interactive
7510    (list (read-string (if current-prefix-arg
7511                           "Exclude subject (regexp): "
7512                         "Limit to subject (regexp): "))
7513          nil current-prefix-arg))
7514   (unless header
7515     (setq header "subject"))
7516   (when (not (equal "" subject))
7517     (prog1
7518         (let ((articles (gnus-summary-find-matching
7519                          (or header "subject") subject 'all nil nil
7520                          not-matching)))
7521           (unless articles
7522             (error "Found no matches for \"%s\"" subject))
7523           (gnus-summary-limit articles))
7524       (gnus-summary-position-point))))
7525
7526 (defun gnus-summary-limit-to-author (from &optional not-matching)
7527   "Limit the summary buffer to articles that have authors that match a regexp.
7528 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7529   (interactive
7530    (list (read-string (if current-prefix-arg
7531                           "Exclude author (regexp): "
7532                         "Limit to author (regexp): "))
7533          current-prefix-arg))
7534   (gnus-summary-limit-to-subject from "from" not-matching))
7535
7536 (defun gnus-summary-limit-to-age (age &optional younger-p)
7537   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7538 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7539 articles that are younger than AGE days."
7540   (interactive
7541    (let ((younger current-prefix-arg)
7542          (days-got nil)
7543          days)
7544      (while (not days-got)
7545        (setq days (if younger
7546                       (read-string "Limit to articles younger than (in days, older when negative): ")
7547                     (read-string
7548                      "Limit to articles older than (in days, younger when negative): ")))
7549        (when (> (length days) 0)
7550          (setq days (read days)))
7551        (if (numberp days)
7552            (progn
7553              (setq days-got t)
7554              (if (< days 0)
7555                  (progn
7556                    (setq younger (not younger))
7557                    (setq days (* days -1)))))
7558          (message "Please enter a number.")
7559          (sleep-for 1)))
7560      (list days younger)))
7561   (prog1
7562       (let ((data gnus-newsgroup-data)
7563             (cutoff (days-to-time age))
7564             articles d date is-younger)
7565         (while (setq d (pop data))
7566           (when (and (vectorp (gnus-data-header d))
7567                      (setq date (mail-header-date (gnus-data-header d))))
7568             (setq is-younger (time-less-p
7569                               (time-since (condition-case ()
7570                                               (date-to-time date)
7571                                             (error '(0 0))))
7572                               cutoff))
7573             (when (if younger-p
7574                       is-younger
7575                     (not is-younger))
7576               (push (gnus-data-number d) articles))))
7577         (gnus-summary-limit (nreverse articles)))
7578     (gnus-summary-position-point)))
7579
7580 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7581   "Limit the summary buffer to articles that match an 'extra' header."
7582   (interactive
7583    (let ((header
7584           (intern
7585            (gnus-completing-read-with-default
7586             (symbol-name (car gnus-extra-headers))
7587             (if current-prefix-arg
7588                 "Exclude extra header:"
7589               "Limit extra header:")
7590             (mapcar (lambda (x)
7591                       (cons (symbol-name x) x))
7592                     gnus-extra-headers)
7593             nil
7594             t))))
7595      (list header
7596            (read-string (format "%s header %s (regexp): "
7597                                 (if current-prefix-arg "Exclude" "Limit to")
7598                                 header))
7599            current-prefix-arg)))
7600   (when (not (equal "" regexp))
7601     (prog1
7602         (let ((articles (gnus-summary-find-matching
7603                          (cons 'extra header) regexp 'all nil nil
7604                          not-matching)))
7605           (unless articles
7606             (error "Found no matches for \"%s\"" regexp))
7607           (gnus-summary-limit articles))
7608       (gnus-summary-position-point))))
7609
7610 (defun gnus-summary-limit-to-display-predicate ()
7611   "Limit the summary buffer to the predicated in the `display' group parameter."
7612   (interactive)
7613   (unless gnus-newsgroup-display
7614     (error "There is no `display' group parameter"))
7615   (let (articles)
7616     (dolist (number gnus-newsgroup-articles)
7617       (when (funcall gnus-newsgroup-display)
7618         (push number articles)))
7619     (gnus-summary-limit articles))
7620   (gnus-summary-position-point))
7621
7622 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7623 (make-obsolete
7624  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7625
7626 (defun gnus-summary-limit-to-unread (&optional all)
7627   "Limit the summary buffer to articles that are not marked as read.
7628 If ALL is non-nil, limit strictly to unread articles."
7629   (interactive "P")
7630   (if all
7631       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7632     (gnus-summary-limit-to-marks
7633      ;; Concat all the marks that say that an article is read and have
7634      ;; those removed.
7635      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7636            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7637            gnus-low-score-mark gnus-expirable-mark
7638            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7639            gnus-duplicate-mark gnus-souped-mark)
7640      'reverse)))
7641
7642 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7643 (make-obsolete 'gnus-summary-delete-marked-with
7644                'gnus-summary-limit-exclude-marks)
7645
7646 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7647   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7648 If REVERSE, limit the summary buffer to articles that are marked
7649 with MARKS.  MARKS can either be a string of marks or a list of marks.
7650 Returns how many articles were removed."
7651   (interactive "sMarks: ")
7652   (gnus-summary-limit-to-marks marks t))
7653
7654 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7655   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7656 If REVERSE (the prefix), limit the summary buffer to articles that are
7657 not marked with MARKS.  MARKS can either be a string of marks or a
7658 list of marks.
7659 Returns how many articles were removed."
7660   (interactive "sMarks: \nP")
7661   (prog1
7662       (let ((data gnus-newsgroup-data)
7663             (marks (if (listp marks) marks
7664                      (append marks nil))) ; Transform to list.
7665             articles)
7666         (while data
7667           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7668                   (memq (gnus-data-mark (car data)) marks))
7669             (push (gnus-data-number (car data)) articles))
7670           (setq data (cdr data)))
7671         (gnus-summary-limit articles))
7672     (gnus-summary-position-point)))
7673
7674 (defun gnus-summary-limit-to-score (score)
7675   "Limit to articles with score at or above SCORE."
7676   (interactive "NLimit to articles with score of at least: ")
7677   (let ((data gnus-newsgroup-data)
7678         articles)
7679     (while data
7680       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7681                 score)
7682         (push (gnus-data-number (car data)) articles))
7683       (setq data (cdr data)))
7684     (prog1
7685         (gnus-summary-limit articles)
7686       (gnus-summary-position-point))))
7687
7688 (defun gnus-summary-limit-to-unseen ()
7689   "Limit to unseen articles."
7690   (interactive)
7691   (prog1
7692       (gnus-summary-limit gnus-newsgroup-unseen)
7693     (gnus-summary-position-point)))
7694
7695 (defun gnus-summary-limit-include-thread (id)
7696   "Display all the hidden articles that is in the thread with ID in it.
7697 When called interactively, ID is the Message-ID of the current
7698 article."
7699   (interactive (list (mail-header-id (gnus-summary-article-header))))
7700   (let ((articles (gnus-articles-in-thread
7701                    (gnus-id-to-thread (gnus-root-id id)))))
7702     (prog1
7703         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7704       (gnus-summary-limit-include-matching-articles
7705        "subject"
7706        (regexp-quote (gnus-simplify-subject-re
7707                       (mail-header-subject (gnus-id-to-header id)))))
7708       (gnus-summary-position-point))))
7709
7710 (defun gnus-summary-limit-include-matching-articles (header regexp)
7711   "Display all the hidden articles that have HEADERs that match REGEXP."
7712   (interactive (list (read-string "Match on header: ")
7713                      (read-string "Regexp: ")))
7714   (let ((articles (gnus-find-matching-articles header regexp)))
7715     (prog1
7716         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7717       (gnus-summary-position-point))))
7718
7719 (defun gnus-summary-insert-dormant-articles ()
7720   "Insert all the dormant articles for this group into the current buffer."
7721   (interactive)
7722   (let ((gnus-verbose (max 6 gnus-verbose)))
7723     (if (not gnus-newsgroup-dormant)
7724         (gnus-message 3 "No cached articles for this group")
7725       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7726
7727 (defun gnus-summary-limit-include-dormant ()
7728   "Display all the hidden articles that are marked as dormant.
7729 Note that this command only works on a subset of the articles currently
7730 fetched for this group."
7731   (interactive)
7732   (unless gnus-newsgroup-dormant
7733     (error "There are no dormant articles in this group"))
7734   (prog1
7735       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7736     (gnus-summary-position-point)))
7737
7738 (defun gnus-summary-limit-exclude-dormant ()
7739   "Hide all dormant articles."
7740   (interactive)
7741   (prog1
7742       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7743     (gnus-summary-position-point)))
7744
7745 (defun gnus-summary-limit-exclude-childless-dormant ()
7746   "Hide all dormant articles that have no children."
7747   (interactive)
7748   (let ((data (gnus-data-list t))
7749         articles d children)
7750     ;; Find all articles that are either not dormant or have
7751     ;; children.
7752     (while (setq d (pop data))
7753       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7754                 (and (setq children
7755                            (gnus-article-children (gnus-data-number d)))
7756                      (let (found)
7757                        (while children
7758                          (when (memq (car children) articles)
7759                            (setq children nil
7760                                  found t))
7761                          (pop children))
7762                        found)))
7763         (push (gnus-data-number d) articles)))
7764     ;; Do the limiting.
7765     (prog1
7766         (gnus-summary-limit articles)
7767       (gnus-summary-position-point))))
7768
7769 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7770   "Mark all unread excluded articles as read.
7771 If ALL, mark even excluded ticked and dormants as read."
7772   (interactive "P")
7773   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7774   (let ((articles (gnus-sorted-ndifference
7775                    (sort
7776                     (mapcar (lambda (h) (mail-header-number h))
7777                             gnus-newsgroup-headers)
7778                     '<)
7779                    gnus-newsgroup-limit))
7780         article)
7781     (setq gnus-newsgroup-unreads
7782           (gnus-sorted-intersection gnus-newsgroup-unreads
7783                                     gnus-newsgroup-limit))
7784     (if all
7785         (setq gnus-newsgroup-dormant nil
7786               gnus-newsgroup-marked nil
7787               gnus-newsgroup-reads
7788               (nconc
7789                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7790                gnus-newsgroup-reads))
7791       (while (setq article (pop articles))
7792         (unless (or (memq article gnus-newsgroup-dormant)
7793                     (memq article gnus-newsgroup-marked))
7794           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7795
7796 (defun gnus-summary-limit (articles &optional pop)
7797   (if pop
7798       ;; We pop the previous limit off the stack and use that.
7799       (setq articles (car gnus-newsgroup-limits)
7800             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7801     ;; We use the new limit, so we push the old limit on the stack.
7802     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7803   ;; Set the limit.
7804   (setq gnus-newsgroup-limit articles)
7805   (let ((total (length gnus-newsgroup-data))
7806         (data (gnus-data-find-list (gnus-summary-article-number)))
7807         (gnus-summary-mark-below nil)   ; Inhibit this.
7808         found)
7809     ;; This will do all the work of generating the new summary buffer
7810     ;; according to the new limit.
7811     (gnus-summary-prepare)
7812     ;; Hide any threads, possibly.
7813     (gnus-summary-maybe-hide-threads)
7814     ;; Try to return to the article you were at, or one in the
7815     ;; neighborhood.
7816     (when data
7817       ;; We try to find some article after the current one.
7818       (while data
7819         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7820           (setq data nil
7821                 found t))
7822         (setq data (cdr data))))
7823     (unless found
7824       ;; If there is no data, that means that we were after the last
7825       ;; article.  The same goes when we can't find any articles
7826       ;; after the current one.
7827       (goto-char (point-max))
7828       (gnus-summary-find-prev))
7829     (gnus-set-mode-line 'summary)
7830     ;; We return how many articles were removed from the summary
7831     ;; buffer as a result of the new limit.
7832     (- total (length gnus-newsgroup-data))))
7833
7834 (defsubst gnus-invisible-cut-children (threads)
7835   (let ((num 0))
7836     (while threads
7837       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7838         (incf num))
7839       (pop threads))
7840     (< num 2)))
7841
7842 (defsubst gnus-cut-thread (thread)
7843   "Go forwards in the thread until we find an article that we want to display."
7844   (when (or (eq gnus-fetch-old-headers 'some)
7845             (eq gnus-fetch-old-headers 'invisible)
7846             (numberp gnus-fetch-old-headers)
7847             (eq gnus-build-sparse-threads 'some)
7848             (eq gnus-build-sparse-threads 'more))
7849     ;; Deal with old-fetched headers and sparse threads.
7850     (while (and
7851             thread
7852             (or
7853              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7854              (gnus-summary-article-ancient-p
7855               (mail-header-number (car thread))))
7856             (if (or (<= (length (cdr thread)) 1)
7857                     (eq gnus-fetch-old-headers 'invisible))
7858                 (setq gnus-newsgroup-limit
7859                       (delq (mail-header-number (car thread))
7860                             gnus-newsgroup-limit)
7861                       thread (cadr thread))
7862               (when (gnus-invisible-cut-children (cdr thread))
7863                 (let ((th (cdr thread)))
7864                   (while th
7865                     (if (memq (mail-header-number (caar th))
7866                               gnus-newsgroup-limit)
7867                         (setq thread (car th)
7868                               th nil)
7869                       (setq th (cdr th))))))))))
7870   thread)
7871
7872 (defun gnus-cut-threads (threads)
7873   "Cut off all uninteresting articles from the beginning of THREADS."
7874   (when (or (eq gnus-fetch-old-headers 'some)
7875             (eq gnus-fetch-old-headers 'invisible)
7876             (numberp gnus-fetch-old-headers)
7877             (eq gnus-build-sparse-threads 'some)
7878             (eq gnus-build-sparse-threads 'more))
7879     (let ((th threads))
7880       (while th
7881         (setcar th (gnus-cut-thread (car th)))
7882         (setq th (cdr th)))))
7883   ;; Remove nixed out threads.
7884   (delq nil threads))
7885
7886 (defun gnus-summary-initial-limit (&optional show-if-empty)
7887   "Figure out what the initial limit is supposed to be on group entry.
7888 This entails weeding out unwanted dormants, low-scored articles,
7889 fetch-old-headers verbiage, and so on."
7890   ;; Most groups have nothing to remove.
7891   (if (or gnus-inhibit-limiting
7892           (and (null gnus-newsgroup-dormant)
7893                (eq gnus-newsgroup-display 'gnus-not-ignore)
7894                (not (eq gnus-fetch-old-headers 'some))
7895                (not (numberp gnus-fetch-old-headers))
7896                (not (eq gnus-fetch-old-headers 'invisible))
7897                (null gnus-summary-expunge-below)
7898                (not (eq gnus-build-sparse-threads 'some))
7899                (not (eq gnus-build-sparse-threads 'more))
7900                (null gnus-thread-expunge-below)
7901                (not gnus-use-nocem)))
7902       ()                                ; Do nothing.
7903     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7904     (setq gnus-newsgroup-limit nil)
7905     (mapatoms
7906      (lambda (node)
7907        (unless (car (symbol-value node))
7908          ;; These threads have no parents -- they are roots.
7909          (let ((nodes (cdr (symbol-value node)))
7910                thread)
7911            (while nodes
7912              (if (and gnus-thread-expunge-below
7913                       (< (gnus-thread-total-score (car nodes))
7914                          gnus-thread-expunge-below))
7915                  (gnus-expunge-thread (pop nodes))
7916                (setq thread (pop nodes))
7917                (gnus-summary-limit-children thread))))))
7918      gnus-newsgroup-dependencies)
7919     ;; If this limitation resulted in an empty group, we might
7920     ;; pop the previous limit and use it instead.
7921     (when (and (not gnus-newsgroup-limit)
7922                show-if-empty)
7923       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7924     gnus-newsgroup-limit))
7925
7926 (defun gnus-summary-limit-children (thread)
7927   "Return 1 if this subthread is visible and 0 if it is not."
7928   ;; First we get the number of visible children to this thread.  This
7929   ;; is done by recursing down the thread using this function, so this
7930   ;; will really go down to a leaf article first, before slowly
7931   ;; working its way up towards the root.
7932   (when thread
7933     (let* ((max-lisp-eval-depth 5000)
7934            (children
7935            (if (cdr thread)
7936                (apply '+ (mapcar 'gnus-summary-limit-children
7937                                  (cdr thread)))
7938              0))
7939           (number (mail-header-number (car thread)))
7940           score)
7941       (if (and
7942            (not (memq number gnus-newsgroup-marked))
7943            (or
7944             ;; If this article is dormant and has absolutely no visible
7945             ;; children, then this article isn't visible.
7946             (and (memq number gnus-newsgroup-dormant)
7947                  (zerop children))
7948             ;; If this is "fetch-old-headered" and there is no
7949             ;; visible children, then we don't want this article.
7950             (and (or (eq gnus-fetch-old-headers 'some)
7951                      (numberp gnus-fetch-old-headers))
7952                  (gnus-summary-article-ancient-p number)
7953                  (zerop children))
7954             ;; If this is "fetch-old-headered" and `invisible', then
7955             ;; we don't want this article.
7956             (and (eq gnus-fetch-old-headers 'invisible)
7957                  (gnus-summary-article-ancient-p number))
7958             ;; If this is a sparsely inserted article with no children,
7959             ;; we don't want it.
7960             (and (eq gnus-build-sparse-threads 'some)
7961                  (gnus-summary-article-sparse-p number)
7962                  (zerop children))
7963             ;; If we use expunging, and this article is really
7964             ;; low-scored, then we don't want this article.
7965             (when (and gnus-summary-expunge-below
7966                        (< (setq score
7967                                 (or (cdr (assq number gnus-newsgroup-scored))
7968                                     gnus-summary-default-score))
7969                           gnus-summary-expunge-below))
7970               ;; We increase the expunge-tally here, but that has
7971               ;; nothing to do with the limits, really.
7972               (incf gnus-newsgroup-expunged-tally)
7973               ;; We also mark as read here, if that's wanted.
7974               (when (and gnus-summary-mark-below
7975                          (< score gnus-summary-mark-below))
7976                 (setq gnus-newsgroup-unreads
7977                       (delq number gnus-newsgroup-unreads))
7978                 (if gnus-newsgroup-auto-expire
7979                     (push number gnus-newsgroup-expirable)
7980                   (push (cons number gnus-low-score-mark)
7981                         gnus-newsgroup-reads)))
7982               t)
7983             ;; Do the `display' group parameter.
7984             (and gnus-newsgroup-display
7985                  (not (funcall gnus-newsgroup-display)))
7986             ;; Check NoCeM things.
7987             (if (and gnus-use-nocem
7988                      (gnus-nocem-unwanted-article-p
7989                       (mail-header-id (car thread))))
7990                 (progn
7991                   (setq gnus-newsgroup-unreads
7992                         (delq number gnus-newsgroup-unreads))
7993                   t))))
7994           ;; Nope, invisible article.
7995           0
7996         ;; Ok, this article is to be visible, so we add it to the limit
7997         ;; and return 1.
7998         (push number gnus-newsgroup-limit)
7999         1))))
8000
8001 (defun gnus-expunge-thread (thread)
8002   "Mark all articles in THREAD as read."
8003   (let* ((number (mail-header-number (car thread))))
8004     (incf gnus-newsgroup-expunged-tally)
8005     ;; We also mark as read here, if that's wanted.
8006     (setq gnus-newsgroup-unreads
8007           (delq number gnus-newsgroup-unreads))
8008     (if gnus-newsgroup-auto-expire
8009         (push number gnus-newsgroup-expirable)
8010       (push (cons number gnus-low-score-mark)
8011             gnus-newsgroup-reads)))
8012   ;; Go recursively through all subthreads.
8013   (mapcar 'gnus-expunge-thread (cdr thread)))
8014
8015 ;; Summary article oriented commands
8016
8017 (defun gnus-summary-refer-parent-article (n)
8018   "Refer parent article N times.
8019 If N is negative, go to ancestor -N instead.
8020 The difference between N and the number of articles fetched is returned."
8021   (interactive "p")
8022   (let ((skip 1)
8023         error header ref)
8024     (when (not (natnump n))
8025       (setq skip (abs n)
8026             n 1))
8027     (while (and (> n 0)
8028                 (not error))
8029       (setq header (gnus-summary-article-header))
8030       (if (and (eq (mail-header-number header)
8031                    (cdr gnus-article-current))
8032                (equal gnus-newsgroup-name
8033                       (car gnus-article-current)))
8034           ;; If we try to find the parent of the currently
8035           ;; displayed article, then we take a look at the actual
8036           ;; References header, since this is slightly more
8037           ;; reliable than the References field we got from the
8038           ;; server.
8039           (save-excursion
8040             (set-buffer gnus-original-article-buffer)
8041             (nnheader-narrow-to-headers)
8042             (unless (setq ref (message-fetch-field "references"))
8043               (when (setq ref (message-fetch-field "in-reply-to"))
8044                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8045             (widen))
8046         (setq ref
8047               ;; It's not the current article, so we take a bet on
8048               ;; the value we got from the server.
8049               (mail-header-references header)))
8050       (if (and ref
8051                (not (equal ref "")))
8052           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8053             (gnus-message 1 "Couldn't find parent"))
8054         (gnus-message 1 "No references in article %d"
8055                       (gnus-summary-article-number))
8056         (setq error t))
8057       (decf n))
8058     (gnus-summary-position-point)
8059     n))
8060
8061 (defun gnus-summary-refer-references ()
8062   "Fetch all articles mentioned in the References header.
8063 Return the number of articles fetched."
8064   (interactive)
8065   (let ((ref (mail-header-references (gnus-summary-article-header)))
8066         (current (gnus-summary-article-number))
8067         (n 0))
8068     (if (or (not ref)
8069             (equal ref ""))
8070         (error "No References in the current article")
8071       ;; For each Message-ID in the References header...
8072       (while (string-match "<[^>]*>" ref)
8073         (incf n)
8074         ;; ... fetch that article.
8075         (gnus-summary-refer-article
8076          (prog1 (match-string 0 ref)
8077            (setq ref (substring ref (match-end 0))))))
8078       (gnus-summary-goto-subject current)
8079       (gnus-summary-position-point)
8080       n)))
8081
8082 (defun gnus-summary-refer-thread (&optional limit)
8083   "Fetch all articles in the current thread.
8084 If LIMIT (the numerical prefix), fetch that many old headers instead
8085 of what's specified by the `gnus-refer-thread-limit' variable."
8086   (interactive "P")
8087   (let ((id (mail-header-id (gnus-summary-article-header)))
8088         (limit (if limit (prefix-numeric-value limit)
8089                  gnus-refer-thread-limit)))
8090     (unless (eq gnus-fetch-old-headers 'invisible)
8091       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8092       ;; Retrieve the headers and read them in.
8093       (if (eq (if (numberp limit)
8094                   (gnus-retrieve-headers
8095                    (list (min
8096                           (+ (mail-header-number
8097                               (gnus-summary-article-header))
8098                              limit)
8099                           gnus-newsgroup-end))
8100                    gnus-newsgroup-name (* limit 2))
8101                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8102                 ;; headers.
8103                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8104                                        gnus-newsgroup-name limit))
8105               'nov)
8106           (gnus-build-all-threads)
8107         (error "Can't fetch thread from back ends that don't support NOV"))
8108       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8109     (gnus-summary-limit-include-thread id)))
8110
8111 (defun gnus-summary-refer-article (message-id)
8112   "Fetch an article specified by MESSAGE-ID."
8113   (interactive "sMessage-ID: ")
8114   (when (and (stringp message-id)
8115              (not (zerop (length message-id))))
8116     (setq message-id (gnus-replace-in-string message-id " " ""))
8117     ;; Construct the correct Message-ID if necessary.
8118     ;; Suggested by tale@pawl.rpi.edu.
8119     (unless (string-match "^<" message-id)
8120       (setq message-id (concat "<" message-id)))
8121     (unless (string-match ">$" message-id)
8122       (setq message-id (concat message-id ">")))
8123     ;; People often post MIDs from URLs, so unhex it:
8124     (unless (string-match "@" message-id)
8125       (setq message-id (gnus-url-unhex-string message-id)))
8126     (let* ((header (gnus-id-to-header message-id))
8127            (sparse (and header
8128                         (gnus-summary-article-sparse-p
8129                          (mail-header-number header))
8130                         (memq (mail-header-number header)
8131                               gnus-newsgroup-limit)))
8132            number)
8133       (cond
8134        ;; If the article is present in the buffer we just go to it.
8135        ((and header
8136              (or (not (gnus-summary-article-sparse-p
8137                        (mail-header-number header)))
8138                  sparse))
8139         (prog1
8140             (gnus-summary-goto-article
8141              (mail-header-number header) nil t)
8142           (when sparse
8143             (gnus-summary-update-article (mail-header-number header)))))
8144        (t
8145         ;; We fetch the article.
8146         (catch 'found
8147           (dolist (gnus-override-method (gnus-refer-article-methods))
8148             (when (and (gnus-check-server gnus-override-method)
8149                        ;; Fetch the header,
8150                        (setq number (gnus-summary-insert-subject message-id)))
8151               ;; and display the article.
8152               (gnus-summary-select-article nil nil nil number)
8153               (throw 'found t)))
8154           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8155
8156 (defun gnus-refer-article-methods ()
8157   "Return a list of referable methods."
8158   (cond
8159    ;; No method, so we default to current and native.
8160    ((null gnus-refer-article-method)
8161     (list gnus-current-select-method gnus-select-method))
8162    ;; Current.
8163    ((eq 'current gnus-refer-article-method)
8164     (list gnus-current-select-method))
8165    ;; List of select methods.
8166    ((not (and (symbolp (car gnus-refer-article-method))
8167               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8168     (let (out)
8169       (dolist (method gnus-refer-article-method)
8170         (push (if (eq 'current method)
8171                   gnus-current-select-method
8172                 method)
8173               out))
8174       (nreverse out)))
8175    ;; One single select method.
8176    (t
8177     (list gnus-refer-article-method))))
8178
8179 (defun gnus-summary-edit-parameters ()
8180   "Edit the group parameters of the current group."
8181   (interactive)
8182   (gnus-group-edit-group gnus-newsgroup-name 'params))
8183
8184 (defun gnus-summary-customize-parameters ()
8185   "Customize the group parameters of the current group."
8186   (interactive)
8187   (gnus-group-customize gnus-newsgroup-name))
8188
8189 (defun gnus-summary-enter-digest-group (&optional force)
8190   "Enter an nndoc group based on the current article.
8191 If FORCE, force a digest interpretation.  If not, try
8192 to guess what the document format is."
8193   (interactive "P")
8194   (let ((conf gnus-current-window-configuration))
8195     (save-window-excursion
8196       (save-excursion
8197         (let (gnus-article-prepare-hook
8198               gnus-display-mime-function
8199               gnus-break-pages)
8200           (gnus-summary-select-article))))
8201     (setq gnus-current-window-configuration conf)
8202     (let* ((name (format "%s-%d"
8203                          (gnus-group-prefixed-name
8204                           gnus-newsgroup-name (list 'nndoc ""))
8205                          (save-excursion
8206                            (set-buffer gnus-summary-buffer)
8207                            gnus-current-article)))
8208            (ogroup gnus-newsgroup-name)
8209            (params (append (gnus-info-params (gnus-get-info ogroup))
8210                            (list (cons 'to-group ogroup))
8211                            (list (cons 'parent-group ogroup))
8212                            (list (cons 'save-article-group ogroup))))
8213            (case-fold-search t)
8214            (buf (current-buffer))
8215            dig to-address)
8216       (save-excursion
8217         (set-buffer gnus-original-article-buffer)
8218         ;; Have the digest group inherit the main mail address of
8219         ;; the parent article.
8220         (when (setq to-address (or (gnus-fetch-field "reply-to")
8221                                    (gnus-fetch-field "from")))
8222           (setq params (append
8223                         (list (cons 'to-address
8224                                     (funcall gnus-decode-encoded-word-function
8225                                              to-address))))))
8226         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8227         (insert-buffer-substring gnus-original-article-buffer)
8228         ;; Remove lines that may lead nndoc to misinterpret the
8229         ;; document type.
8230         (narrow-to-region
8231          (goto-char (point-min))
8232          (or (search-forward "\n\n" nil t) (point)))
8233         (goto-char (point-min))
8234         (delete-matching-lines "^Path:\\|^From ")
8235         (widen))
8236       (unwind-protect
8237           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8238                     (gnus-newsgroup-ephemeral-ignored-charsets
8239                      gnus-newsgroup-ignored-charsets))
8240                 (gnus-group-read-ephemeral-group
8241                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8242                               (nndoc-article-type
8243                                ,(if force 'mbox 'guess)))
8244                  t nil nil nil
8245                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8246                                                         "ADAPT")))))
8247               ;; Make all postings to this group go to the parent group.
8248               (nconc (gnus-info-params (gnus-get-info name))
8249                      params)
8250             ;; Couldn't select this doc group.
8251             (switch-to-buffer buf)
8252             (gnus-set-global-variables)
8253             (gnus-configure-windows 'summary)
8254             (gnus-message 3 "Article couldn't be entered?"))
8255         (kill-buffer dig)))))
8256
8257 (defun gnus-summary-read-document (n)
8258   "Open a new group based on the current article(s).
8259 This will allow you to read digests and other similar
8260 documents as newsgroups.
8261 Obeys the standard process/prefix convention."
8262   (interactive "P")
8263   (let* ((articles (gnus-summary-work-articles n))
8264          (ogroup gnus-newsgroup-name)
8265          (params (append (gnus-info-params (gnus-get-info ogroup))
8266                          (list (cons 'to-group ogroup))))
8267          article group egroup groups vgroup)
8268     (while (setq article (pop articles))
8269       (setq group (format "%s-%d" gnus-newsgroup-name article))
8270       (gnus-summary-remove-process-mark article)
8271       (when (gnus-summary-display-article article)
8272         (save-excursion
8273           (with-temp-buffer
8274             (insert-buffer-substring gnus-original-article-buffer)
8275             ;; Remove some headers that may lead nndoc to make
8276             ;; the wrong guess.
8277             (message-narrow-to-head)
8278             (goto-char (point-min))
8279             (delete-matching-lines "^\\(Path\\):\\|^From ")
8280             (widen)
8281             (if (setq egroup
8282                       (gnus-group-read-ephemeral-group
8283                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8284                                      (nndoc-article-type guess))
8285                        t nil t))
8286                 (progn
8287                   ;; Make all postings to this group go to the parent group.
8288                   (nconc (gnus-info-params (gnus-get-info egroup))
8289                          params)
8290                   (push egroup groups))
8291               ;; Couldn't select this doc group.
8292               (gnus-error 3 "Article couldn't be entered"))))))
8293     ;; Now we have selected all the documents.
8294     (cond
8295      ((not groups)
8296       (error "None of the articles could be interpreted as documents"))
8297      ((gnus-group-read-ephemeral-group
8298        (setq vgroup (format
8299                      "nnvirtual:%s-%s" gnus-newsgroup-name
8300                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8301        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8302        t
8303        (cons (current-buffer) 'summary)))
8304      (t
8305       (error "Couldn't select virtual nndoc group")))))
8306
8307 (defun gnus-summary-isearch-article (&optional regexp-p)
8308   "Do incremental search forward on the current article.
8309 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8310   (interactive "P")
8311   (let* ((gnus-inhibit-treatment t)
8312          (old (gnus-summary-select-article)))
8313     (gnus-configure-windows 'article)
8314     (gnus-eval-in-buffer-window gnus-article-buffer
8315       (save-restriction
8316         (widen)
8317         (when (eq 'old old)
8318           (gnus-article-show-all-headers))
8319         (goto-char (point-min))
8320         (isearch-forward regexp-p)))))
8321
8322 (defun gnus-summary-search-article-forward (regexp &optional backward)
8323   "Search for an article containing REGEXP forward.
8324 If BACKWARD, search backward instead."
8325   (interactive
8326    (list (read-string
8327           (format "Search article %s (regexp%s): "
8328                   (if current-prefix-arg "backward" "forward")
8329                   (if gnus-last-search-regexp
8330                       (concat ", default " gnus-last-search-regexp)
8331                     "")))
8332          current-prefix-arg))
8333   (if (string-equal regexp "")
8334       (setq regexp (or gnus-last-search-regexp ""))
8335     (setq gnus-last-search-regexp regexp)
8336     (setq gnus-article-before-search gnus-current-article))
8337   ;; Intentionally set gnus-last-article.
8338   (setq gnus-last-article gnus-article-before-search)
8339   (let ((gnus-last-article gnus-last-article))
8340     (if (gnus-summary-search-article regexp backward)
8341         (gnus-summary-show-thread)
8342       (error "Search failed: \"%s\"" regexp))))
8343
8344 (defun gnus-summary-search-article-backward (regexp)
8345   "Search for an article containing REGEXP backward."
8346   (interactive
8347    (list (read-string
8348           (format "Search article backward (regexp%s): "
8349                   (if gnus-last-search-regexp
8350                       (concat ", default " gnus-last-search-regexp)
8351                     "")))))
8352   (gnus-summary-search-article-forward regexp 'backward))
8353
8354 (eval-when-compile
8355   (defmacro gnus-summary-search-article-position-point (regexp backward)
8356     "Dehighlight the last matched text and goto the beginning position."
8357     (` (if (and gnus-summary-search-article-matched-data
8358                 (let ((text (caddr gnus-summary-search-article-matched-data))
8359                       (inhibit-read-only t)
8360                       buffer-read-only)
8361                   (delete-region
8362                    (goto-char (car gnus-summary-search-article-matched-data))
8363                    (cadr gnus-summary-search-article-matched-data))
8364                   (insert text)
8365                   (string-match (, regexp) text)))
8366            (if (, backward) (beginning-of-line) (end-of-line))
8367          (goto-char (if (, backward) (point-max) (point-min))))))
8368
8369   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8370     "Place point where X-Face image is displayed."
8371     (if (featurep 'xemacs)
8372         (` (let ((end (if (search-forward "\n\n" nil t)
8373                           (goto-char (1- (point)))
8374                         (point-min)))
8375                  extent)
8376              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8377              (unless (and (re-search-forward "^From:" end t)
8378                           (setq extent (extent-at (point)))
8379                           (extent-begin-glyph extent))
8380                (goto-char (, opoint)))))
8381       (` (let ((end (if (search-forward "\n\n" nil t)
8382                         (goto-char (1- (point)))
8383                       (point-min)))
8384                (start (or (search-backward "\n\n" nil t) (point-min))))
8385            (goto-char
8386             (or (text-property-any start end 'x-face-image t);; x-face-e21
8387                 (text-property-any start end 'x-face-mule-bitmap-image t)
8388                 (, opoint)))))))
8389
8390   (defmacro gnus-summary-search-article-highlight-matched-text
8391     (backward treated x-face)
8392     "Highlight matched text in the function `gnus-summary-search-article'."
8393     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8394              (end (set-marker (make-marker) (match-end 0)))
8395              (inhibit-read-only t)
8396              buffer-read-only)
8397          (unless treated
8398            (let ((,@
8399                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8400                     (mapcar
8401                      (lambda (item) (setq items (delq item items)))
8402                      '(gnus-treat-buttonize
8403                        gnus-treat-fill-article
8404                        gnus-treat-fill-long-lines
8405                        gnus-treat-emphasize
8406                        gnus-treat-highlight-headers
8407                        gnus-treat-highlight-citation
8408                        gnus-treat-highlight-signature
8409                        gnus-treat-overstrike
8410                        gnus-treat-display-x-face
8411                        gnus-treat-buttonize-head
8412                        gnus-treat-decode-article-as-default-mime-charset))
8413                     (static-if (featurep 'xemacs)
8414                         items
8415                       (cons '(x-face-mule-delete-x-face-field
8416                               (quote never))
8417                             items))))
8418                  (gnus-treat-display-x-face
8419                   (when (, x-face) gnus-treat-display-x-face)))
8420              (gnus-article-prepare-mime-display)))
8421          (goto-char (if (, backward) start end))
8422          (when (, x-face)
8423            (gnus-summary-search-article-highlight-goto-x-face (point)))
8424          (setq gnus-summary-search-article-matched-data
8425                (list start end (buffer-substring start end)))
8426          (unless (eq start end);; matched text has been deleted. :-<
8427            (put-text-property start end 'face
8428                               (or (find-face 'isearch)
8429                                   'secondary-selection))))))
8430   )
8431
8432 (defun gnus-summary-search-article (regexp &optional backward)
8433   "Search for an article containing REGEXP.
8434 Optional argument BACKWARD means do search for backward.
8435 `gnus-select-article-hook' is not called during the search."
8436   ;; We have to require this here to make sure that the following
8437   ;; dynamic binding isn't shadowed by autoloading.
8438   (require 'gnus-async)
8439   (require 'gnus-art)
8440   (let ((gnus-select-article-hook nil)  ;Disable hook.
8441         (gnus-article-prepare-hook nil)
8442         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8443         (gnus-use-article-prefetch nil)
8444         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8445         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8446         (gnus-visual nil)
8447         (gnus-keep-backlog nil)
8448         (gnus-break-pages nil)
8449         (gnus-summary-display-arrow nil)
8450         (gnus-updated-mode-lines nil)
8451         (gnus-auto-center-summary nil)
8452         (sum (current-buffer))
8453         (found nil)
8454         point treated)
8455     (gnus-save-hidden-threads
8456       (static-if (featurep 'xemacs)
8457           (let ((gnus-inhibit-treatment t))
8458             (setq treated (eq 'old (gnus-summary-select-article)))
8459             (when (and treated
8460                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8461                                  (window-live-p (get-buffer-window
8462                                                  gnus-article-buffer t)))))
8463               (gnus-summary-select-article nil t)
8464               (setq treated nil)))
8465         (let ((gnus-inhibit-treatment t)
8466               (x-face-mule-delete-x-face-field 'never))
8467           (setq treated (eq 'old (gnus-summary-select-article)))
8468           (when (and treated
8469                      (not
8470                       (and (gnus-buffer-live-p gnus-article-buffer)
8471                            (window-live-p (get-buffer-window
8472                                            gnus-article-buffer t))
8473                            (or (not (string-match "^\\^X-Face:" regexp))
8474                                (with-current-buffer gnus-article-buffer
8475                                  gnus-summary-search-article-matched-data)))))
8476             (gnus-summary-select-article nil t)
8477             (setq treated nil))))
8478       (set-buffer gnus-article-buffer)
8479       (widen)
8480       (if treated
8481           (progn
8482             (gnus-article-show-all-headers)
8483             (gnus-summary-search-article-position-point regexp backward))
8484         (goto-char (if backward (point-max) (point-min))))
8485       (while (not found)
8486         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8487         (if (if backward
8488                 (re-search-backward regexp nil t)
8489               (re-search-forward regexp nil t))
8490             ;; We found the regexp.
8491             (progn
8492               (gnus-summary-search-article-highlight-matched-text
8493                backward treated (string-match "^\\^X-Face:" regexp))
8494               (setq found 'found)
8495               (forward-line
8496                (/ (- 2 (window-height
8497                         (get-buffer-window gnus-article-buffer t)))
8498                   2))
8499               (set-window-start
8500                (get-buffer-window (current-buffer))
8501                (point))
8502               (set-buffer sum)
8503               (setq point (point)))
8504           ;; We didn't find it, so we go to the next article.
8505           (set-buffer sum)
8506           (setq found 'not)
8507           (while (eq found 'not)
8508             (if (not (if backward (gnus-summary-find-prev)
8509                        (gnus-summary-find-next)))
8510                 ;; No more articles.
8511                 (setq found t)
8512               ;; Select the next article and adjust point.
8513               (unless (gnus-summary-article-sparse-p
8514                        (gnus-summary-article-number))
8515                 (setq found nil)
8516                 (let ((gnus-inhibit-treatment t))
8517                   (gnus-summary-select-article))
8518                 (setq treated nil)
8519                 (set-buffer gnus-article-buffer)
8520                 (widen)
8521                 (goto-char (if backward (point-max) (point-min))))))))
8522       (gnus-message 7 ""))
8523     ;; Return whether we found the regexp.
8524     (when (eq found 'found)
8525       (goto-char point)
8526       (gnus-summary-show-thread)
8527       (gnus-summary-goto-subject gnus-current-article)
8528       (gnus-summary-position-point)
8529       t)))
8530
8531 (defun gnus-find-matching-articles (header regexp)
8532   "Return a list of all articles that match REGEXP on HEADER.
8533 This search includes all articles in the current group that Gnus has
8534 fetched headers for, whether they are displayed or not."
8535   (let ((articles nil)
8536         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8537         (case-fold-search t))
8538     (dolist (header gnus-newsgroup-headers)
8539       (when (string-match regexp (funcall func header))
8540         (push (mail-header-number header) articles)))
8541     (nreverse articles)))
8542
8543 (defun gnus-summary-find-matching (header regexp &optional backward unread
8544                                           not-case-fold not-matching)
8545   "Return a list of all articles that match REGEXP on HEADER.
8546 The search stars on the current article and goes forwards unless
8547 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8548 If UNREAD is non-nil, only unread articles will
8549 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8550 in the comparisons. If NOT-MATCHING, return a list of all articles that
8551 not match REGEXP on HEADER."
8552   (let ((case-fold-search (not not-case-fold))
8553         articles d func)
8554     (if (consp header)
8555         (if (eq (car header) 'extra)
8556             (setq func
8557                   `(lambda (h)
8558                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8559                          "")))
8560           (error "%s is an invalid header" header))
8561       (unless (fboundp (intern (concat "mail-header-" header)))
8562         (error "%s is not a valid header" header))
8563       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8564     (dolist (d (if (eq backward 'all)
8565                    gnus-newsgroup-data
8566                  (gnus-data-find-list
8567                   (gnus-summary-article-number)
8568                   (gnus-data-list backward))))
8569       (when (and (or (not unread)       ; We want all articles...
8570                      (gnus-data-unread-p d)) ; Or just unreads.
8571                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8572                  (if not-matching
8573                      (not (string-match
8574                            regexp
8575                            (funcall func (gnus-data-header d))))
8576                    (string-match regexp
8577                                  (funcall func (gnus-data-header d)))))
8578         (push (gnus-data-number d) articles))) ; Success!
8579     (nreverse articles)))
8580
8581 (defun gnus-summary-execute-command (header regexp command &optional backward)
8582   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8583 If HEADER is an empty string (or nil), the match is done on the entire
8584 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8585   (interactive
8586    (list (let ((completion-ignore-case t))
8587            (completing-read
8588             "Header name: "
8589             (mapcar (lambda (header) (list (format "%s" header)))
8590                     (append
8591                      '("Number" "Subject" "From" "Lines" "Date"
8592                        "Message-ID" "Xref" "References" "Body")
8593                      gnus-extra-headers))
8594             nil 'require-match))
8595          (read-string "Regexp: ")
8596          (read-key-sequence "Command: ")
8597          current-prefix-arg))
8598   (when (equal header "Body")
8599     (setq header ""))
8600   ;; Hidden thread subtrees must be searched as well.
8601   (gnus-summary-show-all-threads)
8602   ;; We don't want to change current point nor window configuration.
8603   (save-excursion
8604     (save-window-excursion
8605       (let (gnus-visual
8606             gnus-treat-strip-trailing-blank-lines
8607             gnus-treat-strip-leading-blank-lines
8608             gnus-treat-strip-multiple-blank-lines
8609             gnus-treat-hide-boring-headers
8610             gnus-treat-fold-newsgroups
8611             gnus-article-prepare-hook)
8612         (gnus-message 6 "Executing %s..." (key-description command))
8613         ;; We'd like to execute COMMAND interactively so as to give arguments.
8614         (gnus-execute header regexp
8615                       `(call-interactively ',(key-binding command))
8616                       backward)
8617         (gnus-message 6 "Executing %s...done" (key-description command))))))
8618
8619 (defun gnus-summary-beginning-of-article ()
8620   "Scroll the article back to the beginning."
8621   (interactive)
8622   (gnus-summary-select-article)
8623   (gnus-configure-windows 'article)
8624   (gnus-eval-in-buffer-window gnus-article-buffer
8625     (widen)
8626     (goto-char (point-min))
8627     (when gnus-break-pages
8628       (gnus-narrow-to-page))))
8629
8630 (defun gnus-summary-end-of-article ()
8631   "Scroll to the end of the article."
8632   (interactive)
8633   (gnus-summary-select-article)
8634   (gnus-configure-windows 'article)
8635   (gnus-eval-in-buffer-window gnus-article-buffer
8636     (widen)
8637     (goto-char (point-max))
8638     (recenter -3)
8639     (when gnus-break-pages
8640       (when (re-search-backward page-delimiter nil t)
8641         (narrow-to-region (match-end 0) (point-max)))
8642       (gnus-narrow-to-page))))
8643
8644 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8645   "Truncate to LEN and quote all \"(\"'s in STRING."
8646   (gnus-replace-in-string (if (and len (> (length string) len))
8647                               (substring string 0 len)
8648                             string)
8649                           "[()]" "\\\\\\&"))
8650
8651 (defun gnus-summary-print-article (&optional filename n)
8652   "Generate and print a PostScript image of the process-marked (mail) articles.
8653
8654 If used interactively, print the current article if none are
8655 process-marked.  With prefix arg, prompt the user for the name of the
8656 file to save in.
8657
8658 When used from Lisp, accept two optional args FILENAME and N.  N means
8659 to print the next N articles.  If N is negative, print the N previous
8660 articles.  If N is nil and articles have been marked with the process
8661 mark, print these instead.
8662
8663 If the optional first argument FILENAME is nil, send the image to the
8664 printer.  If FILENAME is a string, save the PostScript image in a file with
8665 that name.  If FILENAME is a number, prompt the user for the name of the file
8666 to save in."
8667   (interactive (list (ps-print-preprint current-prefix-arg)))
8668   (dolist (article (gnus-summary-work-articles n))
8669     (gnus-summary-select-article nil nil 'pseudo article)
8670     (gnus-eval-in-buffer-window gnus-article-buffer
8671       (gnus-print-buffer))
8672     (gnus-summary-remove-process-mark article))
8673   (ps-despool filename))
8674
8675 (defun gnus-print-buffer ()
8676   (let ((buffer (generate-new-buffer " *print*")))
8677     (unwind-protect
8678         (progn
8679           (copy-to-buffer buffer (point-min) (point-max))
8680           (set-buffer buffer)
8681           (gnus-remove-text-with-property 'gnus-decoration)
8682           (when (gnus-visual-p 'article-highlight 'highlight)
8683             ;; Copy-to-buffer doesn't copy overlay.  So redo
8684             ;; highlight.
8685             (let ((gnus-article-buffer buffer))
8686               (gnus-article-highlight-citation t)
8687               (gnus-article-highlight-signature)
8688               (gnus-article-emphasize)
8689               (gnus-article-delete-invisible-text)))
8690           (let ((ps-left-header
8691                  (list
8692                   (concat "("
8693                           (gnus-summary-print-truncate-and-quote
8694                            (mail-header-subject gnus-current-headers)
8695                            66) ")")
8696                   (concat "("
8697                           (gnus-summary-print-truncate-and-quote
8698                            (mail-header-from gnus-current-headers)
8699                            45) ")")))
8700                 (ps-right-header
8701                  (list
8702                   "/pagenumberstring load"
8703                   (concat "("
8704                           (mail-header-date gnus-current-headers) ")"))))
8705             (gnus-run-hooks 'gnus-ps-print-hook)
8706             (save-excursion
8707               (if window-system
8708                   (ps-spool-buffer-with-faces)
8709                 (ps-spool-buffer)))))
8710       (kill-buffer buffer))))
8711
8712 (defun gnus-summary-show-article (&optional arg)
8713   "Force redisplaying of the current article.
8714 If ARG (the prefix) is a number, show the article with the charset
8715 defined in `gnus-summary-show-article-charset-alist', or the charset
8716 input.
8717 If ARG (the prefix) is non-nil and not a number, show the raw article
8718 without any article massaging functions being run.  Normally, the key
8719 strokes are `C-u g'."
8720   (interactive "P")
8721   (cond
8722    ((numberp arg)
8723     (gnus-summary-show-article t)
8724     (let* ((gnus-newsgroup-charset
8725             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8726                 (mm-read-coding-system
8727                  "View as charset: " ;; actually it is coding system.
8728                  (save-excursion
8729                    (set-buffer gnus-article-buffer)
8730                    (mm-detect-coding-region (point) (point-max))))))
8731            (default-mime-charset gnus-newsgroup-charset)
8732            (gnus-newsgroup-ignored-charsets 'gnus-all))
8733       (gnus-summary-select-article nil 'force)
8734       (let ((deps gnus-newsgroup-dependencies)
8735             head header lines)
8736         (save-excursion
8737           (set-buffer gnus-original-article-buffer)
8738           (save-restriction
8739             (message-narrow-to-head)
8740             (setq head (buffer-string))
8741             (goto-char (point-min))
8742             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8743               (goto-char (point-max))
8744               (widen)
8745               (setq lines (1- (count-lines (point) (point-max))))))
8746           (with-temp-buffer
8747             (insert (format "211 %d Article retrieved.\n"
8748                             (cdr gnus-article-current)))
8749             (insert head)
8750             (if lines (insert (format "Lines: %d\n" lines)))
8751             (insert ".\n")
8752             (let ((nntp-server-buffer (current-buffer)))
8753               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8754         (gnus-data-set-header
8755          (gnus-data-find (cdr gnus-article-current))
8756          header)
8757         (gnus-summary-update-article-line
8758          (cdr gnus-article-current) header)
8759         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8760           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8761    ((not arg)
8762     ;; Select the article the normal way.
8763     (gnus-summary-select-article nil 'force))
8764    (t
8765     ;; We have to require this here to make sure that the following
8766     ;; dynamic binding isn't shadowed by autoloading.
8767     (require 'gnus-async)
8768     (require 'gnus-art)
8769     ;; Bind the article treatment functions to nil.
8770     (let ((gnus-have-all-headers t)
8771           gnus-article-prepare-hook
8772           gnus-article-decode-hook
8773           gnus-break-pages
8774           gnus-show-mime
8775           (gnus-inhibit-treatment t))
8776       (gnus-summary-select-article nil 'force))))
8777   (gnus-summary-goto-subject gnus-current-article)
8778   (gnus-summary-position-point))
8779
8780 (defun gnus-summary-show-raw-article ()
8781   "Show the raw article without any article massaging functions being run."
8782   (interactive)
8783   (gnus-summary-show-article t))
8784
8785 (defun gnus-summary-verbose-headers (&optional arg)
8786   "Toggle permanent full header display.
8787 If ARG is a positive number, turn header display on.
8788 If ARG is a negative number, turn header display off."
8789   (interactive "P")
8790   (setq gnus-show-all-headers
8791         (cond ((or (not (numberp arg))
8792                    (zerop arg))
8793                (not gnus-show-all-headers))
8794               ((natnump arg)
8795                t)))
8796   (gnus-summary-show-article))
8797
8798 (defun gnus-summary-toggle-header (&optional arg)
8799   "Show the headers if they are hidden, or hide them if they are shown.
8800 If ARG is a positive number, show the entire header.
8801 If ARG is a negative number, hide the unwanted header lines."
8802   (interactive "P")
8803   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8804                      (get-buffer-window gnus-article-buffer t))))
8805     (with-current-buffer gnus-article-buffer
8806       (widen)
8807       (article-narrow-to-head)
8808       (let* ((buffer-read-only nil)
8809              (inhibit-point-motion-hooks t)
8810              (hidden (if (numberp arg)
8811                          (>= arg 0)
8812                        (gnus-article-hidden-text-p 'headers)))
8813              s e)
8814         (delete-region (point-min) (point-max))
8815         (with-current-buffer gnus-original-article-buffer
8816           (goto-char (setq s (point-min)))
8817           (setq e (if (search-forward "\n\n" nil t)
8818                       (1- (point))
8819                     (point-max))))
8820         (insert-buffer-substring gnus-original-article-buffer s e)
8821         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8822         ;; article-decode-encoded-words by default.
8823         (article-decode-encoded-words)
8824         (run-hooks 'gnus-article-decode-hook)
8825         (if hidden
8826             (let ((gnus-treat-hide-headers nil)
8827                   (gnus-treat-hide-boring-headers nil))
8828               (gnus-delete-wash-type 'headers)
8829               (gnus-treat-article 'head))
8830           (gnus-treat-article 'head))
8831         (widen)
8832         (if window
8833             (set-window-start window (goto-char (point-min))))
8834         (if gnus-break-pages
8835             (gnus-narrow-to-page)
8836           (when (gnus-visual-p 'page-marker)
8837             (let ((buffer-read-only nil))
8838               (gnus-remove-text-with-property 'gnus-prev)
8839               (gnus-remove-text-with-property 'gnus-next))))
8840         (gnus-set-mode-line 'article)))))
8841
8842 (defun gnus-summary-show-all-headers ()
8843   "Make all header lines visible."
8844   (interactive)
8845   (gnus-summary-toggle-header 1))
8846
8847 (defun gnus-summary-toggle-mime (&optional arg)
8848   "Toggle MIME processing.
8849 If ARG is a positive number, turn MIME processing on."
8850   (interactive "P")
8851   (setq gnus-show-mime
8852         (if (null arg)
8853             (not gnus-show-mime)
8854           (> (prefix-numeric-value arg) 0)))
8855   (gnus-summary-select-article t 'force))
8856
8857 (defun gnus-summary-caesar-message (&optional arg)
8858   "Caesar rotate the current article by 13.
8859 The numerical prefix specifies how many places to rotate each letter
8860 forward."
8861   (interactive "P")
8862   (gnus-summary-select-article)
8863   (let ((mail-header-separator ""))
8864     (gnus-eval-in-buffer-window gnus-article-buffer
8865       (save-restriction
8866         (widen)
8867         (let ((start (window-start))
8868               buffer-read-only)
8869           (message-caesar-buffer-body arg)
8870           (set-window-start (get-buffer-window (current-buffer)) start))))))
8871
8872 (autoload 'unmorse-region "morse"
8873   "Convert morse coded text in region to ordinary ASCII text."
8874   t)
8875
8876 (defun gnus-summary-morse-message (&optional arg)
8877   "Morse decode the current article."
8878   (interactive "P")
8879   (gnus-summary-select-article)
8880   (let ((mail-header-separator ""))
8881     (gnus-eval-in-buffer-window gnus-article-buffer
8882       (save-excursion
8883         (save-restriction
8884           (widen)
8885           (let ((pos (window-start))
8886                 buffer-read-only)
8887             (goto-char (point-min))
8888             (when (message-goto-body)
8889               (gnus-narrow-to-body))
8890             (goto-char (point-min))
8891             (while (re-search-forward "·" (point-max) t)
8892               (replace-match "."))
8893             (unmorse-region (point-min) (point-max))
8894             (widen)
8895             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8896
8897 (defun gnus-summary-stop-page-breaking ()
8898   "Stop page breaking in the current article."
8899   (interactive)
8900   (gnus-summary-select-article)
8901   (gnus-eval-in-buffer-window gnus-article-buffer
8902     (widen)
8903     (when (gnus-visual-p 'page-marker)
8904       (let ((buffer-read-only nil))
8905         (gnus-remove-text-with-property 'gnus-prev)
8906         (gnus-remove-text-with-property 'gnus-next))
8907       (setq gnus-page-broken nil))))
8908
8909 (defun gnus-summary-move-article (&optional n to-newsgroup
8910                                             select-method action)
8911   "Move the current article to a different newsgroup.
8912 If N is a positive number, move the N next articles.
8913 If N is a negative number, move the N previous articles.
8914 If N is nil and any articles have been marked with the process mark,
8915 move those articles instead.
8916 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8917 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8918 re-spool using this method.
8919
8920 When called interactively with TO-NEWSGROUP being nil, the value of
8921 the variable `gnus-move-split-methods' is used for finding a default
8922 for the target newsgroup.
8923
8924 For this function to work, both the current newsgroup and the
8925 newsgroup that you want to move to have to support the `request-move'
8926 and `request-accept' functions.
8927
8928 ACTION can be either `move' (the default), `crosspost' or `copy'."
8929   (interactive "P")
8930   (unless action
8931     (setq action 'move))
8932   ;; Check whether the source group supports the required functions.
8933   (cond ((and (eq action 'move)
8934               (not (gnus-check-backend-function
8935                     'request-move-article gnus-newsgroup-name)))
8936          (error "The current group does not support article moving"))
8937         ((and (eq action 'crosspost)
8938               (not (gnus-check-backend-function
8939                     'request-replace-article gnus-newsgroup-name)))
8940          (error "The current group does not support article editing")))
8941   (let ((articles (gnus-summary-work-articles n))
8942         (prefix (if (gnus-check-backend-function
8943                      'request-move-article gnus-newsgroup-name)
8944                     (gnus-group-real-prefix gnus-newsgroup-name)
8945                   ""))
8946         (names '((move "Move" "Moving")
8947                  (copy "Copy" "Copying")
8948                  (crosspost "Crosspost" "Crossposting")))
8949         (copy-buf (save-excursion
8950                     (nnheader-set-temp-buffer " *copy article*")))
8951         art-group to-method new-xref article to-groups)
8952     (unless (assq action names)
8953       (error "Unknown action %s" action))
8954     ;; Read the newsgroup name.
8955     (when (and (not to-newsgroup)
8956                (not select-method))
8957       (if (and gnus-move-split-methods
8958                (not
8959                 (and (memq gnus-current-article articles)
8960                      (gnus-buffer-live-p gnus-original-article-buffer))))
8961           ;; When `gnus-move-split-methods' is non-nil, we have to
8962           ;; select an article to give `gnus-read-move-group-name' an
8963           ;; opportunity to suggest an appropriate default.  However,
8964           ;; we needn't render or mark the article.
8965           (let ((gnus-display-mime-function nil)
8966                 (gnus-article-prepare-hook nil)
8967                 (gnus-mark-article-hook nil))
8968             (gnus-summary-select-article nil nil nil (car articles))))
8969       (setq to-newsgroup
8970             (gnus-read-move-group-name
8971              (cadr (assq action names))
8972              (symbol-value (intern (format "gnus-current-%s-group" action)))
8973              articles prefix))
8974       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8975     (setq to-method (or select-method
8976                         (gnus-server-to-method
8977                          (gnus-group-method to-newsgroup))))
8978     ;; Check the method we are to move this article to...
8979     (unless (gnus-check-backend-function
8980              'request-accept-article (car to-method))
8981       (error "%s does not support article copying" (car to-method)))
8982     (unless (gnus-check-server to-method)
8983       (error "Can't open server %s" (car to-method)))
8984     (gnus-message 6 "%s to %s: %s..."
8985                   (caddr (assq action names))
8986                   (or (car select-method) to-newsgroup) articles)
8987     (while articles
8988       (setq article (pop articles))
8989       (setq
8990        art-group
8991        (cond
8992         ;; Move the article.
8993         ((eq action 'move)
8994          ;; Remove this article from future suppression.
8995          (gnus-dup-unsuppress-article article)
8996          (gnus-request-move-article
8997           article                       ; Article to move
8998           gnus-newsgroup-name           ; From newsgroup
8999           (nth 1 (gnus-find-method-for-group
9000                   gnus-newsgroup-name)) ; Server
9001           (list 'gnus-request-accept-article
9002                 to-newsgroup (list 'quote select-method)
9003                 (not articles) t)       ; Accept form
9004           (not articles)))              ; Only save nov last time
9005         ;; Copy the article.
9006         ((eq action 'copy)
9007          (save-excursion
9008            (set-buffer copy-buf)
9009            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9010              (gnus-request-accept-article
9011               to-newsgroup select-method (not articles) t))))
9012         ;; Crosspost the article.
9013         ((eq action 'crosspost)
9014          (let ((xref (message-tokenize-header
9015                       (mail-header-xref (gnus-summary-article-header article))
9016                       " ")))
9017            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9018                                   ":" (number-to-string article)))
9019            (unless xref
9020              (setq xref (list (system-name))))
9021            (setq new-xref
9022                  (concat
9023                   (mapconcat 'identity
9024                              (delete "Xref:" (delete new-xref xref))
9025                              " ")
9026                   " " new-xref))
9027            (save-excursion
9028              (set-buffer copy-buf)
9029              ;; First put the article in the destination group.
9030              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9031              (when (consp (setq art-group
9032                                 (gnus-request-accept-article
9033                                  to-newsgroup select-method (not articles))))
9034                (setq new-xref (concat new-xref " " (car art-group)
9035                                       ":"
9036                                       (number-to-string (cdr art-group))))
9037                ;; Now we have the new Xrefs header, so we insert
9038                ;; it and replace the new article.
9039                (nnheader-replace-header "Xref" new-xref)
9040                (gnus-request-replace-article
9041                 (cdr art-group) to-newsgroup (current-buffer))
9042                art-group))))))
9043       (cond
9044        ((not art-group)
9045         (gnus-message 1 "Couldn't %s article %s: %s"
9046                       (cadr (assq action names)) article
9047                       (nnheader-get-report (car to-method))))
9048        ((eq art-group 'junk)
9049         (when (eq action 'move)
9050           (gnus-summary-mark-article article gnus-canceled-mark)
9051           (gnus-message 4 "Deleted article %s" article)
9052           ;; run the delete hook
9053           (run-hook-with-args 'gnus-summary-article-delete-hook
9054                               action
9055                               (gnus-data-header
9056                                (assoc article (gnus-data-list nil)))
9057                               gnus-newsgroup-name nil
9058                               select-method)))
9059        (t
9060         (let* ((pto-group (gnus-group-prefixed-name
9061                            (car art-group) to-method))
9062                (entry
9063                 (gnus-gethash pto-group gnus-newsrc-hashtb))
9064                (info (nth 2 entry))
9065                (to-group (gnus-info-group info))
9066                to-marks)
9067           ;; Update the group that has been moved to.
9068           (when (and info
9069                      (memq action '(move copy)))
9070             (unless (member to-group to-groups)
9071               (push to-group to-groups))
9072
9073             (unless (memq article gnus-newsgroup-unreads)
9074               (push 'read to-marks)
9075               (gnus-info-set-read
9076                info (gnus-add-to-range (gnus-info-read info)
9077                                        (list (cdr art-group)))))
9078
9079             ;; See whether the article is to be put in the cache.
9080             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9081                              gnus-article-mark-lists
9082                            (delete '(expirable . expire)
9083                                    (copy-sequence gnus-article-mark-lists))))
9084                   (to-article (cdr art-group)))
9085
9086               ;; Enter the article into the cache in the new group,
9087               ;; if that is required.
9088               (when gnus-use-cache
9089                 (gnus-cache-possibly-enter-article
9090                  to-group to-article
9091                  (let ((header (copy-sequence
9092                                 (gnus-summary-article-header article))))
9093                    (mail-header-set-number header to-article)
9094                    header)
9095                  (memq article gnus-newsgroup-marked)
9096                  (memq article gnus-newsgroup-dormant)
9097                  (memq article gnus-newsgroup-unreads)))
9098
9099               (when gnus-preserve-marks
9100                 ;; Copy any marks over to the new group.
9101                 (when (and (equal to-group gnus-newsgroup-name)
9102                            (not (memq article gnus-newsgroup-unreads)))
9103                   ;; Mark this article as read in this group.
9104                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9105                   (setcdr (gnus-active to-group) to-article)
9106                   (setcdr gnus-newsgroup-active to-article))
9107
9108                 (while marks
9109                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9110                     (when (memq article (symbol-value
9111                                          (intern (format "gnus-newsgroup-%s"
9112                                                          (caar marks)))))
9113                       (push (cdar marks) to-marks)
9114                       ;; If the other group is the same as this group,
9115                       ;; then we have to add the mark to the list.
9116                       (when (equal to-group gnus-newsgroup-name)
9117                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9118                              (cons to-article
9119                                    (symbol-value
9120                                     (intern (format "gnus-newsgroup-%s"
9121                                                     (caar marks)))))))
9122                       ;; Copy the marks to other group.
9123                       (gnus-add-marked-articles
9124                        to-group (cdar marks) (list to-article) info)))
9125                   (setq marks (cdr marks)))
9126
9127                 (gnus-request-set-mark
9128                  to-group (list (list (list to-article) 'add to-marks))))
9129
9130               (gnus-dribble-enter
9131                (concat "(gnus-group-set-info '"
9132                        (gnus-prin1-to-string (gnus-get-info to-group))
9133                        ")"))))
9134
9135           ;; Update the Xref header in this article to point to
9136           ;; the new crossposted article we have just created.
9137           (when (eq action 'crosspost)
9138             (save-excursion
9139               (set-buffer copy-buf)
9140               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9141               (nnheader-replace-header "Xref" new-xref)
9142               (gnus-request-replace-article
9143                article gnus-newsgroup-name (current-buffer))))
9144
9145           ;; run the move/copy/crosspost/respool hook
9146           (run-hook-with-args 'gnus-summary-article-move-hook
9147                               action
9148                               (gnus-data-header
9149                                (assoc article (gnus-data-list nil)))
9150                               gnus-newsgroup-name
9151                               to-newsgroup
9152                               select-method))
9153
9154         ;;;!!!Why is this necessary?
9155         (set-buffer gnus-summary-buffer)
9156         
9157         (gnus-summary-goto-subject article)
9158         (when (eq action 'move)
9159           (gnus-summary-mark-article article gnus-canceled-mark))))
9160       (gnus-summary-remove-process-mark article))
9161     ;; Re-activate all groups that have been moved to.
9162     (save-excursion
9163       (set-buffer gnus-group-buffer)
9164       (let ((gnus-group-marked to-groups))
9165         (gnus-group-get-new-news-this-group nil t)))
9166
9167     (gnus-kill-buffer copy-buf)
9168     (gnus-summary-position-point)
9169     (gnus-set-mode-line 'summary)))
9170
9171 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9172   "Move the current article to a different newsgroup.
9173 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9174 When called interactively, if TO-NEWSGROUP is nil, use the value of
9175 the variable `gnus-move-split-methods' for finding a default target
9176 newsgroup.
9177 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9178 re-spool using this method."
9179   (interactive "P")
9180   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9181
9182 (defun gnus-summary-crosspost-article (&optional n)
9183   "Crosspost the current article to some other group."
9184   (interactive "P")
9185   (gnus-summary-move-article n nil nil 'crosspost))
9186
9187 (defcustom gnus-summary-respool-default-method nil
9188   "Default method type for respooling an article.
9189 If nil, use to the current newsgroup method."
9190   :type 'symbol
9191   :group 'gnus-summary-mail)
9192
9193 (defcustom gnus-summary-display-while-building nil
9194   "If non-nil, show and update the summary buffer as it's being built.
9195 If the value is t, update the buffer after every line is inserted.  If
9196 the value is an integer (N), update the display every N lines."
9197   :group 'gnus-thread
9198   :type '(choice (const :tag "off" nil)
9199                  number
9200                  (const :tag "frequently" t)))
9201
9202 (defun gnus-summary-respool-article (&optional n method)
9203   "Respool the current article.
9204 The article will be squeezed through the mail spooling process again,
9205 which means that it will be put in some mail newsgroup or other
9206 depending on `nnmail-split-methods'.
9207 If N is a positive number, respool the N next articles.
9208 If N is a negative number, respool the N previous articles.
9209 If N is nil and any articles have been marked with the process mark,
9210 respool those articles instead.
9211
9212 Respooling can be done both from mail groups and \"real\" newsgroups.
9213 In the former case, the articles in question will be moved from the
9214 current group into whatever groups they are destined to.  In the
9215 latter case, they will be copied into the relevant groups."
9216   (interactive
9217    (list current-prefix-arg
9218          (let* ((methods (gnus-methods-using 'respool))
9219                 (methname
9220                  (symbol-name (or gnus-summary-respool-default-method
9221                                   (car (gnus-find-method-for-group
9222                                         gnus-newsgroup-name)))))
9223                 (method
9224                  (gnus-completing-read-with-default
9225                   methname "What backend do you want to use when respooling?"
9226                   methods nil t nil 'gnus-mail-method-history))
9227                 ms)
9228            (cond
9229             ((zerop (length (setq ms (gnus-servers-using-backend
9230                                       (intern method)))))
9231              (list (intern method) ""))
9232             ((= 1 (length ms))
9233              (car ms))
9234             (t
9235              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9236                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9237                            ms-alist))))))))
9238   (unless method
9239     (error "No method given for respooling"))
9240   (if (assoc (symbol-name
9241               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9242              (gnus-methods-using 'respool))
9243       (gnus-summary-move-article n nil method)
9244     (gnus-summary-copy-article n nil method)))
9245
9246 (defun gnus-summary-import-article (file &optional edit)
9247   "Import an arbitrary file into a mail newsgroup."
9248   (interactive "fImport file: \nP")
9249   (let ((group gnus-newsgroup-name)
9250         (now (current-time))
9251         atts lines group-art)
9252     (unless (gnus-check-backend-function 'request-accept-article group)
9253       (error "%s does not support article importing" group))
9254     (or (file-readable-p file)
9255         (not (file-regular-p file))
9256         (error "Can't read %s" file))
9257     (save-excursion
9258       (set-buffer (gnus-get-buffer-create " *import file*"))
9259       (erase-buffer)
9260       (nnheader-insert-file-contents file)
9261       (goto-char (point-min))
9262       (if (nnheader-article-p)
9263           (save-restriction
9264             (goto-char (point-min))
9265             (search-forward "\n\n" nil t)
9266             (narrow-to-region (point-min) (1- (point)))
9267             (goto-char (point-min))
9268             (unless (re-search-forward "^date:" nil t)
9269               (goto-char (point-max))
9270               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9271         ;; This doesn't look like an article, so we fudge some headers.
9272         (setq atts (file-attributes file)
9273               lines (count-lines (point-min) (point-max)))
9274         (insert "From: " (read-string "From: ") "\n"
9275                 "Subject: " (read-string "Subject: ") "\n"
9276                 "Date: " (message-make-date (nth 5 atts)) "\n"
9277                 "Message-ID: " (message-make-message-id) "\n"
9278                 "Lines: " (int-to-string lines) "\n"
9279                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9280       (setq group-art (gnus-request-accept-article group nil t))
9281       (kill-buffer (current-buffer)))
9282     (setq gnus-newsgroup-active (gnus-activate-group group))
9283     (forward-line 1)
9284     (gnus-summary-goto-article (cdr group-art) nil t)
9285     (when edit
9286       (gnus-summary-edit-article))))
9287
9288 (defun gnus-summary-create-article ()
9289   "Create an article in a mail newsgroup."
9290   (interactive)
9291   (let ((group gnus-newsgroup-name)
9292         (now (current-time))
9293         group-art)
9294     (unless (gnus-check-backend-function 'request-accept-article group)
9295       (error "%s does not support article importing" group))
9296     (save-excursion
9297       (set-buffer (gnus-get-buffer-create " *import file*"))
9298       (erase-buffer)
9299       (goto-char (point-min))
9300       ;; This doesn't look like an article, so we fudge some headers.
9301       (insert "From: " (read-string "From: ") "\n"
9302               "Subject: " (read-string "Subject: ") "\n"
9303               "Date: " (message-make-date now) "\n"
9304               "Message-ID: " (message-make-message-id) "\n")
9305       (setq group-art (gnus-request-accept-article group nil t))
9306       (kill-buffer (current-buffer)))
9307     (setq gnus-newsgroup-active (gnus-activate-group group))
9308     (forward-line 1)
9309     (gnus-summary-goto-article (cdr group-art) nil t)
9310     (gnus-summary-edit-article)))
9311
9312 (defun gnus-summary-article-posted-p ()
9313   "Say whether the current (mail) article is available from news as well.
9314 This will be the case if the article has both been mailed and posted."
9315   (interactive)
9316   (let ((id (mail-header-references (gnus-summary-article-header)))
9317         (gnus-override-method (car (gnus-refer-article-methods))))
9318     (if (gnus-request-head id "")
9319         (gnus-message 2 "The current message was found on %s"
9320                       gnus-override-method)
9321       (gnus-message 2 "The current message couldn't be found on %s"
9322                     gnus-override-method)
9323       nil)))
9324
9325 (defun gnus-summary-expire-articles (&optional now)
9326   "Expire all articles that are marked as expirable in the current group."
9327   (interactive)
9328   (when (and (not gnus-group-is-exiting-without-update-p)
9329              (gnus-check-backend-function
9330               'request-expire-articles gnus-newsgroup-name))
9331     ;; This backend supports expiry.
9332     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9333            (expirable (if total
9334                           (progn
9335                             ;; We need to update the info for
9336                             ;; this group for `gnus-list-of-read-articles'
9337                             ;; to give us the right answer.
9338                             (gnus-run-hooks 'gnus-exit-group-hook)
9339                             (gnus-summary-update-info)
9340                             (gnus-list-of-read-articles gnus-newsgroup-name))
9341                         (setq gnus-newsgroup-expirable
9342                               (sort gnus-newsgroup-expirable '<))))
9343            (expiry-wait (if now 'immediate
9344                           (gnus-group-find-parameter
9345                            gnus-newsgroup-name 'expiry-wait)))
9346            (nnmail-expiry-target
9347             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9348                 nnmail-expiry-target))
9349            es)
9350       (when expirable
9351         ;; There are expirable articles in this group, so we run them
9352         ;; through the expiry process.
9353         (gnus-message 6 "Expiring articles...")
9354         (unless (gnus-check-group gnus-newsgroup-name)
9355           (error "Can't open server for %s" gnus-newsgroup-name))
9356         ;; The list of articles that weren't expired is returned.
9357         (save-excursion
9358           (if expiry-wait
9359               (let ((nnmail-expiry-wait-function nil)
9360                     (nnmail-expiry-wait expiry-wait))
9361                 (setq es (gnus-request-expire-articles
9362                           expirable gnus-newsgroup-name)))
9363             (setq es (gnus-request-expire-articles
9364                       expirable gnus-newsgroup-name)))
9365           (unless total
9366             (setq gnus-newsgroup-expirable es))
9367           ;; We go through the old list of expirable, and mark all
9368           ;; really expired articles as nonexistent.
9369           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9370             (let ((gnus-use-cache nil))
9371               (dolist (article expirable)
9372                 (when (and (not (memq article es))
9373                            (gnus-data-find article))
9374                   (gnus-summary-mark-article article gnus-canceled-mark)
9375                   (run-hook-with-args 'gnus-summary-article-expire-hook
9376                                       'delete
9377                                       (gnus-data-header
9378                                        (assoc article (gnus-data-list nil)))
9379                                       gnus-newsgroup-name
9380                                       nil
9381                                       nil))))))
9382         (gnus-message 6 "Expiring articles...done")))))
9383
9384 (defun gnus-summary-expire-articles-now ()
9385   "Expunge all expirable articles in the current group.
9386 This means that *all* articles that are marked as expirable will be
9387 deleted forever, right now."
9388   (interactive)
9389   (or gnus-expert-user
9390       (gnus-yes-or-no-p
9391        "Are you really, really, really sure you want to delete all these messages? ")
9392       (error "Phew!"))
9393   (gnus-summary-expire-articles t))
9394
9395 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9396 (defun gnus-summary-delete-article (&optional n)
9397   "Delete the N next (mail) articles.
9398 This command actually deletes articles.  This is not a marking
9399 command.  The article will disappear forever from your life, never to
9400 return.
9401
9402 If N is negative, delete backwards.
9403 If N is nil and articles have been marked with the process mark,
9404 delete these instead.
9405
9406 If `gnus-novice-user' is non-nil you will be asked for
9407 confirmation before the articles are deleted."
9408   (interactive "P")
9409   (unless (gnus-check-backend-function 'request-expire-articles
9410                                        gnus-newsgroup-name)
9411     (error "The current newsgroup does not support article deletion"))
9412   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9413     (error "Couldn't open server"))
9414   ;; Compute the list of articles to delete.
9415   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9416         (nnmail-expiry-target 'delete)
9417         not-deleted)
9418     (if (and gnus-novice-user
9419              (not (gnus-yes-or-no-p
9420                    (format "Do you really want to delete %s forever? "
9421                            (if (> (length articles) 1)
9422                                (format "these %s articles" (length articles))
9423                              "this article")))))
9424         ()
9425       ;; Delete the articles.
9426       (setq not-deleted (gnus-request-expire-articles
9427                          articles gnus-newsgroup-name 'force))
9428       (while articles
9429         (gnus-summary-remove-process-mark (car articles))
9430         ;; The backend might not have been able to delete the article
9431         ;; after all.
9432         (unless (memq (car articles) not-deleted)
9433           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9434         (let* ((article (car articles))
9435                (id (mail-header-id (gnus-data-header
9436                                     (assoc article (gnus-data-list nil))))))
9437           (run-hook-with-args 'gnus-summary-article-delete-hook
9438                               'delete id gnus-newsgroup-name nil
9439                               nil))
9440         (setq articles (cdr articles)))
9441       (when not-deleted
9442         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9443     (gnus-summary-position-point)
9444     (gnus-set-mode-line 'summary)
9445     not-deleted))
9446
9447 (defun gnus-summary-edit-article (&optional force)
9448   "Edit the current article.
9449 This will have permanent effect only in mail groups.
9450 If FORCE is non-nil, allow editing of articles even in read-only
9451 groups."
9452   (interactive "P")
9453   (save-excursion
9454     (set-buffer gnus-summary-buffer)
9455     (let ((mail-parse-charset gnus-newsgroup-charset)
9456           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9457       (gnus-set-global-variables)
9458       (when (and (not force)
9459                  (gnus-group-read-only-p))
9460         (error "The current newsgroup does not support article editing"))
9461       (gnus-summary-show-article t)
9462       (gnus-article-edit-article
9463        'ignore
9464        `(lambda (no-highlight)
9465           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9466                 (message-options message-options)
9467                 (message-options-set-recipient)
9468                 (mail-parse-ignored-charsets
9469                  ',gnus-newsgroup-ignored-charsets))
9470             (gnus-summary-edit-article-done
9471              ,(or (mail-header-references gnus-current-headers) "")
9472              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9473
9474 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9475
9476 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9477                                                  no-highlight)
9478   "Make edits to the current article permanent."
9479   (interactive)
9480   (save-excursion
9481     ;; The buffer restriction contains the entire article if it exists.
9482     (when (article-goto-body)
9483       (let ((lines (count-lines (point) (point-max)))
9484             (length (- (point-max) (point)))
9485             (case-fold-search t)
9486             (body (copy-marker (point))))
9487         (goto-char (point-min))
9488         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9489           (delete-region (match-beginning 1) (match-end 1))
9490           (insert (number-to-string length)))
9491         (goto-char (point-min))
9492         (when (re-search-forward
9493                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9494           (delete-region (match-beginning 1) (match-end 1))
9495           (insert (number-to-string length)))
9496         (goto-char (point-min))
9497         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9498           (delete-region (match-beginning 1) (match-end 1))
9499           (insert (number-to-string lines))))))
9500   ;; Replace the article.
9501   (let ((buf (current-buffer)))
9502     (with-temp-buffer
9503       (insert-buffer-substring buf)
9504
9505       (if (and (not read-only)
9506                (not (gnus-request-replace-article
9507                      (cdr gnus-article-current) (car gnus-article-current)
9508                      (current-buffer) t)))
9509           (error "Couldn't replace article")
9510         ;; Update the summary buffer.
9511         (if (and references
9512                  (equal (message-tokenize-header references " ")
9513                         (message-tokenize-header
9514                          (or (message-fetch-field "references") "") " ")))
9515             ;; We only have to update this line.
9516             (save-excursion
9517               (save-restriction
9518                 (message-narrow-to-head)
9519                 (let ((head (buffer-string))
9520                       header)
9521                   (with-temp-buffer
9522                     (insert (format "211 %d Article retrieved.\n"
9523                                     (cdr gnus-article-current)))
9524                     (insert head)
9525                     (insert ".\n")
9526                     (let ((nntp-server-buffer (current-buffer)))
9527                       (setq header (car (gnus-get-newsgroup-headers
9528                                          nil t))))
9529                     (save-excursion
9530                       (set-buffer gnus-summary-buffer)
9531                       (gnus-data-set-header
9532                        (gnus-data-find (cdr gnus-article-current))
9533                        header)
9534                       (gnus-summary-update-article-line
9535                        (cdr gnus-article-current) header)
9536                       (if (gnus-summary-goto-subject
9537                            (cdr gnus-article-current) nil t)
9538                           (gnus-summary-update-secondary-mark
9539                            (cdr gnus-article-current))))))))
9540           ;; Update threads.
9541           (set-buffer (or buffer gnus-summary-buffer))
9542           (gnus-summary-update-article (cdr gnus-article-current))
9543           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9544               (gnus-summary-update-secondary-mark
9545                (cdr gnus-article-current))))
9546         ;; Prettify the article buffer again.
9547         (unless no-highlight
9548           (save-excursion
9549             (set-buffer gnus-article-buffer)
9550             ;;;!!! Fix this -- article should be rehighlighted.
9551             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9552             (set-buffer gnus-original-article-buffer)
9553             (gnus-request-article
9554              (cdr gnus-article-current)
9555              (car gnus-article-current) (current-buffer))))
9556         ;; Prettify the summary buffer line.
9557         (when (gnus-visual-p 'summary-highlight 'highlight)
9558           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9559
9560 (defun gnus-summary-edit-wash (key)
9561   "Perform editing command KEY in the article buffer."
9562   (interactive
9563    (list
9564     (progn
9565       (message "%s" (concat (this-command-keys) "- "))
9566       (read-char))))
9567   (message "")
9568   (gnus-summary-edit-article)
9569   (execute-kbd-macro (concat (this-command-keys) key))
9570   (gnus-article-edit-done))
9571
9572 ;;; Respooling
9573
9574 (defun gnus-summary-respool-query (&optional silent trace)
9575   "Query where the respool algorithm would put this article."
9576   (interactive)
9577   (let (gnus-mark-article-hook)
9578     (gnus-summary-select-article)
9579     (save-excursion
9580       (set-buffer gnus-original-article-buffer)
9581       (let ((groups (nnmail-article-group 'identity trace)))
9582         (unless silent
9583           (if groups
9584               (message "This message would go to %s"
9585                        (mapconcat 'car groups ", "))
9586             (message "This message would go to no groups"))
9587           groups)))))
9588
9589 (defun gnus-summary-respool-trace ()
9590   "Trace where the respool algorithm would put this article.
9591 Display a buffer showing all fancy splitting patterns which matched."
9592   (interactive)
9593   (gnus-summary-respool-query nil t))
9594
9595 ;; Summary marking commands.
9596
9597 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9598   "Mark articles which has the same subject as read, and then select the next.
9599 If UNMARK is positive, remove any kind of mark.
9600 If UNMARK is negative, tick articles."
9601   (interactive "P")
9602   (when unmark
9603     (setq unmark (prefix-numeric-value unmark)))
9604   (let ((count
9605          (gnus-summary-mark-same-subject
9606           (gnus-summary-article-subject) unmark)))
9607     ;; Select next unread article.  If auto-select-same mode, should
9608     ;; select the first unread article.
9609     (gnus-summary-next-article t (and gnus-auto-select-same
9610                                       (gnus-summary-article-subject)))
9611     (gnus-message 7 "%d article%s marked as %s"
9612                   count (if (= count 1) " is" "s are")
9613                   (if unmark "unread" "read"))))
9614
9615 (defun gnus-summary-kill-same-subject (&optional unmark)
9616   "Mark articles which has the same subject as read.
9617 If UNMARK is positive, remove any kind of mark.
9618 If UNMARK is negative, tick articles."
9619   (interactive "P")
9620   (when unmark
9621     (setq unmark (prefix-numeric-value unmark)))
9622   (let ((count
9623          (gnus-summary-mark-same-subject
9624           (gnus-summary-article-subject) unmark)))
9625     ;; If marked as read, go to next unread subject.
9626     (when (null unmark)
9627       ;; Go to next unread subject.
9628       (gnus-summary-next-subject 1 t))
9629     (gnus-message 7 "%d articles are marked as %s"
9630                   count (if unmark "unread" "read"))))
9631
9632 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9633   "Mark articles with same SUBJECT as read, and return marked number.
9634 If optional argument UNMARK is positive, remove any kinds of marks.
9635 If optional argument UNMARK is negative, mark articles as unread instead."
9636   (let ((count 1))
9637     (save-excursion
9638       (cond
9639        ((null unmark)                   ; Mark as read.
9640         (while (and
9641                 (progn
9642                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9643                   (gnus-summary-show-thread) t)
9644                 (gnus-summary-find-subject subject))
9645           (setq count (1+ count))))
9646        ((> unmark 0)                    ; Tick.
9647         (while (and
9648                 (progn
9649                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9650                   (gnus-summary-show-thread) t)
9651                 (gnus-summary-find-subject subject))
9652           (setq count (1+ count))))
9653        (t                               ; Mark as unread.
9654         (while (and
9655                 (progn
9656                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9657                   (gnus-summary-show-thread) t)
9658                 (gnus-summary-find-subject subject))
9659           (setq count (1+ count)))))
9660       (gnus-set-mode-line 'summary)
9661       ;; Return the number of marked articles.
9662       count)))
9663
9664 (defun gnus-summary-mark-as-processable (n &optional unmark)
9665   "Set the process mark on the next N articles.
9666 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9667 the process mark instead.  The difference between N and the actual
9668 number of articles marked is returned."
9669   (interactive "P")
9670   (if (and (null n) (gnus-region-active-p))
9671       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9672     (setq n (prefix-numeric-value n))
9673     (let ((backward (< n 0))
9674           (n (abs n)))
9675       (while (and
9676               (> n 0)
9677               (if unmark
9678                   (gnus-summary-remove-process-mark
9679                    (gnus-summary-article-number))
9680                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9681               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9682         (setq n (1- n)))
9683       (when (/= 0 n)
9684         (gnus-message 7 "No more articles"))
9685       (gnus-summary-recenter)
9686       (gnus-summary-position-point)
9687       n)))
9688
9689 (defun gnus-summary-unmark-as-processable (n)
9690   "Remove the process mark from the next N articles.
9691 If N is negative, unmark backward instead.  The difference between N and
9692 the actual number of articles unmarked is returned."
9693   (interactive "P")
9694   (gnus-summary-mark-as-processable n t))
9695
9696 (defun gnus-summary-unmark-all-processable ()
9697   "Remove the process mark from all articles."
9698   (interactive)
9699   (save-excursion
9700     (while gnus-newsgroup-processable
9701       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9702   (gnus-summary-position-point))
9703
9704 (defun gnus-summary-add-mark (article type)
9705   "Mark ARTICLE with a mark of TYPE."
9706   (let ((vtype (car (assq type gnus-article-mark-lists)))
9707         var)
9708     (if (not vtype)
9709         (error "No such mark type: %s" type)
9710       (setq var (intern (format "gnus-newsgroup-%s" type)))
9711       (set var (cons article (symbol-value var)))
9712       (if (memq type '(processable cached replied forwarded recent saved))
9713           (gnus-summary-update-secondary-mark article)
9714         ;;; !!! This is bogus.  We should find out what primary
9715         ;;; !!! mark we want to set.
9716         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9717
9718 (defun gnus-summary-mark-as-expirable (n)
9719   "Mark N articles forward as expirable.
9720 If N is negative, mark backward instead.  The difference between N and
9721 the actual number of articles marked is returned."
9722   (interactive "p")
9723   (gnus-summary-mark-forward n gnus-expirable-mark))
9724
9725 (defun gnus-summary-mark-as-spam (n)
9726   "Mark N articles forward as spam.
9727 If N is negative, mark backward instead.  The difference between N and
9728 the actual number of articles marked is returned."
9729   (interactive "p")
9730   (gnus-summary-mark-forward n gnus-spam-mark))
9731
9732 (defun gnus-summary-mark-article-as-replied (article)
9733   "Mark ARTICLE as replied to and update the summary line.
9734 ARTICLE can also be a list of articles."
9735   (interactive (list (gnus-summary-article-number)))
9736   (let ((articles (if (listp article) article (list article))))
9737     (dolist (article articles)
9738       (unless (numberp article)
9739         (error "%s is not a number" article))
9740       (push article gnus-newsgroup-replied)
9741       (let ((buffer-read-only nil))
9742         (when (gnus-summary-goto-subject article nil t)
9743           (gnus-summary-update-secondary-mark article))))))
9744
9745 (defun gnus-summary-mark-article-as-forwarded (article)
9746   "Mark ARTICLE as forwarded and update the summary line.
9747 ARTICLE can also be a list of articles."
9748   (let ((articles (if (listp article) article (list article))))
9749     (dolist (article articles)
9750       (push article gnus-newsgroup-forwarded)
9751       (let ((buffer-read-only nil))
9752         (when (gnus-summary-goto-subject article nil t)
9753           (gnus-summary-update-secondary-mark article))))))
9754
9755 (defun gnus-summary-set-bookmark (article)
9756   "Set a bookmark in current article."
9757   (interactive (list (gnus-summary-article-number)))
9758   (when (or (not (get-buffer gnus-article-buffer))
9759             (not gnus-current-article)
9760             (not gnus-article-current)
9761             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9762     (error "No current article selected"))
9763   ;; Remove old bookmark, if one exists.
9764   (gnus-pull article gnus-newsgroup-bookmarks)
9765   ;; Set the new bookmark, which is on the form
9766   ;; (article-number . line-number-in-body).
9767   (push
9768    (cons article
9769          (save-excursion
9770            (set-buffer gnus-article-buffer)
9771            (count-lines
9772             (min (point)
9773                  (save-excursion
9774                    (article-goto-body)
9775                    (point)))
9776             (point))))
9777    gnus-newsgroup-bookmarks)
9778   (gnus-message 6 "A bookmark has been added to the current article."))
9779
9780 (defun gnus-summary-remove-bookmark (article)
9781   "Remove the bookmark from the current article."
9782   (interactive (list (gnus-summary-article-number)))
9783   ;; Remove old bookmark, if one exists.
9784   (if (not (assq article gnus-newsgroup-bookmarks))
9785       (gnus-message 6 "No bookmark in current article.")
9786     (gnus-pull article gnus-newsgroup-bookmarks)
9787     (gnus-message 6 "Removed bookmark.")))
9788
9789 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9790 (defun gnus-summary-mark-as-dormant (n)
9791   "Mark N articles forward as dormant.
9792 If N is negative, mark backward instead.  The difference between N and
9793 the actual number of articles marked is returned."
9794   (interactive "p")
9795   (gnus-summary-mark-forward n gnus-dormant-mark))
9796
9797 (defun gnus-summary-set-process-mark (article)
9798   "Set the process mark on ARTICLE and update the summary line."
9799   (setq gnus-newsgroup-processable
9800         (cons article
9801               (delq article gnus-newsgroup-processable)))
9802   (when (gnus-summary-goto-subject article)
9803     (gnus-summary-show-thread)
9804     (gnus-summary-goto-subject article)
9805     (gnus-summary-update-secondary-mark article)))
9806
9807 (defun gnus-summary-remove-process-mark (article)
9808   "Remove the process mark from ARTICLE and update the summary line."
9809   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9810   (when (gnus-summary-goto-subject article)
9811     (gnus-summary-show-thread)
9812     (gnus-summary-goto-subject article)
9813     (gnus-summary-update-secondary-mark article)))
9814
9815 (defun gnus-summary-set-saved-mark (article)
9816   "Set the process mark on ARTICLE and update the summary line."
9817   (push article gnus-newsgroup-saved)
9818   (when (gnus-summary-goto-subject article)
9819     (gnus-summary-update-secondary-mark article)))
9820
9821 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9822   "Mark N articles as read forwards.
9823 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9824 The difference between N and the actual number of articles marked is
9825 returned.
9826 If NO-EXPIRE, auto-expiry will be inhibited."
9827   (interactive "p")
9828   (gnus-summary-show-thread)
9829   (let ((backward (< n 0))
9830         (gnus-summary-goto-unread
9831          (and gnus-summary-goto-unread
9832               (not (eq gnus-summary-goto-unread 'never))
9833               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9834                                     gnus-ticked-mark gnus-dormant-mark)))))
9835         (n (abs n))
9836         (mark (or mark gnus-del-mark)))
9837     (while (and (> n 0)
9838                 (gnus-summary-mark-article nil mark no-expire)
9839                 (zerop (gnus-summary-next-subject
9840                         (if backward -1 1)
9841                         (and gnus-summary-goto-unread
9842                              (not (eq gnus-summary-goto-unread 'never)))
9843                         t)))
9844       (setq n (1- n)))
9845     (when (/= 0 n)
9846       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9847     (gnus-summary-recenter)
9848     (gnus-summary-position-point)
9849     (gnus-set-mode-line 'summary)
9850     n))
9851
9852 (defun gnus-summary-mark-article-as-read (mark)
9853   "Mark the current article quickly as read with MARK."
9854   (let ((article (gnus-summary-article-number)))
9855     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9856     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9857     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9858     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9859     (push (cons article mark) gnus-newsgroup-reads)
9860     ;; Possibly remove from cache, if that is used.
9861     (when gnus-use-cache
9862       (gnus-cache-enter-remove-article article))
9863     ;; Allow the backend to change the mark.
9864     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9865     ;; Check for auto-expiry.
9866     (when (and gnus-newsgroup-auto-expire
9867                (memq mark gnus-auto-expirable-marks))
9868       (setq mark gnus-expirable-mark)
9869       ;; Let the backend know about the mark change.
9870       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9871       (push article gnus-newsgroup-expirable))
9872     ;; Set the mark in the buffer.
9873     (gnus-summary-update-mark mark 'unread)
9874     t))
9875
9876 (defun gnus-summary-mark-article-as-unread (mark)
9877   "Mark the current article quickly as unread with MARK."
9878   (let* ((article (gnus-summary-article-number))
9879          (old-mark (gnus-summary-article-mark article)))
9880     ;; Allow the backend to change the mark.
9881     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9882     (if (eq mark old-mark)
9883         t
9884       (if (<= article 0)
9885           (progn
9886             (gnus-error 1 "Can't mark negative article numbers")
9887             nil)
9888         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9889         (setq gnus-newsgroup-spam-marked
9890               (delq article gnus-newsgroup-spam-marked))
9891         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9892         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9893         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9894         (cond ((= mark gnus-ticked-mark)
9895                (setq gnus-newsgroup-marked
9896                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9897                                               article)))
9898               ((= mark gnus-spam-mark)
9899                (setq gnus-newsgroup-spam-marked
9900                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9901                                               article)))
9902               ((= mark gnus-dormant-mark)
9903                (setq gnus-newsgroup-dormant
9904                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9905                                               article)))
9906               (t
9907                (setq gnus-newsgroup-unreads
9908                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9909                                               article))))
9910         (gnus-pull article gnus-newsgroup-reads)
9911
9912         ;; See whether the article is to be put in the cache.
9913         (and gnus-use-cache
9914              (vectorp (gnus-summary-article-header article))
9915              (save-excursion
9916                (gnus-cache-possibly-enter-article
9917                 gnus-newsgroup-name article
9918                 (gnus-summary-article-header article)
9919                 (= mark gnus-ticked-mark)
9920                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9921
9922         ;; Fix the mark.
9923         (gnus-summary-update-mark mark 'unread)
9924         t))))
9925
9926 (defun gnus-summary-mark-article (&optional article mark no-expire)
9927   "Mark ARTICLE with MARK.  MARK can be any character.
9928 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9929 `??' (dormant) and `?E' (expirable).
9930 If MARK is nil, then the default character `?r' is used.
9931 If ARTICLE is nil, then the article on the current line will be
9932 marked.
9933 If NO-EXPIRE, auto-expiry will be inhibited."
9934   ;; The mark might be a string.
9935   (when (stringp mark)
9936     (setq mark (aref mark 0)))
9937   ;; If no mark is given, then we check auto-expiring.
9938   (when (null mark)
9939     (setq mark gnus-del-mark))
9940   (when (and (not no-expire)
9941              gnus-newsgroup-auto-expire
9942              (memq mark gnus-auto-expirable-marks))
9943     (setq mark gnus-expirable-mark))
9944   (let ((article (or article (gnus-summary-article-number)))
9945         (old-mark (gnus-summary-article-mark article)))
9946     ;; Allow the backend to change the mark.
9947     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9948     (if (eq mark old-mark)
9949         t
9950       (unless article
9951         (error "No article on current line"))
9952       (if (not (if (or (= mark gnus-unread-mark)
9953                        (= mark gnus-ticked-mark)
9954                        (= mark gnus-spam-mark)
9955                        (= mark gnus-dormant-mark))
9956                    (gnus-mark-article-as-unread article mark)
9957                  (gnus-mark-article-as-read article mark)))
9958           t
9959         ;; See whether the article is to be put in the cache.
9960         (and gnus-use-cache
9961              (not (= mark gnus-canceled-mark))
9962              (vectorp (gnus-summary-article-header article))
9963              (save-excursion
9964                (gnus-cache-possibly-enter-article
9965                 gnus-newsgroup-name article
9966                 (gnus-summary-article-header article)
9967                 (= mark gnus-ticked-mark)
9968                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9969
9970         (when (gnus-summary-goto-subject article nil t)
9971           (let ((buffer-read-only nil))
9972             (gnus-summary-show-thread)
9973             ;; Fix the mark.
9974             (gnus-summary-update-mark mark 'unread)
9975             t))))))
9976
9977 (defun gnus-summary-update-secondary-mark (article)
9978   "Update the secondary (read, process, cache) mark."
9979   (gnus-summary-update-mark
9980    (cond ((memq article gnus-newsgroup-processable)
9981           gnus-process-mark)
9982          ((memq article gnus-newsgroup-cached)
9983           gnus-cached-mark)
9984          ((memq article gnus-newsgroup-replied)
9985           gnus-replied-mark)
9986          ((memq article gnus-newsgroup-forwarded)
9987           gnus-forwarded-mark)
9988          ((memq article gnus-newsgroup-saved)
9989           gnus-saved-mark)
9990          ((memq article gnus-newsgroup-recent)
9991           gnus-recent-mark)
9992          ((memq article gnus-newsgroup-unseen)
9993           gnus-unseen-mark)
9994          (t gnus-no-mark))
9995    'replied)
9996   (when (gnus-visual-p 'summary-highlight 'highlight)
9997     (gnus-run-hooks 'gnus-summary-update-hook))
9998   t)
9999
10000 (defun gnus-summary-update-download-mark (article)
10001   "Update the download mark."
10002   (gnus-summary-update-mark
10003    (cond ((memq article gnus-newsgroup-undownloaded)
10004           gnus-undownloaded-mark)
10005          (gnus-newsgroup-agentized
10006           gnus-downloaded-mark)
10007          (t
10008           gnus-no-mark))
10009    'download)
10010   (gnus-summary-update-line t)
10011   t)
10012
10013 (defun gnus-summary-update-mark (mark type)
10014   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10015         (buffer-read-only nil))
10016     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10017     (when forward
10018       (when (looking-at "\r")
10019         (incf forward))
10020       (when (<= (+ forward (point)) (point-max))
10021         ;; Go to the right position on the line.
10022         (goto-char (+ forward (point)))
10023         ;; Replace the old mark with the new mark.
10024         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10025         ;; Optionally update the marks by some user rule.
10026         (when (eq type 'unread)
10027           (gnus-data-set-mark
10028            (gnus-data-find (gnus-summary-article-number)) mark)
10029           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10030
10031 (defun gnus-mark-article-as-read (article &optional mark)
10032   "Enter ARTICLE in the pertinent lists and remove it from others."
10033   ;; Make the article expirable.
10034   (let ((mark (or mark gnus-del-mark)))
10035     (setq gnus-newsgroup-expirable
10036           (if (= mark gnus-expirable-mark)
10037               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10038             (delq article gnus-newsgroup-expirable)))
10039     ;; Remove from unread and marked lists.
10040     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10041     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10042     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10043     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10044     (push (cons article mark) gnus-newsgroup-reads)
10045     ;; Possibly remove from cache, if that is used.
10046     (when gnus-use-cache
10047       (gnus-cache-enter-remove-article article))
10048     t))
10049
10050 (defun gnus-mark-article-as-unread (article &optional mark)
10051   "Enter ARTICLE in the pertinent lists and remove it from others."
10052   (let ((mark (or mark gnus-ticked-mark)))
10053     (if (<= article 0)
10054         (progn
10055           (gnus-error 1 "Can't mark negative article numbers")
10056           nil)
10057       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10058             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10059             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10060             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10061             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10062
10063       ;; Unsuppress duplicates?
10064       (when gnus-suppress-duplicates
10065         (gnus-dup-unsuppress-article article))
10066
10067       (cond ((= mark gnus-ticked-mark)
10068              (setq gnus-newsgroup-marked
10069                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10070             ((= mark gnus-spam-mark)
10071              (setq gnus-newsgroup-spam-marked
10072                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10073                                             article)))
10074             ((= mark gnus-dormant-mark)
10075              (setq gnus-newsgroup-dormant
10076                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10077             (t
10078              (setq gnus-newsgroup-unreads
10079                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10080       (gnus-pull article gnus-newsgroup-reads)
10081       t)))
10082
10083 (defalias 'gnus-summary-mark-as-unread-forward
10084   'gnus-summary-tick-article-forward)
10085 (make-obsolete 'gnus-summary-mark-as-unread-forward
10086                'gnus-summary-tick-article-forward)
10087 (defun gnus-summary-tick-article-forward (n)
10088   "Tick N articles forwards.
10089 If N is negative, tick backwards instead.
10090 The difference between N and the number of articles ticked is returned."
10091   (interactive "p")
10092   (gnus-summary-mark-forward n gnus-ticked-mark))
10093
10094 (defalias 'gnus-summary-mark-as-unread-backward
10095   'gnus-summary-tick-article-backward)
10096 (make-obsolete 'gnus-summary-mark-as-unread-backward
10097                'gnus-summary-tick-article-backward)
10098 (defun gnus-summary-tick-article-backward (n)
10099   "Tick N articles backwards.
10100 The difference between N and the number of articles ticked is returned."
10101   (interactive "p")
10102   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10103
10104 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10105 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10106 (defun gnus-summary-tick-article (&optional article clear-mark)
10107   "Mark current article as unread.
10108 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10109 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10110   (interactive)
10111   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10112                                        gnus-ticked-mark)))
10113
10114 (defun gnus-summary-mark-as-read-forward (n)
10115   "Mark N articles as read forwards.
10116 If N is negative, mark backwards instead.
10117 The difference between N and the actual number of articles marked is
10118 returned."
10119   (interactive "p")
10120   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10121
10122 (defun gnus-summary-mark-as-read-backward (n)
10123   "Mark the N articles as read backwards.
10124 The difference between N and the actual number of articles marked is
10125 returned."
10126   (interactive "p")
10127   (gnus-summary-mark-forward
10128    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10129
10130 (defun gnus-summary-mark-as-read (&optional article mark)
10131   "Mark current article as read.
10132 ARTICLE specifies the article to be marked as read.
10133 MARK specifies a string to be inserted at the beginning of the line."
10134   (gnus-summary-mark-article article mark))
10135
10136 (defun gnus-summary-clear-mark-forward (n)
10137   "Clear marks from N articles forward.
10138 If N is negative, clear backward instead.
10139 The difference between N and the number of marks cleared is returned."
10140   (interactive "p")
10141   (gnus-summary-mark-forward n gnus-unread-mark))
10142
10143 (defun gnus-summary-clear-mark-backward (n)
10144   "Clear marks from N articles backward.
10145 The difference between N and the number of marks cleared is returned."
10146   (interactive "p")
10147   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10148
10149 (defun gnus-summary-mark-unread-as-read ()
10150   "Intended to be used by `gnus-summary-mark-article-hook'."
10151   (when (memq gnus-current-article gnus-newsgroup-unreads)
10152     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10153
10154 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10155   "Intended to be used by `gnus-summary-mark-article-hook'."
10156   (let ((mark (gnus-summary-article-mark)))
10157     (when (or (gnus-unread-mark-p mark)
10158               (gnus-read-mark-p mark))
10159       (gnus-summary-mark-article gnus-current-article
10160                                  (or new-mark gnus-read-mark)))))
10161
10162 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10163   "Intended to be used by `gnus-summary-mark-article-hook'."
10164   (let ((mark (gnus-summary-article-mark)))
10165     (when (or (gnus-unread-mark-p mark)
10166               (gnus-read-mark-p mark))
10167       (gnus-summary-mark-article (gnus-summary-article-number)
10168                                  (or new-mark gnus-read-mark)))))
10169
10170 (defun gnus-summary-mark-unread-as-ticked ()
10171   "Intended to be used by `gnus-summary-mark-article-hook'."
10172   (when (memq gnus-current-article gnus-newsgroup-unreads)
10173     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10174
10175 (defun gnus-summary-mark-region-as-read (point mark all)
10176   "Mark all unread articles between point and mark as read.
10177 If given a prefix, mark all articles between point and mark as read,
10178 even ticked and dormant ones."
10179   (interactive "r\nP")
10180   (save-excursion
10181     (let (article)
10182       (goto-char point)
10183       (beginning-of-line)
10184       (while (and
10185               (< (point) mark)
10186               (progn
10187                 (when (or all
10188                           (memq (setq article (gnus-summary-article-number))
10189                                 gnus-newsgroup-unreads))
10190                   (gnus-summary-mark-article article gnus-del-mark))
10191                 t)
10192               (gnus-summary-find-next))))))
10193
10194 (defun gnus-summary-mark-below (score mark)
10195   "Mark articles with score less than SCORE with MARK."
10196   (interactive "P\ncMark: ")
10197   (setq score (if score
10198                   (prefix-numeric-value score)
10199                 (or gnus-summary-default-score 0)))
10200   (save-excursion
10201     (set-buffer gnus-summary-buffer)
10202     (goto-char (point-min))
10203     (while
10204         (progn
10205           (and (< (gnus-summary-article-score) score)
10206                (gnus-summary-mark-article nil mark))
10207           (gnus-summary-find-next)))))
10208
10209 (defun gnus-summary-kill-below (&optional score)
10210   "Mark articles with score below SCORE as read."
10211   (interactive "P")
10212   (gnus-summary-mark-below score gnus-killed-mark))
10213
10214 (defun gnus-summary-clear-above (&optional score)
10215   "Clear all marks from articles with score above SCORE."
10216   (interactive "P")
10217   (gnus-summary-mark-above score gnus-unread-mark))
10218
10219 (defun gnus-summary-tick-above (&optional score)
10220   "Tick all articles with score above SCORE."
10221   (interactive "P")
10222   (gnus-summary-mark-above score gnus-ticked-mark))
10223
10224 (defun gnus-summary-mark-above (score mark)
10225   "Mark articles with score over SCORE with MARK."
10226   (interactive "P\ncMark: ")
10227   (setq score (if score
10228                   (prefix-numeric-value score)
10229                 (or gnus-summary-default-score 0)))
10230   (save-excursion
10231     (set-buffer gnus-summary-buffer)
10232     (goto-char (point-min))
10233     (while (and (progn
10234                   (when (> (gnus-summary-article-score) score)
10235                     (gnus-summary-mark-article nil mark))
10236                   t)
10237                 (gnus-summary-find-next)))))
10238
10239 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10240 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10241 (defun gnus-summary-limit-include-expunged (&optional no-error)
10242   "Display all the hidden articles that were expunged for low scores."
10243   (interactive)
10244   (let ((buffer-read-only nil))
10245     (let ((scored gnus-newsgroup-scored)
10246           headers h)
10247       (while scored
10248         (unless (gnus-summary-article-header (caar scored))
10249           (and (setq h (gnus-number-to-header (caar scored)))
10250                (< (cdar scored) gnus-summary-expunge-below)
10251                (push h headers)))
10252         (setq scored (cdr scored)))
10253       (if (not headers)
10254           (when (not no-error)
10255             (error "No expunged articles hidden"))
10256         (goto-char (point-min))
10257         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10258         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10259         (mapcar (lambda (x) (push (mail-header-number x)
10260                                   gnus-newsgroup-limit))
10261                 headers)
10262         (gnus-summary-prepare-unthreaded (nreverse headers))
10263         (goto-char (point-min))
10264         (gnus-summary-position-point)
10265         t))))
10266
10267 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10268   "Mark all unread articles in this newsgroup as read.
10269 If prefix argument ALL is non-nil, ticked and dormant articles will
10270 also be marked as read.
10271 If QUIETLY is non-nil, no questions will be asked.
10272
10273 If TO-HERE is non-nil, it should be a point in the buffer.  All
10274 articles before (after, if REVERSE is set) this point will be marked
10275 as read.
10276
10277 Note that this function will only catch up the unread article
10278 in the current summary buffer limitation.
10279
10280 The number of articles marked as read is returned."
10281   (interactive "P")
10282   (prog1
10283       (save-excursion
10284         (when (or quietly
10285                   (not gnus-interactive-catchup) ;Without confirmation?
10286                   gnus-expert-user
10287                   (gnus-y-or-n-p
10288                    (if all
10289                        "Mark absolutely all articles as read? "
10290                      "Mark all unread articles as read? ")))
10291           (if (and not-mark
10292                    (not gnus-newsgroup-adaptive)
10293                    (not gnus-newsgroup-auto-expire)
10294                    (not gnus-suppress-duplicates)
10295                    (or (not gnus-use-cache)
10296                        (eq gnus-use-cache 'passive)))
10297               (progn
10298                 (when all
10299                   (setq gnus-newsgroup-marked nil
10300                         gnus-newsgroup-spam-marked nil
10301                         gnus-newsgroup-dormant nil))
10302                 (setq gnus-newsgroup-unreads
10303                       (gnus-sorted-nunion
10304                        (gnus-intersection gnus-newsgroup-unreads
10305                                           gnus-newsgroup-downloadable)
10306                        gnus-newsgroup-unfetched)))
10307             ;; We actually mark all articles as canceled, which we
10308             ;; have to do when using auto-expiry or adaptive scoring.
10309             (gnus-summary-show-all-threads)
10310             (if (and to-here reverse)
10311                 (progn
10312                   (goto-char to-here)
10313                   (gnus-summary-mark-current-read-and-unread-as-read
10314                    gnus-catchup-mark)
10315                   (while (gnus-summary-find-next (not all))
10316                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10317               (when (gnus-summary-first-subject (not all))
10318                 (while (and
10319                         (if to-here (< (point) to-here) t)
10320                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10321                         (gnus-summary-find-next (not all))))))
10322             (gnus-set-mode-line 'summary))
10323           t))
10324     (gnus-summary-position-point)))
10325
10326 (defun gnus-summary-catchup-to-here (&optional all)
10327   "Mark all unticked articles before the current one as read.
10328 If ALL is non-nil, also mark ticked and dormant articles as read."
10329   (interactive "P")
10330   (save-excursion
10331     (gnus-save-hidden-threads
10332       (let ((beg (point)))
10333         ;; We check that there are unread articles.
10334         (when (or all (gnus-summary-find-prev))
10335           (gnus-summary-catchup all t beg)))))
10336   (gnus-summary-position-point))
10337
10338 (defun gnus-summary-catchup-from-here (&optional all)
10339   "Mark all unticked articles after (and including) the current one as read.
10340 If ALL is non-nil, also mark ticked and dormant articles as read."
10341   (interactive "P")
10342   (save-excursion
10343     (gnus-save-hidden-threads
10344       (let ((beg (point)))
10345         ;; We check that there are unread articles.
10346         (when (or all (gnus-summary-find-next))
10347           (gnus-summary-catchup all t beg nil t)))))
10348   (gnus-summary-position-point))
10349
10350 (defun gnus-summary-catchup-all (&optional quietly)
10351   "Mark all articles in this newsgroup as read.
10352 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10353 instead, which marks only unread articles as read."
10354   (interactive "P")
10355   (gnus-summary-catchup t quietly))
10356
10357 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10358   "Mark all unread articles in this group as read, then exit.
10359 If prefix argument ALL is non-nil, all articles are marked as read.
10360 If QUIETLY is non-nil, no questions will be asked."
10361   (interactive "P")
10362   (when (gnus-summary-catchup all quietly nil 'fast)
10363     ;; Select next newsgroup or exit.
10364     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10365              (eq gnus-auto-select-next 'quietly))
10366         (gnus-summary-next-group nil)
10367       (gnus-summary-exit))))
10368
10369 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10370   "Mark all articles in this newsgroup as read, and then exit.
10371 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10372 instead, which marks only unread articles as read."
10373   (interactive "P")
10374   (gnus-summary-catchup-and-exit t quietly))
10375
10376 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10377   "Mark all articles in this group as read and select the next group.
10378 If given a prefix, mark all articles, unread as well as ticked, as
10379 read."
10380   (interactive "P")
10381   (save-excursion
10382     (gnus-summary-catchup all))
10383   (gnus-summary-next-group))
10384
10385 ;;;
10386 ;;; with article
10387 ;;;
10388
10389 (defmacro gnus-with-article (article &rest forms)
10390   "Select ARTICLE and perform FORMS in the original article buffer.
10391 Then replace the article with the result."
10392   `(progn
10393      ;; We don't want the article to be marked as read.
10394      (let (gnus-mark-article-hook)
10395        (gnus-summary-select-article t t nil ,article))
10396      (set-buffer gnus-original-article-buffer)
10397      ,@forms
10398      (if (not (gnus-check-backend-function
10399                'request-replace-article (car gnus-article-current)))
10400          (gnus-message 5 "Read-only group; not replacing")
10401        (unless (gnus-request-replace-article
10402                 ,article (car gnus-article-current)
10403                 (current-buffer) t)
10404          (error "Couldn't replace article")))
10405      ;; The cache and backlog have to be flushed somewhat.
10406      (when gnus-keep-backlog
10407        (gnus-backlog-remove-article
10408         (car gnus-article-current) (cdr gnus-article-current)))
10409      (when gnus-use-cache
10410        (gnus-cache-update-article
10411         (car gnus-article-current) (cdr gnus-article-current)))))
10412
10413 (put 'gnus-with-article 'lisp-indent-function 1)
10414 (put 'gnus-with-article 'edebug-form-spec '(form body))
10415
10416 ;; Thread-based commands.
10417
10418 (defun gnus-summary-articles-in-thread (&optional article)
10419   "Return a list of all articles in the current thread.
10420 If ARTICLE is non-nil, return all articles in the thread that starts
10421 with that article."
10422   (let* ((article (or article (gnus-summary-article-number)))
10423          (data (gnus-data-find-list article))
10424          (top-level (gnus-data-level (car data)))
10425          (top-subject
10426           (cond ((null gnus-thread-operation-ignore-subject)
10427                  (gnus-simplify-subject-re
10428                   (mail-header-subject (gnus-data-header (car data)))))
10429                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10430                  (gnus-simplify-subject-fuzzy
10431                   (mail-header-subject (gnus-data-header (car data)))))
10432                 (t nil)))
10433          (end-point (save-excursion
10434                       (if (gnus-summary-go-to-next-thread)
10435                           (point) (point-max))))
10436          articles)
10437     (while (and data
10438                 (< (gnus-data-pos (car data)) end-point))
10439       (when (or (not top-subject)
10440                 (string= top-subject
10441                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10442                              (gnus-simplify-subject-fuzzy
10443                               (mail-header-subject
10444                                (gnus-data-header (car data))))
10445                            (gnus-simplify-subject-re
10446                             (mail-header-subject
10447                              (gnus-data-header (car data)))))))
10448         (push (gnus-data-number (car data)) articles))
10449       (unless (and (setq data (cdr data))
10450                    (> (gnus-data-level (car data)) top-level))
10451         (setq data nil)))
10452     ;; Return the list of articles.
10453     (nreverse articles)))
10454
10455 (defun gnus-summary-rethread-current ()
10456   "Rethread the thread the current article is part of."
10457   (interactive)
10458   (let* ((gnus-show-threads t)
10459          (article (gnus-summary-article-number))
10460          (id (mail-header-id (gnus-summary-article-header)))
10461          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10462     (unless id
10463       (error "No article on the current line"))
10464     (gnus-rebuild-thread id)
10465     (gnus-summary-goto-subject article)))
10466
10467 (defun gnus-summary-reparent-thread ()
10468   "Make the current article child of the marked (or previous) article.
10469
10470 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10471 is non-nil or the Subject: of both articles are the same."
10472   (interactive)
10473   (unless (not (gnus-group-read-only-p))
10474     (error "The current newsgroup does not support article editing"))
10475   (unless (<= (length gnus-newsgroup-processable) 1)
10476     (error "No more than one article may be marked"))
10477   (save-window-excursion
10478     (let ((gnus-article-buffer " *reparent*")
10479           (current-article (gnus-summary-article-number))
10480           ;; First grab the marked article, otherwise one line up.
10481           (parent-article (if (not (null gnus-newsgroup-processable))
10482                               (car gnus-newsgroup-processable)
10483                             (save-excursion
10484                               (if (eq (forward-line -1) 0)
10485                                   (gnus-summary-article-number)
10486                                 (error "Beginning of summary buffer"))))))
10487       (unless (not (eq current-article parent-article))
10488         (error "An article may not be self-referential"))
10489       (let ((message-id (mail-header-id
10490                          (gnus-summary-article-header parent-article))))
10491         (unless (and message-id (not (equal message-id "")))
10492           (error "No message-id in desired parent"))
10493         (gnus-with-article current-article
10494           (save-restriction
10495             (goto-char (point-min))
10496             (message-narrow-to-head)
10497             (if (re-search-forward "^References: " nil t)
10498                 (progn
10499                   (re-search-forward "^[^ \t]" nil t)
10500                   (forward-line -1)
10501                   (end-of-line)
10502                   (insert " " message-id))
10503               (insert "References: " message-id "\n"))))
10504         (set-buffer gnus-summary-buffer)
10505         (gnus-summary-unmark-all-processable)
10506         (gnus-summary-update-article current-article)
10507         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10508             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10509         (gnus-summary-rethread-current)
10510         (gnus-message 3 "Article %d is now the child of article %d"
10511                       current-article parent-article)))))
10512
10513 (defun gnus-summary-toggle-threads (&optional arg)
10514   "Toggle showing conversation threads.
10515 If ARG is positive number, turn showing conversation threads on."
10516   (interactive "P")
10517   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10518     (setq gnus-show-threads
10519           (if (null arg) (not gnus-show-threads)
10520             (> (prefix-numeric-value arg) 0)))
10521     (gnus-summary-prepare)
10522     (gnus-summary-goto-subject current)
10523     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10524     (gnus-summary-position-point)))
10525
10526 (defun gnus-summary-show-all-threads ()
10527   "Show all threads."
10528   (interactive)
10529   (save-excursion
10530     (let ((buffer-read-only nil))
10531       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10532   (gnus-summary-position-point))
10533
10534 (defun gnus-summary-show-thread ()
10535   "Show thread subtrees.
10536 Returns nil if no thread was there to be shown."
10537   (interactive)
10538   (let ((buffer-read-only nil)
10539         (orig (point))
10540         (end (point-at-eol))
10541         ;; Leave point at bol
10542         (beg (progn (beginning-of-line) (point))))
10543     (prog1
10544         ;; Any hidden lines here?
10545         (search-forward "\r" end t)
10546       (subst-char-in-region beg end ?\^M ?\n t)
10547       (goto-char orig)
10548       (gnus-summary-position-point))))
10549
10550 (defun gnus-summary-maybe-hide-threads ()
10551   "If requested, hide the threads that should be hidden."
10552   (when (and gnus-show-threads
10553              gnus-thread-hide-subtree)
10554     (gnus-summary-hide-all-threads
10555      (if (or (consp gnus-thread-hide-subtree)
10556              (functionp gnus-thread-hide-subtree))
10557          (gnus-make-predicate gnus-thread-hide-subtree)
10558        nil))))
10559
10560 ;;; Hiding predicates.
10561
10562 (defun gnus-article-unread-p (header)
10563   (memq (mail-header-number header) gnus-newsgroup-unreads))
10564
10565 (defun gnus-article-unseen-p (header)
10566   (memq (mail-header-number header) gnus-newsgroup-unseen))
10567
10568 (defun gnus-map-articles (predicate articles)
10569   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10570   (apply 'gnus-or (mapcar predicate
10571                           (mapcar 'gnus-summary-article-header articles))))
10572
10573 (defun gnus-summary-hide-all-threads (&optional predicate)
10574   "Hide all thread subtrees.
10575 If PREDICATE is supplied, threads that satisfy this predicate
10576 will not be hidden."
10577   (interactive)
10578   (save-excursion
10579     (goto-char (point-min))
10580     (let ((end nil))
10581       (while (not end)
10582         (when (or (not predicate)
10583                   (gnus-map-articles
10584                    predicate (gnus-summary-article-children)))
10585             (gnus-summary-hide-thread))
10586         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10587   (gnus-summary-position-point))
10588
10589 (defun gnus-summary-hide-thread ()
10590   "Hide thread subtrees.
10591 If PREDICATE is supplied, threads that satisfy this predicate
10592 will not be hidden.
10593 Returns nil if no threads were there to be hidden."
10594   (interactive)
10595   (let ((buffer-read-only nil)
10596         (start (point))
10597         (article (gnus-summary-article-number)))
10598     (goto-char start)
10599     ;; Go forward until either the buffer ends or the subthread
10600     ;; ends.
10601     (when (and (not (eobp))
10602                (or (zerop (gnus-summary-next-thread 1 t))
10603                    (goto-char (point-max))))
10604       (prog1
10605           (if (and (> (point) start)
10606                    (search-backward "\n" start t))
10607               (progn
10608                 (subst-char-in-region start (point) ?\n ?\^M)
10609                 (gnus-summary-goto-subject article))
10610             (goto-char start)
10611             nil)))))
10612
10613 (defun gnus-summary-go-to-next-thread (&optional previous)
10614   "Go to the same level (or less) next thread.
10615 If PREVIOUS is non-nil, go to previous thread instead.
10616 Return the article number moved to, or nil if moving was impossible."
10617   (let ((level (gnus-summary-thread-level))
10618         (way (if previous -1 1))
10619         (beg (point)))
10620     (forward-line way)
10621     (while (and (not (eobp))
10622                 (< level (gnus-summary-thread-level)))
10623       (forward-line way))
10624     (if (eobp)
10625         (progn
10626           (goto-char beg)
10627           nil)
10628       (setq beg (point))
10629       (prog1
10630           (gnus-summary-article-number)
10631         (goto-char beg)))))
10632
10633 (defun gnus-summary-next-thread (n &optional silent)
10634   "Go to the same level next N'th thread.
10635 If N is negative, search backward instead.
10636 Returns the difference between N and the number of skips actually
10637 done.
10638
10639 If SILENT, don't output messages."
10640   (interactive "p")
10641   (let ((backward (< n 0))
10642         (n (abs n)))
10643     (while (and (> n 0)
10644                 (gnus-summary-go-to-next-thread backward))
10645       (decf n))
10646     (unless silent
10647       (gnus-summary-position-point))
10648     (when (and (not silent) (/= 0 n))
10649       (gnus-message 7 "No more threads"))
10650     n))
10651
10652 (defun gnus-summary-prev-thread (n)
10653   "Go to the same level previous N'th thread.
10654 Returns the difference between N and the number of skips actually
10655 done."
10656   (interactive "p")
10657   (gnus-summary-next-thread (- n)))
10658
10659 (defun gnus-summary-go-down-thread ()
10660   "Go down one level in the current thread."
10661   (let ((children (gnus-summary-article-children)))
10662     (when children
10663       (gnus-summary-goto-subject (car children)))))
10664
10665 (defun gnus-summary-go-up-thread ()
10666   "Go up one level in the current thread."
10667   (let ((parent (gnus-summary-article-parent)))
10668     (when parent
10669       (gnus-summary-goto-subject parent))))
10670
10671 (defun gnus-summary-down-thread (n)
10672   "Go down thread N steps.
10673 If N is negative, go up instead.
10674 Returns the difference between N and how many steps down that were
10675 taken."
10676   (interactive "p")
10677   (let ((up (< n 0))
10678         (n (abs n)))
10679     (while (and (> n 0)
10680                 (if up (gnus-summary-go-up-thread)
10681                   (gnus-summary-go-down-thread)))
10682       (setq n (1- n)))
10683     (gnus-summary-position-point)
10684     (when (/= 0 n)
10685       (gnus-message 7 "Can't go further"))
10686     n))
10687
10688 (defun gnus-summary-up-thread (n)
10689   "Go up thread N steps.
10690 If N is negative, go down instead.
10691 Returns the difference between N and how many steps down that were
10692 taken."
10693   (interactive "p")
10694   (gnus-summary-down-thread (- n)))
10695
10696 (defun gnus-summary-top-thread ()
10697   "Go to the top of the thread."
10698   (interactive)
10699   (while (gnus-summary-go-up-thread))
10700   (gnus-summary-article-number))
10701
10702 (defun gnus-summary-kill-thread (&optional unmark)
10703   "Mark articles under current thread as read.
10704 If the prefix argument is positive, remove any kinds of marks.
10705 If the prefix argument is negative, tick articles instead."
10706   (interactive "P")
10707   (when unmark
10708     (setq unmark (prefix-numeric-value unmark)))
10709   (let ((articles (gnus-summary-articles-in-thread)))
10710     (save-excursion
10711       ;; Expand the thread.
10712       (gnus-summary-show-thread)
10713       ;; Mark all the articles.
10714       (while articles
10715         (gnus-summary-goto-subject (car articles))
10716         (cond ((null unmark)
10717                (gnus-summary-mark-article-as-read gnus-killed-mark))
10718               ((> unmark 0)
10719                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10720               (t
10721                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10722         (setq articles (cdr articles))))
10723     ;; Hide killed subtrees.
10724     (and (null unmark)
10725          gnus-thread-hide-killed
10726          (gnus-summary-hide-thread))
10727     ;; If marked as read, go to next unread subject.
10728     (when (null unmark)
10729       ;; Go to next unread subject.
10730       (gnus-summary-next-subject 1 t)))
10731   (gnus-set-mode-line 'summary))
10732
10733 ;; Summary sorting commands
10734
10735 (defun gnus-summary-sort-by-number (&optional reverse)
10736   "Sort the summary buffer by article number.
10737 Argument REVERSE means reverse order."
10738   (interactive "P")
10739   (gnus-summary-sort 'number reverse))
10740
10741 (defun gnus-summary-sort-by-random (&optional reverse)
10742   "Randomize the order in the summary buffer.
10743 Argument REVERSE means to randomize in reverse order."
10744   (interactive "P")
10745   (gnus-summary-sort 'random reverse))
10746
10747 (defun gnus-summary-sort-by-author (&optional reverse)
10748   "Sort the summary buffer by author name alphabetically.
10749 If `case-fold-search' is non-nil, case of letters is ignored.
10750 Argument REVERSE means reverse order."
10751   (interactive "P")
10752   (gnus-summary-sort 'author reverse))
10753
10754 (defun gnus-summary-sort-by-subject (&optional reverse)
10755   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10756 If `case-fold-search' is non-nil, case of letters is ignored.
10757 Argument REVERSE means reverse order."
10758   (interactive "P")
10759   (gnus-summary-sort 'subject reverse))
10760
10761 (defun gnus-summary-sort-by-date (&optional reverse)
10762   "Sort the summary buffer by date.
10763 Argument REVERSE means reverse order."
10764   (interactive "P")
10765   (gnus-summary-sort 'date reverse))
10766
10767 (defun gnus-summary-sort-by-score (&optional reverse)
10768   "Sort the summary buffer by score.
10769 Argument REVERSE means reverse order."
10770   (interactive "P")
10771   (gnus-summary-sort 'score reverse))
10772
10773 (defun gnus-summary-sort-by-lines (&optional reverse)
10774   "Sort the summary buffer by the number of lines.
10775 Argument REVERSE means reverse order."
10776   (interactive "P")
10777   (gnus-summary-sort 'lines reverse))
10778
10779 (defun gnus-summary-sort-by-chars (&optional reverse)
10780   "Sort the summary buffer by article length.
10781 Argument REVERSE means reverse order."
10782   (interactive "P")
10783   (gnus-summary-sort 'chars reverse))
10784
10785 (defun gnus-summary-sort-by-original (&optional reverse)
10786   "Sort the summary buffer using the default sorting method.
10787 Argument REVERSE means reverse order."
10788   (interactive "P")
10789   (let* ((buffer-read-only)
10790          (gnus-summary-prepare-hook nil))
10791     ;; We do the sorting by regenerating the threads.
10792     (gnus-summary-prepare)
10793     ;; Hide subthreads if needed.
10794     (gnus-summary-maybe-hide-threads)))
10795
10796 (defun gnus-summary-sort (predicate reverse)
10797   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10798   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10799          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10800          (gnus-thread-sort-functions
10801           (if (not reverse)
10802               thread
10803             `(lambda (t1 t2)
10804                (,thread t2 t1))))
10805          (gnus-sort-gathered-threads-function
10806           gnus-thread-sort-functions)
10807          (gnus-article-sort-functions
10808           (if (not reverse)
10809               article
10810             `(lambda (t1 t2)
10811                (,article t2 t1))))
10812          (buffer-read-only)
10813          (gnus-summary-prepare-hook nil))
10814     ;; We do the sorting by regenerating the threads.
10815     (gnus-summary-prepare)
10816     ;; Hide subthreads if needed.
10817     (gnus-summary-maybe-hide-threads)))
10818
10819 ;; Summary saving commands.
10820
10821 (defun gnus-summary-save-article (&optional n not-saved)
10822   "Save the current article using the default saver function.
10823 If N is a positive number, save the N next articles.
10824 If N is a negative number, save the N previous articles.
10825 If N is nil and any articles have been marked with the process mark,
10826 save those articles instead.
10827 The variable `gnus-default-article-saver' specifies the saver function."
10828   (interactive "P")
10829   (let* ((articles (gnus-summary-work-articles n))
10830          (save-buffer (save-excursion
10831                         (nnheader-set-temp-buffer " *Gnus Save*")))
10832          (num (length articles))
10833          header file)
10834     (dolist (article articles)
10835       (setq header (gnus-summary-article-header article))
10836       (if (not (vectorp header))
10837           ;; This is a pseudo-article.
10838           (if (assq 'name header)
10839               (gnus-copy-file (cdr (assq 'name header)))
10840             (gnus-message 1 "Article %d is unsaveable" article))
10841         ;; This is a real article.
10842         (save-window-excursion
10843           (let ((gnus-display-mime-function nil)
10844                 (gnus-article-prepare-hook nil))
10845             (gnus-summary-select-article t nil nil article)))
10846         (save-excursion
10847           (set-buffer save-buffer)
10848           (erase-buffer)
10849           (insert-buffer-substring gnus-original-article-buffer))
10850         (setq file (gnus-article-save save-buffer file num))
10851         (gnus-summary-remove-process-mark article)
10852         (unless not-saved
10853           (gnus-summary-set-saved-mark article))))
10854     (gnus-kill-buffer save-buffer)
10855     (gnus-summary-position-point)
10856     (gnus-set-mode-line 'summary)
10857     n))
10858
10859 (defun gnus-summary-pipe-output (&optional arg headers)
10860   "Pipe the current article to a subprocess.
10861 If N is a positive number, pipe the N next articles.
10862 If N is a negative number, pipe the N previous articles.
10863 If N is nil and any articles have been marked with the process mark,
10864 pipe those articles instead.
10865 If HEADERS (the symbolic prefix), include the headers, too."
10866   (interactive (gnus-interactive "P\ny"))
10867   (require 'gnus-art)
10868   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10869         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10870     (gnus-summary-save-article arg t))
10871   (let ((buffer (get-buffer "*Shell Command Output*")))
10872     (when (and buffer
10873                (not (zerop (buffer-size buffer))))
10874       (gnus-configure-windows 'pipe))))
10875
10876 (defun gnus-summary-save-article-mail (&optional arg)
10877   "Append the current article to an mail file.
10878 If N is a positive number, save the N next articles.
10879 If N is a negative number, save the N previous articles.
10880 If N is nil and any articles have been marked with the process mark,
10881 save those articles instead."
10882   (interactive "P")
10883   (require 'gnus-art)
10884   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10885     (gnus-summary-save-article arg)))
10886
10887 (defun gnus-summary-save-article-rmail (&optional arg)
10888   "Append the current article to an rmail file.
10889 If N is a positive number, save the N next articles.
10890 If N is a negative number, save the N previous articles.
10891 If N is nil and any articles have been marked with the process mark,
10892 save those articles instead."
10893   (interactive "P")
10894   (require 'gnus-art)
10895   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10896     (gnus-summary-save-article arg)))
10897
10898 (defun gnus-summary-save-article-file (&optional arg)
10899   "Append the current article to a file.
10900 If N is a positive number, save the N next articles.
10901 If N is a negative number, save the N previous articles.
10902 If N is nil and any articles have been marked with the process mark,
10903 save those articles instead."
10904   (interactive "P")
10905   (require 'gnus-art)
10906   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10907     (gnus-summary-save-article arg)))
10908
10909 (defun gnus-summary-write-article-file (&optional arg)
10910   "Write the current article to a file, deleting the previous file.
10911 If N is a positive number, save the N next articles.
10912 If N is a negative number, save the N previous articles.
10913 If N is nil and any articles have been marked with the process mark,
10914 save those articles instead."
10915   (interactive "P")
10916   (require 'gnus-art)
10917   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10918     (gnus-summary-save-article arg)))
10919
10920 (defun gnus-summary-save-article-body-file (&optional arg)
10921   "Append the current article body to a file.
10922 If N is a positive number, save the N next articles.
10923 If N is a negative number, save the N previous articles.
10924 If N is nil and any articles have been marked with the process mark,
10925 save those articles instead."
10926   (interactive "P")
10927   (require 'gnus-art)
10928   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10929     (gnus-summary-save-article arg)))
10930
10931 (defun gnus-summary-muttprint (&optional arg)
10932   "Print the current article using Muttprint.
10933 If N is a positive number, save the N next articles.
10934 If N is a negative number, save the N previous articles.
10935 If N is nil and any articles have been marked with the process mark,
10936 save those articles instead."
10937   (interactive "P")
10938   (require 'gnus-art)
10939   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10940     (gnus-summary-save-article arg t)))
10941
10942 (defun gnus-summary-pipe-message (program)
10943   "Pipe the current article through PROGRAM."
10944   (interactive "sProgram: ")
10945   (gnus-summary-select-article)
10946   (let ((mail-header-separator ""))
10947     (gnus-eval-in-buffer-window gnus-article-buffer
10948       (save-restriction
10949         (widen)
10950         (let ((start (window-start))
10951               buffer-read-only)
10952           (message-pipe-buffer-body program)
10953           (set-window-start (get-buffer-window (current-buffer)) start))))))
10954
10955 (defun gnus-get-split-value (methods)
10956   "Return a value based on the split METHODS."
10957   (let (split-name method result match)
10958     (when methods
10959       (save-excursion
10960         (set-buffer gnus-original-article-buffer)
10961         (save-restriction
10962           (nnheader-narrow-to-headers)
10963           (while (and methods (not split-name))
10964             (goto-char (point-min))
10965             (setq method (pop methods))
10966             (setq match (car method))
10967             (when (cond
10968                    ((stringp match)
10969                     ;; Regular expression.
10970                     (ignore-errors
10971                       (re-search-forward match nil t)))
10972                    ((functionp match)
10973                     ;; Function.
10974                     (save-restriction
10975                       (widen)
10976                       (setq result (funcall match gnus-newsgroup-name))))
10977                    ((consp match)
10978                     ;; Form.
10979                     (save-restriction
10980                       (widen)
10981                       (setq result (eval match)))))
10982               (setq split-name (cdr method))
10983               (cond ((stringp result)
10984                      (push (expand-file-name
10985                             result gnus-article-save-directory)
10986                            split-name))
10987                     ((consp result)
10988                      (setq split-name (append result split-name)))))))))
10989     (nreverse split-name)))
10990
10991 (defun gnus-valid-move-group-p (group)
10992   (and (boundp group)
10993        (symbol-name group)
10994        (symbol-value group)
10995        (gnus-get-function (gnus-find-method-for-group
10996                            (symbol-name group)) 'request-accept-article t)))
10997
10998 (defun gnus-read-move-group-name (prompt default articles prefix)
10999   "Read a group name."
11000   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11001          (minibuffer-confirm-incomplete nil) ; XEmacs
11002          (prom
11003           (format "%s %s to:"
11004                   prompt
11005                   (if (> (length articles) 1)
11006                       (format "these %d articles" (length articles))
11007                     "this article")))
11008          (to-newsgroup
11009           (cond
11010            ((null split-name)
11011             (gnus-completing-read-with-default
11012              default prom
11013              gnus-active-hashtb
11014              'gnus-valid-move-group-p
11015              nil prefix
11016              'gnus-group-history))
11017            ((= 1 (length split-name))
11018             (gnus-completing-read-with-default
11019              (car split-name) prom
11020              gnus-active-hashtb
11021              'gnus-valid-move-group-p
11022              nil nil
11023              'gnus-group-history))
11024            (t
11025             (gnus-completing-read-with-default
11026              nil prom
11027              (mapcar (lambda (el) (list el))
11028                      (nreverse split-name))
11029              nil nil nil
11030              'gnus-group-history))))
11031          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11032     (when to-newsgroup
11033       (if (or (string= to-newsgroup "")
11034               (string= to-newsgroup prefix))
11035           (setq to-newsgroup default))
11036       (unless to-newsgroup
11037         (error "No group name entered"))
11038       (or (gnus-active to-newsgroup)
11039           (gnus-activate-group to-newsgroup nil nil to-method)
11040           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11041                                      to-newsgroup))
11042               (or (and (gnus-request-create-group to-newsgroup to-method)
11043                        (gnus-activate-group
11044                         to-newsgroup nil nil to-method)
11045                        (gnus-subscribe-group to-newsgroup))
11046                   (error "Couldn't create group %s" to-newsgroup)))
11047           (error "No such group: %s" to-newsgroup)))
11048     to-newsgroup))
11049
11050 (defun gnus-summary-save-parts (type dir n &optional reverse)
11051   "Save parts matching TYPE to DIR.
11052 If REVERSE, save parts that do not match TYPE."
11053   (interactive
11054    (list (read-string "Save parts of type: "
11055                       (or (car gnus-summary-save-parts-type-history)
11056                           gnus-summary-save-parts-default-mime)
11057                       'gnus-summary-save-parts-type-history)
11058          (setq gnus-summary-save-parts-last-directory
11059                (read-file-name "Save to directory: "
11060                                gnus-summary-save-parts-last-directory
11061                                nil t))
11062          current-prefix-arg))
11063   (gnus-summary-iterate n
11064     (let ((gnus-display-mime-function nil)
11065           (gnus-inhibit-treatment t))
11066       (gnus-summary-select-article))
11067     (save-excursion
11068       (set-buffer gnus-article-buffer)
11069       (let ((handles (or gnus-article-mime-handles
11070                          (mm-dissect-buffer nil gnus-article-loose-mime)
11071                          (and gnus-article-emulate-mime
11072                               (mm-uu-dissect)))))
11073         (when handles
11074           (gnus-summary-save-parts-1 type dir handles reverse)
11075           (unless gnus-article-mime-handles ;; Don't destroy this case.
11076             (mm-destroy-parts handles)))))))
11077
11078 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11079   (if (stringp (car handle))
11080       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11081               (cdr handle))
11082     (when (if reverse
11083               (not (string-match type (mm-handle-media-type handle)))
11084             (string-match type (mm-handle-media-type handle)))
11085       (let ((file (expand-file-name
11086                    (gnus-map-function
11087                     mm-file-name-rewrite-functions
11088                     (file-name-nondirectory
11089                      (or
11090                       (mail-content-type-get
11091                        (mm-handle-disposition handle) 'filename)
11092                       (mail-content-type-get
11093                        (mm-handle-type handle) 'name)
11094                       (concat gnus-newsgroup-name
11095                               "." (number-to-string
11096                                    (cdr gnus-article-current))))))
11097                    dir)))
11098         (unless (file-exists-p file)
11099           (mm-save-part-to-file handle file))))))
11100
11101 ;; Summary extract commands
11102
11103 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11104   (let ((buffer-read-only nil)
11105         (article (gnus-summary-article-number))
11106         after-article b e)
11107     (unless (gnus-summary-goto-subject article)
11108       (error "No such article: %d" article))
11109     (gnus-summary-position-point)
11110     ;; If all commands are to be bunched up on one line, we collect
11111     ;; them here.
11112     (unless gnus-view-pseudos-separately
11113       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11114             files action)
11115         (while ps
11116           (setq action (cdr (assq 'action (car ps))))
11117           (setq files (list (cdr (assq 'name (car ps)))))
11118           (while (and ps (cdr ps)
11119                       (string= (or action "1")
11120                                (or (cdr (assq 'action (cadr ps))) "2")))
11121             (push (cdr (assq 'name (cadr ps))) files)
11122             (setcdr ps (cddr ps)))
11123           (when files
11124             (when (not (string-match "%s" action))
11125               (push " " files))
11126             (push " " files)
11127             (when (assq 'execute (car ps))
11128               (setcdr (assq 'execute (car ps))
11129                       (funcall (if (string-match "%s" action)
11130                                    'format 'concat)
11131                                action
11132                                (mapconcat
11133                                 (lambda (f)
11134                                   (if (equal f " ")
11135                                       f
11136                                     (shell-quote-argument f)))
11137                                 files " ")))))
11138           (setq ps (cdr ps)))))
11139     (if (and gnus-view-pseudos (not not-view))
11140         (while pslist
11141           (when (assq 'execute (car pslist))
11142             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11143                                   (eq gnus-view-pseudos 'not-confirm)))
11144           (setq pslist (cdr pslist)))
11145       (save-excursion
11146         (while pslist
11147           (setq after-article (or (cdr (assq 'article (car pslist)))
11148                                   (gnus-summary-article-number)))
11149           (gnus-summary-goto-subject after-article)
11150           (forward-line 1)
11151           (setq b (point))
11152           (insert "    " (file-name-nondirectory
11153                           (cdr (assq 'name (car pslist))))
11154                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11155           (setq e (point))
11156           (forward-line -1)             ; back to `b'
11157           (gnus-add-text-properties
11158            b (1- e) (list 'gnus-number gnus-reffed-article-number
11159                           gnus-mouse-face-prop gnus-mouse-face))
11160           (gnus-data-enter
11161            after-article gnus-reffed-article-number
11162            gnus-unread-mark b (car pslist) 0 (- e b))
11163           (setq gnus-newsgroup-unreads
11164                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11165                                          gnus-reffed-article-number))
11166           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11167           (setq pslist (cdr pslist)))))))
11168
11169 (defun gnus-pseudos< (p1 p2)
11170   (let ((c1 (cdr (assq 'action p1)))
11171         (c2 (cdr (assq 'action p2))))
11172     (and c1 c2 (string< c1 c2))))
11173
11174 (defun gnus-request-pseudo-article (props)
11175   (cond ((assq 'execute props)
11176          (gnus-execute-command (cdr (assq 'execute props)))))
11177   (let ((gnus-current-article (gnus-summary-article-number)))
11178     (gnus-run-hooks 'gnus-mark-article-hook)))
11179
11180 (defun gnus-execute-command (command &optional automatic)
11181   (save-excursion
11182     (gnus-article-setup-buffer)
11183     (set-buffer gnus-article-buffer)
11184     (setq buffer-read-only nil)
11185     (let ((command (if automatic command
11186                      (read-string "Command: " (cons command 0)))))
11187       (erase-buffer)
11188       (insert "$ " command "\n\n")
11189       (if gnus-view-pseudo-asynchronously
11190           (start-process "gnus-execute" (current-buffer) shell-file-name
11191                          shell-command-switch command)
11192         (call-process shell-file-name nil t nil
11193                       shell-command-switch command)))))
11194
11195 ;; Summary kill commands.
11196
11197 (defun gnus-summary-edit-global-kill (article)
11198   "Edit the \"global\" kill file."
11199   (interactive (list (gnus-summary-article-number)))
11200   (gnus-group-edit-global-kill article))
11201
11202 (defun gnus-summary-edit-local-kill ()
11203   "Edit a local kill file applied to the current newsgroup."
11204   (interactive)
11205   (setq gnus-current-headers (gnus-summary-article-header))
11206   (gnus-group-edit-local-kill
11207    (gnus-summary-article-number) gnus-newsgroup-name))
11208
11209 ;;; Header reading.
11210
11211 (defun gnus-read-header (id &optional header)
11212   "Read the headers of article ID and enter them into the Gnus system."
11213   (let ((group gnus-newsgroup-name)
11214         (gnus-override-method
11215          (or
11216           gnus-override-method
11217           (and (gnus-news-group-p gnus-newsgroup-name)
11218                (car (gnus-refer-article-methods)))))
11219         where)
11220     ;; First we check to see whether the header in question is already
11221     ;; fetched.
11222     (if (stringp id)
11223         ;; This is a Message-ID.
11224         (setq header (or header (gnus-id-to-header id)))
11225       ;; This is an article number.
11226       (setq header (or header (gnus-summary-article-header id))))
11227     (if (and header
11228              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11229         ;; We have found the header.
11230         header
11231       ;; If this is a sparse article, we have to nix out its
11232       ;; previous entry in the thread hashtb.
11233       (when (and header
11234                  (gnus-summary-article-sparse-p (mail-header-number header)))
11235         (let* ((parent (gnus-parent-id (mail-header-references header)))
11236                (thread (and parent (gnus-id-to-thread parent))))
11237           (when thread
11238             (delq (assq header thread) thread))))
11239       ;; We have to really fetch the header to this article.
11240       (save-excursion
11241         (set-buffer nntp-server-buffer)
11242         (when (setq where (gnus-request-head id group))
11243           (nnheader-fold-continuation-lines)
11244           (goto-char (point-max))
11245           (insert ".\n")
11246           (goto-char (point-min))
11247           (insert "211 ")
11248           (princ (cond
11249                   ((numberp id) id)
11250                   ((cdr where) (cdr where))
11251                   (header (mail-header-number header))
11252                   (t gnus-reffed-article-number))
11253                  (current-buffer))
11254           (insert " Article retrieved.\n"))
11255         (if (or (not where)
11256                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11257             ()                          ; Malformed head.
11258           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11259             (when (and (stringp id)
11260                        (not (string= (gnus-group-real-name group)
11261                                      (car where))))
11262               ;; If we fetched by Message-ID and the article came
11263               ;; from a different group, we fudge some bogus article
11264               ;; numbers for this article.
11265               (mail-header-set-number header gnus-reffed-article-number))
11266             (save-excursion
11267               (set-buffer gnus-summary-buffer)
11268               (decf gnus-reffed-article-number)
11269               (gnus-remove-header (mail-header-number header))
11270               (push header gnus-newsgroup-headers)
11271               (setq gnus-current-headers header)
11272               (push (mail-header-number header) gnus-newsgroup-limit)))
11273           header)))))
11274
11275 (defun gnus-remove-header (number)
11276   "Remove header NUMBER from `gnus-newsgroup-headers'."
11277   (if (and gnus-newsgroup-headers
11278            (= number (mail-header-number (car gnus-newsgroup-headers))))
11279       (pop gnus-newsgroup-headers)
11280     (let ((headers gnus-newsgroup-headers))
11281       (while (and (cdr headers)
11282                   (not (= number (mail-header-number (cadr headers)))))
11283         (pop headers))
11284       (when (cdr headers)
11285         (setcdr headers (cddr headers))))))
11286
11287 ;;;
11288 ;;; summary highlights
11289 ;;;
11290
11291 (defun gnus-highlight-selected-summary ()
11292   "Highlight selected article in summary buffer."
11293   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11294   (when gnus-summary-selected-face
11295     (save-excursion
11296       (let* ((beg (point-at-bol))
11297              (end (point-at-eol))
11298              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11299              (from (if (get-text-property beg gnus-mouse-face-prop)
11300                        beg
11301                      (or (next-single-property-change
11302                           beg gnus-mouse-face-prop nil end)
11303                          beg)))
11304              (to
11305               (if (= from end)
11306                   (- from 2)
11307                 (or (next-single-property-change
11308                      from gnus-mouse-face-prop nil end)
11309                     end))))
11310         ;; If no mouse-face prop on line we will have to = from = end,
11311         ;; so we highlight the entire line instead.
11312         (when (= (+ to 2) from)
11313           (setq from beg)
11314           (setq to end))
11315         (if gnus-newsgroup-selected-overlay
11316             ;; Move old overlay.
11317             (gnus-move-overlay
11318              gnus-newsgroup-selected-overlay from to (current-buffer))
11319           ;; Create new overlay.
11320           (gnus-overlay-put
11321            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11322            'face gnus-summary-selected-face))))))
11323
11324 (defvar gnus-summary-highlight-line-cached nil)
11325 (defvar gnus-summary-highlight-line-trigger nil)
11326
11327 (defun gnus-summary-highlight-line-0 ()
11328   (if (and (eq gnus-summary-highlight-line-trigger
11329                gnus-summary-highlight)
11330            gnus-summary-highlight-line-cached)
11331       gnus-summary-highlight-line-cached
11332     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11333           gnus-summary-highlight-line-cached
11334           (let* ((cond (list 'cond))
11335                  (c cond)
11336                  (list gnus-summary-highlight))
11337             (while list
11338               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11339                               nil))
11340               (setq c (cdr c)
11341                     list (cdr list)))
11342             (gnus-byte-compile (list 'lambda nil cond))))))
11343
11344 (defun gnus-summary-highlight-line ()
11345   "Highlight current line according to `gnus-summary-highlight'."
11346   (let* ((beg (point-at-bol))
11347          (article (or (gnus-summary-article-number) gnus-current-article))
11348          (score (or (cdr (assq article
11349                                gnus-newsgroup-scored))
11350                     gnus-summary-default-score 0))
11351          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11352          (inhibit-read-only t)
11353          (default gnus-summary-default-score)
11354          (default-high gnus-summary-default-high-score)
11355          (default-low gnus-summary-default-low-score)
11356          (uncached (and gnus-summary-use-undownloaded-faces
11357                         (memq article gnus-newsgroup-undownloaded))))
11358     (let ((face (funcall (gnus-summary-highlight-line-0))))
11359       (unless (eq face (get-text-property beg 'face))
11360         (gnus-put-text-property-excluding-characters-with-faces
11361          beg (point-at-eol) 'face
11362          (setq face (if (boundp face) (symbol-value face) face)))
11363         (when gnus-summary-highlight-line-function
11364           (funcall gnus-summary-highlight-line-function article face))))))
11365
11366 (defun gnus-update-read-articles (group unread &optional compute)
11367   "Update the list of read articles in GROUP.
11368 UNREAD is a sorted list."
11369   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11370          (entry (gnus-gethash group gnus-newsrc-hashtb))
11371          (info (nth 2 entry))
11372          (prev 1)
11373          read)
11374     (if (or (not info) (not active))
11375         ;; There is no info on this group if it was, in fact,
11376         ;; killed.  Gnus stores no information on killed groups, so
11377         ;; there's nothing to be done.
11378         ;; One could store the information somewhere temporarily,
11379         ;; perhaps...  Hmmm...
11380         ()
11381       ;; Remove any negative articles numbers.
11382       (while (and unread (< (car unread) 0))
11383         (setq unread (cdr unread)))
11384       ;; Remove any expired article numbers
11385       (while (and unread (< (car unread) (car active)))
11386         (setq unread (cdr unread)))
11387       ;; Compute the ranges of read articles by looking at the list of
11388       ;; unread articles.
11389       (while unread
11390         (when (/= (car unread) prev)
11391           (push (if (= prev (1- (car unread))) prev
11392                   (cons prev (1- (car unread))))
11393                 read))
11394         (setq prev (1+ (car unread)))
11395         (setq unread (cdr unread)))
11396       (when (<= prev (cdr active))
11397         (push (cons prev (cdr active)) read))
11398       (setq read (if (> (length read) 1) (nreverse read) read))
11399       (if compute
11400           read
11401         (save-excursion
11402           (let (setmarkundo)
11403             ;; Propagate the read marks to the backend.
11404             (when (gnus-check-backend-function 'request-set-mark group)
11405               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11406                     (add (gnus-remove-from-range read (gnus-info-read info))))
11407                 (when (or add del)
11408                   (unless (gnus-check-group group)
11409                     (error "Can't open server for %s" group))
11410                   (gnus-request-set-mark
11411                    group (delq nil (list (if add (list add 'add '(read)))
11412                                          (if del (list del 'del '(read))))))
11413                   (setq setmarkundo
11414                         `(gnus-request-set-mark
11415                           ,group
11416                           ',(delq nil (list
11417                                        (if del (list del 'add '(read)))
11418                                        (if add (list add 'del '(read))))))))))
11419             (set-buffer gnus-group-buffer)
11420             (gnus-undo-register
11421               `(progn
11422                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11423                  (gnus-info-set-read ',info ',(gnus-info-read info))
11424                  (gnus-get-unread-articles-in-group ',info
11425                                                     (gnus-active ,group))
11426                  (gnus-group-update-group ,group t)
11427                  ,setmarkundo))))
11428         ;; Enter this list into the group info.
11429         (gnus-info-set-read info read)
11430         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11431         (gnus-get-unread-articles-in-group info (gnus-active group))
11432         t))))
11433
11434 (defun gnus-offer-save-summaries ()
11435   "Offer to save all active summary buffers."
11436   (let (buffers)
11437     ;; Go through all buffers and find all summaries.
11438     (dolist (buffer (buffer-list))
11439       (when (and (setq buffer (buffer-name buffer))
11440                  (string-match "Summary" buffer)
11441                  (save-excursion
11442                    (set-buffer buffer)
11443                    ;; We check that this is, indeed, a summary buffer.
11444                    (and (eq major-mode 'gnus-summary-mode)
11445                         ;; Also make sure this isn't bogus.
11446                         gnus-newsgroup-prepared
11447                         ;; Also make sure that this isn't a
11448                         ;; dead summary buffer.
11449                         (not gnus-dead-summary-mode))))
11450         (push buffer buffers)))
11451     ;; Go through all these summary buffers and offer to save them.
11452     (when buffers
11453       (save-excursion
11454         (map-y-or-n-p
11455          "Update summary buffer %s? "
11456          (lambda (buf)
11457            (switch-to-buffer buf)
11458            (gnus-summary-exit))
11459          buffers)))))
11460
11461
11462 ;;; @ for mime-partial
11463 ;;;
11464
11465 (defun gnus-request-partial-message ()
11466   (save-excursion
11467     (let ((number (gnus-summary-article-number))
11468           (group gnus-newsgroup-name)
11469           (mother gnus-article-buffer))
11470       (set-buffer (get-buffer-create " *Partial Article*"))
11471       (erase-buffer)
11472       (setq mime-preview-buffer mother)
11473       (gnus-request-article-this-buffer number group)
11474       (mime-parse-buffer)
11475       )))
11476
11477 (autoload 'mime-combine-message/partial-pieces-automatically
11478   "mime-partial"
11479   "Internal method to combine message/partial messages automatically.")
11480
11481 (mime-add-condition
11482  'action '((type . message)(subtype . partial)
11483            (major-mode . gnus-original-article-mode)
11484            (method . mime-combine-message/partial-pieces-automatically)
11485            (summary-buffer-exp . gnus-summary-buffer)
11486            (request-partial-message-method . gnus-request-partial-message)
11487            ))
11488
11489
11490 ;;; @ for message/rfc822
11491 ;;;
11492
11493 (defun gnus-mime-extract-message/rfc822 (entity situation)
11494   "Burst a forwarded article."
11495   (save-excursion
11496     (set-buffer gnus-summary-buffer)
11497     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11498                                    gnus-newsgroup-name 'gnus-group-history))
11499            (gnus-group-marked (list group))
11500            article info)
11501       (with-temp-buffer
11502         (mime-insert-entity-content entity)
11503         (setq article (gnus-request-accept-article group)))
11504       (when (and (consp article)
11505                  (numberp (setq article (cdr article))))
11506         (setq info (gnus-get-info group))
11507         (gnus-info-set-read info
11508                             (gnus-remove-from-range (gnus-info-read info)
11509                                                     (list article)))
11510         (when (string-equal group gnus-newsgroup-name)
11511           (forward-line 1)
11512           (let (gnus-show-threads)
11513             (gnus-summary-goto-subject article t))
11514           (gnus-summary-clear-mark-forward 1))
11515         (set-buffer gnus-group-buffer)
11516         (gnus-group-get-new-news-this-group nil t)))))
11517
11518 (mime-add-condition
11519  'action '((type . message)(subtype . rfc822)
11520            (major-mode . gnus-original-article-mode)
11521            (method . gnus-mime-extract-message/rfc822)
11522            (mode . "extract")
11523            ))
11524
11525 (mime-add-condition
11526  'action '((type . message)(subtype . news)
11527            (major-mode . gnus-original-article-mode)
11528            (method . gnus-mime-extract-message/rfc822)
11529            (mode . "extract")
11530            ))
11531
11532 (defun gnus-mime-extract-multipart (entity situation)
11533   (let ((children (mime-entity-children entity))
11534         mime-acting-situation-to-override
11535         f)
11536     (while children
11537       (mime-play-entity (car children)
11538                         (cons (assq 'mode situation)
11539                               mime-acting-situation-to-override))
11540       (setq children (cdr children)))
11541     (if (setq f (cdr (assq 'after-method
11542                            mime-acting-situation-to-override)))
11543         (eval f)
11544       )))
11545
11546 (mime-add-condition
11547  'action '((type . multipart)
11548            (method . gnus-mime-extract-multipart)
11549            (mode . "extract")
11550            )
11551  'with-default)
11552
11553
11554 ;;; @ end
11555 ;;;
11556
11557 (defun gnus-summary-inherit-default-charset ()
11558   "Import `default-mime-charset' from summary buffer.
11559 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11560 of FLIM is used."
11561   (if (buffer-live-p gnus-summary-buffer)
11562       (let (d-m-c d-m-c-u)
11563         (with-current-buffer gnus-summary-buffer
11564           (setq d-m-c (if (local-variable-p 'default-mime-charset
11565                                             gnus-summary-buffer)
11566                           default-mime-charset
11567                         t)
11568                 ;; LIMIT
11569                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11570                                               gnus-summary-buffer)
11571                             (symbol-value 'default-mime-charset-unlimited)
11572                           t)))
11573         (if (eq t d-m-c)
11574             (kill-local-variable 'default-mime-charset)
11575           (set (make-local-variable 'default-mime-charset) d-m-c))
11576         (if (eq t d-m-c-u)
11577             (kill-local-variable 'default-mime-charset-unlimited)
11578           (set (make-local-variable 'default-mime-charset-unlimited)
11579                d-m-c-u)))))
11580
11581 (defun gnus-summary-setup-default-charset ()
11582   "Setup newsgroup default charset."
11583   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11584       (progn
11585         (setq gnus-newsgroup-charset nil)
11586         (set (make-local-variable 'default-mime-charset) nil)
11587         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11588           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11589     (let ((ignored-charsets
11590            (or gnus-newsgroup-ephemeral-ignored-charsets
11591                (append
11592                 (and gnus-newsgroup-name
11593                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11594                 gnus-newsgroup-ignored-charsets)))
11595           charset)
11596       (setq gnus-newsgroup-charset
11597             (or gnus-newsgroup-ephemeral-charset
11598                 (when (and gnus-newsgroup-name
11599                            (setq charset (gnus-parameter-charset
11600                                           gnus-newsgroup-name)))
11601                   (make-local-variable 'default-mime-charset)
11602                   (setq default-mime-charset charset))
11603                 gnus-default-charset))
11604       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11605            ignored-charsets))))
11606
11607 ;;;
11608 ;;; Mime Commands
11609 ;;;
11610
11611 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11612   "Display the current article buffer fully MIME-buttonized.
11613 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11614 treated as multipart/mixed."
11615   (interactive "P")
11616   (require 'gnus-art)
11617   (let ((gnus-unbuttonized-mime-types nil)
11618         (gnus-mime-display-multipart-as-mixed show-all-parts))
11619     (gnus-summary-show-article)))
11620
11621 (defun gnus-summary-repair-multipart (article)
11622   "Add a Content-Type header to a multipart article without one."
11623   (interactive (list (gnus-summary-article-number)))
11624   (gnus-with-article article
11625     (message-narrow-to-head)
11626     (message-remove-header "Mime-Version")
11627     (goto-char (point-max))
11628     (insert "Mime-Version: 1.0\n")
11629     (widen)
11630     (when (search-forward "\n--" nil t)
11631       (let ((separator (buffer-substring (point) (point-at-eol))))
11632         (message-narrow-to-head)
11633         (message-remove-header "Content-Type")
11634         (goto-char (point-max))
11635         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11636                         separator))
11637         (widen))))
11638   (let (gnus-mark-article-hook)
11639     (gnus-summary-select-article t t nil article)))
11640
11641 (defun gnus-summary-toggle-display-buttonized ()
11642   "Toggle the buttonizing of the article buffer."
11643   (interactive)
11644   (require 'gnus-art)
11645   (if (setq gnus-inhibit-mime-unbuttonizing
11646             (not gnus-inhibit-mime-unbuttonizing))
11647       (let ((gnus-unbuttonized-mime-types nil))
11648         (gnus-summary-show-article))
11649     (gnus-summary-show-article)))
11650
11651 ;;;
11652 ;;; Intelli-mouse commmands
11653 ;;;
11654
11655 (defun gnus-wheel-summary-scroll (event)
11656   (interactive "e")
11657   (let ((amount (if (memq 'shift (event-modifiers event))
11658                     (car gnus-wheel-scroll-amount)
11659                   (cdr gnus-wheel-scroll-amount)))
11660         (direction (- (* (static-if (featurep 'xemacs)
11661                              (event-button event)
11662                            (cond ((eq 'mouse-4 (event-basic-type event))
11663                                   4)
11664                                  ((eq 'mouse-5 (event-basic-type event))
11665                                   5)))
11666                          2) 9))
11667         edge)
11668     (gnus-summary-scroll-up (* amount direction))
11669     (when (gnus-eval-in-buffer-window gnus-article-buffer
11670             (save-restriction
11671               (widen)
11672               (and (if (< 0 direction)
11673                        (gnus-article-next-page 0)
11674                      (gnus-article-prev-page 0)
11675                      (bobp))
11676                    (if (setq edge (get-text-property
11677                                    (point-min) 'gnus-wheel-edge))
11678                        (setq edge (* edge direction))
11679                      (setq edge -1))
11680                    (or (plusp edge)
11681                        (let ((buffer-read-only nil)
11682                              (inhibit-read-only t))
11683                          (put-text-property (point-min) (point-max)
11684                                             'gnus-wheel-edge direction)
11685                          nil))
11686                    (or (> edge gnus-wheel-edge-resistance)
11687                        (let ((buffer-read-only nil)
11688                              (inhibit-read-only t))
11689                          (put-text-property (point-min) (point-max)
11690                                             'gnus-wheel-edge
11691                                             (* (1+ edge) direction))
11692                          nil))
11693                    (eq last-command 'gnus-wheel-summary-scroll))))
11694       (gnus-summary-next-article nil nil (minusp direction)))))
11695
11696 (defun gnus-wheel-install ()
11697   "Enable mouse wheel support on summary window."
11698   (when gnus-use-wheel
11699     (let ((keys
11700            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11701       (dolist (key keys)
11702         (define-key gnus-summary-mode-map key
11703           'gnus-wheel-summary-scroll)))))
11704
11705 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11706
11707 ;;;
11708 ;;; Traditional PGP commmands
11709 ;;;
11710
11711 (defun gnus-summary-decrypt-article (&optional force)
11712   "Decrypt the current article in traditional PGP way.
11713 This will have permanent effect only in mail groups.
11714 If FORCE is non-nil, allow editing of articles even in read-only
11715 groups."
11716   (interactive "P")
11717   (gnus-summary-select-article t)
11718   (gnus-eval-in-buffer-window gnus-article-buffer
11719     (save-excursion
11720       (save-restriction
11721         (widen)
11722         (goto-char (point-min))
11723         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11724           (error "Not a traditional PGP message!"))
11725         (let ((armor-start (match-beginning 0)))
11726           (if (and (pgg-decrypt-region armor-start (point-max))
11727                    (or force (not (gnus-group-read-only-p))))
11728               (let ((inhibit-read-only t)
11729                     buffer-read-only)
11730                 (delete-region armor-start
11731                                (progn
11732                                  (re-search-forward "^-+END PGP" nil t)
11733                                  (beginning-of-line 2)
11734                                  (point)))
11735                 (insert-buffer-substring pgg-output-buffer))))))))
11736
11737 (defun gnus-summary-verify-article ()
11738   "Verify the current article in traditional PGP way."
11739   (interactive)
11740   (save-excursion
11741     (set-buffer gnus-original-article-buffer)
11742     (goto-char (point-min))
11743     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11744       (error "Not a traditional PGP message!"))
11745     (re-search-forward "^-+END PGP" nil t)
11746     (beginning-of-line 2)
11747     (call-interactively (function pgg-verify-region))))
11748
11749 ;;;
11750 ;;; Generic summary marking commands
11751 ;;;
11752
11753 (defvar gnus-summary-marking-alist
11754   '((read gnus-del-mark "d")
11755     (unread gnus-unread-mark "u")
11756     (ticked gnus-ticked-mark "!")
11757     (dormant gnus-dormant-mark "?")
11758     (expirable gnus-expirable-mark "e"))
11759   "An alist of names/marks/keystrokes.")
11760
11761 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11762 (defvar gnus-summary-mark-map)
11763
11764 (defun gnus-summary-make-all-marking-commands ()
11765   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11766   (dolist (elem gnus-summary-marking-alist)
11767     (apply 'gnus-summary-make-marking-command elem)))
11768
11769 (defun gnus-summary-make-marking-command (name mark keystroke)
11770   (let ((map (make-sparse-keymap)))
11771     (define-key gnus-summary-generic-mark-map keystroke map)
11772     (dolist (lway `((next "next" next nil "n")
11773                     (next-unread "next unread" next t "N")
11774                     (prev "previous" prev nil "p")
11775                     (prev-unread "previous unread" prev t "P")
11776                     (nomove "" nil nil ,keystroke)))
11777       (let ((func (gnus-summary-make-marking-command-1
11778                    mark (car lway) lway name)))
11779         (setq func (eval func))
11780         (define-key map (nth 4 lway) func)))))
11781
11782 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11783   `(defun ,(intern
11784             (format "gnus-summary-put-mark-as-%s%s"
11785                     name (if (eq way 'nomove)
11786                              ""
11787                            (concat "-" (symbol-name way)))))
11788      (n)
11789      ,(format
11790        "Mark the current article as %s%s.
11791 If N, the prefix, then repeat N times.
11792 If N is negative, move in reverse order.
11793 The difference between N and the actual number of articles marked is
11794 returned."
11795        name (car (cdr lway)))
11796      (interactive "p")
11797      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11798
11799 (defun gnus-summary-generic-mark (n mark move unread)
11800   "Mark N articles with MARK."
11801   (unless (eq major-mode 'gnus-summary-mode)
11802     (error "This command can only be used in the summary buffer"))
11803   (gnus-summary-show-thread)
11804   (let ((nummove
11805          (cond
11806           ((eq move 'next) 1)
11807           ((eq move 'prev) -1)
11808           (t 0))))
11809     (if (zerop nummove)
11810         (setq n 1)
11811       (when (< n 0)
11812         (setq n (abs n)
11813               nummove (* -1 nummove))))
11814     (while (and (> n 0)
11815                 (gnus-summary-mark-article nil mark)
11816                 (zerop (gnus-summary-next-subject nummove unread t)))
11817       (setq n (1- n)))
11818     (when (/= 0 n)
11819       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11820     (gnus-summary-recenter)
11821     (gnus-summary-position-point)
11822     (gnus-set-mode-line 'summary)
11823     n))
11824
11825 (defun gnus-summary-insert-articles (articles)
11826   (when (setq articles
11827               (gnus-sorted-difference articles
11828                                       (mapcar (lambda (h)
11829                                                 (mail-header-number h))
11830                                               gnus-newsgroup-headers)))
11831     (setq gnus-newsgroup-headers
11832           (gnus-merge 'list
11833                       gnus-newsgroup-headers
11834                       (gnus-fetch-headers articles)
11835                       'gnus-article-sort-by-number))
11836     ;; Suppress duplicates?
11837     (when gnus-suppress-duplicates
11838       (gnus-dup-suppress-articles))
11839
11840     ;; We might want to build some more threads first.
11841     (when (and gnus-fetch-old-headers
11842                (eq gnus-headers-retrieved-by 'nov))
11843       (if (eq gnus-fetch-old-headers 'invisible)
11844           (gnus-build-all-threads)
11845         (gnus-build-old-threads)))
11846     ;; Let the Gnus agent mark articles as read.
11847     (when gnus-agent
11848       (gnus-agent-get-undownloaded-list))
11849     ;; Remove list identifiers from subject
11850     (when gnus-list-identifiers
11851       (gnus-summary-remove-list-identifiers))
11852     ;; First and last article in this newsgroup.
11853     (when gnus-newsgroup-headers
11854       (setq gnus-newsgroup-begin
11855             (mail-header-number (car gnus-newsgroup-headers))
11856             gnus-newsgroup-end
11857             (mail-header-number
11858              (gnus-last-element gnus-newsgroup-headers))))
11859     (when gnus-use-scoring
11860       (gnus-possibly-score-headers))))
11861
11862 (defun gnus-summary-insert-old-articles (&optional all)
11863   "Insert all old articles in this group.
11864 If ALL is non-nil, already read articles become readable.
11865 If ALL is a number, fetch this number of articles."
11866   (interactive "P")
11867   (prog1
11868       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11869             older len)
11870         (setq older
11871               ;; Some nntp servers lie about their active range.  When
11872               ;; this happens, the active range can be in the millions.
11873               ;; Use a compressed range to avoid creating a huge list.
11874               (gnus-range-difference (list gnus-newsgroup-active) old))
11875         (setq len (gnus-range-length older))
11876         (cond
11877          ((null older) nil)
11878          ((numberp all)
11879           (if (< all len)
11880               (let ((older-range (nreverse older)))
11881                 (setq older nil)
11882
11883                 (while (> all 0)
11884                   (let* ((r (pop older-range))
11885                          (min (if (numberp r) r (car r)))
11886                          (max (if (numberp r) r (cdr r))))
11887                     (while (and (<= min max)
11888                                 (> all 0))
11889                       (push max older)
11890                       (setq all (1- all)
11891                             max (1- max))))))
11892             (setq older (gnus-uncompress-range older))))
11893          (all
11894           (setq older (gnus-uncompress-range older)))
11895          (t
11896           (when (and (numberp gnus-large-newsgroup)
11897                    (> len gnus-large-newsgroup))
11898               (let* ((cursor-in-echo-area nil)
11899                      (initial (gnus-parameter-large-newsgroup-initial
11900                                gnus-newsgroup-name))
11901                      (input
11902                       (read-string
11903                        (format
11904                         "How many articles from %s (%s %d): "
11905                         (gnus-limit-string
11906                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11907                         (if initial "max" "default")
11908                         len)
11909                        (if initial
11910                            (cons (number-to-string initial)
11911                                  0)))))
11912                 (unless (string-match "^[ \t]*$" input)
11913                   (setq all (string-to-number input))
11914                   (if (< all len)
11915                       (let ((older-range (nreverse older)))
11916                         (setq older nil)
11917
11918                         (while (> all 0)
11919                           (let* ((r (pop older-range))
11920                                  (min (if (numberp r) r (car r)))
11921                                  (max (if (numberp r) r (cdr r))))
11922                             (while (and (<= min max)
11923                                         (> all 0))
11924                               (push max older)
11925                               (setq all (1- all)
11926                                     max (1- max))))))))))
11927           (setq older (gnus-uncompress-range older))))
11928         (if (not older)
11929             (message "No old news.")
11930           (gnus-summary-insert-articles older)
11931           (gnus-summary-limit (gnus-sorted-nunion old older))))
11932     (gnus-summary-position-point)))
11933
11934 (defun gnus-summary-insert-new-articles ()
11935   "Insert all new articles in this group."
11936   (interactive)
11937   (prog1
11938       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11939             (old-active gnus-newsgroup-active)
11940             (nnmail-fetched-sources (list t))
11941             i new)
11942         (setq gnus-newsgroup-active
11943               (gnus-activate-group gnus-newsgroup-name 'scan))
11944         (setq i (cdr gnus-newsgroup-active))
11945         (while (> i (cdr old-active))
11946           (push i new)
11947           (decf i))
11948         (if (not new)
11949             (message "No gnus is bad news")
11950           (gnus-summary-insert-articles new)
11951           (setq gnus-newsgroup-unreads
11952                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11953           (gnus-summary-limit (gnus-sorted-nunion old new))))
11954     (gnus-summary-position-point)))
11955
11956 (gnus-summary-make-all-marking-commands)
11957
11958 (gnus-ems-redefine)
11959
11960 (provide 'gnus-sum)
11961
11962 (run-hooks 'gnus-sum-load-hook)
11963
11964 ;; Local Variables:
11965 ;; coding: iso-8859-1
11966 ;; End:
11967
11968 ;;; gnus-sum.el ends here