Import Gnus v5.10.2.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)
31   (defvar tool-bar-map))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mm-decode)
41 (require 'nnoo)
42
43 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
44 (autoload 'gnus-cache-write-active "gnus-cache")
45 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
46 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
47 (autoload 'mm-uu-dissect "mm-uu")
48 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
49   "Deuglify broken Outlook (Express) articles and redisplay."
50   t)
51 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
52 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
53 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
54
55 (defcustom gnus-kill-summary-on-exit t
56   "*If non-nil, kill the summary buffer when you exit from it.
57 If nil, the summary will become a \"*Dead Summary*\" buffer, and
58 it will be killed sometime later."
59   :group 'gnus-summary-exit
60   :type 'boolean)
61
62 (defcustom gnus-fetch-old-headers nil
63   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
64 If an unread article in the group refers to an older, already read (or
65 just marked as read) article, the old article will not normally be
66 displayed in the Summary buffer.  If this variable is non-nil, Gnus
67 will attempt to grab the headers to the old articles, and thereby
68 build complete threads.  If it has the value `some', only enough
69 headers to connect otherwise loose threads will be displayed.  This
70 variable can also be a number.  In that case, no more than that number
71 of old headers will be fetched.  If it has the value `invisible', all
72 old headers will be fetched, but none will be displayed.
73
74 The server has to support NOV for any of this to work."
75   :group 'gnus-thread
76   :type '(choice (const :tag "off" nil)
77                  (const some)
78                  number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-refer-thread-limit 200
82   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
83 If t, fetch all the available old headers."
84   :group 'gnus-thread
85   :type '(choice number
86                  (sexp :menu-tag "other" t)))
87
88 (defcustom gnus-summary-make-false-root 'adopt
89   "*nil means that Gnus won't gather loose threads.
90 If the root of a thread has expired or been read in a previous
91 session, the information necessary to build a complete thread has been
92 lost.  Instead of having many small sub-threads from this original thread
93 scattered all over the summary buffer, Gnus can gather them.
94
95 If non-nil, Gnus will try to gather all loose sub-threads from an
96 original thread into one large thread.
97
98 If this variable is non-nil, it should be one of `none', `adopt',
99 `dummy' or `empty'.
100
101 If this variable is `none', Gnus will not make a false root, but just
102 present the sub-threads after another.
103 If this variable is `dummy', Gnus will create a dummy root that will
104 have all the sub-threads as children.
105 If this variable is `adopt', Gnus will make one of the \"children\"
106 the parent and mark all the step-children as such.
107 If this variable is `empty', the \"children\" are printed with empty
108 subject fields.  (Or rather, they will be printed with a string
109 given by the `gnus-summary-same-subject' variable.)"
110   :group 'gnus-thread
111   :type '(choice (const :tag "off" nil)
112                  (const none)
113                  (const dummy)
114                  (const adopt)
115                  (const empty)))
116
117 (defcustom gnus-summary-make-false-root-always nil
118   "Always make a false dummy root."
119   :group 'gnus-thread
120   :type 'boolean)
121
122 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
123   "*A regexp to match subjects to be excluded from loose thread gathering.
124 As loose thread gathering is done on subjects only, that means that
125 there can be many false gatherings performed.  By rooting out certain
126 common subjects, gathering might become saner."
127   :group 'gnus-thread
128   :type 'regexp)
129
130 (defcustom gnus-summary-gather-subject-limit nil
131   "*Maximum length of subject comparisons when gathering loose threads.
132 Use nil to compare full subjects.  Setting this variable to a low
133 number will help gather threads that have been corrupted by
134 newsreaders chopping off subject lines, but it might also mean that
135 unrelated articles that have subject that happen to begin with the
136 same few characters will be incorrectly gathered.
137
138 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
139 comparing subjects."
140   :group 'gnus-thread
141   :type '(choice (const :tag "off" nil)
142                  (const fuzzy)
143                  (sexp :menu-tag "on" t)))
144
145 (defcustom gnus-simplify-subject-functions nil
146   "List of functions taking a string argument that simplify subjects.
147 The functions are applied recursively.
148
149 Useful functions to put in this list include:
150 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
151 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
152   :group 'gnus-thread
153   :type '(repeat function))
154
155 (defcustom gnus-simplify-ignored-prefixes nil
156   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
157   :group 'gnus-thread
158   :type '(choice (const :tag "off" nil)
159                  regexp))
160
161 (defcustom gnus-build-sparse-threads nil
162   "*If non-nil, fill in the gaps in threads.
163 If `some', only fill in the gaps that are needed to tie loose threads
164 together.  If `more', fill in all leaf nodes that Gnus can find.  If
165 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
166   :group 'gnus-thread
167   :type '(choice (const :tag "off" nil)
168                  (const some)
169                  (const more)
170                  (sexp :menu-tag "all" t)))
171
172 (defcustom gnus-summary-thread-gathering-function
173   'gnus-gather-threads-by-subject
174   "*Function used for gathering loose threads.
175 There are two pre-defined functions: `gnus-gather-threads-by-subject',
176 which only takes Subjects into consideration; and
177 `gnus-gather-threads-by-references', which compared the References
178 headers of the articles to find matches."
179   :group 'gnus-thread
180   :type '(radio (function-item gnus-gather-threads-by-subject)
181                 (function-item gnus-gather-threads-by-references)
182                 (function :tag "other")))
183
184 (defcustom gnus-summary-same-subject ""
185   "*String indicating that the current article has the same subject as the previous.
186 This variable will only be used if the value of
187 `gnus-summary-make-false-root' is `empty'."
188   :group 'gnus-summary-format
189   :type 'string)
190
191 (defcustom gnus-summary-goto-unread t
192   "*If t, many commands will go to the next unread article.
193 This applies to marking commands as well as other commands that
194 \"naturally\" select the next article, like, for instance, `SPC' at
195 the end of an article.
196
197 If nil, the marking commands do NOT go to the next unread article
198 \(they go to the next article instead).  If `never', commands that
199 usually go to the next unread article, will go to the next article,
200 whether it is read or not."
201   :group 'gnus-summary-marks
202   :link '(custom-manual "(gnus)Setting Marks")
203   :type '(choice (const :tag "off" nil)
204                  (const never)
205                  (sexp :menu-tag "on" t)))
206
207 (defcustom gnus-summary-default-score 0
208   "*Default article score level.
209 All scores generated by the score files will be added to this score.
210 If this variable is nil, scoring will be disabled."
211   :group 'gnus-score-default
212   :type '(choice (const :tag "disable")
213                  integer))
214
215 (defcustom gnus-summary-default-high-score 0
216   "*Default threshold for a high scored article.
217 An article will be highlighted as high scored if its score is greater
218 than this score."
219   :group 'gnus-score-default
220   :type 'integer)
221
222 (defcustom gnus-summary-default-low-score 0
223   "*Default threshold for a low scored article.
224 An article will be highlighted as low scored if its score is smaller
225 than this score."
226   :group 'gnus-score-default
227   :type 'integer)
228
229 (defcustom gnus-summary-zcore-fuzz 0
230   "*Fuzziness factor for the zcore in the summary buffer.
231 Articles with scores closer than this to `gnus-summary-default-score'
232 will not be marked."
233   :group 'gnus-summary-format
234   :type 'integer)
235
236 (defcustom gnus-simplify-subject-fuzzy-regexp nil
237   "*Strings to be removed when doing fuzzy matches.
238 This can either be a regular expression or list of regular expressions
239 that will be removed from subject strings if fuzzy subject
240 simplification is selected."
241   :group 'gnus-thread
242   :type '(repeat regexp))
243
244 (defcustom gnus-show-threads t
245   "*If non-nil, display threads in summary mode."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-hide-subtree nil
250   "*If non-nil, hide all threads initially.
251 This can be a predicate specifier which says which threads to hide.
252 If threads are hidden, you have to run the command
253 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
254 to expose hidden threads."
255   :group 'gnus-thread
256   :type 'boolean)
257
258 (defcustom gnus-thread-hide-killed t
259   "*If non-nil, hide killed threads automatically."
260   :group 'gnus-thread
261   :type 'boolean)
262
263 (defcustom gnus-thread-ignore-subject t
264   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
265 If nil, articles that have different subjects from their parents will
266 start separate threads."
267   :group 'gnus-thread
268   :type 'boolean)
269
270 (defcustom gnus-thread-operation-ignore-subject t
271   "*If non-nil, subjects will be ignored when doing thread commands.
272 This affects commands like `gnus-summary-kill-thread' and
273 `gnus-summary-lower-thread'.
274
275 If this variable is nil, articles in the same thread with different
276 subjects will not be included in the operation in question.  If this
277 variable is `fuzzy', only articles that have subjects that are fuzzily
278 equal will be included."
279   :group 'gnus-thread
280   :type '(choice (const :tag "off" nil)
281                  (const fuzzy)
282                  (sexp :tag "on" t)))
283
284 (defcustom gnus-thread-indent-level 4
285   "*Number that says how much each sub-thread should be indented."
286   :group 'gnus-thread
287   :type 'integer)
288
289 (defcustom gnus-auto-extend-newsgroup t
290   "*If non-nil, extend newsgroup forward and backward when requested."
291   :group 'gnus-summary-choose
292   :type 'boolean)
293
294 (defcustom gnus-auto-select-first t
295   "*If non-nil, select the article under point.
296 Which article this is is controlled by the `gnus-auto-select-subject'
297 variable.
298
299 If you want to prevent automatic selection of articles in some
300 newsgroups, set the variable to nil in `gnus-select-group-hook'."
301   :group 'gnus-group-select
302   :type '(choice (const :tag "none" nil)
303                  (sexp :menu-tag "first" t)))
304
305 (defcustom gnus-auto-select-subject 'unread
306   "*Says what subject to place under point when entering a group.
307
308 This variable can either be the symbols `first' (place point on the
309 first subject), `unread' (place point on the subject line of the first
310 unread article), `best' (place point on the subject line of the
311 higest-scored article), `unseen' (place point on the subject line of
312 the first unseen article), 'unseen-or-unread' (place point on the subject
313 line of the first unseen article or, if all article have been seen, on the
314 subject line of the first unread article), or a function to be called to
315 place point on some subject line."
316   :group 'gnus-group-select
317   :type '(choice (const best)
318                  (const unread)
319                  (const first)
320                  (const unseen)
321                  (const unseen-or-unread)))
322
323 (defcustom gnus-auto-select-next t
324   "*If non-nil, offer to go to the next group from the end of the previous.
325 If the value is t and the next newsgroup is empty, Gnus will exit
326 summary mode and go back to group mode.  If the value is neither nil
327 nor t, Gnus will select the following unread newsgroup.  In
328 particular, if the value is the symbol `quietly', the next unread
329 newsgroup will be selected without any confirmation, and if it is
330 `almost-quietly', the next group will be selected without any
331 confirmation if you are located on the last article in the group.
332 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
333 will go to the next group without confirmation."
334   :group 'gnus-summary-maneuvering
335   :type '(choice (const :tag "off" nil)
336                  (const quietly)
337                  (const almost-quietly)
338                  (const slightly-quietly)
339                  (sexp :menu-tag "on" t)))
340
341 (defcustom gnus-auto-select-same nil
342   "*If non-nil, select the next article with the same subject.
343 If there are no more articles with the same subject, go to
344 the first unread article."
345   :group 'gnus-summary-maneuvering
346   :type 'boolean)
347
348 (defcustom gnus-auto-goto-ignores 'unfetched
349   "*Says how to handle unfetched articles when maneuvering.
350
351 This variable can either be the symbols nil (maneuver to any
352 article), `undownloaded' (maneuvering while unplugged ignores articles
353 that have not been fetched), `always-undownloaded' (maneuvering always
354 ignores articles that have not been fetched), `unfetched' (maneuvering
355 ignores articles whose headers have not been fetched).
356
357 NOTE: The list of unfetched articles will always be nil when plugged
358 and, when unplugged, a subset of the undownloaded article list."
359   :group 'gnus-summary-maneuvering
360   :type '(choice (const :tag "None" nil)
361                  (const :tag "Undownloaded when unplugged" undownloaded)
362                  (const :tag "Undownloaded" always-undownloaded)
363                  (const :tag "Unfetched" unfetched)))
364
365 (defcustom gnus-summary-check-current nil
366   "*If non-nil, consider the current article when moving.
367 The \"unread\" movement commands will stay on the same line if the
368 current article is unread."
369   :group 'gnus-summary-maneuvering
370   :type 'boolean)
371
372 (defcustom gnus-auto-center-summary t
373   "*If non-nil, always center the current summary buffer.
374 In particular, if `vertical' do only vertical recentering.  If non-nil
375 and non-`vertical', do both horizontal and vertical recentering."
376   :group 'gnus-summary-maneuvering
377   :type '(choice (const :tag "none" nil)
378                  (const vertical)
379                  (integer :tag "height")
380                  (sexp :menu-tag "both" t)))
381
382 (defvar gnus-auto-center-group t
383   "*If non-nil, always center the group buffer.")
384
385 (defcustom gnus-show-all-headers nil
386   "*If non-nil, don't hide any headers."
387   :group 'gnus-article-hiding
388   :group 'gnus-article-headers
389   :type 'boolean)
390
391 (defcustom gnus-summary-ignore-duplicates nil
392   "*If non-nil, ignore articles with identical Message-ID headers."
393   :group 'gnus-summary
394   :type 'boolean)
395
396 (defcustom gnus-single-article-buffer t
397   "*If non-nil, display all articles in the same buffer.
398 If nil, each group will get its own article buffer."
399   :group 'gnus-article-various
400   :type 'boolean)
401
402 (defcustom gnus-break-pages t
403   "*If non-nil, do page breaking on articles.
404 The page delimiter is specified by the `gnus-page-delimiter'
405 variable."
406   :group 'gnus-article-various
407   :type 'boolean)
408
409 (defcustom gnus-move-split-methods nil
410   "*Variable used to suggest where articles are to be moved to.
411 It uses the same syntax as the `gnus-split-methods' variable.
412 However, whereas `gnus-split-methods' specifies file names as targets,
413 this variable specifies group names."
414   :group 'gnus-summary-mail
415   :type '(repeat (choice (list :value (fun) function)
416                          (cons :value ("" "") regexp (repeat string))
417                          (sexp :value nil))))
418
419 (defcustom gnus-unread-mark ?           ;Whitespace
420   "*Mark used for unread articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-ticked-mark ?!
425   "*Mark used for ticked articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-dormant-mark ??
430   "*Mark used for dormant articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-del-mark ?r
435   "*Mark used for del'd articles."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-read-mark ?R
440   "*Mark used for read articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-expirable-mark ?E
445   "*Mark used for expirable articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-killed-mark ?K
450   "*Mark used for killed articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-spam-mark ?$
455   "*Mark used for spam articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-souped-mark ?F
460   "*Mark used for souped articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-kill-file-mark ?X
465   "*Mark used for articles killed by kill files."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-low-score-mark ?Y
470   "*Mark used for articles with a low score."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-catchup-mark ?C
475   "*Mark used for articles that are caught up."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-replied-mark ?A
480   "*Mark used for articles that have been replied to."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-forwarded-mark ?F
485   "*Mark used for articles that have been forwarded."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-recent-mark ?N
490   "*Mark used for articles that are recent."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-cached-mark ?*
495   "*Mark used for articles that are in the cache."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-saved-mark ?S
500   "*Mark used for articles that have been saved."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-unseen-mark ?.
505   "*Mark used for articles that haven't been seen."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-no-mark ?               ;Whitespace
510   "*Mark used for articles that have no other secondary mark."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-ancient-mark ?O
515   "*Mark used for ancient articles."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-sparse-mark ?Q
520   "*Mark used for sparsely reffed articles."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-canceled-mark ?G
525   "*Mark used for canceled articles."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-duplicate-mark ?M
530   "*Mark used for duplicate articles."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-undownloaded-mark ?-
535   "*Mark used for articles that weren't downloaded."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-downloaded-mark ?+
540   "*Mark used for articles that were downloaded."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-downloadable-mark ?%
545   "*Mark used for articles that are to be downloaded."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-unsendable-mark ?=
550   "*Mark used for articles that won't be sent."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-score-over-mark ?+
555   "*Score mark used for articles with high scores."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-score-below-mark ?-
560   "*Score mark used for articles with low scores."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-empty-thread-mark ?     ;Whitespace
565   "*There is no thread under the article."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-not-empty-thread-mark ?=
570   "*There is a thread under the article."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-view-pseudo-asynchronously nil
575   "*If non-nil, Gnus will view pseudo-articles asynchronously."
576   :group 'gnus-extract-view
577   :type 'boolean)
578
579 (defcustom gnus-auto-expirable-marks
580   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
581         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
582         gnus-souped-mark gnus-duplicate-mark)
583   "*The list of marks converted into expiration if a group is auto-expirable."
584   :version "21.1"
585   :group 'gnus-summary
586   :type '(repeat character))
587
588 (defcustom gnus-inhibit-user-auto-expire t
589   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
590   :version "21.1"
591   :group 'gnus-summary
592   :type 'boolean)
593
594 (defcustom gnus-view-pseudos nil
595   "*If `automatic', pseudo-articles will be viewed automatically.
596 If `not-confirm', pseudos will be viewed automatically, and the user
597 will not be asked to confirm the command."
598   :group 'gnus-extract-view
599   :type '(choice (const :tag "off" nil)
600                  (const automatic)
601                  (const not-confirm)))
602
603 (defcustom gnus-view-pseudos-separately t
604   "*If non-nil, one pseudo-article will be created for each file to be viewed.
605 If nil, all files that use the same viewing command will be given as a
606 list of parameters to that command."
607   :group 'gnus-extract-view
608   :type 'boolean)
609
610 (defcustom gnus-insert-pseudo-articles t
611   "*If non-nil, insert pseudo-articles when decoding articles."
612   :group 'gnus-extract-view
613   :type 'boolean)
614
615 (defcustom gnus-summary-dummy-line-format
616   "   %(:                             :%) %S\n"
617   "*The format specification for the dummy roots in the summary buffer.
618 It works along the same lines as a normal formatting string,
619 with some simple extensions.
620
621 %S  The subject
622
623 General format specifiers can also be used.
624 See `(gnus)Formatting Variables'."
625   :link '(custom-manual "(gnus)Formatting Variables")
626   :group 'gnus-threading
627   :type 'string)
628
629 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
630   "*The format specification for the summary mode line.
631 It works along the same lines as a normal formatting string,
632 with some simple extensions:
633
634 %G  Group name
635 %p  Unprefixed group name
636 %A  Current article number
637 %z  Current article score
638 %V  Gnus version
639 %U  Number of unread articles in the group
640 %e  Number of unselected articles in the group
641 %Z  A string with unread/unselected article counts
642 %g  Shortish group name
643 %S  Subject of the current article
644 %u  User-defined spec
645 %s  Current score file name
646 %d  Number of dormant articles
647 %r  Number of articles that have been marked as read in this session
648 %E  Number of articles expunged by the score files"
649   :group 'gnus-summary-format
650   :type 'string)
651
652 (defcustom gnus-list-identifiers nil
653   "Regexp that matches list identifiers to be removed from subject.
654 This can also be a list of regexps."
655   :version "21.1"
656   :group 'gnus-summary-format
657   :group 'gnus-article-hiding
658   :type '(choice (const :tag "none" nil)
659                  (regexp :value ".*")
660                  (repeat :value (".*") regexp)))
661
662 (defcustom gnus-summary-mark-below 0
663   "*Mark all articles with a score below this variable as read.
664 This variable is local to each summary buffer and usually set by the
665 score file."
666   :group 'gnus-score-default
667   :type 'integer)
668
669 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
670   "*List of functions used for sorting articles in the summary buffer.
671
672 Each function takes two articles and returns non-nil if the first
673 article should be sorted before the other.  If you use more than one
674 function, the primary sort function should be the last.  You should
675 probably always include `gnus-article-sort-by-number' in the list of
676 sorting functions -- preferably first.  Also note that sorting by date
677 is often much slower than sorting by number, and the sorting order is
678 very similar.  (Sorting by date means sorting by the time the message
679 was sent, sorting by number means sorting by arrival time.)
680
681 Ready-made functions include `gnus-article-sort-by-number',
682 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
683 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
684 and `gnus-article-sort-by-score'.
685
686 When threading is turned on, the variable `gnus-thread-sort-functions'
687 controls how articles are sorted."
688   :group 'gnus-summary-sort
689   :type '(repeat (choice (function-item gnus-article-sort-by-number)
690                          (function-item gnus-article-sort-by-author)
691                          (function-item gnus-article-sort-by-subject)
692                          (function-item gnus-article-sort-by-date)
693                          (function-item gnus-article-sort-by-score)
694                          (function-item gnus-article-sort-by-random)
695                          (function :tag "other"))))
696
697 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
698   "*List of functions used for sorting threads in the summary buffer.
699 By default, threads are sorted by article number.
700
701 Each function takes two threads and returns non-nil if the first
702 thread 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-thread-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-thread-sort-by-number',
711 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
712 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
713 `gnus-thread-sort-by-most-recent-number',
714 `gnus-thread-sort-by-most-recent-date',
715 `gnus-thread-sort-by-random', and
716 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
717
718 When threading is turned off, the variable
719 `gnus-article-sort-functions' controls how articles are sorted."
720   :group 'gnus-summary-sort
721   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
722                          (function-item gnus-thread-sort-by-author)
723                          (function-item gnus-thread-sort-by-subject)
724                          (function-item gnus-thread-sort-by-date)
725                          (function-item gnus-thread-sort-by-score)
726                          (function-item gnus-thread-sort-by-total-score)
727                          (function-item gnus-thread-sort-by-random)
728                          (function :tag "other"))))
729
730 (defcustom gnus-thread-score-function '+
731   "*Function used for calculating the total score of a thread.
732
733 The function is called with the scores of the article and each
734 subthread and should then return the score of the thread.
735
736 Some functions you can use are `+', `max', or `min'."
737   :group 'gnus-summary-sort
738   :type 'function)
739
740 (defcustom gnus-summary-expunge-below nil
741   "All articles that have a score less than this variable will be expunged.
742 This variable is local to the summary buffers."
743   :group 'gnus-score-default
744   :type '(choice (const :tag "off" nil)
745                  integer))
746
747 (defcustom gnus-thread-expunge-below nil
748   "All threads that have a total score less than this variable will be expunged.
749 See `gnus-thread-score-function' for en explanation of what a
750 \"thread score\" is.
751
752 This variable is local to the summary buffers."
753   :group 'gnus-threading
754   :group 'gnus-score-default
755   :type '(choice (const :tag "off" nil)
756                  integer))
757
758 (defcustom gnus-summary-mode-hook nil
759   "*A hook for Gnus summary mode.
760 This hook is run before any variables are set in the summary buffer."
761   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
762   :group 'gnus-summary-various
763   :type 'hook)
764
765 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
766 (when (featurep 'xemacs)
767   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
768   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
769   (add-hook 'gnus-summary-mode-hook
770             'gnus-xmas-switch-horizontal-scrollbar-off))
771
772 (defcustom gnus-summary-menu-hook nil
773   "*Hook run after the creation of the summary mode menu."
774   :group 'gnus-summary-visual
775   :type 'hook)
776
777 (defcustom gnus-summary-exit-hook nil
778   "*A hook called on exit from the summary buffer.
779 It will be called with point in the group buffer."
780   :group 'gnus-summary-exit
781   :type 'hook)
782
783 (defcustom gnus-summary-prepare-hook nil
784   "*A hook called after the summary buffer has been generated.
785 If you want to modify the summary buffer, you can use this hook."
786   :group 'gnus-summary-various
787   :type 'hook)
788
789 (defcustom gnus-summary-prepared-hook nil
790   "*A hook called as the last thing after the summary buffer has been generated."
791   :group 'gnus-summary-various
792   :type 'hook)
793
794 (defcustom gnus-summary-generate-hook nil
795   "*A hook run just before generating the summary buffer.
796 This hook is commonly used to customize threading variables and the
797 like."
798   :group 'gnus-summary-various
799   :type 'hook)
800
801 (defcustom gnus-select-group-hook nil
802   "*A hook called when a newsgroup is selected.
803
804 If you'd like to simplify subjects like the
805 `gnus-summary-next-same-subject' command does, you can use the
806 following hook:
807
808  (add-hook gnus-select-group-hook
809            (lambda ()
810              (mapcar (lambda (header)
811                        (mail-header-set-subject
812                         header
813                         (gnus-simplify-subject
814                          (mail-header-subject header) 're-only)))
815                      gnus-newsgroup-headers)))"
816   :group 'gnus-group-select
817   :type 'hook)
818
819 (defcustom gnus-select-article-hook nil
820   "*A hook called when an article is selected."
821   :group 'gnus-summary-choose
822   :options '(gnus-agent-fetch-selected-article)
823   :type 'hook)
824
825 (defcustom gnus-visual-mark-article-hook
826   (list 'gnus-highlight-selected-summary)
827   "*Hook run after selecting an article in the summary buffer.
828 It is meant to be used for highlighting the article in some way.  It
829 is not run if `gnus-visual' is nil."
830   :group 'gnus-summary-visual
831   :type 'hook)
832
833 (defcustom gnus-parse-headers-hook nil
834   "*A hook called before parsing the headers."
835   :group 'gnus-various
836   :type 'hook)
837
838 (defcustom gnus-exit-group-hook nil
839   "*A hook called when exiting summary mode.
840 This hook is not called from the non-updating exit commands like `Q'."
841   :group 'gnus-various
842   :type 'hook)
843
844 (defcustom gnus-summary-update-hook
845   (list 'gnus-summary-highlight-line)
846   "*A hook called when a summary line is changed.
847 The hook will not be called if `gnus-visual' is nil.
848
849 The default function `gnus-summary-highlight-line' will
850 highlight the line according to the `gnus-summary-highlight'
851 variable."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
856   "*A hook called when an article is selected for the first time.
857 The hook is intended to mark an article as read (or unread)
858 automatically when it is selected."
859   :group 'gnus-summary-choose
860   :type 'hook)
861
862 (defcustom gnus-group-no-more-groups-hook nil
863   "*A hook run when returning to group mode having no more (unread) groups."
864   :group 'gnus-group-select
865   :type 'hook)
866
867 (defcustom gnus-ps-print-hook nil
868   "*A hook run before ps-printing something from Gnus."
869   :group 'gnus-summary
870   :type 'hook)
871
872 (defcustom gnus-summary-article-move-hook nil
873   "*A hook called after an article is moved, copied, respooled, or crossposted."
874   :group 'gnus-summary
875   :type 'hook)
876
877 (defcustom gnus-summary-article-delete-hook nil
878   "*A hook called after an article is deleted."
879   :group 'gnus-summary
880   :type 'hook)
881
882 (defcustom gnus-summary-article-expire-hook nil
883   "*A hook called after an article is expired."
884   :group 'gnus-summary
885   :type 'hook)
886
887 (defcustom gnus-summary-display-arrow
888   (and (fboundp 'display-graphic-p)
889        (display-graphic-p))
890   "*If non-nil, display an arrow highlighting the current article."
891   :version "21.1"
892   :group 'gnus-summary
893   :type 'boolean)
894
895 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
896   "Face used for highlighting the current article in the summary buffer."
897   :group 'gnus-summary-visual
898   :type 'face)
899
900 (defvar gnus-tmp-downloaded nil)
901
902 (defcustom gnus-summary-highlight
903   '(((eq mark gnus-canceled-mark)
904      . gnus-summary-cancelled-face)
905     ((and uncached (> score default-high))
906      . gnus-summary-high-undownloaded-face)
907     ((and uncached (< score default-low))
908      . gnus-summary-low-undownloaded-face)
909     (uncached
910      . gnus-summary-normal-undownloaded-face)
911     ((and (> score default-high)
912           (or (eq mark gnus-dormant-mark)
913               (eq mark gnus-ticked-mark)))
914      . gnus-summary-high-ticked-face)
915     ((and (< score default-low)
916           (or (eq mark gnus-dormant-mark)
917               (eq mark gnus-ticked-mark)))
918      . gnus-summary-low-ticked-face)
919     ((or (eq mark gnus-dormant-mark)
920          (eq mark gnus-ticked-mark))
921      . gnus-summary-normal-ticked-face)
922     ((and (> score default-high) (eq mark gnus-ancient-mark))
923      . gnus-summary-high-ancient-face)
924     ((and (< score default-low) (eq mark gnus-ancient-mark))
925      . gnus-summary-low-ancient-face)
926     ((eq mark gnus-ancient-mark)
927      . gnus-summary-normal-ancient-face)
928     ((and (> score default-high) (eq mark gnus-unread-mark))
929      . gnus-summary-high-unread-face)
930     ((and (< score default-low) (eq mark gnus-unread-mark))
931      . gnus-summary-low-unread-face)
932     ((eq mark gnus-unread-mark)
933      . gnus-summary-normal-unread-face)
934     ((> score default-high)
935      . gnus-summary-high-read-face)
936     ((< score default-low)
937      . gnus-summary-low-read-face)
938     (t
939      . gnus-summary-normal-read-face))
940   "*Controls the highlighting of summary buffer lines.
941
942 A list of (FORM . FACE) pairs.  When deciding how a a particular
943 summary line should be displayed, each form is evaluated.  The content
944 of the face field after the first true form is used.  You can change
945 how those summary lines are displayed, by editing the face field.
946
947 You can use the following variables in the FORM field.
948
949 score:        The article's score
950 default:      The default article score.
951 default-high: The default score for high scored articles.
952 default-low:  The default score for low scored articles.
953 below:        The score below which articles are automatically marked as read.
954 mark:         The articles mark."
955   :group 'gnus-summary-visual
956   :type '(repeat (cons (sexp :tag "Form" nil)
957                        face)))
958
959 (defcustom gnus-alter-header-function nil
960   "Function called to allow alteration of article header structures.
961 The function is called with one parameter, the article header vector,
962 which it may alter in any way."
963   :type '(choice (const :tag "None" nil)
964                  function)
965   :group 'gnus-summary)
966
967 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
968   "Variable that says which function should be used to decode a string with encoded words.")
969
970 (defcustom gnus-extra-headers '(To Newsgroups)
971   "*Extra headers to parse."
972   :version "21.1"
973   :group 'gnus-summary
974   :type '(repeat symbol))
975
976 (defcustom gnus-ignored-from-addresses
977   (and user-mail-address (regexp-quote user-mail-address))
978   "*Regexp of From headers that may be suppressed in favor of To headers."
979   :version "21.1"
980   :group 'gnus-summary
981   :type 'regexp)
982
983 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
984   "List of charsets that should be ignored.
985 When these charsets are used in the \"charset\" parameter, the
986 default charset will be used instead."
987   :version "21.1"
988   :type '(repeat symbol)
989   :group 'gnus-charset)
990
991 (gnus-define-group-parameter
992  ignored-charsets
993  :type list
994  :function-document
995  "Return the ignored charsets of GROUP."
996  :variable gnus-group-ignored-charsets-alist
997  :variable-default
998  '(("alt\\.chinese\\.text" iso-8859-1))
999  :variable-document
1000  "Alist of regexps (to match group names) and charsets that should be ignored.
1001 When these charsets are used in the \"charset\" parameter, the
1002 default charset will be used instead."
1003  :variable-group gnus-charset
1004  :variable-type '(repeat (cons (regexp :tag "Group")
1005                                (repeat symbol)))
1006  :parameter-type '(choice :tag "Ignored charsets"
1007                           :value nil
1008                           (repeat (symbol)))
1009  :parameter-document       "\
1010 List of charsets that should be ignored.
1011
1012 When these charsets are used in the \"charset\" parameter, the
1013 default charset will be used instead.")
1014
1015 (defcustom gnus-group-highlight-words-alist nil
1016   "Alist of group regexps and highlight regexps.
1017 This variable uses the same syntax as `gnus-emphasis-alist'."
1018   :version "21.1"
1019   :type '(repeat (cons (regexp :tag "Group")
1020                        (repeat (list (regexp :tag "Highlight regexp")
1021                                      (number :tag "Group for entire word" 0)
1022                                      (number :tag "Group for displayed part" 0)
1023                                      (symbol :tag "Face"
1024                                              gnus-emphasis-highlight-words)))))
1025   :group 'gnus-summary-visual)
1026
1027 (defcustom gnus-summary-show-article-charset-alist
1028   nil
1029   "Alist of number and charset.
1030 The article will be shown with the charset corresponding to the
1031 numbered argument.
1032 For example: ((1 . cn-gb-2312) (2 . big5))."
1033   :version "21.1"
1034   :type '(repeat (cons (number :tag "Argument" 1)
1035                        (symbol :tag "Charset")))
1036   :group 'gnus-charset)
1037
1038 (defcustom gnus-preserve-marks t
1039   "Whether marks are preserved when moving, copying and respooling messages."
1040   :version "21.1"
1041   :type 'boolean
1042   :group 'gnus-summary-marks)
1043
1044 (defcustom gnus-alter-articles-to-read-function nil
1045   "Function to be called to alter the list of articles to be selected."
1046   :type '(choice (const nil) function)
1047   :group 'gnus-summary)
1048
1049 (defcustom gnus-orphan-score nil
1050   "*All orphans get this score added.  Set in the score file."
1051   :group 'gnus-score-default
1052   :type '(choice (const nil)
1053                  integer))
1054
1055 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1056   "*A regexp to match MIME parts when saving multiple parts of a
1057 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1058 This regexp will be used by default when prompting the user for which
1059 type of files to save."
1060   :group 'gnus-summary
1061   :type 'regexp)
1062
1063 (defcustom gnus-read-all-available-headers nil
1064   "Whether Gnus should parse all headers made available to it.
1065 This is mostly relevant for slow backends where the user may
1066 wish to widen the summary buffer to include all headers
1067 that were fetched.  Say, for nnultimate groups."
1068   :group 'gnus-summary
1069   :type '(choice boolean regexp))
1070
1071 (defcustom gnus-summary-muttprint-program "muttprint"
1072   "Command (and optional arguments) used to run Muttprint."
1073   :version "21.3"
1074   :group 'gnus-summary
1075   :type 'string)
1076
1077 (defcustom gnus-article-loose-mime nil
1078   "If non-nil, don't require MIME-Version header.
1079 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1080 supply the MIME-Version header or deliberately strip it From the mail.
1081 Set it to non-nil, Gnus will treat some articles as MIME even if
1082 the MIME-Version header is missed."
1083   :version "21.3"
1084   :type 'boolean
1085   :group 'gnus-article-mime)
1086
1087 (defcustom gnus-article-emulate-mime t
1088   "If non-nil, use MIME emulation for uuencode and the like.
1089 This means that Gnus will search message bodies for text that look
1090 like uuencoded bits, yEncoded bits, and so on, and present that using
1091 the normal Gnus MIME machinery."
1092   :type 'boolean
1093   :group 'gnus-article-mime)
1094
1095 ;;; Internal variables
1096
1097 (defvar gnus-summary-display-cache nil)
1098 (defvar gnus-article-mime-handles nil)
1099 (defvar gnus-article-decoded-p nil)
1100 (defvar gnus-article-charset nil)
1101 (defvar gnus-article-ignored-charsets nil)
1102 (defvar gnus-scores-exclude-files nil)
1103 (defvar gnus-page-broken nil)
1104
1105 (defvar gnus-original-article nil)
1106 (defvar gnus-article-internal-prepare-hook nil)
1107 (defvar gnus-newsgroup-process-stack nil)
1108
1109 (defvar gnus-thread-indent-array nil)
1110 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1111 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1112   "Function called to sort the articles within a thread after it has been gathered together.")
1113
1114 (defvar gnus-summary-save-parts-type-history nil)
1115 (defvar gnus-summary-save-parts-last-directory nil)
1116
1117 ;; Avoid highlighting in kill files.
1118 (defvar gnus-summary-inhibit-highlight nil)
1119 (defvar gnus-newsgroup-selected-overlay nil)
1120 (defvar gnus-inhibit-limiting nil)
1121 (defvar gnus-newsgroup-adaptive-score-file nil)
1122 (defvar gnus-current-score-file nil)
1123 (defvar gnus-current-move-group nil)
1124 (defvar gnus-current-copy-group nil)
1125 (defvar gnus-current-crosspost-group nil)
1126 (defvar gnus-newsgroup-display nil)
1127
1128 (defvar gnus-newsgroup-dependencies nil)
1129 (defvar gnus-newsgroup-adaptive nil)
1130 (defvar gnus-summary-display-article-function nil)
1131 (defvar gnus-summary-highlight-line-function nil
1132   "Function called after highlighting a summary line.")
1133
1134 (defvar gnus-summary-line-format-alist
1135   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1136     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1137     (?s gnus-tmp-subject-or-nil ?s)
1138     (?n gnus-tmp-name ?s)
1139     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1140         ?s)
1141     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1142             gnus-tmp-from) ?s)
1143     (?F gnus-tmp-from ?s)
1144     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1145     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1146     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1147     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1148     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1149     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1150     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1151     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1152     (?L gnus-tmp-lines ?s)
1153     (?O gnus-tmp-downloaded ?c)
1154     (?I gnus-tmp-indentation ?s)
1155     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1156     (?R gnus-tmp-replied ?c)
1157     (?\[ gnus-tmp-opening-bracket ?c)
1158     (?\] gnus-tmp-closing-bracket ?c)
1159     (?\> (make-string gnus-tmp-level ? ) ?s)
1160     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1161     (?i gnus-tmp-score ?d)
1162     (?z gnus-tmp-score-char ?c)
1163     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1164     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1165     (?U gnus-tmp-unread ?c)
1166     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1167         ?s)
1168     (?t (gnus-summary-number-of-articles-in-thread
1169          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1170         ?d)
1171     (?e (gnus-summary-number-of-articles-in-thread
1172          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1173         ?c)
1174     (?u gnus-tmp-user-defined ?s)
1175     (?P (gnus-pick-line-number) ?d)
1176     (?B gnus-tmp-thread-tree-header-string ?s)
1177     (user-date (gnus-user-date
1178                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1179   "An alist of format specifications that can appear in summary lines.
1180 These are paired with what variables they correspond with, along with
1181 the type of the variable (string, integer, character, etc).")
1182
1183 (defvar gnus-summary-dummy-line-format-alist
1184   `((?S gnus-tmp-subject ?s)
1185     (?N gnus-tmp-number ?d)
1186     (?u gnus-tmp-user-defined ?s)))
1187
1188 (defvar gnus-summary-mode-line-format-alist
1189   `((?G gnus-tmp-group-name ?s)
1190     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1191     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1192     (?A gnus-tmp-article-number ?d)
1193     (?Z gnus-tmp-unread-and-unselected ?s)
1194     (?V gnus-version ?s)
1195     (?U gnus-tmp-unread-and-unticked ?d)
1196     (?S gnus-tmp-subject ?s)
1197     (?e gnus-tmp-unselected ?d)
1198     (?u gnus-tmp-user-defined ?s)
1199     (?d (length gnus-newsgroup-dormant) ?d)
1200     (?t (length gnus-newsgroup-marked) ?d)
1201     (?h (length gnus-newsgroup-spam-marked) ?d)
1202     (?r (length gnus-newsgroup-reads) ?d)
1203     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1204     (?E gnus-newsgroup-expunged-tally ?d)
1205     (?s (gnus-current-score-file-nondirectory) ?s)))
1206
1207 (defvar gnus-last-search-regexp nil
1208   "Default regexp for article search command.")
1209
1210 (defvar gnus-last-shell-command nil
1211   "Default shell command on article.")
1212
1213 (defvar gnus-newsgroup-agentized nil
1214   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1215 (defvar gnus-newsgroup-begin nil)
1216 (defvar gnus-newsgroup-end nil)
1217 (defvar gnus-newsgroup-last-rmail nil)
1218 (defvar gnus-newsgroup-last-mail nil)
1219 (defvar gnus-newsgroup-last-folder nil)
1220 (defvar gnus-newsgroup-last-file nil)
1221 (defvar gnus-newsgroup-auto-expire nil)
1222 (defvar gnus-newsgroup-active nil)
1223
1224 (defvar gnus-newsgroup-data nil)
1225 (defvar gnus-newsgroup-data-reverse nil)
1226 (defvar gnus-newsgroup-limit nil)
1227 (defvar gnus-newsgroup-limits nil)
1228 (defvar gnus-summary-use-undownloaded-faces nil)
1229
1230 (defvar gnus-newsgroup-unreads nil
1231   "Sorted list of unread articles in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-unselected nil
1234   "Sorted list of unselected unread articles in the current newsgroup.")
1235
1236 (defvar gnus-newsgroup-reads nil
1237   "Alist of read articles and article marks in the current newsgroup.")
1238
1239 (defvar gnus-newsgroup-expunged-tally nil)
1240
1241 (defvar gnus-newsgroup-marked nil
1242   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1243
1244 (defvar gnus-newsgroup-spam-marked nil
1245   "List of ranges of articles that have been marked as spam.")
1246
1247 (defvar gnus-newsgroup-killed nil
1248   "List of ranges of articles that have been through the scoring process.")
1249
1250 (defvar gnus-newsgroup-cached nil
1251   "Sorted list of articles that come from the article cache.")
1252
1253 (defvar gnus-newsgroup-saved nil
1254   "List of articles that have been saved.")
1255
1256 (defvar gnus-newsgroup-kill-headers nil)
1257
1258 (defvar gnus-newsgroup-replied nil
1259   "List of articles that have been replied to in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-forwarded nil
1262   "List of articles that have been forwarded in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-recent nil
1265   "List of articles that have are recent in the current newsgroup.")
1266
1267 (defvar gnus-newsgroup-expirable nil
1268   "Sorted list of articles in the current newsgroup that can be expired.")
1269
1270 (defvar gnus-newsgroup-processable nil
1271   "List of articles in the current newsgroup that can be processed.")
1272
1273 (defvar gnus-newsgroup-downloadable nil
1274   "Sorted list of articles in the current newsgroup that can be processed.")
1275
1276 (defvar gnus-newsgroup-unfetched nil
1277   "Sorted list of articles in the current newsgroup whose headers have
1278 not been fetched into the agent.
1279
1280 This list will always be a subset of gnus-newsgroup-undownloaded.")
1281
1282 (defvar gnus-newsgroup-undownloaded nil
1283   "List of articles in the current newsgroup that haven't been downloaded.")
1284
1285 (defvar gnus-newsgroup-unsendable nil
1286   "List of articles in the current newsgroup that won't be sent.")
1287
1288 (defvar gnus-newsgroup-bookmarks nil
1289   "List of articles in the current newsgroup that have bookmarks.")
1290
1291 (defvar gnus-newsgroup-dormant nil
1292   "Sorted list of dormant articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-unseen nil
1295   "List of unseen articles in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-seen nil
1298   "Range of seen articles in the current newsgroup.")
1299
1300 (defvar gnus-newsgroup-articles nil
1301   "List of articles in the current newsgroup.")
1302
1303 (defvar gnus-newsgroup-scored nil
1304   "List of scored articles in the current newsgroup.")
1305
1306 (defvar gnus-newsgroup-headers nil
1307   "List of article headers in the current newsgroup.")
1308
1309 (defvar gnus-newsgroup-threads nil)
1310
1311 (defvar gnus-newsgroup-prepared nil
1312   "Whether the current group has been prepared properly.")
1313
1314 (defvar gnus-newsgroup-ancient nil
1315   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1316
1317 (defvar gnus-newsgroup-sparse nil)
1318
1319 (defvar gnus-current-article nil)
1320 (defvar gnus-article-current nil)
1321 (defvar gnus-current-headers nil)
1322 (defvar gnus-have-all-headers nil)
1323 (defvar gnus-last-article nil)
1324 (defvar gnus-newsgroup-history nil)
1325 (defvar gnus-newsgroup-charset nil)
1326 (defvar gnus-newsgroup-ephemeral-charset nil)
1327 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1328
1329 (defvar gnus-article-before-search nil)
1330
1331 (defvar gnus-summary-local-variables
1332   '(gnus-newsgroup-name
1333     gnus-newsgroup-begin gnus-newsgroup-end
1334     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1335     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1336     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1337     gnus-newsgroup-unselected gnus-newsgroup-marked
1338     gnus-newsgroup-spam-marked
1339     gnus-newsgroup-reads gnus-newsgroup-saved
1340     gnus-newsgroup-replied gnus-newsgroup-forwarded
1341     gnus-newsgroup-recent
1342     gnus-newsgroup-expirable
1343     gnus-newsgroup-processable gnus-newsgroup-killed
1344     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1345     gnus-newsgroup-unfetched
1346     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1347     gnus-newsgroup-seen gnus-newsgroup-articles
1348     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1349     gnus-newsgroup-headers gnus-newsgroup-threads
1350     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1351     gnus-current-article gnus-current-headers gnus-have-all-headers
1352     gnus-last-article gnus-article-internal-prepare-hook
1353     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1354     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1355     gnus-thread-expunge-below
1356     gnus-score-alist gnus-current-score-file
1357     (gnus-summary-expunge-below . global)
1358     (gnus-summary-mark-below . global)
1359     (gnus-orphan-score . global)
1360     gnus-newsgroup-active gnus-scores-exclude-files
1361     gnus-newsgroup-history gnus-newsgroup-ancient
1362     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1363     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1364     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1365     (gnus-newsgroup-expunged-tally . 0)
1366     gnus-cache-removable-articles gnus-newsgroup-cached
1367     gnus-newsgroup-data gnus-newsgroup-data-reverse
1368     gnus-newsgroup-limit gnus-newsgroup-limits
1369     gnus-newsgroup-charset gnus-newsgroup-display
1370     gnus-summary-use-undownloaded-faces)
1371   "Variables that are buffer-local to the summary buffers.")
1372
1373 (defvar gnus-newsgroup-variables nil
1374   "A list of variables that have separate values in different newsgroups.
1375 A list of newsgroup (summary buffer) local variables, or cons of
1376 variables and their default values (when the default values are not
1377 nil), that should be made global while the summary buffer is active.
1378 These variables can be used to set variables in the group parameters
1379 while still allowing them to affect operations done in other
1380 buffers. For example:
1381
1382 \(setq gnus-newsgroup-variables
1383      '(message-use-followup-to
1384        (gnus-visible-headers .
1385          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1386 ")
1387
1388 ;; Byte-compiler warning.
1389 (eval-when-compile
1390   ;; Bind features so that require will believe that gnus-sum has
1391   ;; already been loaded (avoids infinite recursion)
1392   (let ((features (cons 'gnus-sum features)))
1393     ;; Several of the declarations in gnus-sum are needed to load the
1394     ;; following files. Right now, these definitions have been
1395     ;; compiled but not defined (evaluated).  We could either do a
1396     ;; eval-and-compile about all of the declarations or evaluate the
1397     ;; source file.
1398     (if (boundp 'gnus-newsgroup-variables)
1399         nil
1400       (load "gnus-sum.el" t t t))
1401     (require 'gnus)
1402     (require 'gnus-agent)
1403     (require 'gnus-art)))
1404
1405 ;; MIME stuff.
1406
1407 (defvar gnus-decode-encoded-word-methods
1408   '(mail-decode-encoded-word-string)
1409   "List of methods used to decode encoded words.
1410
1411 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1412 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1413 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1414 whose names match REGEXP.
1415
1416 For example:
1417 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1418  mail-decode-encoded-word-string
1419  (\"chinese\" . rfc1843-decode-string))")
1420
1421 (defvar gnus-decode-encoded-word-methods-cache nil)
1422
1423 (defun gnus-multi-decode-encoded-word-string (string)
1424   "Apply the functions from `gnus-encoded-word-methods' that match."
1425   (unless (and gnus-decode-encoded-word-methods-cache
1426                (eq gnus-newsgroup-name
1427                    (car gnus-decode-encoded-word-methods-cache)))
1428     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1429     (mapcar (lambda (x)
1430               (if (symbolp x)
1431                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1432                 (if (and gnus-newsgroup-name
1433                          (string-match (car x) gnus-newsgroup-name))
1434                     (nconc gnus-decode-encoded-word-methods-cache
1435                            (list (cdr x))))))
1436             gnus-decode-encoded-word-methods))
1437   (let ((xlist gnus-decode-encoded-word-methods-cache))
1438     (pop xlist)
1439     (while xlist
1440       (setq string (funcall (pop xlist) string))))
1441   string)
1442
1443 ;; Subject simplification.
1444
1445 (defun gnus-simplify-whitespace (str)
1446   "Remove excessive whitespace from STR."
1447   ;; Multiple spaces.
1448   (while (string-match "[ \t][ \t]+" str)
1449     (setq str (concat (substring str 0 (match-beginning 0))
1450                         " "
1451                         (substring str (match-end 0)))))
1452   ;; Leading spaces.
1453   (when (string-match "^[ \t]+" str)
1454     (setq str (substring str (match-end 0))))
1455   ;; Trailing spaces.
1456   (when (string-match "[ \t]+$" str)
1457     (setq str (substring str 0 (match-beginning 0))))
1458   str)
1459
1460 (defun gnus-simplify-all-whitespace (str)
1461   "Remove all whitespace from STR."
1462   (while (string-match "[ \t\n]+" str)
1463     (setq str (replace-match "" nil nil str)))
1464   str)
1465
1466 (defsubst gnus-simplify-subject-re (subject)
1467   "Remove \"Re:\" from subject lines."
1468   (if (string-match message-subject-re-regexp subject)
1469       (substring subject (match-end 0))
1470     subject))
1471
1472 (defun gnus-simplify-subject (subject &optional re-only)
1473   "Remove `Re:' and words in parentheses.
1474 If RE-ONLY is non-nil, strip leading `Re:'s only."
1475   (let ((case-fold-search t))           ;Ignore case.
1476     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1477     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1478       (setq subject (substring subject (match-end 0))))
1479     ;; Remove uninteresting prefixes.
1480     (when (and (not re-only)
1481                gnus-simplify-ignored-prefixes
1482                (string-match gnus-simplify-ignored-prefixes subject))
1483       (setq subject (substring subject (match-end 0))))
1484     ;; Remove words in parentheses from end.
1485     (unless re-only
1486       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1487         (setq subject (substring subject 0 (match-beginning 0)))))
1488     ;; Return subject string.
1489     subject))
1490
1491 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1492 ;; all whitespace.
1493 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1494   (goto-char (point-min))
1495   (while (re-search-forward regexp nil t)
1496     (replace-match (or newtext ""))))
1497
1498 (defun gnus-simplify-buffer-fuzzy ()
1499   "Simplify string in the buffer fuzzily.
1500 The string in the accessible portion of the current buffer is simplified.
1501 It is assumed to be a single-line subject.
1502 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1503 matter is removed.  Additional things can be deleted by setting
1504 `gnus-simplify-subject-fuzzy-regexp'."
1505   (let ((case-fold-search t)
1506         (modified-tick))
1507     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1508
1509     (while (not (eq modified-tick (buffer-modified-tick)))
1510       (setq modified-tick (buffer-modified-tick))
1511       (cond
1512        ((listp gnus-simplify-subject-fuzzy-regexp)
1513         (mapcar 'gnus-simplify-buffer-fuzzy-step
1514                 gnus-simplify-subject-fuzzy-regexp))
1515        (gnus-simplify-subject-fuzzy-regexp
1516         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1517       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1518       (gnus-simplify-buffer-fuzzy-step
1519        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1520       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1521
1522     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1523     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1524     (gnus-simplify-buffer-fuzzy-step " $")
1525     (gnus-simplify-buffer-fuzzy-step "^ +")))
1526
1527 (defun gnus-simplify-subject-fuzzy (subject)
1528   "Simplify a subject string fuzzily.
1529 See `gnus-simplify-buffer-fuzzy' for details."
1530   (save-excursion
1531     (gnus-set-work-buffer)
1532     (let ((case-fold-search t))
1533       ;; Remove uninteresting prefixes.
1534       (when (and gnus-simplify-ignored-prefixes
1535                  (string-match gnus-simplify-ignored-prefixes subject))
1536         (setq subject (substring subject (match-end 0))))
1537       (insert subject)
1538       (inline (gnus-simplify-buffer-fuzzy))
1539       (buffer-string))))
1540
1541 (defsubst gnus-simplify-subject-fully (subject)
1542   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1543   (cond
1544    (gnus-simplify-subject-functions
1545     (gnus-map-function gnus-simplify-subject-functions subject))
1546    ((null gnus-summary-gather-subject-limit)
1547     (gnus-simplify-subject-re subject))
1548    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1549     (gnus-simplify-subject-fuzzy subject))
1550    ((numberp gnus-summary-gather-subject-limit)
1551     (gnus-limit-string (gnus-simplify-subject-re subject)
1552                        gnus-summary-gather-subject-limit))
1553    (t
1554     subject)))
1555
1556 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1557   "Check whether two subjects are equal.
1558 If optional argument SIMPLE-FIRST is t, first argument is already
1559 simplified."
1560   (cond
1561    ((null simple-first)
1562     (equal (gnus-simplify-subject-fully s1)
1563            (gnus-simplify-subject-fully s2)))
1564    (t
1565     (equal s1
1566            (gnus-simplify-subject-fully s2)))))
1567
1568 (defun gnus-summary-bubble-group ()
1569   "Increase the score of the current group.
1570 This is a handy function to add to `gnus-summary-exit-hook' to
1571 increase the score of each group you read."
1572   (gnus-group-add-score gnus-newsgroup-name))
1573
1574 \f
1575 ;;;
1576 ;;; Gnus summary mode
1577 ;;;
1578
1579 (put 'gnus-summary-mode 'mode-class 'special)
1580
1581 (defvar gnus-article-commands-menu)
1582
1583 ;; Non-orthogonal keys
1584
1585 (gnus-define-keys gnus-summary-mode-map
1586   " " gnus-summary-next-page
1587   "\177" gnus-summary-prev-page
1588   [delete] gnus-summary-prev-page
1589   [backspace] gnus-summary-prev-page
1590   "\r" gnus-summary-scroll-up
1591   "\M-\r" gnus-summary-scroll-down
1592   "n" gnus-summary-next-unread-article
1593   "p" gnus-summary-prev-unread-article
1594   "N" gnus-summary-next-article
1595   "P" gnus-summary-prev-article
1596   "\M-\C-n" gnus-summary-next-same-subject
1597   "\M-\C-p" gnus-summary-prev-same-subject
1598   "\M-n" gnus-summary-next-unread-subject
1599   "\M-p" gnus-summary-prev-unread-subject
1600   "." gnus-summary-first-unread-article
1601   "," gnus-summary-best-unread-article
1602   "\M-s" gnus-summary-search-article-forward
1603   "\M-r" gnus-summary-search-article-backward
1604   "<" gnus-summary-beginning-of-article
1605   ">" gnus-summary-end-of-article
1606   "j" gnus-summary-goto-article
1607   "^" gnus-summary-refer-parent-article
1608   "\M-^" gnus-summary-refer-article
1609   "u" gnus-summary-tick-article-forward
1610   "!" gnus-summary-tick-article-forward
1611   "U" gnus-summary-tick-article-backward
1612   "d" gnus-summary-mark-as-read-forward
1613   "D" gnus-summary-mark-as-read-backward
1614   "E" gnus-summary-mark-as-expirable
1615   "\M-u" gnus-summary-clear-mark-forward
1616   "\M-U" gnus-summary-clear-mark-backward
1617   "k" gnus-summary-kill-same-subject-and-select
1618   "\C-k" gnus-summary-kill-same-subject
1619   "\M-\C-k" gnus-summary-kill-thread
1620   "\M-\C-l" gnus-summary-lower-thread
1621   "e" gnus-summary-edit-article
1622   "#" gnus-summary-mark-as-processable
1623   "\M-#" gnus-summary-unmark-as-processable
1624   "\M-\C-t" gnus-summary-toggle-threads
1625   "\M-\C-s" gnus-summary-show-thread
1626   "\M-\C-h" gnus-summary-hide-thread
1627   "\M-\C-f" gnus-summary-next-thread
1628   "\M-\C-b" gnus-summary-prev-thread
1629   [(meta down)] gnus-summary-next-thread
1630   [(meta up)] gnus-summary-prev-thread
1631   "\M-\C-u" gnus-summary-up-thread
1632   "\M-\C-d" gnus-summary-down-thread
1633   "&" gnus-summary-execute-command
1634   "c" gnus-summary-catchup-and-exit
1635   "\C-w" gnus-summary-mark-region-as-read
1636   "\C-t" gnus-summary-toggle-truncation
1637   "?" gnus-summary-mark-as-dormant
1638   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1639   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1640   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1641   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1642   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1643   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1644   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1645   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1646   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1647   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1648   "=" gnus-summary-expand-window
1649   "\C-x\C-s" gnus-summary-reselect-current-group
1650   "\M-g" gnus-summary-rescan-group
1651   "w" gnus-summary-stop-page-breaking
1652   "\C-c\C-r" gnus-summary-caesar-message
1653   "f" gnus-summary-followup
1654   "F" gnus-summary-followup-with-original
1655   "C" gnus-summary-cancel-article
1656   "r" gnus-summary-reply
1657   "R" gnus-summary-reply-with-original
1658   "\C-c\C-f" gnus-summary-mail-forward
1659   "o" gnus-summary-save-article
1660   "\C-o" gnus-summary-save-article-mail
1661   "|" gnus-summary-pipe-output
1662   "\M-k" gnus-summary-edit-local-kill
1663   "\M-K" gnus-summary-edit-global-kill
1664   ;; "V" gnus-version
1665   "\C-c\C-d" gnus-summary-describe-group
1666   "q" gnus-summary-exit
1667   "Q" gnus-summary-exit-no-update
1668   "\C-c\C-i" gnus-info-find-node
1669   gnus-mouse-2 gnus-mouse-pick-article
1670   "m" gnus-summary-mail-other-window
1671   "a" gnus-summary-post-news
1672   "i" gnus-summary-news-other-window
1673   "x" gnus-summary-limit-to-unread
1674   "s" gnus-summary-isearch-article
1675   "t" gnus-summary-toggle-header
1676   "g" gnus-summary-show-article
1677   "l" gnus-summary-goto-last-article
1678   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1679   "\C-d" gnus-summary-enter-digest-group
1680   "\M-\C-d" gnus-summary-read-document
1681   "\M-\C-e" gnus-summary-edit-parameters
1682   "\M-\C-a" gnus-summary-customize-parameters
1683   "\C-c\C-b" gnus-bug
1684   "*" gnus-cache-enter-article
1685   "\M-*" gnus-cache-remove-article
1686   "\M-&" gnus-summary-universal-argument
1687   "\C-l" gnus-recenter
1688   "I" gnus-summary-increase-score
1689   "L" gnus-summary-lower-score
1690   "\M-i" gnus-symbolic-argument
1691   "h" gnus-summary-select-article-buffer
1692
1693   "b" gnus-article-view-part
1694   "\M-t" gnus-summary-toggle-display-buttonized
1695
1696   "V" gnus-summary-score-map
1697   "X" gnus-uu-extract-map
1698   "S" gnus-summary-send-map)
1699
1700 ;; Sort of orthogonal keymap
1701 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1702   "t" gnus-summary-tick-article-forward
1703   "!" gnus-summary-tick-article-forward
1704   "d" gnus-summary-mark-as-read-forward
1705   "r" gnus-summary-mark-as-read-forward
1706   "c" gnus-summary-clear-mark-forward
1707   " " gnus-summary-clear-mark-forward
1708   "e" gnus-summary-mark-as-expirable
1709   "x" gnus-summary-mark-as-expirable
1710   "?" gnus-summary-mark-as-dormant
1711   "b" gnus-summary-set-bookmark
1712   "B" gnus-summary-remove-bookmark
1713   "#" gnus-summary-mark-as-processable
1714   "\M-#" gnus-summary-unmark-as-processable
1715   "S" gnus-summary-limit-include-expunged
1716   "C" gnus-summary-catchup
1717   "H" gnus-summary-catchup-to-here
1718   "h" gnus-summary-catchup-from-here
1719   "\C-c" gnus-summary-catchup-all
1720   "k" gnus-summary-kill-same-subject-and-select
1721   "K" gnus-summary-kill-same-subject
1722   "P" gnus-uu-mark-map)
1723
1724 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1725   "c" gnus-summary-clear-above
1726   "u" gnus-summary-tick-above
1727   "m" gnus-summary-mark-above
1728   "k" gnus-summary-kill-below)
1729
1730 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1731   "/" gnus-summary-limit-to-subject
1732   "n" gnus-summary-limit-to-articles
1733   "w" gnus-summary-pop-limit
1734   "s" gnus-summary-limit-to-subject
1735   "a" gnus-summary-limit-to-author
1736   "u" gnus-summary-limit-to-unread
1737   "m" gnus-summary-limit-to-marks
1738   "M" gnus-summary-limit-exclude-marks
1739   "v" gnus-summary-limit-to-score
1740   "*" gnus-summary-limit-include-cached
1741   "D" gnus-summary-limit-include-dormant
1742   "T" gnus-summary-limit-include-thread
1743   "d" gnus-summary-limit-exclude-dormant
1744   "t" gnus-summary-limit-to-age
1745   "." gnus-summary-limit-to-unseen
1746   "x" gnus-summary-limit-to-extra
1747   "p" gnus-summary-limit-to-display-predicate
1748   "E" gnus-summary-limit-include-expunged
1749   "c" gnus-summary-limit-exclude-childless-dormant
1750   "C" gnus-summary-limit-mark-excluded-as-read
1751   "o" gnus-summary-insert-old-articles
1752   "N" gnus-summary-insert-new-articles)
1753
1754 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1755   "n" gnus-summary-next-unread-article
1756   "p" gnus-summary-prev-unread-article
1757   "N" gnus-summary-next-article
1758   "P" gnus-summary-prev-article
1759   "\C-n" gnus-summary-next-same-subject
1760   "\C-p" gnus-summary-prev-same-subject
1761   "\M-n" gnus-summary-next-unread-subject
1762   "\M-p" gnus-summary-prev-unread-subject
1763   "f" gnus-summary-first-unread-article
1764   "b" gnus-summary-best-unread-article
1765   "j" gnus-summary-goto-article
1766   "g" gnus-summary-goto-subject
1767   "l" gnus-summary-goto-last-article
1768   "o" gnus-summary-pop-article)
1769
1770 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1771   "k" gnus-summary-kill-thread
1772   "l" gnus-summary-lower-thread
1773   "i" gnus-summary-raise-thread
1774   "T" gnus-summary-toggle-threads
1775   "t" gnus-summary-rethread-current
1776   "^" gnus-summary-reparent-thread
1777   "s" gnus-summary-show-thread
1778   "S" gnus-summary-show-all-threads
1779   "h" gnus-summary-hide-thread
1780   "H" gnus-summary-hide-all-threads
1781   "n" gnus-summary-next-thread
1782   "p" gnus-summary-prev-thread
1783   "u" gnus-summary-up-thread
1784   "o" gnus-summary-top-thread
1785   "d" gnus-summary-down-thread
1786   "#" gnus-uu-mark-thread
1787   "\M-#" gnus-uu-unmark-thread)
1788
1789 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1790   "g" gnus-summary-prepare
1791   "c" gnus-summary-insert-cached-articles
1792   "d" gnus-summary-insert-dormant-articles)
1793
1794 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1795   "c" gnus-summary-catchup-and-exit
1796   "C" gnus-summary-catchup-all-and-exit
1797   "E" gnus-summary-exit-no-update
1798   "Q" gnus-summary-exit
1799   "Z" gnus-summary-exit
1800   "n" gnus-summary-catchup-and-goto-next-group
1801   "R" gnus-summary-reselect-current-group
1802   "G" gnus-summary-rescan-group
1803   "N" gnus-summary-next-group
1804   "s" gnus-summary-save-newsrc
1805   "P" gnus-summary-prev-group)
1806
1807 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1808   " " gnus-summary-next-page
1809   "n" gnus-summary-next-page
1810   "\177" gnus-summary-prev-page
1811   [delete] gnus-summary-prev-page
1812   "p" gnus-summary-prev-page
1813   "\r" gnus-summary-scroll-up
1814   "\M-\r" gnus-summary-scroll-down
1815   "<" gnus-summary-beginning-of-article
1816   ">" gnus-summary-end-of-article
1817   "b" gnus-summary-beginning-of-article
1818   "e" gnus-summary-end-of-article
1819   "^" gnus-summary-refer-parent-article
1820   "r" gnus-summary-refer-parent-article
1821   "D" gnus-summary-enter-digest-group
1822   "R" gnus-summary-refer-references
1823   "T" gnus-summary-refer-thread
1824   "g" gnus-summary-show-article
1825   "s" gnus-summary-isearch-article
1826   "P" gnus-summary-print-article
1827   "M" gnus-mailing-list-insinuate
1828   "t" gnus-article-babel)
1829
1830 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1831   "b" gnus-article-add-buttons
1832   "B" gnus-article-add-buttons-to-head
1833   "o" gnus-article-treat-overstrike
1834   "e" gnus-article-emphasize
1835   "w" gnus-article-fill-cited-article
1836   "Q" gnus-article-fill-long-lines
1837   "C" gnus-article-capitalize-sentences
1838   "c" gnus-article-remove-cr
1839   "q" gnus-article-de-quoted-unreadable
1840   "6" gnus-article-de-base64-unreadable
1841   "Z" gnus-article-decode-HZ
1842   "h" gnus-article-wash-html
1843   "u" gnus-article-unsplit-urls
1844   "s" gnus-summary-force-verify-and-decrypt
1845   "f" gnus-article-display-x-face
1846   "l" gnus-summary-stop-page-breaking
1847   "r" gnus-summary-caesar-message
1848   "m" gnus-summary-morse-message
1849   "t" gnus-summary-toggle-header
1850   "g" gnus-treat-smiley
1851   "v" gnus-summary-verbose-headers
1852   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1853   "p" gnus-article-verify-x-pgp-sig
1854   "d" gnus-article-treat-dumbquotes)
1855
1856 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1857   ;; mnemonic: deuglif*Y*
1858   "u" gnus-article-outlook-unwrap-lines
1859   "a" gnus-article-outlook-repair-attribution
1860   "c" gnus-article-outlook-rearrange-citation
1861   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1862
1863 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1864   "a" gnus-article-hide
1865   "h" gnus-article-hide-headers
1866   "b" gnus-article-hide-boring-headers
1867   "s" gnus-article-hide-signature
1868   "c" gnus-article-hide-citation
1869   "C" gnus-article-hide-citation-in-followups
1870   "l" gnus-article-hide-list-identifiers
1871   "B" gnus-article-strip-banner
1872   "P" gnus-article-hide-pem
1873   "\C-c" gnus-article-hide-citation-maybe)
1874
1875 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1876   "a" gnus-article-highlight
1877   "h" gnus-article-highlight-headers
1878   "c" gnus-article-highlight-citation
1879   "s" gnus-article-highlight-signature)
1880
1881 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1882   "f" gnus-article-treat-fold-headers
1883   "u" gnus-article-treat-unfold-headers
1884   "n" gnus-article-treat-fold-newsgroups)
1885
1886 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1887   "x" gnus-article-display-x-face
1888   "d" gnus-article-display-face
1889   "s" gnus-treat-smiley
1890   "D" gnus-article-remove-images
1891   "f" gnus-treat-from-picon
1892   "m" gnus-treat-mail-picon
1893   "n" gnus-treat-newsgroups-picon)
1894
1895 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1896   "w" gnus-article-decode-mime-words
1897   "c" gnus-article-decode-charset
1898   "v" gnus-mime-view-all-parts
1899   "b" gnus-article-view-part)
1900
1901 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1902   "z" gnus-article-date-ut
1903   "u" gnus-article-date-ut
1904   "l" gnus-article-date-local
1905   "p" gnus-article-date-english
1906   "e" gnus-article-date-lapsed
1907   "o" gnus-article-date-original
1908   "i" gnus-article-date-iso8601
1909   "s" gnus-article-date-user)
1910
1911 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1912   "t" gnus-article-remove-trailing-blank-lines
1913   "l" gnus-article-strip-leading-blank-lines
1914   "m" gnus-article-strip-multiple-blank-lines
1915   "a" gnus-article-strip-blank-lines
1916   "A" gnus-article-strip-all-blank-lines
1917   "s" gnus-article-strip-leading-space
1918   "e" gnus-article-strip-trailing-space
1919   "w" gnus-article-remove-leading-whitespace)
1920
1921 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1922   "v" gnus-version
1923   "f" gnus-summary-fetch-faq
1924   "d" gnus-summary-describe-group
1925   "h" gnus-summary-describe-briefly
1926   "i" gnus-info-find-node
1927   "c" gnus-group-fetch-charter
1928   "C" gnus-group-fetch-control)
1929
1930 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1931   "e" gnus-summary-expire-articles
1932   "\M-\C-e" gnus-summary-expire-articles-now
1933   "\177" gnus-summary-delete-article
1934   [delete] gnus-summary-delete-article
1935   [backspace] gnus-summary-delete-article
1936   "m" gnus-summary-move-article
1937   "r" gnus-summary-respool-article
1938   "w" gnus-summary-edit-article
1939   "c" gnus-summary-copy-article
1940   "B" gnus-summary-crosspost-article
1941   "q" gnus-summary-respool-query
1942   "t" gnus-summary-respool-trace
1943   "i" gnus-summary-import-article
1944   "I" gnus-summary-create-article
1945   "p" gnus-summary-article-posted-p)
1946
1947 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1948   "o" gnus-summary-save-article
1949   "m" gnus-summary-save-article-mail
1950   "F" gnus-summary-write-article-file
1951   "r" gnus-summary-save-article-rmail
1952   "f" gnus-summary-save-article-file
1953   "b" gnus-summary-save-article-body-file
1954   "h" gnus-summary-save-article-folder
1955   "v" gnus-summary-save-article-vm
1956   "p" gnus-summary-pipe-output
1957   "P" gnus-summary-muttprint
1958   "s" gnus-soup-add-article)
1959
1960 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1961   "b" gnus-summary-display-buttonized
1962   "m" gnus-summary-repair-multipart
1963   "v" gnus-article-view-part
1964   "o" gnus-article-save-part
1965   "c" gnus-article-copy-part
1966   "C" gnus-article-view-part-as-charset
1967   "e" gnus-article-view-part-externally
1968   "E" gnus-article-encrypt-body
1969   "i" gnus-article-inline-part
1970   "|" gnus-article-pipe-part)
1971
1972 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1973   "p" gnus-summary-mark-as-processable
1974   "u" gnus-summary-unmark-as-processable
1975   "U" gnus-summary-unmark-all-processable
1976   "v" gnus-uu-mark-over
1977   "s" gnus-uu-mark-series
1978   "r" gnus-uu-mark-region
1979   "g" gnus-uu-unmark-region
1980   "R" gnus-uu-mark-by-regexp
1981   "G" gnus-uu-unmark-by-regexp
1982   "t" gnus-uu-mark-thread
1983   "T" gnus-uu-unmark-thread
1984   "a" gnus-uu-mark-all
1985   "b" gnus-uu-mark-buffer
1986   "S" gnus-uu-mark-sparse
1987   "k" gnus-summary-kill-process-mark
1988   "y" gnus-summary-yank-process-mark
1989   "w" gnus-summary-save-process-mark
1990   "i" gnus-uu-invert-processable)
1991
1992 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1993   ;;"x" gnus-uu-extract-any
1994   "m" gnus-summary-save-parts
1995   "u" gnus-uu-decode-uu
1996   "U" gnus-uu-decode-uu-and-save
1997   "s" gnus-uu-decode-unshar
1998   "S" gnus-uu-decode-unshar-and-save
1999   "o" gnus-uu-decode-save
2000   "O" gnus-uu-decode-save
2001   "b" gnus-uu-decode-binhex
2002   "B" gnus-uu-decode-binhex
2003   "p" gnus-uu-decode-postscript
2004   "P" gnus-uu-decode-postscript-and-save)
2005
2006 (gnus-define-keys
2007     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2008   "u" gnus-uu-decode-uu-view
2009   "U" gnus-uu-decode-uu-and-save-view
2010   "s" gnus-uu-decode-unshar-view
2011   "S" gnus-uu-decode-unshar-and-save-view
2012   "o" gnus-uu-decode-save-view
2013   "O" gnus-uu-decode-save-view
2014   "b" gnus-uu-decode-binhex-view
2015   "B" gnus-uu-decode-binhex-view
2016   "p" gnus-uu-decode-postscript-view
2017   "P" gnus-uu-decode-postscript-and-save-view)
2018
2019 (defvar gnus-article-post-menu nil)
2020
2021 (defconst gnus-summary-menu-maxlen 20)
2022
2023 (defun gnus-summary-menu-split (menu)
2024   ;; If we have lots of elements, divide them into groups of 20
2025   ;; and make a pane (or submenu) for each one.
2026   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2027       (let ((menu menu) sublists next
2028             (i 1))
2029         (while menu
2030           ;; Pull off the next gnus-summary-menu-maxlen elements
2031           ;; and make them the next element of sublist.
2032           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2033           (if next
2034               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2035                       nil))
2036           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2037                                              (aref (car (last menu)) 0)) menu)
2038                                sublists))
2039           (setq i (1+ i))
2040           (setq menu next))
2041         (nreverse sublists))
2042     ;; Few elements--put them all in one pane.
2043     menu))
2044
2045 (defun gnus-summary-make-menu-bar ()
2046   (gnus-turn-off-edit-menu 'summary)
2047
2048   (unless (boundp 'gnus-summary-misc-menu)
2049
2050     (easy-menu-define
2051       gnus-summary-kill-menu gnus-summary-mode-map ""
2052       (cons
2053        "Score"
2054        (nconc
2055         (list
2056          ["Customize" gnus-score-customize t])
2057         (gnus-make-score-map 'increase)
2058         (gnus-make-score-map 'lower)
2059         '(("Mark"
2060            ["Kill below" gnus-summary-kill-below t]
2061            ["Mark above" gnus-summary-mark-above t]
2062            ["Tick above" gnus-summary-tick-above t]
2063            ["Clear above" gnus-summary-clear-above t])
2064           ["Current score" gnus-summary-current-score t]
2065           ["Set score" gnus-summary-set-score t]
2066           ["Switch current score file..." gnus-score-change-score-file t]
2067           ["Set mark below..." gnus-score-set-mark-below t]
2068           ["Set expunge below..." gnus-score-set-expunge-below t]
2069           ["Edit current score file" gnus-score-edit-current-scores t]
2070           ["Edit score file" gnus-score-edit-file t]
2071           ["Trace score" gnus-score-find-trace t]
2072           ["Find words" gnus-score-find-favourite-words t]
2073           ["Rescore buffer" gnus-summary-rescore t]
2074           ["Increase score..." gnus-summary-increase-score t]
2075           ["Lower score..." gnus-summary-lower-score t]))))
2076
2077     ;; Define both the Article menu in the summary buffer and the
2078     ;; equivalent Commands menu in the article buffer here for
2079     ;; consistency.
2080     (let ((innards
2081            `(("Hide"
2082               ["All" gnus-article-hide t]
2083               ["Headers" gnus-article-hide-headers t]
2084               ["Signature" gnus-article-hide-signature t]
2085               ["Citation" gnus-article-hide-citation t]
2086               ["List identifiers" gnus-article-hide-list-identifiers t]
2087               ["Banner" gnus-article-strip-banner t]
2088               ["Boring headers" gnus-article-hide-boring-headers t])
2089              ("Highlight"
2090               ["All" gnus-article-highlight t]
2091               ["Headers" gnus-article-highlight-headers t]
2092               ["Signature" gnus-article-highlight-signature t]
2093               ["Citation" gnus-article-highlight-citation t])
2094              ("MIME"
2095               ["Words" gnus-article-decode-mime-words t]
2096               ["Charset" gnus-article-decode-charset t]
2097               ["QP" gnus-article-de-quoted-unreadable t]
2098               ["Base64" gnus-article-de-base64-unreadable t]
2099               ["View MIME buttons" gnus-summary-display-buttonized t]
2100               ["View all" gnus-mime-view-all-parts t]
2101               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2102               ["Encrypt body" gnus-article-encrypt-body
2103                :active (not (gnus-group-read-only-p))
2104                ,@(if (featurep 'xemacs) nil
2105                    '(:help "Encrypt the message body on disk"))]
2106               ["Extract all parts" gnus-summary-save-parts t]
2107               ("Multipart"
2108                ["Repair multipart" gnus-summary-repair-multipart t]
2109                ["Add buttons" gnus-summary-display-buttonized t]
2110                ["Pipe part" gnus-article-pipe-part t]
2111                ["Inline part" gnus-article-inline-part t]
2112                ["Encrypt body" gnus-article-encrypt-body
2113                 :active (not (gnus-group-read-only-p))
2114                ,@(if (featurep 'xemacs) nil
2115                    '(:help "Encrypt the message body on disk"))]
2116                ["View part externally" gnus-article-view-part-externally t]
2117                ["View part with charset" gnus-article-view-part-as-charset t]
2118                ["Copy part" gnus-article-copy-part t]
2119                ["Save part" gnus-article-save-part t]
2120                ["View part" gnus-article-view-part t]))
2121              ("Date"
2122               ["Local" gnus-article-date-local t]
2123               ["ISO8601" gnus-article-date-iso8601 t]
2124               ["UT" gnus-article-date-ut t]
2125               ["Original" gnus-article-date-original t]
2126               ["Lapsed" gnus-article-date-lapsed t]
2127               ["User-defined" gnus-article-date-user t])
2128              ("Display"
2129               ["Remove images" gnus-article-remove-images t]
2130               ["Toggle smiley" gnus-treat-smiley t]
2131               ["Show X-Face" gnus-article-display-x-face t]
2132               ["Show picons in From" gnus-treat-from-picon t]
2133               ["Show picons in mail headers" gnus-treat-mail-picon t]
2134               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2135               ("View as different encoding"
2136                ,@(gnus-summary-menu-split
2137                   (mapcar
2138                    (lambda (cs)
2139                      ;; Since easymenu under Emacs doesn't allow
2140                      ;; lambda forms for menu commands, we should
2141                      ;; provide intern'ed function symbols.
2142                      (let ((command (intern (format "\
2143 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2144                        (fset command
2145                              `(lambda ()
2146                                 (interactive)
2147                                 (let ((gnus-summary-show-article-charset-alist
2148                                        '((1 . ,cs))))
2149                                   (gnus-summary-show-article 1))))
2150                        `[,(symbol-name cs) ,command t]))
2151                    (sort (if (fboundp 'coding-system-list)
2152                              (coding-system-list)
2153                            (mapcar 'car mm-mime-mule-charset-alist))
2154                          'string<)))))
2155              ("Washing"
2156               ("Remove Blanks"
2157                ["Leading" gnus-article-strip-leading-blank-lines t]
2158                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2159                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2160                ["All of the above" gnus-article-strip-blank-lines t]
2161                ["All" gnus-article-strip-all-blank-lines t]
2162                ["Leading space" gnus-article-strip-leading-space t]
2163                ["Trailing space" gnus-article-strip-trailing-space t]
2164                ["Leading space in headers"
2165                 gnus-article-remove-leading-whitespace t])
2166               ["Overstrike" gnus-article-treat-overstrike t]
2167               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2168               ["Emphasis" gnus-article-emphasize t]
2169               ["Word wrap" gnus-article-fill-cited-article t]
2170               ["Fill long lines" gnus-article-fill-long-lines t]
2171               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2172               ["CR" gnus-article-remove-cr t]
2173               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2174               ["Base64" gnus-article-de-base64-unreadable t]
2175               ["Rot 13" gnus-summary-caesar-message
2176                ,@(if (featurep 'xemacs) '(t)
2177                    '(:help "\"Caesar rotate\" article by 13"))]
2178               ["Morse decode" gnus-summary-morse-message t]
2179               ["Unix pipe..." gnus-summary-pipe-message t]
2180               ["Add buttons" gnus-article-add-buttons t]
2181               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2182               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2183               ["Verbose header" gnus-summary-verbose-headers t]
2184               ["Toggle header" gnus-summary-toggle-header t]
2185               ["Unfold headers" gnus-article-treat-unfold-headers t]
2186               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2187               ["Html" gnus-article-wash-html t]
2188               ["URLs" gnus-article-unsplit-urls t]
2189               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2190               ["HZ" gnus-article-decode-HZ t]
2191               ("(Outlook) Deuglify"
2192                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2193                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2194                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2195                ["Full (Outlook) deuglify"
2196                 gnus-article-outlook-deuglify-article t])
2197               )
2198              ("Output"
2199               ["Save in default format" gnus-summary-save-article
2200                ,@(if (featurep 'xemacs) '(t)
2201                    '(:help "Save article using default method"))]
2202               ["Save in file" gnus-summary-save-article-file
2203                ,@(if (featurep 'xemacs) '(t)
2204                    '(:help "Save article in file"))]
2205               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2206               ["Save in MH folder" gnus-summary-save-article-folder t]
2207               ["Save in VM folder" gnus-summary-save-article-vm t]
2208               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2209               ["Save body in file" gnus-summary-save-article-body-file t]
2210               ["Pipe through a filter" gnus-summary-pipe-output t]
2211               ["Add to SOUP packet" gnus-soup-add-article t]
2212               ["Print with Muttprint" gnus-summary-muttprint t]
2213               ["Print" gnus-summary-print-article t])
2214              ("Backend"
2215               ["Respool article..." gnus-summary-respool-article t]
2216               ["Move article..." gnus-summary-move-article
2217                (gnus-check-backend-function
2218                 'request-move-article gnus-newsgroup-name)]
2219               ["Copy article..." gnus-summary-copy-article t]
2220               ["Crosspost article..." gnus-summary-crosspost-article
2221                (gnus-check-backend-function
2222                 'request-replace-article gnus-newsgroup-name)]
2223               ["Import file..." gnus-summary-import-article
2224                (gnus-check-backend-function
2225                 'request-accept-article gnus-newsgroup-name)]
2226               ["Create article..." gnus-summary-create-article
2227                (gnus-check-backend-function
2228                 'request-accept-article gnus-newsgroup-name)]
2229               ["Check if posted" gnus-summary-article-posted-p t]
2230               ["Edit article" gnus-summary-edit-article
2231                (not (gnus-group-read-only-p))]
2232               ["Delete article" gnus-summary-delete-article
2233                (gnus-check-backend-function
2234                 'request-expire-articles gnus-newsgroup-name)]
2235               ["Query respool" gnus-summary-respool-query t]
2236               ["Trace respool" gnus-summary-respool-trace t]
2237               ["Delete expirable articles" gnus-summary-expire-articles-now
2238                (gnus-check-backend-function
2239                 'request-expire-articles gnus-newsgroup-name)])
2240              ("Extract"
2241               ["Uudecode" gnus-uu-decode-uu
2242                ,@(if (featurep 'xemacs) '(t)
2243                    '(:help "Decode uuencoded article(s)"))]
2244               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2245               ["Unshar" gnus-uu-decode-unshar t]
2246               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2247               ["Save" gnus-uu-decode-save t]
2248               ["Binhex" gnus-uu-decode-binhex t]
2249               ["Postscript" gnus-uu-decode-postscript t]
2250               ["All MIME parts" gnus-summary-save-parts t])
2251              ("Cache"
2252               ["Enter article" gnus-cache-enter-article t]
2253               ["Remove article" gnus-cache-remove-article t])
2254              ["Translate" gnus-article-babel t]
2255              ["Select article buffer" gnus-summary-select-article-buffer t]
2256              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2257              ["Isearch article..." gnus-summary-isearch-article t]
2258              ["Beginning of the article" gnus-summary-beginning-of-article t]
2259              ["End of the article" gnus-summary-end-of-article t]
2260              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2261              ["Fetch referenced articles" gnus-summary-refer-references t]
2262              ["Fetch current thread" gnus-summary-refer-thread t]
2263              ["Fetch article with id..." gnus-summary-refer-article t]
2264              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2265              ["Redisplay" gnus-summary-show-article t]
2266              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2267       (easy-menu-define
2268         gnus-summary-article-menu gnus-summary-mode-map ""
2269         (cons "Article" innards))
2270
2271       (if (not (keymapp gnus-summary-article-menu))
2272           (easy-menu-define
2273             gnus-article-commands-menu gnus-article-mode-map ""
2274             (cons "Commands" innards))
2275         ;; in Emacs, don't share menu.
2276         (setq gnus-article-commands-menu
2277               (copy-keymap gnus-summary-article-menu))
2278         (define-key gnus-article-mode-map [menu-bar commands]
2279           (cons "Commands" gnus-article-commands-menu))))
2280
2281     (easy-menu-define
2282       gnus-summary-thread-menu gnus-summary-mode-map ""
2283       '("Threads"
2284         ["Find all messages in thread" gnus-summary-refer-thread t]
2285         ["Toggle threading" gnus-summary-toggle-threads t]
2286         ["Hide threads" gnus-summary-hide-all-threads t]
2287         ["Show threads" gnus-summary-show-all-threads t]
2288         ["Hide thread" gnus-summary-hide-thread t]
2289         ["Show thread" gnus-summary-show-thread t]
2290         ["Go to next thread" gnus-summary-next-thread t]
2291         ["Go to previous thread" gnus-summary-prev-thread t]
2292         ["Go down thread" gnus-summary-down-thread t]
2293         ["Go up thread" gnus-summary-up-thread t]
2294         ["Top of thread" gnus-summary-top-thread t]
2295         ["Mark thread as read" gnus-summary-kill-thread t]
2296         ["Lower thread score" gnus-summary-lower-thread t]
2297         ["Raise thread score" gnus-summary-raise-thread t]
2298         ["Rethread current" gnus-summary-rethread-current t]))
2299
2300     (easy-menu-define
2301       gnus-summary-post-menu gnus-summary-mode-map ""
2302       `("Post"
2303         ["Send a message (mail or news)" gnus-summary-post-news
2304          ,@(if (featurep 'xemacs) '(t)
2305              '(:help "Post an article"))]
2306         ["Followup" gnus-summary-followup
2307          ,@(if (featurep 'xemacs) '(t)
2308              '(:help "Post followup to this article"))]
2309         ["Followup and yank" gnus-summary-followup-with-original
2310          ,@(if (featurep 'xemacs) '(t)
2311              '(:help "Post followup to this article, quoting its contents"))]
2312         ["Supersede article" gnus-summary-supersede-article t]
2313         ["Cancel article" gnus-summary-cancel-article
2314          ,@(if (featurep 'xemacs) '(t)
2315              '(:help "Cancel an article you posted"))]
2316         ["Reply" gnus-summary-reply t]
2317         ["Reply and yank" gnus-summary-reply-with-original t]
2318         ["Wide reply" gnus-summary-wide-reply t]
2319         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2320          ,@(if (featurep 'xemacs) '(t)
2321              '(:help "Mail a reply, quoting this article"))]
2322         ["Very wide reply" gnus-summary-very-wide-reply t]
2323         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2324          ,@(if (featurep 'xemacs) '(t)
2325              '(:help "Mail a very wide reply, quoting this article"))]
2326         ["Mail forward" gnus-summary-mail-forward t]
2327         ["Post forward" gnus-summary-post-forward t]
2328         ["Digest and mail" gnus-uu-digest-mail-forward t]
2329         ["Digest and post" gnus-uu-digest-post-forward t]
2330         ["Resend message" gnus-summary-resend-message t]
2331         ["Resend message edit" gnus-summary-resend-message-edit t]
2332         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2333         ["Send a mail" gnus-summary-mail-other-window t]
2334         ["Create a local message" gnus-summary-news-other-window t]
2335         ["Uuencode and post" gnus-uu-post-news
2336          ,@(if (featurep 'xemacs) '(t)
2337              '(:help "Post a uuencoded article"))]
2338         ["Followup via news" gnus-summary-followup-to-mail t]
2339         ["Followup via news and yank"
2340          gnus-summary-followup-to-mail-with-original t]
2341         ;;("Draft"
2342         ;;["Send" gnus-summary-send-draft t]
2343         ;;["Send bounced" gnus-resend-bounced-mail t])
2344         ))
2345
2346     (cond
2347      ((not (keymapp gnus-summary-post-menu))
2348       (setq gnus-article-post-menu gnus-summary-post-menu))
2349      ((not gnus-article-post-menu)
2350       ;; Don't share post menu.
2351       (setq gnus-article-post-menu
2352             (copy-keymap gnus-summary-post-menu))))
2353     (define-key gnus-article-mode-map [menu-bar post]
2354       (cons "Post" gnus-article-post-menu))
2355
2356     (easy-menu-define
2357       gnus-summary-misc-menu gnus-summary-mode-map ""
2358       `("Gnus"
2359         ("Mark Read"
2360          ["Mark as read" gnus-summary-mark-as-read-forward t]
2361          ["Mark same subject and select"
2362           gnus-summary-kill-same-subject-and-select t]
2363          ["Mark same subject" gnus-summary-kill-same-subject t]
2364          ["Catchup" gnus-summary-catchup
2365           ,@(if (featurep 'xemacs) '(t)
2366               '(:help "Mark unread articles in this group as read"))]
2367          ["Catchup all" gnus-summary-catchup-all t]
2368          ["Catchup to here" gnus-summary-catchup-to-here t]
2369          ["Catchup from here" gnus-summary-catchup-from-here t]
2370          ["Catchup region" gnus-summary-mark-region-as-read 
2371           (gnus-mark-active-p)]
2372          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2373         ("Mark Various"
2374          ["Tick" gnus-summary-tick-article-forward t]
2375          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2376          ["Remove marks" gnus-summary-clear-mark-forward t]
2377          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2378          ["Set bookmark" gnus-summary-set-bookmark t]
2379          ["Remove bookmark" gnus-summary-remove-bookmark t])
2380         ("Limit to"
2381          ["Marks..." gnus-summary-limit-to-marks t]
2382          ["Subject..." gnus-summary-limit-to-subject t]
2383          ["Author..." gnus-summary-limit-to-author t]
2384          ["Age..." gnus-summary-limit-to-age t]
2385          ["Extra..." gnus-summary-limit-to-extra t]
2386          ["Score..." gnus-summary-limit-to-score t]
2387          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2388          ["Unread" gnus-summary-limit-to-unread t]
2389          ["Unseen" gnus-summary-limit-to-unseen t]
2390          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2391          ["Next articles" gnus-summary-limit-to-articles t]
2392          ["Pop limit" gnus-summary-pop-limit t]
2393          ["Show dormant" gnus-summary-limit-include-dormant t]
2394          ["Hide childless dormant"
2395           gnus-summary-limit-exclude-childless-dormant t]
2396          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2397          ["Hide marked" gnus-summary-limit-exclude-marks t]
2398          ["Show expunged" gnus-summary-limit-include-expunged t])
2399         ("Process Mark"
2400          ["Set mark" gnus-summary-mark-as-processable t]
2401          ["Remove mark" gnus-summary-unmark-as-processable t]
2402          ["Remove all marks" gnus-summary-unmark-all-processable t]
2403          ["Mark above" gnus-uu-mark-over t]
2404          ["Mark series" gnus-uu-mark-series t]
2405          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2406          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2407          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2408          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2409          ["Mark all" gnus-uu-mark-all t]
2410          ["Mark buffer" gnus-uu-mark-buffer t]
2411          ["Mark sparse" gnus-uu-mark-sparse t]
2412          ["Mark thread" gnus-uu-mark-thread t]
2413          ["Unmark thread" gnus-uu-unmark-thread t]
2414          ("Process Mark Sets"
2415           ["Kill" gnus-summary-kill-process-mark t]
2416           ["Yank" gnus-summary-yank-process-mark
2417            gnus-newsgroup-process-stack]
2418           ["Save" gnus-summary-save-process-mark t]
2419           ["Run command on marked..." gnus-summary-universal-argument t]))
2420         ("Scroll article"
2421          ["Page forward" gnus-summary-next-page
2422           ,@(if (featurep 'xemacs) '(t)
2423               '(:help "Show next page of article"))]
2424          ["Page backward" gnus-summary-prev-page
2425           ,@(if (featurep 'xemacs) '(t)
2426               '(:help "Show previous page of article"))]
2427          ["Line forward" gnus-summary-scroll-up t])
2428         ("Move"
2429          ["Next unread article" gnus-summary-next-unread-article t]
2430          ["Previous unread article" gnus-summary-prev-unread-article t]
2431          ["Next article" gnus-summary-next-article t]
2432          ["Previous article" gnus-summary-prev-article t]
2433          ["Next unread subject" gnus-summary-next-unread-subject t]
2434          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2435          ["Next article same subject" gnus-summary-next-same-subject t]
2436          ["Previous article same subject" gnus-summary-prev-same-subject t]
2437          ["First unread article" gnus-summary-first-unread-article t]
2438          ["Best unread article" gnus-summary-best-unread-article t]
2439          ["Go to subject number..." gnus-summary-goto-subject t]
2440          ["Go to article number..." gnus-summary-goto-article t]
2441          ["Go to the last article" gnus-summary-goto-last-article t]
2442          ["Pop article off history" gnus-summary-pop-article t])
2443         ("Sort"
2444          ["Sort by number" gnus-summary-sort-by-number t]
2445          ["Sort by author" gnus-summary-sort-by-author t]
2446          ["Sort by subject" gnus-summary-sort-by-subject t]
2447          ["Sort by date" gnus-summary-sort-by-date t]
2448          ["Sort by score" gnus-summary-sort-by-score t]
2449          ["Sort by lines" gnus-summary-sort-by-lines t]
2450          ["Sort by characters" gnus-summary-sort-by-chars t]
2451          ["Randomize" gnus-summary-sort-by-random t]
2452          ["Original sort" gnus-summary-sort-by-original t])
2453         ("Help"
2454          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2455          ["Describe group" gnus-summary-describe-group t]
2456          ["Fetch charter" gnus-group-fetch-charter
2457           ,@(if (featurep 'xemacs) nil
2458               '(:help "Display the charter of the current group"))]
2459          ["Fetch control message" gnus-group-fetch-control
2460           ,@(if (featurep 'xemacs) nil
2461               '(:help "Display the archived control message for the current group"))]
2462          ["Read manual" gnus-info-find-node t])
2463         ("Modes"
2464          ["Pick and read" gnus-pick-mode t]
2465          ["Binary" gnus-binary-mode t])
2466         ("Regeneration"
2467          ["Regenerate" gnus-summary-prepare t]
2468          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2469          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2470          ["Toggle threading" gnus-summary-toggle-threads t])
2471         ["See old articles" gnus-summary-insert-old-articles t]
2472         ["See new articles" gnus-summary-insert-new-articles t]
2473         ["Filter articles..." gnus-summary-execute-command t]
2474         ["Run command on articles..." gnus-summary-universal-argument t]
2475         ["Search articles forward..." gnus-summary-search-article-forward t]
2476         ["Search articles backward..." gnus-summary-search-article-backward t]
2477         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2478         ["Expand window" gnus-summary-expand-window t]
2479         ["Expire expirable articles" gnus-summary-expire-articles
2480          (gnus-check-backend-function
2481           'request-expire-articles gnus-newsgroup-name)]
2482         ["Edit local kill file" gnus-summary-edit-local-kill t]
2483         ["Edit main kill file" gnus-summary-edit-global-kill t]
2484         ["Edit group parameters" gnus-summary-edit-parameters t]
2485         ["Customize group parameters" gnus-summary-customize-parameters t]
2486         ["Send a bug report" gnus-bug t]
2487         ("Exit"
2488          ["Catchup and exit" gnus-summary-catchup-and-exit
2489           ,@(if (featurep 'xemacs) '(t)
2490               '(:help "Mark unread articles in this group as read, then exit"))]
2491          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2492          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2493          ["Exit group" gnus-summary-exit
2494           ,@(if (featurep 'xemacs) '(t)
2495               '(:help "Exit current group, return to group selection mode"))]
2496          ["Exit group without updating" gnus-summary-exit-no-update t]
2497          ["Exit and goto next group" gnus-summary-next-group t]
2498          ["Exit and goto prev group" gnus-summary-prev-group t]
2499          ["Reselect group" gnus-summary-reselect-current-group t]
2500          ["Rescan group" gnus-summary-rescan-group t]
2501          ["Update dribble" gnus-summary-save-newsrc t])))
2502
2503     (gnus-run-hooks 'gnus-summary-menu-hook)))
2504
2505 (defvar gnus-summary-tool-bar-map nil)
2506
2507 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2508 (defun gnus-summary-make-tool-bar ()
2509   (if (and (fboundp 'tool-bar-add-item-from-menu)
2510            (default-value 'tool-bar-mode)
2511            (not gnus-summary-tool-bar-map))
2512       (setq gnus-summary-tool-bar-map
2513             (let ((tool-bar-map (make-sparse-keymap))
2514                   (load-path (mm-image-load-path)))
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-post-news "post" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-summary-followup "followup" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-reply "reply" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2537               (tool-bar-add-item-from-menu
2538                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2543               (tool-bar-add-item-from-menu
2544                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2545               tool-bar-map)))
2546   (if gnus-summary-tool-bar-map
2547       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2548
2549 (defun gnus-score-set-default (var value)
2550   "A version of set that updates the GNU Emacs menu-bar."
2551   (set var value)
2552   ;; It is the message that forces the active status to be updated.
2553   (message ""))
2554
2555 (defun gnus-make-score-map (type)
2556   "Make a summary score map of type TYPE."
2557   (if t
2558       nil
2559     (let ((headers '(("author" "from" string)
2560                      ("subject" "subject" string)
2561                      ("article body" "body" string)
2562                      ("article head" "head" string)
2563                      ("xref" "xref" string)
2564                      ("extra header" "extra" string)
2565                      ("lines" "lines" number)
2566                      ("followups to author" "followup" string)))
2567           (types '((number ("less than" <)
2568                            ("greater than" >)
2569                            ("equal" =))
2570                    (string ("substring" s)
2571                            ("exact string" e)
2572                            ("fuzzy string" f)
2573                            ("regexp" r))))
2574           (perms '(("temporary" (current-time-string))
2575                    ("permanent" nil)
2576                    ("immediate" now)))
2577           header)
2578       (list
2579        (apply
2580         'nconc
2581         (list
2582          (if (eq type 'lower)
2583              "Lower score"
2584            "Increase score"))
2585         (let (outh)
2586           (while headers
2587             (setq header (car headers))
2588             (setq outh
2589                   (cons
2590                    (apply
2591                     'nconc
2592                     (list (car header))
2593                     (let ((ts (cdr (assoc (nth 2 header) types)))
2594                           outt)
2595                       (while ts
2596                         (setq outt
2597                               (cons
2598                                (apply
2599                                 'nconc
2600                                 (list (caar ts))
2601                                 (let ((ps perms)
2602                                       outp)
2603                                   (while ps
2604                                     (setq outp
2605                                           (cons
2606                                            (vector
2607                                             (caar ps)
2608                                             (list
2609                                              'gnus-summary-score-entry
2610                                              (nth 1 header)
2611                                              (if (or (string= (nth 1 header)
2612                                                               "head")
2613                                                      (string= (nth 1 header)
2614                                                               "body"))
2615                                                  ""
2616                                                (list 'gnus-summary-header
2617                                                      (nth 1 header)))
2618                                              (list 'quote (nth 1 (car ts)))
2619                                              (list 'gnus-score-delta-default
2620                                                    nil)
2621                                              (nth 1 (car ps))
2622                                              t)
2623                                             t)
2624                                            outp))
2625                                     (setq ps (cdr ps)))
2626                                   (list (nreverse outp))))
2627                                outt))
2628                         (setq ts (cdr ts)))
2629                       (list (nreverse outt))))
2630                    outh))
2631             (setq headers (cdr headers)))
2632           (list (nreverse outh))))))))
2633
2634 \f
2635
2636 (defun gnus-summary-mode (&optional group)
2637   "Major mode for reading articles.
2638
2639 All normal editing commands are switched off.
2640 \\<gnus-summary-mode-map>
2641 Each line in this buffer represents one article.  To read an
2642 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2643 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2644 respectively.
2645
2646 You can also post articles and send mail from this buffer.  To
2647 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2648 of an article, type `\\[gnus-summary-reply]'.
2649
2650 There are approx. one gazillion commands you can execute in this
2651 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2652
2653 The following commands are available:
2654
2655 \\{gnus-summary-mode-map}"
2656   (interactive)
2657   (kill-all-local-variables)
2658   (when (gnus-visual-p 'summary-menu 'menu)
2659     (gnus-summary-make-menu-bar)
2660     (gnus-summary-make-tool-bar))
2661   (gnus-summary-make-local-variables)
2662   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2663     (gnus-summary-make-local-variables))
2664   (gnus-make-thread-indent-array)
2665   (gnus-simplify-mode-line)
2666   (setq major-mode 'gnus-summary-mode)
2667   (setq mode-name "Summary")
2668   (make-local-variable 'minor-mode-alist)
2669   (use-local-map gnus-summary-mode-map)
2670   (buffer-disable-undo)
2671   (setq buffer-read-only t)             ;Disable modification
2672   (setq truncate-lines t)
2673   (setq selective-display t)
2674   (setq selective-display-ellipses t)   ;Display `...'
2675   (gnus-summary-set-display-table)
2676   (gnus-set-default-directory)
2677   (setq gnus-newsgroup-name group)
2678   (make-local-variable 'gnus-summary-line-format)
2679   (make-local-variable 'gnus-summary-line-format-spec)
2680   (make-local-variable 'gnus-summary-dummy-line-format)
2681   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2682   (make-local-variable 'gnus-summary-mark-positions)
2683   (gnus-make-local-hook 'pre-command-hook)
2684   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2685   (gnus-run-hooks 'gnus-summary-mode-hook)
2686   (turn-on-gnus-mailing-list-mode)
2687   (mm-enable-multibyte)
2688   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2689   (gnus-update-summary-mark-positions))
2690
2691 (defun gnus-summary-make-local-variables ()
2692   "Make all the local summary buffer variables."
2693   (let (global)
2694     (dolist (local gnus-summary-local-variables)
2695       (if (consp local)
2696           (progn
2697             (if (eq (cdr local) 'global)
2698                 ;; Copy the global value of the variable.
2699                 (setq global (symbol-value (car local)))
2700               ;; Use the value from the list.
2701               (setq global (eval (cdr local))))
2702             (set (make-local-variable (car local)) global))
2703         ;; Simple nil-valued local variable.
2704         (set (make-local-variable local) nil)))))
2705
2706 (defun gnus-summary-clear-local-variables ()
2707   (let ((locals gnus-summary-local-variables))
2708     (while locals
2709       (if (consp (car locals))
2710           (and (vectorp (caar locals))
2711                (set (caar locals) nil))
2712         (and (vectorp (car locals))
2713              (set (car locals) nil)))
2714       (setq locals (cdr locals)))))
2715
2716 ;; Summary data functions.
2717
2718 (defmacro gnus-data-number (data)
2719   `(car ,data))
2720
2721 (defmacro gnus-data-set-number (data number)
2722   `(setcar ,data ,number))
2723
2724 (defmacro gnus-data-mark (data)
2725   `(nth 1 ,data))
2726
2727 (defmacro gnus-data-set-mark (data mark)
2728   `(setcar (nthcdr 1 ,data) ,mark))
2729
2730 (defmacro gnus-data-pos (data)
2731   `(nth 2 ,data))
2732
2733 (defmacro gnus-data-set-pos (data pos)
2734   `(setcar (nthcdr 2 ,data) ,pos))
2735
2736 (defmacro gnus-data-header (data)
2737   `(nth 3 ,data))
2738
2739 (defmacro gnus-data-set-header (data header)
2740   `(setf (nth 3 ,data) ,header))
2741
2742 (defmacro gnus-data-level (data)
2743   `(nth 4 ,data))
2744
2745 (defmacro gnus-data-unread-p (data)
2746   `(= (nth 1 ,data) gnus-unread-mark))
2747
2748 (defmacro gnus-data-read-p (data)
2749   `(/= (nth 1 ,data) gnus-unread-mark))
2750
2751 (defmacro gnus-data-pseudo-p (data)
2752   `(consp (nth 3 ,data)))
2753
2754 (defmacro gnus-data-find (number)
2755   `(assq ,number gnus-newsgroup-data))
2756
2757 (defmacro gnus-data-find-list (number &optional data)
2758   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2759      (memq (assq ,number bdata)
2760            bdata)))
2761
2762 (defmacro gnus-data-make (number mark pos header level)
2763   `(list ,number ,mark ,pos ,header ,level))
2764
2765 (defun gnus-data-enter (after-article number mark pos header level offset)
2766   (let ((data (gnus-data-find-list after-article)))
2767     (unless data
2768       (error "No such article: %d" after-article))
2769     (setcdr data (cons (gnus-data-make number mark pos header level)
2770                        (cdr data)))
2771     (setq gnus-newsgroup-data-reverse nil)
2772     (gnus-data-update-list (cddr data) offset)))
2773
2774 (defun gnus-data-enter-list (after-article list &optional offset)
2775   (when list
2776     (let ((data (and after-article (gnus-data-find-list after-article)))
2777           (ilist list))
2778       (if (not (or data
2779                    after-article))
2780           (let ((odata gnus-newsgroup-data))
2781             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2782             (when offset
2783               (gnus-data-update-list odata offset)))
2784       ;; Find the last element in the list to be spliced into the main
2785         ;; list.
2786         (while (cdr list)
2787           (setq list (cdr list)))
2788         (if (not data)
2789             (progn
2790               (setcdr list gnus-newsgroup-data)
2791               (setq gnus-newsgroup-data ilist)
2792               (when offset
2793                 (gnus-data-update-list (cdr list) offset)))
2794           (setcdr list (cdr data))
2795           (setcdr data ilist)
2796           (when offset
2797             (gnus-data-update-list (cdr list) offset))))
2798       (setq gnus-newsgroup-data-reverse nil))))
2799
2800 (defun gnus-data-remove (article &optional offset)
2801   (let ((data gnus-newsgroup-data))
2802     (if (= (gnus-data-number (car data)) article)
2803         (progn
2804           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2805                 gnus-newsgroup-data-reverse nil)
2806           (when offset
2807             (gnus-data-update-list gnus-newsgroup-data offset)))
2808       (while (cdr data)
2809         (when (= (gnus-data-number (cadr data)) article)
2810           (setcdr data (cddr data))
2811           (when offset
2812             (gnus-data-update-list (cdr data) offset))
2813           (setq data nil
2814                 gnus-newsgroup-data-reverse nil))
2815         (setq data (cdr data))))))
2816
2817 (defmacro gnus-data-list (backward)
2818   `(if ,backward
2819        (or gnus-newsgroup-data-reverse
2820            (setq gnus-newsgroup-data-reverse
2821                  (reverse gnus-newsgroup-data)))
2822      gnus-newsgroup-data))
2823
2824 (defun gnus-data-update-list (data offset)
2825   "Add OFFSET to the POS of all data entries in DATA."
2826   (setq gnus-newsgroup-data-reverse nil)
2827   (while data
2828     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2829     (setq data (cdr data))))
2830
2831 (defun gnus-summary-article-pseudo-p (article)
2832   "Say whether this article is a pseudo article or not."
2833   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2834
2835 (defmacro gnus-summary-article-sparse-p (article)
2836   "Say whether this article is a sparse article or not."
2837   `(memq ,article gnus-newsgroup-sparse))
2838
2839 (defmacro gnus-summary-article-ancient-p (article)
2840   "Say whether this article is a sparse article or not."
2841   `(memq ,article gnus-newsgroup-ancient))
2842
2843 (defun gnus-article-parent-p (number)
2844   "Say whether this article is a parent or not."
2845   (let ((data (gnus-data-find-list number)))
2846     (and (cdr data)              ; There has to be an article after...
2847          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2848             (gnus-data-level (nth 1 data))))))
2849
2850 (defun gnus-article-children (number)
2851   "Return a list of all children to NUMBER."
2852   (let* ((data (gnus-data-find-list number))
2853          (level (gnus-data-level (car data)))
2854          children)
2855     (setq data (cdr data))
2856     (while (and data
2857                 (= (gnus-data-level (car data)) (1+ level)))
2858       (push (gnus-data-number (car data)) children)
2859       (setq data (cdr data)))
2860     children))
2861
2862 (defmacro gnus-summary-skip-intangible ()
2863   "If the current article is intangible, then jump to a different article."
2864   '(let ((to (get-text-property (point) 'gnus-intangible)))
2865      (and to (gnus-summary-goto-subject to))))
2866
2867 (defmacro gnus-summary-article-intangible-p ()
2868   "Say whether this article is intangible or not."
2869   '(get-text-property (point) 'gnus-intangible))
2870
2871 (defun gnus-article-read-p (article)
2872   "Say whether ARTICLE is read or not."
2873   (not (or (memq article gnus-newsgroup-marked)
2874            (memq article gnus-newsgroup-spam-marked)
2875            (memq article gnus-newsgroup-unreads)
2876            (memq article gnus-newsgroup-unselected)
2877            (memq article gnus-newsgroup-dormant))))
2878
2879 ;; Some summary mode macros.
2880
2881 (defmacro gnus-summary-article-number ()
2882   "The article number of the article on the current line.
2883 If there isn't an article number here, then we return the current
2884 article number."
2885   '(progn
2886      (gnus-summary-skip-intangible)
2887      (or (get-text-property (point) 'gnus-number)
2888          (gnus-summary-last-subject))))
2889
2890 (defmacro gnus-summary-article-header (&optional number)
2891   "Return the header of article NUMBER."
2892   `(gnus-data-header (gnus-data-find
2893                       ,(or number '(gnus-summary-article-number)))))
2894
2895 (defmacro gnus-summary-thread-level (&optional number)
2896   "Return the level of thread that starts with article NUMBER."
2897   `(if (and (eq gnus-summary-make-false-root 'dummy)
2898             (get-text-property (point) 'gnus-intangible))
2899        0
2900      (gnus-data-level (gnus-data-find
2901                        ,(or number '(gnus-summary-article-number))))))
2902
2903 (defmacro gnus-summary-article-mark (&optional number)
2904   "Return the mark of article NUMBER."
2905   `(gnus-data-mark (gnus-data-find
2906                     ,(or number '(gnus-summary-article-number)))))
2907
2908 (defmacro gnus-summary-article-pos (&optional number)
2909   "Return the position of the line of article NUMBER."
2910   `(gnus-data-pos (gnus-data-find
2911                    ,(or number '(gnus-summary-article-number)))))
2912
2913 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2914 (defmacro gnus-summary-article-subject (&optional number)
2915   "Return current subject string or nil if nothing."
2916   `(let ((headers
2917           ,(if number
2918                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2919              '(gnus-data-header (assq (gnus-summary-article-number)
2920                                       gnus-newsgroup-data)))))
2921      (and headers
2922           (vectorp headers)
2923           (mail-header-subject headers))))
2924
2925 (defmacro gnus-summary-article-score (&optional number)
2926   "Return current article score."
2927   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2928                   gnus-newsgroup-scored))
2929        gnus-summary-default-score 0))
2930
2931 (defun gnus-summary-article-children (&optional number)
2932   "Return a list of article numbers that are children of article NUMBER."
2933   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2934          (level (gnus-data-level (car data)))
2935          l children)
2936     (while (and (setq data (cdr data))
2937                 (> (setq l (gnus-data-level (car data))) level))
2938       (and (= (1+ level) l)
2939            (push (gnus-data-number (car data))
2940                  children)))
2941     (nreverse children)))
2942
2943 (defun gnus-summary-article-parent (&optional number)
2944   "Return the article number of the parent of article NUMBER."
2945   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2946                                     (gnus-data-list t)))
2947          (level (gnus-data-level (car data))))
2948     (if (zerop level)
2949         ()                              ; This is a root.
2950       ;; We search until we find an article with a level less than
2951       ;; this one.  That function has to be the parent.
2952       (while (and (setq data (cdr data))
2953                   (not (< (gnus-data-level (car data)) level))))
2954       (and data (gnus-data-number (car data))))))
2955
2956 (defun gnus-unread-mark-p (mark)
2957   "Say whether MARK is the unread mark."
2958   (= mark gnus-unread-mark))
2959
2960 (defun gnus-read-mark-p (mark)
2961   "Say whether MARK is one of the marks that mark as read.
2962 This is all marks except unread, ticked, dormant, and expirable."
2963   (not (or (= mark gnus-unread-mark)
2964            (= mark gnus-ticked-mark)
2965            (= mark gnus-spam-mark)
2966            (= mark gnus-dormant-mark)
2967            (= mark gnus-expirable-mark))))
2968
2969 (defmacro gnus-article-mark (number)
2970   "Return the MARK of article NUMBER.
2971 This macro should only be used when computing the mark the \"first\"
2972 time; i.e., when generating the summary lines.  After that,
2973 `gnus-summary-article-mark' should be used to examine the
2974 marks of articles."
2975   `(cond
2976     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2977     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2978     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2979     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2980     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2981     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2982     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2983     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2984            gnus-ancient-mark))))
2985
2986 ;; Saving hidden threads.
2987
2988 (defmacro gnus-save-hidden-threads (&rest forms)
2989   "Save hidden threads, eval FORMS, and restore the hidden threads."
2990   (let ((config (make-symbol "config")))
2991     `(let ((,config (gnus-hidden-threads-configuration)))
2992        (unwind-protect
2993            (save-excursion
2994              ,@forms)
2995          (gnus-restore-hidden-threads-configuration ,config)))))
2996 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2997 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2998
2999 (defun gnus-data-compute-positions ()
3000   "Compute the positions of all articles."
3001   (setq gnus-newsgroup-data-reverse nil)
3002   (let ((data gnus-newsgroup-data))
3003     (save-excursion
3004       (gnus-save-hidden-threads
3005         (gnus-summary-show-all-threads)
3006         (goto-char (point-min))
3007         (while data
3008           (while (get-text-property (point) 'gnus-intangible)
3009             (forward-line 1))
3010           (gnus-data-set-pos (car data) (+ (point) 3))
3011           (setq data (cdr data))
3012           (forward-line 1))))))
3013
3014 (defun gnus-hidden-threads-configuration ()
3015   "Return the current hidden threads configuration."
3016   (save-excursion
3017     (let (config)
3018       (goto-char (point-min))
3019       (while (search-forward "\r" nil t)
3020         (push (1- (point)) config))
3021       config)))
3022
3023 (defun gnus-restore-hidden-threads-configuration (config)
3024   "Restore hidden threads configuration from CONFIG."
3025   (save-excursion
3026     (let (point buffer-read-only)
3027       (while (setq point (pop config))
3028         (when (and (< point (point-max))
3029                    (goto-char point)
3030                    (eq (char-after) ?\n))
3031           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3032
3033 ;; Various summary mode internalish functions.
3034
3035 (defun gnus-mouse-pick-article (e)
3036   (interactive "e")
3037   (mouse-set-point e)
3038   (gnus-summary-next-page nil t))
3039
3040 (defun gnus-summary-set-display-table ()
3041   "Change the display table.
3042 Odd characters have a tendency to mess
3043 up nicely formatted displays - we make all possible glyphs
3044 display only a single character."
3045
3046   ;; We start from the standard display table, if any.
3047   (let ((table (or (copy-sequence standard-display-table)
3048                    (make-display-table)))
3049         (i 32))
3050     ;; Nix out all the control chars...
3051     (while (>= (setq i (1- i)) 0)
3052       (aset table i [??]))
3053    ;; ... but not newline and cr, of course.  (cr is necessary for the
3054     ;; selective display).
3055     (aset table ?\n nil)
3056     (aset table ?\r nil)
3057     ;; We keep TAB as well.
3058     (aset table ?\t nil)
3059     ;; We nix out any glyphs over 126 that are not set already.
3060     (let ((i 256))
3061       (while (>= (setq i (1- i)) 127)
3062         ;; Only modify if the entry is nil.
3063         (unless (aref table i)
3064           (aset table i [??]))))
3065     (setq buffer-display-table table)))
3066
3067 (defun gnus-summary-set-article-display-arrow (pos)
3068   "Update the overlay arrow to point to line at position POS."
3069   (when (and gnus-summary-display-arrow
3070              (boundp 'overlay-arrow-position)
3071              (boundp 'overlay-arrow-string))
3072     (save-excursion
3073       (goto-char pos)
3074       (beginning-of-line)
3075       (unless overlay-arrow-position
3076         (setq overlay-arrow-position (make-marker)))
3077       (setq overlay-arrow-string "=>"
3078             overlay-arrow-position (set-marker overlay-arrow-position
3079                                                (point)
3080                                                (current-buffer))))))
3081
3082 (defun gnus-summary-setup-buffer (group)
3083   "Initialize summary buffer."
3084   (let ((buffer (gnus-summary-buffer-name group))
3085         (dead-name (concat "*Dead Summary "
3086                            (gnus-group-decoded-name group) "*")))
3087     ;; If a dead summary buffer exists, we kill it.
3088     (when (gnus-buffer-live-p dead-name)
3089       (gnus-kill-buffer dead-name))
3090     (if (get-buffer buffer)
3091         (progn
3092           (set-buffer buffer)
3093           (setq gnus-summary-buffer (current-buffer))
3094           (not gnus-newsgroup-prepared))
3095       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3096       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3097       (gnus-summary-mode group)
3098       (when gnus-carpal
3099         (gnus-carpal-setup-buffer 'summary))
3100       (unless gnus-single-article-buffer
3101         (make-local-variable 'gnus-article-buffer)
3102         (make-local-variable 'gnus-article-current)
3103         (make-local-variable 'gnus-original-article-buffer))
3104       (setq gnus-newsgroup-name group)
3105       ;; Set any local variables in the group parameters.
3106       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3107       t)))
3108
3109 (defun gnus-set-global-variables ()
3110   "Set the global equivalents of the buffer-local variables.
3111 They are set to the latest values they had.  These reflect the summary
3112 buffer that was in action when the last article was fetched."
3113   (when (eq major-mode 'gnus-summary-mode)
3114     (setq gnus-summary-buffer (current-buffer))
3115     (let ((name gnus-newsgroup-name)
3116           (marked gnus-newsgroup-marked)
3117           (spam gnus-newsgroup-spam-marked)
3118           (unread gnus-newsgroup-unreads)
3119           (headers gnus-current-headers)
3120           (data gnus-newsgroup-data)
3121           (summary gnus-summary-buffer)
3122           (article-buffer gnus-article-buffer)
3123           (original gnus-original-article-buffer)
3124           (gac gnus-article-current)
3125           (reffed gnus-reffed-article-number)
3126           (score-file gnus-current-score-file)
3127           (default-charset gnus-newsgroup-charset)
3128           vlist)
3129       (let ((locals gnus-newsgroup-variables))
3130         (while locals
3131           (if (consp (car locals))
3132               (push (eval (caar locals)) vlist)
3133             (push (eval (car locals)) vlist))
3134           (setq locals (cdr locals)))
3135         (setq vlist (nreverse vlist)))
3136       (save-excursion
3137         (set-buffer gnus-group-buffer)
3138         (setq gnus-newsgroup-name name
3139               gnus-newsgroup-marked marked
3140               gnus-newsgroup-spam-marked spam
3141               gnus-newsgroup-unreads unread
3142               gnus-current-headers headers
3143               gnus-newsgroup-data data
3144               gnus-article-current gac
3145               gnus-summary-buffer summary
3146               gnus-article-buffer article-buffer
3147               gnus-original-article-buffer original
3148               gnus-reffed-article-number reffed
3149               gnus-current-score-file score-file
3150               gnus-newsgroup-charset default-charset)
3151         (let ((locals gnus-newsgroup-variables))
3152           (while locals
3153             (if (consp (car locals))
3154                 (set (caar locals) (pop vlist))
3155               (set (car locals) (pop vlist)))
3156             (setq locals (cdr locals))))
3157         ;; The article buffer also has local variables.
3158         (when (gnus-buffer-live-p gnus-article-buffer)
3159           (set-buffer gnus-article-buffer)
3160           (setq gnus-summary-buffer summary))))))
3161
3162 (defun gnus-summary-article-unread-p (article)
3163   "Say whether ARTICLE is unread or not."
3164   (memq article gnus-newsgroup-unreads))
3165
3166 (defun gnus-summary-first-article-p (&optional article)
3167   "Return whether ARTICLE is the first article in the buffer."
3168   (if (not (setq article (or article (gnus-summary-article-number))))
3169       nil
3170     (eq article (caar gnus-newsgroup-data))))
3171
3172 (defun gnus-summary-last-article-p (&optional article)
3173   "Return whether ARTICLE is the last article in the buffer."
3174   (if (not (setq article (or article (gnus-summary-article-number))))
3175       ;; All non-existent numbers are the last article.  :-)
3176       t
3177     (not (cdr (gnus-data-find-list article)))))
3178
3179 (defun gnus-make-thread-indent-array ()
3180   (let ((n 200))
3181     (unless (and gnus-thread-indent-array
3182                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3183       (setq gnus-thread-indent-array (make-vector 201 "")
3184             gnus-thread-indent-array-level gnus-thread-indent-level)
3185       (while (>= n 0)
3186         (aset gnus-thread-indent-array n
3187               (make-string (* n gnus-thread-indent-level) ? ))
3188         (setq n (1- n))))))
3189
3190 (defun gnus-update-summary-mark-positions ()
3191   "Compute where the summary marks are to go."
3192   (save-excursion
3193     (when (gnus-buffer-exists-p gnus-summary-buffer)
3194       (set-buffer gnus-summary-buffer))
3195     (let ((gnus-replied-mark 129)
3196           (gnus-score-below-mark 130)
3197           (gnus-score-over-mark 130)
3198           (gnus-undownloaded-mark 131)
3199           (spec gnus-summary-line-format-spec)
3200           gnus-visual pos)
3201       (save-excursion
3202         (gnus-set-work-buffer)
3203         (let ((gnus-summary-line-format-spec spec)
3204               (gnus-newsgroup-downloadable '(0)))
3205           (gnus-summary-insert-line
3206            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3207            0 nil t 128 t nil "" nil 1)
3208           (goto-char (point-min))
3209           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3210                                              (- (point) (point-min) 1)))))
3211           (goto-char (point-min))
3212           (push (cons 'replied (and (search-forward "\201" nil t)
3213                                     (- (point) (point-min) 1)))
3214                 pos)
3215           (goto-char (point-min))
3216           (push (cons 'score (and (search-forward "\202" nil t)
3217                                   (- (point) (point-min) 1)))
3218                 pos)
3219           (goto-char (point-min))
3220           (push (cons 'download
3221                       (and (search-forward "\203" nil t)
3222                            (- (point) (point-min) 1)))
3223                 pos)))
3224       (setq gnus-summary-mark-positions pos))))
3225
3226 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3227   "Insert a dummy root in the summary buffer."
3228   (beginning-of-line)
3229   (gnus-add-text-properties
3230    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3231    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3232
3233 (defun gnus-summary-extract-address-component (from)
3234   (or (car (funcall gnus-extract-address-components from))
3235       from))
3236
3237 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3238   (let ((mail-parse-charset gnus-newsgroup-charset)
3239         ; Is it really necessary to do this next part for each summary line?
3240         ; Luckily, doesn't seem to slow things down much.
3241         (mail-parse-ignored-charsets
3242          (save-excursion (set-buffer gnus-summary-buffer)
3243                          gnus-newsgroup-ignored-charsets)))
3244     (or
3245      (and gnus-ignored-from-addresses
3246           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3247           (let ((extra-headers (mail-header-extra header))
3248                 to
3249                 newsgroups)
3250             (cond
3251              ((setq to (cdr (assq 'To extra-headers)))
3252               (concat "-> "
3253                       (inline
3254                         (gnus-summary-extract-address-component
3255                          (funcall gnus-decode-encoded-word-function to)))))
3256              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3257               (concat "=> " newsgroups)))))
3258      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3259
3260 (defun gnus-summary-insert-line (gnus-tmp-header
3261                                  gnus-tmp-level gnus-tmp-current
3262                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3263                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3264                                  &optional gnus-tmp-dummy gnus-tmp-score
3265                                  gnus-tmp-process)
3266   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3267          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3268          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3269          (gnus-tmp-score-char
3270           (if (or (null gnus-summary-default-score)
3271                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3272                       gnus-summary-zcore-fuzz))
3273               ?                         ;Whitespace
3274             (if (< gnus-tmp-score gnus-summary-default-score)
3275                 gnus-score-below-mark gnus-score-over-mark)))
3276          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3277          (gnus-tmp-replied
3278           (cond (gnus-tmp-process gnus-process-mark)
3279                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3280                  gnus-cached-mark)
3281                 (gnus-tmp-replied gnus-replied-mark)
3282                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3283                  gnus-forwarded-mark)
3284                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3285                  gnus-saved-mark)
3286                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3287                  gnus-recent-mark)
3288                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3289                  gnus-unseen-mark)
3290                 (t gnus-no-mark)))
3291          (gnus-tmp-downloaded
3292           (cond (undownloaded 
3293                  gnus-undownloaded-mark)
3294                 (gnus-newsgroup-agentized
3295                  gnus-downloaded-mark)
3296                 (t
3297                  gnus-no-mark)))
3298          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3299          (gnus-tmp-name
3300           (cond
3301            ((string-match "<[^>]+> *$" gnus-tmp-from)
3302             (let ((beg (match-beginning 0)))
3303               (or (and (string-match "^\".+\"" gnus-tmp-from)
3304                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3305                   (substring gnus-tmp-from 0 beg))))
3306            ((string-match "(.+)" gnus-tmp-from)
3307             (substring gnus-tmp-from
3308                        (1+ (match-beginning 0)) (1- (match-end 0))))
3309            (t gnus-tmp-from)))
3310          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3311          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3312          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3313          (buffer-read-only nil))
3314     (when (string= gnus-tmp-name "")
3315       (setq gnus-tmp-name gnus-tmp-from))
3316     (unless (numberp gnus-tmp-lines)
3317       (setq gnus-tmp-lines -1))
3318     (if (= gnus-tmp-lines -1)
3319         (setq gnus-tmp-lines "?")
3320       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3321       (gnus-put-text-property
3322      (point)
3323      (progn (eval gnus-summary-line-format-spec) (point))
3324        'gnus-number gnus-tmp-number)
3325     (when (gnus-visual-p 'summary-highlight 'highlight)
3326       (forward-line -1)
3327       (gnus-run-hooks 'gnus-summary-update-hook)
3328       (forward-line 1))))
3329
3330 (defun gnus-summary-update-line (&optional dont-update)
3331   "Update summary line after change."
3332   (when (and gnus-summary-default-score
3333              (not gnus-summary-inhibit-highlight))
3334     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3335            (article (gnus-summary-article-number))
3336            (score (gnus-summary-article-score article)))
3337       (unless dont-update
3338         (if (and gnus-summary-mark-below
3339                  (< (gnus-summary-article-score)
3340                     gnus-summary-mark-below))
3341             ;; This article has a low score, so we mark it as read.
3342             (when (memq article gnus-newsgroup-unreads)
3343               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3344           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3345             ;; This article was previously marked as read on account
3346             ;; of a low score, but now it has risen, so we mark it as
3347             ;; unread.
3348             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3349         (gnus-summary-update-mark
3350          (if (or (null gnus-summary-default-score)
3351                  (<= (abs (- score gnus-summary-default-score))
3352                      gnus-summary-zcore-fuzz))
3353              ?                          ;Whitespace
3354            (if (< score gnus-summary-default-score)
3355                gnus-score-below-mark gnus-score-over-mark))
3356          'score))
3357       ;; Do visual highlighting.
3358       (when (gnus-visual-p 'summary-highlight 'highlight)
3359         (gnus-run-hooks 'gnus-summary-update-hook)))))
3360
3361 (defvar gnus-tmp-new-adopts nil)
3362
3363 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3364   "Return the number of articles in THREAD.
3365 This may be 0 in some cases -- if none of the articles in
3366 the thread are to be displayed."
3367   (let* ((number
3368          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3369           (cond
3370            ((not (listp thread))
3371             1)
3372            ((and (consp thread) (cdr thread))
3373             (apply
3374              '+ 1 (mapcar
3375                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3376            ((null thread)
3377             1)
3378            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3379             1)
3380            (t 0))))
3381     (when (and level (zerop level) gnus-tmp-new-adopts)
3382       (incf number
3383             (apply '+ (mapcar
3384                        'gnus-summary-number-of-articles-in-thread
3385                        gnus-tmp-new-adopts))))
3386     (if char
3387         (if (> number 1) gnus-not-empty-thread-mark
3388           gnus-empty-thread-mark)
3389       number)))
3390
3391 (defsubst gnus-summary-line-message-size (head)
3392   "Return pretty-printed version of message size.
3393 This function is intended to be used in
3394 `gnus-summary-line-format-alist'."
3395   (let ((c (or (mail-header-chars head) -1)))
3396     (cond ((< c 0) "n/a")               ; chars not available
3397           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3398           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3399           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3400           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3401
3402
3403 (defun gnus-summary-set-local-parameters (group)
3404   "Go through the local params of GROUP and set all variable specs in that list."
3405   (let ((params (gnus-group-find-parameter group))
3406         (vars '(quit-config))           ; Ignore quit-config.
3407         elem)
3408     (while params
3409       (setq elem (car params)
3410             params (cdr params))
3411       (and (consp elem)                 ; Has to be a cons.
3412            (consp (cdr elem))           ; The cdr has to be a list.
3413            (symbolp (car elem))         ; Has to be a symbol in there.
3414            (not (memq (car elem) vars))
3415            (ignore-errors               ; So we set it.
3416              (push (car elem) vars)
3417              (make-local-variable (car elem))
3418              (set (car elem) (eval (nth 1 elem))))))))
3419
3420 (defun gnus-summary-read-group (group &optional show-all no-article
3421                                       kill-buffer no-display backward
3422                                       select-articles)
3423   "Start reading news in newsgroup GROUP.
3424 If SHOW-ALL is non-nil, already read articles are also listed.
3425 If NO-ARTICLE is non-nil, no article is selected initially.
3426 If NO-DISPLAY, don't generate a summary buffer."
3427   (let (result)
3428     (while (and group
3429                 (null (setq result
3430                             (let ((gnus-auto-select-next nil))
3431                               (or (gnus-summary-read-group-1
3432                                    group show-all no-article
3433                                    kill-buffer no-display
3434                                    select-articles)
3435                                   (setq show-all nil
3436                                         select-articles nil)))))
3437                 (eq gnus-auto-select-next 'quietly))
3438       (set-buffer gnus-group-buffer)
3439       ;; The entry function called above goes to the next
3440       ;; group automatically, so we go two groups back
3441       ;; if we are searching for the previous group.
3442       (when backward
3443         (gnus-group-prev-unread-group 2))
3444       (if (not (equal group (gnus-group-group-name)))
3445           (setq group (gnus-group-group-name))
3446         (setq group nil)))
3447     result))
3448
3449 (defun gnus-summary-read-group-1 (group show-all no-article
3450                                         kill-buffer no-display
3451                                         &optional select-articles)
3452   ;; Killed foreign groups can't be entered.
3453   ;;  (when (and (not (gnus-group-native-p group))
3454   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3455   ;;    (error "Dead non-native groups can't be entered"))
3456   (gnus-message 5 "Retrieving newsgroup: %s..."
3457                 (gnus-group-decoded-name group))
3458   (let* ((new-group (gnus-summary-setup-buffer group))
3459          (quit-config (gnus-group-quit-config group))
3460          (did-select (and new-group (gnus-select-newsgroup
3461                                      group show-all select-articles))))
3462     (cond
3463      ;; This summary buffer exists already, so we just select it.
3464      ((not new-group)
3465       (gnus-set-global-variables)
3466       (when kill-buffer
3467         (gnus-kill-or-deaden-summary kill-buffer))
3468       (gnus-configure-windows 'summary 'force)
3469       (gnus-set-mode-line 'summary)
3470       (gnus-summary-position-point)
3471       (message "")
3472       t)
3473      ;; We couldn't select this group.
3474      ((null did-select)
3475       (when (and (eq major-mode 'gnus-summary-mode)
3476                  (not (equal (current-buffer) kill-buffer)))
3477         (kill-buffer (current-buffer))
3478         (if (not quit-config)
3479             (progn
3480               ;; Update the info -- marks might need to be removed,
3481               ;; for instance.
3482               (gnus-summary-update-info)
3483               (set-buffer gnus-group-buffer)
3484               (gnus-group-jump-to-group group)
3485               (gnus-group-next-unread-group 1))
3486           (gnus-handle-ephemeral-exit quit-config)))
3487       (let ((grpinfo (gnus-get-info group)))
3488         (if (null (gnus-info-read grpinfo))
3489             (gnus-message 3 "Group %s contains no messages"
3490                           (gnus-group-decoded-name group))
3491           (gnus-message 3 "Can't select group")))
3492       nil)
3493      ;; The user did a `C-g' while prompting for number of articles,
3494      ;; so we exit this group.
3495      ((eq did-select 'quit)
3496       (and (eq major-mode 'gnus-summary-mode)
3497            (not (equal (current-buffer) kill-buffer))
3498            (kill-buffer (current-buffer)))
3499       (when kill-buffer
3500         (gnus-kill-or-deaden-summary kill-buffer))
3501       (if (not quit-config)
3502           (progn
3503             (set-buffer gnus-group-buffer)
3504             (gnus-group-jump-to-group group)
3505             (gnus-group-next-unread-group 1)
3506             (gnus-configure-windows 'group 'force))
3507         (gnus-handle-ephemeral-exit quit-config))
3508       ;; Finally signal the quit.
3509       (signal 'quit nil))
3510      ;; The group was successfully selected.
3511      (t
3512       (gnus-set-global-variables)
3513       ;; Save the active value in effect when the group was entered.
3514       (setq gnus-newsgroup-active
3515             (gnus-copy-sequence
3516              (gnus-active gnus-newsgroup-name)))
3517       ;; You can change the summary buffer in some way with this hook.
3518       (gnus-run-hooks 'gnus-select-group-hook)
3519       (gnus-update-format-specifications
3520        nil 'summary 'summary-mode 'summary-dummy)
3521       (gnus-update-summary-mark-positions)
3522       ;; Do score processing.
3523       (when gnus-use-scoring
3524         (gnus-possibly-score-headers))
3525       ;; Check whether to fill in the gaps in the threads.
3526       (when gnus-build-sparse-threads
3527         (gnus-build-sparse-threads))
3528       ;; Find the initial limit.
3529       (if gnus-show-threads
3530           (if show-all
3531               (let ((gnus-newsgroup-dormant nil))
3532                 (gnus-summary-initial-limit show-all))
3533             (gnus-summary-initial-limit show-all))
3534         ;; When unthreaded, all articles are always shown.
3535         (setq gnus-newsgroup-limit
3536               (mapcar
3537                (lambda (header) (mail-header-number header))
3538                gnus-newsgroup-headers)))
3539       ;; Generate the summary buffer.
3540       (unless no-display
3541         (gnus-summary-prepare))
3542       (when gnus-use-trees
3543         (gnus-tree-open group)
3544         (setq gnus-summary-highlight-line-function
3545               'gnus-tree-highlight-article))
3546       ;; If the summary buffer is empty, but there are some low-scored
3547       ;; articles or some excluded dormants, we include these in the
3548       ;; buffer.
3549       (when (and (zerop (buffer-size))
3550                  (not no-display))
3551         (cond (gnus-newsgroup-dormant
3552                (gnus-summary-limit-include-dormant))
3553               ((and gnus-newsgroup-scored show-all)
3554                (gnus-summary-limit-include-expunged t))))
3555       ;; Function `gnus-apply-kill-file' must be called in this hook.
3556       (gnus-run-hooks 'gnus-apply-kill-hook)
3557       (if (and (zerop (buffer-size))
3558                (not no-display))
3559           (progn
3560             ;; This newsgroup is empty.
3561             (gnus-summary-catchup-and-exit nil t)
3562             (gnus-message 6 "No unread news")
3563             (when kill-buffer
3564               (gnus-kill-or-deaden-summary kill-buffer))
3565             ;; Return nil from this function.
3566             nil)
3567         ;; Hide conversation thread subtrees.  We cannot do this in
3568         ;; gnus-summary-prepare-hook since kill processing may not
3569         ;; work with hidden articles.
3570         (gnus-summary-maybe-hide-threads)
3571         (when kill-buffer
3572           (gnus-kill-or-deaden-summary kill-buffer))
3573         (gnus-summary-auto-select-subject)
3574         ;; Show first unread article if requested.
3575         (if (and (not no-article)
3576                  (not no-display)
3577                  gnus-newsgroup-unreads
3578                  gnus-auto-select-first)
3579             (progn
3580               (gnus-configure-windows 'summary)
3581               (let ((art (gnus-summary-article-number)))
3582                 (unless (and (not gnus-plugged)
3583                              (or (memq art gnus-newsgroup-undownloaded)
3584                                  (memq art gnus-newsgroup-downloadable)))
3585                   (gnus-summary-goto-article art))))
3586           ;; Don't select any articles.
3587           (gnus-summary-position-point)
3588           (gnus-configure-windows 'summary 'force)
3589           (gnus-set-mode-line 'summary))
3590         (when (and gnus-auto-center-group
3591                    (get-buffer-window gnus-group-buffer t))
3592           ;; Gotta use windows, because recenter does weird stuff if
3593           ;; the current buffer ain't the displayed window.
3594           (let ((owin (selected-window)))
3595             (select-window (get-buffer-window gnus-group-buffer t))
3596             (when (gnus-group-goto-group group)
3597               (recenter))
3598             (select-window owin)))
3599         ;; Mark this buffer as "prepared".
3600         (setq gnus-newsgroup-prepared t)
3601         (gnus-run-hooks 'gnus-summary-prepared-hook)
3602         (unless (gnus-ephemeral-group-p group)
3603           (gnus-group-update-group group))
3604         t)))))
3605
3606 (defun gnus-summary-auto-select-subject ()
3607   "Select the subject line on initial group entry."
3608   (goto-char (point-min))
3609   (cond
3610    ((eq gnus-auto-select-subject 'best)
3611     (gnus-summary-best-unread-subject))
3612    ((eq gnus-auto-select-subject 'unread)
3613     (gnus-summary-first-unread-subject))
3614    ((eq gnus-auto-select-subject 'unseen)
3615     (gnus-summary-first-unseen-subject))
3616    ((eq gnus-auto-select-subject 'unseen-or-unread)
3617     (gnus-summary-first-unseen-or-unread-subject))
3618    ((eq gnus-auto-select-subject 'first)
3619     ;; Do nothing.
3620     )
3621    ((functionp gnus-auto-select-subject)
3622     (funcall gnus-auto-select-subject))))
3623
3624 (defun gnus-summary-prepare ()
3625   "Generate the summary buffer."
3626   (interactive)
3627   (let ((buffer-read-only nil))
3628     (erase-buffer)
3629     (setq gnus-newsgroup-data nil
3630           gnus-newsgroup-data-reverse nil)
3631     (gnus-run-hooks 'gnus-summary-generate-hook)
3632     ;; Generate the buffer, either with threads or without.
3633     (when gnus-newsgroup-headers
3634       (gnus-summary-prepare-threads
3635        (if gnus-show-threads
3636            (gnus-sort-gathered-threads
3637             (funcall gnus-summary-thread-gathering-function
3638                      (gnus-sort-threads
3639                       (gnus-cut-threads (gnus-make-threads)))))
3640          ;; Unthreaded display.
3641          (gnus-sort-articles gnus-newsgroup-headers))))
3642     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3643     ;; Call hooks for modifying summary buffer.
3644     (goto-char (point-min))
3645     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3646
3647 (defsubst gnus-general-simplify-subject (subject)
3648   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3649   (setq subject
3650         (cond
3651          ;; Truncate the subject.
3652          (gnus-simplify-subject-functions
3653           (gnus-map-function gnus-simplify-subject-functions subject))
3654          ((numberp gnus-summary-gather-subject-limit)
3655           (setq subject (gnus-simplify-subject-re subject))
3656           (if (> (length subject) gnus-summary-gather-subject-limit)
3657               (substring subject 0 gnus-summary-gather-subject-limit)
3658             subject))
3659          ;; Fuzzily simplify it.
3660          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3661           (gnus-simplify-subject-fuzzy subject))
3662          ;; Just remove the leading "Re:".
3663          (t
3664           (gnus-simplify-subject-re subject))))
3665
3666   (if (and gnus-summary-gather-exclude-subject
3667            (string-match gnus-summary-gather-exclude-subject subject))
3668       nil                         ; This article shouldn't be gathered
3669     subject))
3670
3671 (defun gnus-summary-simplify-subject-query ()
3672   "Query where the respool algorithm would put this article."
3673   (interactive)
3674   (gnus-summary-select-article)
3675   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3676
3677 (defun gnus-gather-threads-by-subject (threads)
3678   "Gather threads by looking at Subject headers."
3679   (if (not gnus-summary-make-false-root)
3680       threads
3681     (let ((hashtb (gnus-make-hashtable 1024))
3682           (prev threads)
3683           (result threads)
3684           subject hthread whole-subject)
3685       (while threads
3686         (setq subject (gnus-general-simplify-subject
3687                        (setq whole-subject (mail-header-subject
3688                                             (caar threads)))))
3689         (when subject
3690           (if (setq hthread (gnus-gethash subject hashtb))
3691               (progn
3692                 ;; We enter a dummy root into the thread, if we
3693                 ;; haven't done that already.
3694                 (unless (stringp (caar hthread))
3695                   (setcar hthread (list whole-subject (car hthread))))
3696                 ;; We add this new gathered thread to this gathered
3697                 ;; thread.
3698                 (setcdr (car hthread)
3699                         (nconc (cdar hthread) (list (car threads))))
3700                 ;; Remove it from the list of threads.
3701                 (setcdr prev (cdr threads))
3702                 (setq threads prev))
3703             ;; Enter this thread into the hash table.
3704             (gnus-sethash subject
3705                           (if gnus-summary-make-false-root-always
3706                               (progn
3707                                 ;; If you want a dummy root above all
3708                                 ;; threads...
3709                                 (setcar threads (list whole-subject
3710                                                       (car threads)))
3711                                 threads)
3712                             threads)
3713                           hashtb)))
3714         (setq prev threads)
3715         (setq threads (cdr threads)))
3716       result)))
3717
3718 (defun gnus-gather-threads-by-references (threads)
3719   "Gather threads by looking at References headers."
3720   (let ((idhashtb (gnus-make-hashtable 1024))
3721         (thhashtb (gnus-make-hashtable 1024))
3722         (prev threads)
3723         (result threads)
3724         ids references id gthread gid entered ref)
3725     (while threads
3726       (when (setq references (mail-header-references (caar threads)))
3727         (setq id (mail-header-id (caar threads))
3728               ids (inline (gnus-split-references references))
3729               entered nil)
3730         (while (setq ref (pop ids))
3731           (setq ids (delete ref ids))
3732           (if (not (setq gid (gnus-gethash ref idhashtb)))
3733               (progn
3734                 (gnus-sethash ref id idhashtb)
3735                 (gnus-sethash id threads thhashtb))
3736             (setq gthread (gnus-gethash gid thhashtb))
3737             (unless entered
3738               ;; We enter a dummy root into the thread, if we
3739               ;; haven't done that already.
3740               (unless (stringp (caar gthread))
3741                 (setcar gthread (list (mail-header-subject (caar gthread))
3742                                       (car gthread))))
3743               ;; We add this new gathered thread to this gathered
3744               ;; thread.
3745               (setcdr (car gthread)
3746                       (nconc (cdar gthread) (list (car threads)))))
3747             ;; Add it into the thread hash table.
3748             (gnus-sethash id gthread thhashtb)
3749             (setq entered t)
3750             ;; Remove it from the list of threads.
3751             (setcdr prev (cdr threads))
3752             (setq threads prev))))
3753       (setq prev threads)
3754       (setq threads (cdr threads)))
3755     result))
3756
3757 (defun gnus-sort-gathered-threads (threads)
3758   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3759   (let ((result threads))
3760     (while threads
3761       (when (stringp (caar threads))
3762         (setcdr (car threads)
3763                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3764       (setq threads (cdr threads)))
3765     result))
3766
3767 (defun gnus-thread-loop-p (root thread)
3768   "Say whether ROOT is in THREAD."
3769   (let ((stack (list thread))
3770         (infloop 0)
3771         th)
3772     (while (setq thread (pop stack))
3773       (setq th (cdr thread))
3774       (while (and th
3775                   (not (eq (caar th) root)))
3776         (pop th))
3777       (if th
3778           ;; We have found a loop.
3779           (let (ref-dep)
3780             (setcdr thread (delq (car th) (cdr thread)))
3781             (if (boundp (setq ref-dep (intern "none"
3782                                               gnus-newsgroup-dependencies)))
3783                 (setcdr (symbol-value ref-dep)
3784                         (nconc (cdr (symbol-value ref-dep))
3785                                (list (car th))))
3786               (set ref-dep (list nil (car th))))
3787             (setq infloop 1
3788                   stack nil))
3789         ;; Push all the subthreads onto the stack.
3790         (push (cdr thread) stack)))
3791     infloop))
3792
3793 (defun gnus-make-threads ()
3794   "Go through the dependency hashtb and find the roots.  Return all threads."
3795   (let (threads)
3796     (while (catch 'infloop
3797              (mapatoms
3798               (lambda (refs)
3799                 ;; Deal with self-referencing References loops.
3800                 (when (and (car (symbol-value refs))
3801                            (not (zerop
3802                                  (apply
3803                                   '+
3804                                   (mapcar
3805                                    (lambda (thread)
3806                                      (gnus-thread-loop-p
3807                                       (car (symbol-value refs)) thread))
3808                                    (cdr (symbol-value refs)))))))
3809                   (setq threads nil)
3810                   (throw 'infloop t))
3811                 (unless (car (symbol-value refs))
3812                   ;; These threads do not refer back to any other
3813                   ;; articles, so they're roots.
3814                   (setq threads (append (cdr (symbol-value refs)) threads))))
3815               gnus-newsgroup-dependencies)))
3816     threads))
3817
3818 ;; Build the thread tree.
3819 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3820   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3821
3822 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3823 if it was already present.
3824
3825 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3826 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3827 Message-IDs will be renamed to a unique Message-ID before being
3828 entered.
3829
3830 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3831   (let* ((id (mail-header-id header))
3832          (id-dep (and id (intern id dependencies)))
3833          parent-id ref ref-dep ref-header replaced)
3834     ;; Enter this `header' in the `dependencies' table.
3835     (cond
3836      ((not id-dep)
3837       (setq header nil))
3838      ;; The first two cases do the normal part: enter a new `header'
3839      ;; in the `dependencies' table.
3840      ((not (boundp id-dep))
3841       (set id-dep (list header)))
3842      ((null (car (symbol-value id-dep)))
3843       (setcar (symbol-value id-dep) header))
3844
3845      ;; From here the `header' was already present in the
3846      ;; `dependencies' table.
3847      (force-new
3848       ;; Overrides an existing entry;
3849       ;; just set the header part of the entry.
3850       (setcar (symbol-value id-dep) header)
3851       (setq replaced t))
3852
3853      ;; Renames the existing `header' to a unique Message-ID.
3854      ((not gnus-summary-ignore-duplicates)
3855       ;; An article with this Message-ID has already been seen.
3856       ;; We rename the Message-ID.
3857       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3858            (list header))
3859       (mail-header-set-id header id))
3860
3861      ;; The last case ignores an existing entry, except it adds any
3862      ;; additional Xrefs (in case the two articles came from different
3863      ;; servers.
3864      ;; Also sets `header' to `nil' meaning that the `dependencies'
3865      ;; table was *not* modified.
3866      (t
3867       (mail-header-set-xref
3868        (car (symbol-value id-dep))
3869        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3870                    "")
3871                (or (mail-header-xref header) "")))
3872       (setq header nil)))
3873
3874     (when (and header (not replaced))
3875       ;; First check that we are not creating a References loop.
3876       (setq parent-id (gnus-parent-id (mail-header-references header)))
3877       (setq ref parent-id)
3878       (while (and ref
3879                   (setq ref-dep (intern-soft ref dependencies))
3880                   (boundp ref-dep)
3881                   (setq ref-header (car (symbol-value ref-dep))))
3882         (if (string= id ref)
3883             ;; Yuk!  This is a reference loop.  Make the article be a
3884             ;; root article.
3885             (progn
3886               (mail-header-set-references (car (symbol-value id-dep)) "none")
3887               (setq ref nil)
3888               (setq parent-id nil))
3889           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3890       (setq ref-dep (intern (or parent-id "none") dependencies))
3891       (if (boundp ref-dep)
3892           (setcdr (symbol-value ref-dep)
3893                   (nconc (cdr (symbol-value ref-dep))
3894                          (list (symbol-value id-dep))))
3895         (set ref-dep (list nil (symbol-value id-dep)))))
3896     header))
3897
3898 (defun gnus-extract-message-id-from-in-reply-to (string)
3899   (if (string-match "<[^>]+>" string)
3900       (substring string (match-beginning 0) (match-end 0))
3901     nil))
3902
3903 (defun gnus-build-sparse-threads ()
3904   (let ((headers gnus-newsgroup-headers)
3905         (mail-parse-charset gnus-newsgroup-charset)
3906         (gnus-summary-ignore-duplicates t)
3907         header references generation relations
3908         subject child end new-child date)
3909     ;; First we create an alist of generations/relations, where
3910     ;; generations is how much we trust the relation, and the relation
3911     ;; is parent/child.
3912     (gnus-message 7 "Making sparse threads...")
3913     (save-excursion
3914       (nnheader-set-temp-buffer " *gnus sparse threads*")
3915       (while (setq header (pop headers))
3916         (when (and (setq references (mail-header-references header))
3917                    (not (string= references "")))
3918           (insert references)
3919           (setq child (mail-header-id header)
3920                 subject (mail-header-subject header)
3921                 date (mail-header-date header)
3922                 generation 0)
3923           (while (search-backward ">" nil t)
3924             (setq end (1+ (point)))
3925             (when (search-backward "<" nil t)
3926               (setq new-child (buffer-substring (point) end))
3927               (push (list (incf generation)
3928                           child (setq child new-child)
3929                           subject date)
3930                     relations)))
3931           (when child
3932             (push (list (1+ generation) child nil subject) relations))
3933           (erase-buffer)))
3934       (kill-buffer (current-buffer)))
3935     ;; Sort over trustworthiness.
3936     (mapcar
3937      (lambda (relation)
3938        (when (gnus-dependencies-add-header
3939               (make-full-mail-header
3940                gnus-reffed-article-number
3941                (nth 3 relation) "" (or (nth 4 relation) "")
3942                (nth 1 relation)
3943                (or (nth 2 relation) "") 0 0 "")
3944               gnus-newsgroup-dependencies nil)
3945          (push gnus-reffed-article-number gnus-newsgroup-limit)
3946          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3947          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3948                gnus-newsgroup-reads)
3949          (decf gnus-reffed-article-number)))
3950      (sort relations 'car-less-than-car))
3951     (gnus-message 7 "Making sparse threads...done")))
3952
3953 (defun gnus-build-old-threads ()
3954   ;; Look at all the articles that refer back to old articles, and
3955   ;; fetch the headers for the articles that aren't there.  This will
3956   ;; build complete threads - if the roots haven't been expired by the
3957   ;; server, that is.
3958   (let ((mail-parse-charset gnus-newsgroup-charset)
3959         id heads)
3960     (mapatoms
3961      (lambda (refs)
3962        (when (not (car (symbol-value refs)))
3963          (setq heads (cdr (symbol-value refs)))
3964          (while heads
3965            (if (memq (mail-header-number (caar heads))
3966                      gnus-newsgroup-dormant)
3967                (setq heads (cdr heads))
3968              (setq id (symbol-name refs))
3969              (while (and (setq id (gnus-build-get-header id))
3970                          (not (car (gnus-id-to-thread id)))))
3971              (setq heads nil)))))
3972      gnus-newsgroup-dependencies)))
3973
3974 ;; This function has to be called with point after the article number
3975 ;; on the beginning of the line.
3976 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3977   (let ((eol (gnus-point-at-eol))
3978         (buffer (current-buffer))
3979         header references in-reply-to)
3980
3981     ;; overview: [num subject from date id refs chars lines misc]
3982     (unwind-protect
3983         (let (x)
3984           (narrow-to-region (point) eol)
3985           (unless (eobp)
3986             (forward-char))
3987
3988           (setq header
3989                 (make-full-mail-header
3990                  number                 ; number
3991                  (condition-case ()     ; subject
3992                      (funcall gnus-decode-encoded-word-function
3993                               (setq x (nnheader-nov-field)))
3994                    (error x))
3995                  (condition-case ()     ; from
3996                      (funcall gnus-decode-encoded-word-function
3997                               (setq x (nnheader-nov-field)))
3998                    (error x))
3999                  (nnheader-nov-field)   ; date
4000                  (nnheader-nov-read-message-id) ; id
4001                  (setq references (nnheader-nov-field)) ; refs
4002                  (nnheader-nov-read-integer) ; chars
4003                  (nnheader-nov-read-integer) ; lines
4004                  (unless (eobp)
4005                    (if (looking-at "Xref: ")
4006                        (goto-char (match-end 0)))
4007                    (nnheader-nov-field)) ; Xref
4008                  (nnheader-nov-parse-extra)))) ; extra
4009
4010       (widen))
4011
4012     (when (and (string= references "")
4013                (setq in-reply-to (mail-header-extra header))
4014                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4015       (mail-header-set-references
4016        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4017
4018     (when gnus-alter-header-function
4019       (funcall gnus-alter-header-function header))
4020     (gnus-dependencies-add-header header dependencies force-new)))
4021
4022 (defun gnus-build-get-header (id)
4023   "Look through the buffer of NOV lines and find the header to ID.
4024 Enter this line into the dependencies hash table, and return
4025 the id of the parent article (if any)."
4026   (let ((deps gnus-newsgroup-dependencies)
4027         found header)
4028     (prog1
4029         (save-excursion
4030           (set-buffer nntp-server-buffer)
4031           (let ((case-fold-search nil))
4032             (goto-char (point-min))
4033             (while (and (not found)
4034                         (search-forward id nil t))
4035               (beginning-of-line)
4036               (setq found (looking-at
4037                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4038                                    (regexp-quote id))))
4039               (or found (beginning-of-line 2)))
4040             (when found
4041               (beginning-of-line)
4042               (and
4043                (setq header (gnus-nov-parse-line
4044                              (read (current-buffer)) deps))
4045                (gnus-parent-id (mail-header-references header))))))
4046       (when header
4047         (let ((number (mail-header-number header)))
4048           (push number gnus-newsgroup-limit)
4049           (push header gnus-newsgroup-headers)
4050           (if (memq number gnus-newsgroup-unselected)
4051               (progn
4052                 (setq gnus-newsgroup-unreads
4053                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4054                                                number))
4055                 (setq gnus-newsgroup-unselected
4056                       (delq number gnus-newsgroup-unselected)))
4057             (push number gnus-newsgroup-ancient)))))))
4058
4059 (defun gnus-build-all-threads ()
4060   "Read all the headers."
4061   (let ((gnus-summary-ignore-duplicates t)
4062         (mail-parse-charset gnus-newsgroup-charset)
4063         (dependencies gnus-newsgroup-dependencies)
4064         header article)
4065     (save-excursion
4066       (set-buffer nntp-server-buffer)
4067       (let ((case-fold-search nil))
4068         (goto-char (point-min))
4069         (while (not (eobp))
4070           (ignore-errors
4071             (setq article (read (current-buffer))
4072                   header (gnus-nov-parse-line article dependencies)))
4073           (when header
4074             (save-excursion
4075               (set-buffer gnus-summary-buffer)
4076               (push header gnus-newsgroup-headers)
4077               (if (memq (setq article (mail-header-number header))
4078                         gnus-newsgroup-unselected)
4079                   (progn
4080                     (setq gnus-newsgroup-unreads
4081                           (gnus-add-to-sorted-list
4082                            gnus-newsgroup-unreads article))
4083                     (setq gnus-newsgroup-unselected
4084                           (delq article gnus-newsgroup-unselected)))
4085                 (push article gnus-newsgroup-ancient)))
4086             (forward-line 1)))))))
4087
4088 (defun gnus-summary-update-article-line (article header)
4089   "Update the line for ARTICLE using HEADERS."
4090   (let* ((id (mail-header-id header))
4091          (thread (gnus-id-to-thread id)))
4092     (unless thread
4093       (error "Article in no thread"))
4094     ;; Update the thread.
4095     (setcar thread header)
4096     (gnus-summary-goto-subject article)
4097     (let* ((datal (gnus-data-find-list article))
4098            (data (car datal))
4099            (buffer-read-only nil)
4100            (level (gnus-summary-thread-level)))
4101       (gnus-delete-line)
4102       (let ((inserted (- (point)
4103                          (progn
4104                            (gnus-summary-insert-line
4105                             header level nil 
4106                             (memq article gnus-newsgroup-undownloaded)
4107                             (gnus-article-mark article)
4108                             (memq article gnus-newsgroup-replied)
4109                             (memq article gnus-newsgroup-expirable)
4110                             ;; Only insert the Subject string when it's different
4111                             ;; from the previous Subject string.
4112                             (if (and
4113                                  gnus-show-threads
4114                                  (gnus-subject-equal
4115                                   (condition-case ()
4116                                       (mail-header-subject
4117                                        (gnus-data-header
4118                                         (cadr
4119                                          (gnus-data-find-list
4120                                           article
4121                                           (gnus-data-list t)))))
4122                                     ;; Error on the side of excessive subjects.
4123                                     (error ""))
4124                                   (mail-header-subject header)))
4125                                 ""
4126                               (mail-header-subject header))
4127                             nil (cdr (assq article gnus-newsgroup-scored))
4128                             (memq article gnus-newsgroup-processable))
4129                            (point)))))
4130         (when (cdr datal)
4131           (gnus-data-update-list
4132            (cdr datal) 
4133            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4134
4135 (defun gnus-summary-update-article (article &optional iheader)
4136   "Update ARTICLE in the summary buffer."
4137   (set-buffer gnus-summary-buffer)
4138   (let* ((header (gnus-summary-article-header article))
4139          (id (mail-header-id header))
4140          (data (gnus-data-find article))
4141          (thread (gnus-id-to-thread id))
4142          (references (mail-header-references header))
4143          (parent
4144           (gnus-id-to-thread
4145            (or (gnus-parent-id
4146                 (when (and references
4147                            (not (equal "" references)))
4148                   references))
4149                "none")))
4150          (buffer-read-only nil)
4151          (old (car thread)))
4152     (when thread
4153       (unless iheader
4154         (setcar thread nil)
4155         (when parent
4156           (delq thread parent)))
4157       (if (gnus-summary-insert-subject id header)
4158           ;; Set the (possibly) new article number in the data structure.
4159           (gnus-data-set-number data (gnus-id-to-article id))
4160         (setcar thread old)
4161         nil))))
4162
4163 (defun gnus-rebuild-thread (id &optional line)
4164   "Rebuild the thread containing ID.
4165 If LINE, insert the rebuilt thread starting on line LINE."
4166   (let ((buffer-read-only nil)
4167         old-pos current thread data)
4168     (if (not gnus-show-threads)
4169         (setq thread (list (car (gnus-id-to-thread id))))
4170       ;; Get the thread this article is part of.
4171       (setq thread (gnus-remove-thread id)))
4172     (setq old-pos (gnus-point-at-bol))
4173     (setq current (save-excursion
4174                     (and (re-search-backward "[\r\n]" nil t)
4175                          (gnus-summary-article-number))))
4176     ;; If this is a gathered thread, we have to go some re-gathering.
4177     (when (stringp (car thread))
4178       (let ((subject (car thread))
4179             roots thr)
4180         (setq thread (cdr thread))
4181         (while thread
4182           (unless (memq (setq thr (gnus-id-to-thread
4183                                    (gnus-root-id
4184                                     (mail-header-id (caar thread)))))
4185                         roots)
4186             (push thr roots))
4187           (setq thread (cdr thread)))
4188         ;; We now have all (unique) roots.
4189         (if (= (length roots) 1)
4190             ;; All the loose roots are now one solid root.
4191             (setq thread (car roots))
4192           (setq thread (cons subject (gnus-sort-threads roots))))))
4193     (let (threads)
4194       ;; We then insert this thread into the summary buffer.
4195       (when line
4196         (goto-char (point-min))
4197         (forward-line (1- line)))
4198       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4199         (if gnus-show-threads
4200             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4201           (gnus-summary-prepare-unthreaded thread))
4202         (setq data (nreverse gnus-newsgroup-data))
4203         (setq threads gnus-newsgroup-threads))
4204       ;; We splice the new data into the data structure.
4205       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4206       ;;!!! then we want to insert at the beginning of the buffer.
4207       ;;!!! That happens to be true with Gnus now, but that may
4208       ;;!!! change in the future.  Perhaps.
4209       (gnus-data-enter-list
4210        (if line nil current) data (- (point) old-pos))
4211       (setq gnus-newsgroup-threads
4212             (nconc threads gnus-newsgroup-threads))
4213       (gnus-data-compute-positions))))
4214
4215 (defun gnus-number-to-header (number)
4216   "Return the header for article NUMBER."
4217   (let ((headers gnus-newsgroup-headers))
4218     (while (and headers
4219                 (not (= number (mail-header-number (car headers)))))
4220       (pop headers))
4221     (when headers
4222       (car headers))))
4223
4224 (defun gnus-parent-headers (in-headers &optional generation)
4225   "Return the headers of the GENERATIONeth parent of HEADERS."
4226   (unless generation
4227     (setq generation 1))
4228   (let ((parent t)
4229         (headers in-headers)
4230         references)
4231     (while (and parent
4232                 (not (zerop generation))
4233                 (setq references (mail-header-references headers)))
4234       (setq headers (if (and references
4235                              (setq parent (gnus-parent-id references)))
4236                         (car (gnus-id-to-thread parent))
4237                       nil))
4238       (decf generation))
4239     (and (not (eq headers in-headers))
4240          headers)))
4241
4242 (defun gnus-id-to-thread (id)
4243   "Return the (sub-)thread where ID appears."
4244   (gnus-gethash id gnus-newsgroup-dependencies))
4245
4246 (defun gnus-id-to-article (id)
4247   "Return the article number of ID."
4248   (let ((thread (gnus-id-to-thread id)))
4249     (when (and thread
4250                (car thread))
4251       (mail-header-number (car thread)))))
4252
4253 (defun gnus-id-to-header (id)
4254   "Return the article headers of ID."
4255   (car (gnus-id-to-thread id)))
4256
4257 (defun gnus-article-displayed-root-p (article)
4258   "Say whether ARTICLE is a root(ish) article."
4259   (let ((level (gnus-summary-thread-level article))
4260         (refs (mail-header-references  (gnus-summary-article-header article)))
4261         particle)
4262     (cond
4263      ((null level) nil)
4264      ((zerop level) t)
4265      ((null refs) t)
4266      ((null (gnus-parent-id refs)) t)
4267      ((and (= 1 level)
4268            (null (setq particle (gnus-id-to-article
4269                                  (gnus-parent-id refs))))
4270            (null (gnus-summary-thread-level particle)))))))
4271
4272 (defun gnus-root-id (id)
4273   "Return the id of the root of the thread where ID appears."
4274   (let (last-id prev)
4275     (while (and id (setq prev (car (gnus-id-to-thread id))))
4276       (setq last-id id
4277             id (gnus-parent-id (mail-header-references prev))))
4278     last-id))
4279
4280 (defun gnus-articles-in-thread (thread)
4281   "Return the list of articles in THREAD."
4282   (cons (mail-header-number (car thread))
4283         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4284
4285 (defun gnus-remove-thread (id &optional dont-remove)
4286   "Remove the thread that has ID in it."
4287   (let (headers thread last-id)
4288     ;; First go up in this thread until we find the root.
4289     (setq last-id (gnus-root-id id)
4290           headers (message-flatten-list (gnus-id-to-thread last-id)))
4291     ;; We have now found the real root of this thread.  It might have
4292     ;; been gathered into some loose thread, so we have to search
4293     ;; through the threads to find the thread we wanted.
4294     (let ((threads gnus-newsgroup-threads)
4295           sub)
4296       (while threads
4297         (setq sub (car threads))
4298         (if (stringp (car sub))
4299             ;; This is a gathered thread, so we look at the roots
4300             ;; below it to find whether this article is in this
4301             ;; gathered root.
4302             (progn
4303               (setq sub (cdr sub))
4304               (while sub
4305                 (when (member (caar sub) headers)
4306                   (setq thread (car threads)
4307                         threads nil
4308                         sub nil))
4309                 (setq sub (cdr sub))))
4310           ;; It's an ordinary thread, so we check it.
4311           (when (eq (car sub) (car headers))
4312             (setq thread sub
4313                   threads nil)))
4314         (setq threads (cdr threads)))
4315       ;; If this article is in no thread, then it's a root.
4316       (if thread
4317           (unless dont-remove
4318             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4319         (setq thread (gnus-id-to-thread last-id)))
4320       (when thread
4321         (prog1
4322             thread                      ; We return this thread.
4323           (unless dont-remove
4324             (if (stringp (car thread))
4325                 (progn
4326                   ;; If we use dummy roots, then we have to remove the
4327                   ;; dummy root as well.
4328                   (when (eq gnus-summary-make-false-root 'dummy)
4329                     ;; We go to the dummy root by going to
4330                     ;; the first sub-"thread", and then one line up.
4331                     (gnus-summary-goto-article
4332                      (mail-header-number (caadr thread)))
4333                     (forward-line -1)
4334                     (gnus-delete-line)
4335                     (gnus-data-compute-positions))
4336                   (setq thread (cdr thread))
4337                   (while thread
4338                     (gnus-remove-thread-1 (car thread))
4339                     (setq thread (cdr thread))))
4340               (gnus-remove-thread-1 thread))))))))
4341
4342 (defun gnus-remove-thread-1 (thread)
4343   "Remove the thread THREAD recursively."
4344   (let ((number (mail-header-number (pop thread)))
4345         d)
4346     (setq thread (reverse thread))
4347     (while thread
4348       (gnus-remove-thread-1 (pop thread)))
4349     (when (setq d (gnus-data-find number))
4350       (goto-char (gnus-data-pos d))
4351       (gnus-summary-show-thread)
4352       (gnus-data-remove
4353        number
4354        (- (gnus-point-at-bol)
4355           (prog1
4356               (1+ (gnus-point-at-eol))
4357             (gnus-delete-line)))))))
4358
4359 (defun gnus-sort-threads-1 (threads func)
4360   (sort (mapcar (lambda (thread)
4361                   (cons (car thread)
4362                         (and (cdr thread)
4363                              (gnus-sort-threads-1 (cdr thread) func))))
4364                 threads) func))
4365
4366 (defun gnus-sort-threads (threads)
4367   "Sort THREADS."
4368   (if (not gnus-thread-sort-functions)
4369       threads
4370     (gnus-message 8 "Sorting threads...")
4371     (let ((max-lisp-eval-depth 5000))
4372       (prog1 (gnus-sort-threads-1
4373          threads
4374          (gnus-make-sort-function gnus-thread-sort-functions))
4375         (gnus-message 8 "Sorting threads...done")))))
4376
4377 (defun gnus-sort-articles (articles)
4378   "Sort ARTICLES."
4379   (when gnus-article-sort-functions
4380     (gnus-message 7 "Sorting articles...")
4381     (prog1
4382         (setq gnus-newsgroup-headers
4383               (sort articles (gnus-make-sort-function
4384                               gnus-article-sort-functions)))
4385       (gnus-message 7 "Sorting articles...done"))))
4386
4387 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4388 (defmacro gnus-thread-header (thread)
4389   "Return header of first article in THREAD.
4390 Note that THREAD must never, ever be anything else than a variable -
4391 using some other form will lead to serious barfage."
4392   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4393   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4394   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4395         (vector thread) 2))
4396
4397 (defsubst gnus-article-sort-by-number (h1 h2)
4398   "Sort articles by article number."
4399   (< (mail-header-number h1)
4400      (mail-header-number h2)))
4401
4402 (defun gnus-thread-sort-by-number (h1 h2)
4403   "Sort threads by root article number."
4404   (gnus-article-sort-by-number
4405    (gnus-thread-header h1) (gnus-thread-header h2)))
4406
4407 (defsubst gnus-article-sort-by-random (h1 h2)
4408   "Sort articles by article number."
4409   (zerop (random 2)))
4410
4411 (defun gnus-thread-sort-by-random (h1 h2)
4412   "Sort threads by root article number."
4413   (gnus-article-sort-by-random
4414    (gnus-thread-header h1) (gnus-thread-header h2)))
4415
4416 (defsubst gnus-article-sort-by-lines (h1 h2)
4417   "Sort articles by article Lines header."
4418   (< (mail-header-lines h1)
4419      (mail-header-lines h2)))
4420
4421 (defun gnus-thread-sort-by-lines (h1 h2)
4422   "Sort threads by root article Lines header."
4423   (gnus-article-sort-by-lines
4424    (gnus-thread-header h1) (gnus-thread-header h2)))
4425
4426 (defsubst gnus-article-sort-by-chars (h1 h2)
4427   "Sort articles by octet length."
4428   (< (mail-header-chars h1)
4429      (mail-header-chars h2)))
4430
4431 (defun gnus-thread-sort-by-chars (h1 h2)
4432   "Sort threads by root article octet length."
4433   (gnus-article-sort-by-chars
4434    (gnus-thread-header h1) (gnus-thread-header h2)))
4435
4436 (defsubst gnus-article-sort-by-author (h1 h2)
4437   "Sort articles by root author."
4438   (string-lessp
4439    (let ((extract (funcall
4440                    gnus-extract-address-components
4441                    (mail-header-from h1))))
4442      (or (car extract) (cadr extract) ""))
4443    (let ((extract (funcall
4444                    gnus-extract-address-components
4445                    (mail-header-from h2))))
4446      (or (car extract) (cadr extract) ""))))
4447
4448 (defun gnus-thread-sort-by-author (h1 h2)
4449   "Sort threads by root author."
4450   (gnus-article-sort-by-author
4451    (gnus-thread-header h1)  (gnus-thread-header h2)))
4452
4453 (defsubst gnus-article-sort-by-subject (h1 h2)
4454   "Sort articles by root subject."
4455   (string-lessp
4456    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4457    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4458
4459 (defun gnus-thread-sort-by-subject (h1 h2)
4460   "Sort threads by root subject."
4461   (gnus-article-sort-by-subject
4462    (gnus-thread-header h1) (gnus-thread-header h2)))
4463
4464 (defsubst gnus-article-sort-by-date (h1 h2)
4465   "Sort articles by root article date."
4466   (time-less-p
4467    (gnus-date-get-time (mail-header-date h1))
4468    (gnus-date-get-time (mail-header-date h2))))
4469
4470 (defun gnus-thread-sort-by-date (h1 h2)
4471   "Sort threads by root article date."
4472   (gnus-article-sort-by-date
4473    (gnus-thread-header h1) (gnus-thread-header h2)))
4474
4475 (defsubst gnus-article-sort-by-score (h1 h2)
4476   "Sort articles by root article score.
4477 Unscored articles will be counted as having a score of zero."
4478   (> (or (cdr (assq (mail-header-number h1)
4479                     gnus-newsgroup-scored))
4480          gnus-summary-default-score 0)
4481      (or (cdr (assq (mail-header-number h2)
4482                     gnus-newsgroup-scored))
4483          gnus-summary-default-score 0)))
4484
4485 (defun gnus-thread-sort-by-score (h1 h2)
4486   "Sort threads by root article score."
4487   (gnus-article-sort-by-score
4488    (gnus-thread-header h1) (gnus-thread-header h2)))
4489
4490 (defun gnus-thread-sort-by-total-score (h1 h2)
4491   "Sort threads by the sum of all scores in the thread.
4492 Unscored articles will be counted as having a score of zero."
4493   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4494
4495 (defun gnus-thread-total-score (thread)
4496   ;; This function find the total score of THREAD.
4497   (cond
4498    ((null thread)
4499     0)
4500    ((consp thread)
4501     (if (stringp (car thread))
4502         (apply gnus-thread-score-function 0
4503                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4504       (gnus-thread-total-score-1 thread)))
4505    (t
4506     (gnus-thread-total-score-1 (list thread)))))
4507
4508 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4509   "Sort threads such that the thread with the most recently arrived article comes first."
4510   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4511
4512 (defun gnus-thread-highest-number (thread)
4513   "Return the highest article number in THREAD."
4514   (apply 'max (mapcar (lambda (header)
4515                         (mail-header-number header))
4516                       (message-flatten-list thread))))
4517
4518 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4519   "Sort threads such that the thread with the most recently dated article comes first."
4520   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4521
4522 (defun gnus-thread-latest-date (thread)
4523   "Return the highest article date in THREAD."
4524   (let ((previous-time 0))
4525     (apply 'max
4526            (mapcar
4527             (lambda (header)
4528               (setq previous-time
4529                     (time-to-seconds
4530                      (condition-case ()
4531                          (mail-header-parse-date (mail-header-date header))
4532                        (error previous-time)))))
4533             (sort
4534              (message-flatten-list thread)
4535              (lambda (h1 h2)
4536                (< (mail-header-number h1)
4537                   (mail-header-number h2))))))))
4538
4539 (defun gnus-thread-total-score-1 (root)
4540   ;; This function find the total score of the thread below ROOT.
4541   (setq root (car root))
4542   (apply gnus-thread-score-function
4543          (or (append
4544               (mapcar 'gnus-thread-total-score
4545                       (cdr (gnus-id-to-thread (mail-header-id root))))
4546               (when (> (mail-header-number root) 0)
4547                 (list (or (cdr (assq (mail-header-number root)
4548                                      gnus-newsgroup-scored))
4549                           gnus-summary-default-score 0))))
4550              (list gnus-summary-default-score)
4551              '(0))))
4552
4553 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4554 (defvar gnus-tmp-prev-subject nil)
4555 (defvar gnus-tmp-false-parent nil)
4556 (defvar gnus-tmp-root-expunged nil)
4557 (defvar gnus-tmp-dummy-line nil)
4558
4559 (eval-when-compile (defvar gnus-tmp-header))
4560 (defun gnus-extra-header (type &optional header)
4561   "Return the extra header of TYPE."
4562   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4563       ""))
4564
4565 (defvar gnus-tmp-thread-tree-header-string "")
4566
4567 (defcustom gnus-sum-thread-tree-root "> "
4568   "With %B spec, used for the root of a thread.
4569 If nil, use subject instead."
4570   :type 'string
4571   :group 'gnus-thread)
4572 (defcustom gnus-sum-thread-tree-false-root "> "
4573   "With %B spec, used for a false root of a thread.
4574 If nil, use subject instead."
4575   :type 'string
4576   :group 'gnus-thread)
4577 (defcustom gnus-sum-thread-tree-single-indent ""
4578   "With %B spec, used for a thread with just one message.
4579 If nil, use subject instead."
4580   :type 'string
4581   :group 'gnus-thread)
4582 (defcustom gnus-sum-thread-tree-vertical "| "
4583   "With %B spec, used for drawing a vertical line."
4584   :type 'string
4585   :group 'gnus-thread)
4586 (defcustom gnus-sum-thread-tree-indent "  "
4587   "With %B spec, used for indenting."
4588   :type 'string
4589   :group 'gnus-thread)
4590 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4591   "With %B spec, used for a leaf with brothers."
4592   :type 'string
4593   :group 'gnus-thread)
4594 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4595   "With %B spec, used for a leaf without brothers."
4596   :type 'string
4597   :group 'gnus-thread)
4598
4599 (defun gnus-summary-prepare-threads (threads)
4600   "Prepare summary buffer from THREADS and indentation LEVEL.
4601 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4602 or a straight list of headers."
4603   (gnus-message 7 "Generating summary...")
4604
4605   (setq gnus-newsgroup-threads threads)
4606   (beginning-of-line)
4607
4608   (let ((gnus-tmp-level 0)
4609         (default-score (or gnus-summary-default-score 0))
4610         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4611         (building-line-count gnus-summary-display-while-building)
4612         (building-count (integerp gnus-summary-display-while-building))
4613         thread number subject stack state gnus-tmp-gathered beg-match
4614         new-roots gnus-tmp-new-adopts thread-end simp-subject
4615         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4616         gnus-tmp-replied gnus-tmp-subject-or-nil
4617         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4618         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4619         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4620         tree-stack)
4621
4622     (setq gnus-tmp-prev-subject nil
4623           gnus-tmp-thread-tree-header-string "")
4624
4625     (if (vectorp (car threads))
4626         ;; If this is a straight (sic) list of headers, then a
4627         ;; threaded summary display isn't required, so we just create
4628         ;; an unthreaded one.
4629         (gnus-summary-prepare-unthreaded threads)
4630
4631       ;; Do the threaded display.
4632
4633       (if gnus-summary-display-while-building
4634           (switch-to-buffer (buffer-name)))
4635       (while (or threads stack gnus-tmp-new-adopts new-roots)
4636
4637         (if (and (= gnus-tmp-level 0)
4638                  (or (not stack)
4639                      (= (caar stack) 0))
4640                  (not gnus-tmp-false-parent)
4641                  (or gnus-tmp-new-adopts new-roots))
4642             (if gnus-tmp-new-adopts
4643                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4644                       thread (list (car gnus-tmp-new-adopts))
4645                       gnus-tmp-header (caar thread)
4646                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4647               (when new-roots
4648                 (setq thread (list (car new-roots))
4649                       gnus-tmp-header (caar thread)
4650                       new-roots (cdr new-roots))))
4651
4652           (if threads
4653               ;; If there are some threads, we do them before the
4654               ;; threads on the stack.
4655               (setq thread threads
4656                     gnus-tmp-header (caar thread))
4657             ;; There were no current threads, so we pop something off
4658             ;; the stack.
4659             (setq state (car stack)
4660                   gnus-tmp-level (car state)
4661                   tree-stack (cadr state)
4662                   thread (caddr state)
4663                   stack (cdr stack)
4664                   gnus-tmp-header (caar thread))))
4665
4666         (setq gnus-tmp-false-parent nil)
4667         (setq gnus-tmp-root-expunged nil)
4668         (setq thread-end nil)
4669
4670         (if (stringp gnus-tmp-header)
4671             ;; The header is a dummy root.
4672             (cond
4673              ((eq gnus-summary-make-false-root 'adopt)
4674               ;; We let the first article adopt the rest.
4675               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4676                                                (cddar thread)))
4677               (setq gnus-tmp-gathered
4678                     (nconc (mapcar
4679                             (lambda (h) (mail-header-number (car h)))
4680                             (cddar thread))
4681                            gnus-tmp-gathered))
4682               (setq thread (cons (list (caar thread)
4683                                        (cadar thread))
4684                                  (cdr thread)))
4685               (setq gnus-tmp-level -1
4686                     gnus-tmp-false-parent t))
4687              ((eq gnus-summary-make-false-root 'empty)
4688               ;; We print adopted articles with empty subject fields.
4689               (setq gnus-tmp-gathered
4690                     (nconc (mapcar
4691                             (lambda (h) (mail-header-number (car h)))
4692                             (cddar thread))
4693                            gnus-tmp-gathered))
4694               (setq gnus-tmp-level -1))
4695              ((eq gnus-summary-make-false-root 'dummy)
4696               ;; We remember that we probably want to output a dummy
4697               ;; root.
4698               (setq gnus-tmp-dummy-line gnus-tmp-header)
4699               (setq gnus-tmp-prev-subject gnus-tmp-header))
4700              (t
4701               ;; We do not make a root for the gathered
4702               ;; sub-threads at all.
4703               (setq gnus-tmp-level -1)))
4704
4705           (setq number (mail-header-number gnus-tmp-header)
4706                 subject (mail-header-subject gnus-tmp-header)
4707                 simp-subject (gnus-simplify-subject-fully subject))
4708
4709           (cond
4710            ;; If the thread has changed subject, we might want to make
4711            ;; this subthread into a root.
4712            ((and (null gnus-thread-ignore-subject)
4713                  (not (zerop gnus-tmp-level))
4714                  gnus-tmp-prev-subject
4715                  (not (string= gnus-tmp-prev-subject simp-subject)))
4716             (setq new-roots (nconc new-roots (list (car thread)))
4717                   thread-end t
4718                   gnus-tmp-header nil))
4719            ;; If the article lies outside the current limit,
4720            ;; then we do not display it.
4721            ((not (memq number gnus-newsgroup-limit))
4722             (setq gnus-tmp-gathered
4723                   (nconc (mapcar
4724                           (lambda (h) (mail-header-number (car h)))
4725                           (cdar thread))
4726                          gnus-tmp-gathered))
4727             (setq gnus-tmp-new-adopts (if (cdar thread)
4728                                           (append gnus-tmp-new-adopts
4729                                                   (cdar thread))
4730                                         gnus-tmp-new-adopts)
4731                   thread-end t
4732                   gnus-tmp-header nil)
4733             (when (zerop gnus-tmp-level)
4734               (setq gnus-tmp-root-expunged t)))
4735            ;; Perhaps this article is to be marked as read?
4736            ((and gnus-summary-mark-below
4737                  (< (or (cdr (assq number gnus-newsgroup-scored))
4738                         default-score)
4739                     gnus-summary-mark-below)
4740                  ;; Don't touch sparse articles.
4741                  (not (gnus-summary-article-sparse-p number))
4742                  (not (gnus-summary-article-ancient-p number)))
4743             (setq gnus-newsgroup-unreads
4744                   (delq number gnus-newsgroup-unreads))
4745             (if gnus-newsgroup-auto-expire
4746                 (setq gnus-newsgroup-expirable
4747                       (gnus-add-to-sorted-list
4748                        gnus-newsgroup-expirable number))
4749               (push (cons number gnus-low-score-mark)
4750                     gnus-newsgroup-reads))))
4751
4752           (when gnus-tmp-header
4753             ;; We may have an old dummy line to output before this
4754             ;; article.
4755             (when (and gnus-tmp-dummy-line
4756                        (gnus-subject-equal
4757                         gnus-tmp-dummy-line
4758                         (mail-header-subject gnus-tmp-header)))
4759               (gnus-summary-insert-dummy-line
4760                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4761               (setq gnus-tmp-dummy-line nil))
4762
4763             ;; Compute the mark.
4764             (setq gnus-tmp-unread (gnus-article-mark number))
4765
4766             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4767                                   gnus-tmp-header gnus-tmp-level)
4768                   gnus-newsgroup-data)
4769
4770             ;; Actually insert the line.
4771             (setq
4772              gnus-tmp-subject-or-nil
4773              (cond
4774               ((and gnus-thread-ignore-subject
4775                     gnus-tmp-prev-subject
4776                     (not (string= gnus-tmp-prev-subject simp-subject)))
4777                subject)
4778               ((zerop gnus-tmp-level)
4779                (if (and (eq gnus-summary-make-false-root 'empty)
4780                         (memq number gnus-tmp-gathered)
4781                         gnus-tmp-prev-subject
4782                         (string= gnus-tmp-prev-subject simp-subject))
4783                    gnus-summary-same-subject
4784                  subject))
4785               (t gnus-summary-same-subject)))
4786             (if (and (eq gnus-summary-make-false-root 'adopt)
4787                      (= gnus-tmp-level 1)
4788                      (memq number gnus-tmp-gathered))
4789                 (setq gnus-tmp-opening-bracket ?\<
4790                       gnus-tmp-closing-bracket ?\>)
4791               (setq gnus-tmp-opening-bracket ?\[
4792                     gnus-tmp-closing-bracket ?\]))
4793             (setq
4794              gnus-tmp-indentation
4795              (aref gnus-thread-indent-array gnus-tmp-level)
4796              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4797              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4798                                 gnus-summary-default-score 0)
4799              gnus-tmp-score-char
4800              (if (or (null gnus-summary-default-score)
4801                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4802                          gnus-summary-zcore-fuzz))
4803                  ?                      ;Whitespace
4804                (if (< gnus-tmp-score gnus-summary-default-score)
4805                    gnus-score-below-mark gnus-score-over-mark))
4806              gnus-tmp-replied
4807              (cond ((memq number gnus-newsgroup-processable)
4808                     gnus-process-mark)
4809                    ((memq number gnus-newsgroup-cached)
4810                     gnus-cached-mark)
4811                    ((memq number gnus-newsgroup-replied)
4812                     gnus-replied-mark)
4813                    ((memq number gnus-newsgroup-forwarded)
4814                     gnus-forwarded-mark)
4815                    ((memq number gnus-newsgroup-saved)
4816                     gnus-saved-mark)
4817                    ((memq number gnus-newsgroup-recent)
4818                     gnus-recent-mark)
4819                    ((memq number gnus-newsgroup-unseen)
4820                     gnus-unseen-mark)
4821                    (t gnus-no-mark))
4822              gnus-tmp-downloaded
4823              (cond ((memq number gnus-newsgroup-undownloaded) 
4824                     gnus-undownloaded-mark)
4825                    (gnus-newsgroup-agentized
4826                     gnus-downloaded-mark)
4827                    (t
4828                     gnus-no-mark))
4829              gnus-tmp-from (mail-header-from gnus-tmp-header)
4830              gnus-tmp-name
4831              (cond
4832               ((string-match "<[^>]+> *$" gnus-tmp-from)
4833                (setq beg-match (match-beginning 0))
4834                (or (and (string-match "^\".+\"" gnus-tmp-from)
4835                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4836                    (substring gnus-tmp-from 0 beg-match)))
4837               ((string-match "(.+)" gnus-tmp-from)
4838                (substring gnus-tmp-from
4839                           (1+ (match-beginning 0)) (1- (match-end 0))))
4840               (t gnus-tmp-from))
4841
4842              ;; Do the %B string
4843              gnus-tmp-thread-tree-header-string
4844              (cond
4845               ((not gnus-show-threads) "")
4846               ((zerop gnus-tmp-level)
4847                (cond ((cdar thread)
4848                       (or gnus-sum-thread-tree-root subject))
4849                      (gnus-tmp-new-adopts
4850                       (or gnus-sum-thread-tree-false-root subject))
4851                      (t
4852                       (or gnus-sum-thread-tree-single-indent subject))))
4853               (t
4854                (concat (apply 'concat
4855                               (mapcar (lambda (item)
4856                                         (if (= item 1)
4857                                             gnus-sum-thread-tree-vertical
4858                                           gnus-sum-thread-tree-indent))
4859                                       (cdr (reverse tree-stack))))
4860                        (if (nth 1 thread)
4861                            gnus-sum-thread-tree-leaf-with-other
4862                          gnus-sum-thread-tree-single-leaf)))))
4863             (when (string= gnus-tmp-name "")
4864               (setq gnus-tmp-name gnus-tmp-from))
4865             (unless (numberp gnus-tmp-lines)
4866               (setq gnus-tmp-lines -1))
4867             (if (= gnus-tmp-lines -1)
4868                 (setq gnus-tmp-lines "?")
4869               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4870               (gnus-put-text-property
4871              (point)
4872              (progn (eval gnus-summary-line-format-spec) (point))
4873                'gnus-number number)
4874             (when gnus-visual-p
4875               (forward-line -1)
4876               (gnus-run-hooks 'gnus-summary-update-hook)
4877               (forward-line 1))
4878
4879             (setq gnus-tmp-prev-subject simp-subject)))
4880
4881         (when (nth 1 thread)
4882           (push (list (max 0 gnus-tmp-level)
4883                       (copy-sequence tree-stack)
4884                       (nthcdr 1 thread))
4885                 stack))
4886         (push (if (nth 1 thread) 1 0) tree-stack)
4887         (incf gnus-tmp-level)
4888         (setq threads (if thread-end nil (cdar thread)))
4889         (if gnus-summary-display-while-building
4890             (if building-count
4891                 (progn
4892                   ;; use a set frequency
4893                   (setq building-line-count (1- building-line-count))
4894                   (when (= building-line-count 0)
4895                     (sit-for 0)
4896                     (setq building-line-count
4897                           gnus-summary-display-while-building)))
4898               ;; always
4899               (sit-for 0)))
4900         (unless threads
4901           (setq gnus-tmp-level 0)))))
4902   (gnus-message 7 "Generating summary...done"))
4903
4904 (defun gnus-summary-prepare-unthreaded (headers)
4905   "Generate an unthreaded summary buffer based on HEADERS."
4906   (let (header number mark)
4907
4908     (beginning-of-line)
4909
4910     (while headers
4911       ;; We may have to root out some bad articles...
4912       (when (memq (setq number (mail-header-number
4913                                 (setq header (pop headers))))
4914                   gnus-newsgroup-limit)
4915         ;; Mark article as read when it has a low score.
4916         (when (and gnus-summary-mark-below
4917                    (< (or (cdr (assq number gnus-newsgroup-scored))
4918                           gnus-summary-default-score 0)
4919                       gnus-summary-mark-below)
4920                    (not (gnus-summary-article-ancient-p number)))
4921           (setq gnus-newsgroup-unreads
4922                 (delq number gnus-newsgroup-unreads))
4923           (if gnus-newsgroup-auto-expire
4924               (push number gnus-newsgroup-expirable)
4925             (push (cons number gnus-low-score-mark)
4926                   gnus-newsgroup-reads)))
4927
4928         (setq mark (gnus-article-mark number))
4929         (push (gnus-data-make number mark (1+ (point)) header 0)
4930               gnus-newsgroup-data)
4931         (gnus-summary-insert-line
4932          header 0 number
4933          (memq number gnus-newsgroup-undownloaded)
4934          mark (memq number gnus-newsgroup-replied)
4935          (memq number gnus-newsgroup-expirable)
4936          (mail-header-subject header) nil
4937          (cdr (assq number gnus-newsgroup-scored))
4938          (memq number gnus-newsgroup-processable))))))
4939
4940 (defun gnus-summary-remove-list-identifiers ()
4941   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4942   (let ((regexp (if (consp gnus-list-identifiers)
4943                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4944                   gnus-list-identifiers))
4945         changed subject)
4946     (when regexp
4947       (dolist (header gnus-newsgroup-headers)
4948         (setq subject (mail-header-subject header)
4949               changed nil)
4950         (while (string-match
4951                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4952                 subject)
4953           (setq subject
4954                 (concat (substring subject 0 (match-beginning 2))
4955                         (substring subject (match-end 0)))
4956                 changed t))
4957         (when (and changed
4958                    (string-match
4959                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4960           (setq subject
4961                 (concat (substring subject 0 (match-beginning 1))
4962                         (substring subject (match-end 1)))))
4963         (when changed
4964           (mail-header-set-subject header subject))))))
4965
4966 (defun gnus-fetch-headers (articles)
4967   "Fetch headers of ARTICLES."
4968   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4969     (gnus-message 5 "Fetching headers for %s..." name)
4970     (prog1
4971         (if (eq 'nov
4972                 (setq gnus-headers-retrieved-by
4973                       (gnus-retrieve-headers
4974                        articles gnus-newsgroup-name
4975                        ;; We might want to fetch old headers, but
4976                        ;; not if there is only 1 article.
4977                        (and (or (and
4978                                  (not (eq gnus-fetch-old-headers 'some))
4979                                  (not (numberp gnus-fetch-old-headers)))
4980                                 (> (length articles) 1))
4981                             gnus-fetch-old-headers))))
4982             (gnus-get-newsgroup-headers-xover
4983              articles nil nil gnus-newsgroup-name t)
4984           (gnus-get-newsgroup-headers))
4985       (gnus-message 5 "Fetching headers for %s...done" name))))
4986
4987 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4988   "Select newsgroup GROUP.
4989 If READ-ALL is non-nil, all articles in the group are selected.
4990 If SELECT-ARTICLES, only select those articles from GROUP."
4991   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4992          ;;!!! Dirty hack; should be removed.
4993          (gnus-summary-ignore-duplicates
4994           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4995               t
4996             gnus-summary-ignore-duplicates))
4997          (info (nth 2 entry))
4998          articles fetched-articles cached)
4999
5000     (unless (gnus-check-server
5001              (set (make-local-variable 'gnus-current-select-method)
5002                   (gnus-find-method-for-group group)))
5003       (error "Couldn't open server"))
5004
5005     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5006         (gnus-activate-group group)     ; Or we can activate it...
5007         (progn                          ; Or we bug out.
5008           (when (equal major-mode 'gnus-summary-mode)
5009             (gnus-kill-buffer (current-buffer)))
5010           (error "Couldn't activate group %s: %s"
5011                  group (gnus-status-message group))))
5012
5013     (unless (gnus-request-group group t)
5014       (when (equal major-mode 'gnus-summary-mode)
5015         (gnus-kill-buffer (current-buffer)))
5016       (error "Couldn't request group %s: %s"
5017              group (gnus-status-message group)))
5018
5019     (when gnus-agent
5020       ;; The agent may be storing articles that are no longer in the
5021       ;; server's active range.  If that is the case, the active range
5022       ;; needs to be expanded such that the agent's articles can be
5023       ;; included in the summary.
5024       (let* ((gnus-command-method (gnus-find-method-for-group group))
5025              (alist (gnus-agent-load-alist group))
5026              (active (gnus-active group)))
5027         (if (and (car alist)
5028                  (< (caar alist) (car active)))
5029             (gnus-set-active group (cons (caar alist) (cdr active)))))
5030
5031       (setq gnus-summary-use-undownloaded-faces
5032             (not (gnus-agent-find-parameter 
5033                   group
5034                   'agent-disable-undownloaded-faces))))
5035
5036     (setq gnus-newsgroup-name group
5037           gnus-newsgroup-unselected nil
5038           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5039
5040     (let ((display (gnus-group-find-parameter group 'display)))
5041       (setq gnus-newsgroup-display
5042             (cond
5043              ((not (zerop (or (car-safe read-all) 0)))
5044               ;; The user entered the group with C-u SPC/RET, let's show
5045               ;; all articles.
5046               'gnus-not-ignore)
5047              ((eq display 'all)
5048               'gnus-not-ignore)
5049              ((arrayp display)
5050               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5051              ((numberp display)
5052               ;; The following is probably the "correct" solution, but
5053               ;; it makes Gnus fetch all headers and then limit the
5054               ;; articles (which is slow), so instead we hack the
5055               ;; select-articles parameter instead. -- Simon Josefsson
5056               ;; <jas@kth.se>
5057               ;;
5058               ;; (gnus-byte-compile
5059               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5060               ;;                         display)))))
5061               (setq select-articles
5062                     (gnus-uncompress-range
5063                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5064                              (if (> tmp 0)
5065                                  tmp
5066                                1))
5067                            (cdr (gnus-active group)))))
5068               nil)
5069              (t
5070               nil))))
5071
5072     (gnus-summary-setup-default-charset)
5073
5074     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5075     (when (gnus-virtual-group-p group)
5076       (setq cached gnus-newsgroup-cached))
5077
5078     (setq gnus-newsgroup-unreads
5079           (gnus-sorted-ndifference
5080            (gnus-sorted-ndifference gnus-newsgroup-unreads
5081                                     gnus-newsgroup-marked)
5082            gnus-newsgroup-dormant))
5083
5084     (setq gnus-newsgroup-processable nil)
5085
5086     (gnus-update-read-articles group gnus-newsgroup-unreads)
5087
5088     ;; Adjust and set lists of article marks.
5089     (when info
5090       (gnus-adjust-marked-articles info))
5091     (if (setq articles select-articles)
5092         (setq gnus-newsgroup-unselected
5093               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5094       (setq articles (gnus-articles-to-read group read-all)))
5095
5096     (cond
5097      ((null articles)
5098       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5099       'quit)
5100      ((eq articles 0) nil)
5101      (t
5102       ;; Init the dependencies hash table.
5103       (setq gnus-newsgroup-dependencies
5104             (gnus-make-hashtable (length articles)))
5105       (gnus-set-global-variables)
5106       ;; Retrieve the headers and read them in.
5107
5108       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5109
5110       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5111       (when cached
5112         (setq gnus-newsgroup-cached cached))
5113
5114       ;; Suppress duplicates?
5115       (when gnus-suppress-duplicates
5116         (gnus-dup-suppress-articles))
5117
5118       ;; Set the initial limit.
5119       (setq gnus-newsgroup-limit (copy-sequence articles))
5120       ;; Remove canceled articles from the list of unread articles.
5121       (setq fetched-articles
5122             (mapcar (lambda (headers) (mail-header-number headers))
5123                     gnus-newsgroup-headers))
5124       (setq gnus-newsgroup-articles fetched-articles)
5125       (setq gnus-newsgroup-unreads
5126             (gnus-sorted-nintersection
5127              gnus-newsgroup-unreads fetched-articles))
5128       (gnus-compute-unseen-list)
5129
5130       ;; Removed marked articles that do not exist.
5131       (gnus-update-missing-marks
5132        (gnus-sorted-difference articles fetched-articles))
5133       ;; We might want to build some more threads first.
5134       (when (and gnus-fetch-old-headers
5135                  (eq gnus-headers-retrieved-by 'nov))
5136         (if (eq gnus-fetch-old-headers 'invisible)
5137             (gnus-build-all-threads)
5138           (gnus-build-old-threads)))
5139       ;; Let the Gnus agent mark articles as read.
5140       (when gnus-agent
5141         (gnus-agent-get-undownloaded-list))
5142       ;; Remove list identifiers from subject
5143       (when gnus-list-identifiers
5144         (gnus-summary-remove-list-identifiers))
5145       ;; Check whether auto-expire is to be done in this group.
5146       (setq gnus-newsgroup-auto-expire
5147             (gnus-group-auto-expirable-p group))
5148       ;; Set up the article buffer now, if necessary.
5149       (unless gnus-single-article-buffer
5150         (gnus-article-setup-buffer))
5151       ;; First and last article in this newsgroup.
5152       (when gnus-newsgroup-headers
5153         (setq gnus-newsgroup-begin
5154               (mail-header-number (car gnus-newsgroup-headers))
5155               gnus-newsgroup-end
5156               (mail-header-number
5157                (gnus-last-element gnus-newsgroup-headers))))
5158       ;; GROUP is successfully selected.
5159       (or gnus-newsgroup-headers t)))))
5160
5161 (defun gnus-compute-unseen-list ()
5162   ;; The `seen' marks are treated specially.
5163   (if (not gnus-newsgroup-seen)
5164       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5165     (setq gnus-newsgroup-unseen
5166           (gnus-inverse-list-range-intersection
5167            gnus-newsgroup-articles gnus-newsgroup-seen))))
5168
5169 (defun gnus-summary-display-make-predicate (display)
5170   (require 'gnus-agent)
5171   (when (= (length display) 1)
5172     (setq display (car display)))
5173   (unless gnus-summary-display-cache
5174     (dolist (elem (append '((unread . unread)
5175                             (read . read)
5176                             (unseen . unseen))
5177                           gnus-article-mark-lists))
5178       (push (cons (cdr elem)
5179                   (gnus-byte-compile
5180                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5181             gnus-summary-display-cache)))
5182   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5183         (gnus-category-predicate-cache gnus-summary-display-cache))
5184     (gnus-get-predicate display)))
5185
5186 ;; Uses the dynamically bound `number' variable.
5187 (eval-when-compile
5188   (defvar number))
5189 (defun gnus-article-marked-p (type &optional article)
5190   (let ((article (or article number)))
5191     (cond
5192      ((eq type 'tick)
5193       (memq article gnus-newsgroup-marked))
5194      ((eq type 'spam)
5195       (memq article gnus-newsgroup-spam-marked))
5196      ((eq type 'unsend)
5197       (memq article gnus-newsgroup-unsendable))
5198      ((eq type 'undownload)
5199       (memq article gnus-newsgroup-undownloaded))
5200      ((eq type 'download)
5201       (memq article gnus-newsgroup-downloadable))
5202      ((eq type 'unread)
5203       (memq article gnus-newsgroup-unreads))
5204      ((eq type 'read)
5205       (memq article gnus-newsgroup-reads))
5206      ((eq type 'dormant)
5207       (memq article gnus-newsgroup-dormant) )
5208      ((eq type 'expire)
5209       (memq article gnus-newsgroup-expirable))
5210      ((eq type 'reply)
5211       (memq article gnus-newsgroup-replied))
5212      ((eq type 'killed)
5213       (memq article gnus-newsgroup-killed))
5214      ((eq type 'bookmark)
5215       (assq article gnus-newsgroup-bookmarks))
5216      ((eq type 'score)
5217       (assq article gnus-newsgroup-scored))
5218      ((eq type 'save)
5219       (memq article gnus-newsgroup-saved))
5220      ((eq type 'cache)
5221       (memq article gnus-newsgroup-cached))
5222      ((eq type 'forward)
5223       (memq article gnus-newsgroup-forwarded))
5224      ((eq type 'seen)
5225       (not (memq article gnus-newsgroup-unseen)))
5226      ((eq type 'recent)
5227       (memq article gnus-newsgroup-recent))
5228      (t t))))
5229
5230 (defun gnus-articles-to-read (group &optional read-all)
5231   "Find out what articles the user wants to read."
5232   (let* ((display (gnus-group-find-parameter group 'display))
5233          (articles
5234           ;; Select all articles if `read-all' is non-nil, or if there
5235           ;; are no unread articles.
5236           (if (or read-all
5237                   (and (zerop (length gnus-newsgroup-marked))
5238                        (zerop (length gnus-newsgroup-unreads)))
5239                   ;; Fetch all if the predicate is non-nil.
5240                   gnus-newsgroup-display)
5241               ;; We want to select the headers for all the articles in
5242               ;; the group, so we select either all the active
5243               ;; articles in the group, or (if that's nil), the
5244               ;; articles in the cache.
5245               (or
5246                (gnus-uncompress-range (gnus-active group))
5247                (gnus-cache-articles-in-group group))
5248             ;; Select only the "normal" subset of articles.
5249             (gnus-sorted-nunion
5250              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5251              gnus-newsgroup-unreads)))
5252          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5253          (scored (length scored-list))
5254          (number (length articles))
5255          (marked (+ (length gnus-newsgroup-marked)
5256                     (length gnus-newsgroup-dormant)))
5257          (select
5258           (cond
5259            ((numberp read-all)
5260             read-all)
5261            ((numberp gnus-newsgroup-display)
5262             gnus-newsgroup-display)
5263            (t
5264             (condition-case ()
5265                 (cond
5266                  ((and (or (<= scored marked) (= scored number))
5267                        (numberp gnus-large-newsgroup)
5268                        (> number gnus-large-newsgroup))
5269                   (let* ((cursor-in-echo-area nil)
5270                          (initial (gnus-parameter-large-newsgroup-initial
5271                                    gnus-newsgroup-name))
5272                          (input
5273                           (read-string
5274                            (format
5275                             "How many articles from %s (%s %d): "
5276                             (gnus-limit-string
5277                              (gnus-group-decoded-name gnus-newsgroup-name)
5278                              35)
5279                             (if initial "max" "default")
5280                             number)
5281                            (if initial
5282                                (cons (number-to-string initial)
5283                                      0)))))
5284                     (if (string-match "^[ \t]*$" input) number input)))
5285                  ((and (> scored marked) (< scored number)
5286                        (> (- scored number) 20))
5287                   (let ((input
5288                          (read-string
5289                           (format "%s %s (%d scored, %d total): "
5290                                   "How many articles from"
5291                                   (gnus-group-decoded-name group)
5292                                   scored number))))
5293                     (if (string-match "^[ \t]*$" input)
5294                         number input)))
5295                  (t number))
5296               (quit
5297                (message "Quit getting the articles to read")
5298                nil))))))
5299     (setq select (if (stringp select) (string-to-number select) select))
5300     (if (or (null select) (zerop select))
5301         select
5302       (if (and (not (zerop scored)) (<= (abs select) scored))
5303           (progn
5304             (setq articles (sort scored-list '<))
5305             (setq number (length articles)))
5306         (setq articles (copy-sequence articles)))
5307
5308       (when (< (abs select) number)
5309         (if (< select 0)
5310             ;; Select the N oldest articles.
5311             (setcdr (nthcdr (1- (abs select)) articles) nil)
5312           ;; Select the N most recent articles.
5313           (setq articles (nthcdr (- number select) articles))))
5314       (setq gnus-newsgroup-unselected
5315             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5316       (when gnus-alter-articles-to-read-function
5317         (setq articles
5318               (sort
5319                (funcall gnus-alter-articles-to-read-function
5320                         gnus-newsgroup-name articles)
5321                '<)))
5322       articles)))
5323
5324 (defun gnus-killed-articles (killed articles)
5325   (let (out)
5326     (while articles
5327       (when (inline (gnus-member-of-range (car articles) killed))
5328         (push (car articles) out))
5329       (setq articles (cdr articles)))
5330     out))
5331
5332 (defun gnus-uncompress-marks (marks)
5333   "Uncompress the mark ranges in MARKS."
5334   (let ((uncompressed '(score bookmark))
5335         out)
5336     (while marks
5337       (if (memq (caar marks) uncompressed)
5338           (push (car marks) out)
5339         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5340       (setq marks (cdr marks)))
5341     out))
5342
5343 (defun gnus-article-mark-to-type (mark)
5344   "Return the type of MARK."
5345   (or (cadr (assq mark gnus-article-special-mark-lists))
5346       'list))
5347
5348 (defun gnus-article-unpropagatable-p (mark)
5349   "Return whether MARK should be propagated to backend."
5350   (memq mark gnus-article-unpropagated-mark-lists))
5351
5352 (defun gnus-adjust-marked-articles (info)
5353   "Set all article lists and remove all marks that are no longer valid."
5354   (let* ((marked-lists (gnus-info-marks info))
5355          (active (gnus-active (gnus-info-group info)))
5356          (min (car active))
5357          (max (cdr active))
5358          (types gnus-article-mark-lists)
5359          marks var articles article mark mark-type)
5360
5361     (dolist (marks marked-lists)
5362       (setq mark (car marks)
5363             mark-type (gnus-article-mark-to-type mark)
5364             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5365
5366       ;; We set the variable according to the type of the marks list,
5367       ;; and then adjust the marks to a subset of the active articles.
5368       (cond
5369        ;; Adjust "simple" lists.
5370        ((eq mark-type 'list)
5371         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5372         (when (memq mark '(tick dormant expire reply save))
5373           (while articles
5374             (when (or (< (setq article (pop articles)) min) (> article max))
5375               (set var (delq article (symbol-value var)))))))
5376        ;; Adjust assocs.
5377        ((eq mark-type 'tuple)
5378         (set var (setq articles (cdr marks)))
5379         (when (not (listp (cdr (symbol-value var))))
5380           (set var (list (symbol-value var))))
5381         (when (not (listp (cdr articles)))
5382           (setq articles (list articles)))
5383         (while articles
5384           (when (or (not (consp (setq article (pop articles))))
5385                     (< (car article) min)
5386                     (> (car article) max))
5387             (set var (delq article (symbol-value var))))))
5388        ;; Adjust ranges (sloppily).
5389        ((eq mark-type 'range)
5390         (cond
5391          ((eq mark 'seen)
5392           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5393           ;; It should be (seen (NUM1 . NUM2)).
5394           (when (numberp (cddr marks))
5395             (setcdr marks (list (cdr marks))))
5396           (setq articles (cdr marks))
5397           (while (and articles
5398                       (or (and (consp (car articles))
5399                                (> min (cdar articles)))
5400                           (and (numberp (car articles))
5401                                (> min (car articles)))))
5402             (pop articles))
5403           (set var articles))))))))
5404
5405 (defun gnus-update-missing-marks (missing)
5406   "Go through the list of MISSING articles and remove them from the mark lists."
5407   (when missing
5408     (let (var m)
5409       ;; Go through all types.
5410       (dolist (elem gnus-article-mark-lists)
5411         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5412           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5413           (when (symbol-value var)
5414             ;; This list has articles.  So we delete all missing
5415             ;; articles from it.
5416             (setq m missing)
5417             (while m
5418               (set var (delq (pop m) (symbol-value var))))))))))
5419
5420 (defun gnus-update-marks ()
5421   "Enter the various lists of marked articles into the newsgroup info list."
5422   (let ((types gnus-article-mark-lists)
5423         (info (gnus-get-info gnus-newsgroup-name))
5424         type list newmarked symbol delta-marks)
5425     (when info
5426       ;; Add all marks lists to the list of marks lists.
5427       (while (setq type (pop types))
5428         (setq list (symbol-value
5429                     (setq symbol
5430                           (intern (format "gnus-newsgroup-%s" (car type))))))
5431
5432         (when list
5433           ;; Get rid of the entries of the articles that have the
5434           ;; default score.
5435           (when (and (eq (cdr type) 'score)
5436                      gnus-save-score
5437                      list)
5438             (let* ((arts list)
5439                    (prev (cons nil list))
5440                    (all prev))
5441               (while arts
5442                 (if (or (not (consp (car arts)))
5443                         (= (cdar arts) gnus-summary-default-score))
5444                     (setcdr prev (cdr arts))
5445                   (setq prev arts))
5446                 (setq arts (cdr arts)))
5447               (setq list (cdr all)))))
5448
5449         (when (eq (cdr type) 'seen)
5450           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5451
5452         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5453           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5454
5455         (when (and (gnus-check-backend-function
5456                     'request-set-mark gnus-newsgroup-name)
5457                    (not (gnus-article-unpropagatable-p (cdr type))))
5458           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5459                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5460                  (add (gnus-remove-from-range
5461                        (gnus-copy-sequence list) old)))
5462             (when add
5463               (push (list add 'add (list (cdr type))) delta-marks))
5464             (when del
5465               (push (list del 'del (list (cdr type))) delta-marks))))
5466
5467         (when list
5468           (push (cons (cdr type) list) newmarked)))
5469
5470       (when delta-marks
5471         (unless (gnus-check-group gnus-newsgroup-name)
5472           (error "Can't open server for %s" gnus-newsgroup-name))
5473         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5474
5475       ;; Enter these new marks into the info of the group.
5476       (if (nthcdr 3 info)
5477           (setcar (nthcdr 3 info) newmarked)
5478         ;; Add the marks lists to the end of the info.
5479         (when newmarked
5480           (setcdr (nthcdr 2 info) (list newmarked))))
5481
5482       ;; Cut off the end of the info if there's nothing else there.
5483       (let ((i 5))
5484         (while (and (> i 2)
5485                     (not (nth i info)))
5486           (when (nthcdr (decf i) info)
5487             (setcdr (nthcdr i info) nil)))))))
5488
5489 (defun gnus-set-mode-line (where)
5490   "Set the mode line of the article or summary buffers.
5491 If WHERE is `summary', the summary mode line format will be used."
5492   ;; Is this mode line one we keep updated?
5493   (when (and (memq where gnus-updated-mode-lines)
5494              (symbol-value
5495               (intern (format "gnus-%s-mode-line-format-spec" where))))
5496     (let (mode-string)
5497       (save-excursion
5498         ;; We evaluate this in the summary buffer since these
5499         ;; variables are buffer-local to that buffer.
5500         (set-buffer gnus-summary-buffer)
5501        ;; We bind all these variables that are used in the `eval' form
5502         ;; below.
5503         (let* ((mformat (symbol-value
5504                          (intern
5505                           (format "gnus-%s-mode-line-format-spec" where))))
5506                (gnus-tmp-group-name (gnus-group-decoded-name
5507                                      gnus-newsgroup-name))
5508                (gnus-tmp-article-number (or gnus-current-article 0))
5509                (gnus-tmp-unread gnus-newsgroup-unreads)
5510                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5511                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5512                (gnus-tmp-unread-and-unselected
5513                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5514                             (zerop gnus-tmp-unselected))
5515                        "")
5516                       ((zerop gnus-tmp-unselected)
5517                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5518                       (t (format "{%d(+%d) more}"
5519                                  gnus-tmp-unread-and-unticked
5520                                  gnus-tmp-unselected))))
5521                (gnus-tmp-subject
5522                 (if (and gnus-current-headers
5523                          (vectorp gnus-current-headers))
5524                     (gnus-mode-string-quote
5525                      (mail-header-subject gnus-current-headers))
5526                   ""))
5527                bufname-length max-len
5528                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5529           (setq mode-string (eval mformat))
5530           (setq bufname-length (if (string-match "%b" mode-string)
5531                                    (- (length
5532                                        (buffer-name
5533                                         (if (eq where 'summary)
5534                                             nil
5535                                           (get-buffer gnus-article-buffer))))
5536                                       2)
5537                                  0))
5538           (setq max-len (max 4 (if gnus-mode-non-string-length
5539                                    (- (window-width)
5540                                       gnus-mode-non-string-length
5541                                       bufname-length)
5542                                  (length mode-string))))
5543           ;; We might have to chop a bit of the string off...
5544           (when (> (length mode-string) max-len)
5545             (setq mode-string
5546                   (concat (truncate-string-to-width mode-string (- max-len 3))
5547                           "...")))
5548           ;; Pad the mode string a bit.
5549           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5550       ;; Update the mode line.
5551       (setq mode-line-buffer-identification
5552             (gnus-mode-line-buffer-identification (list mode-string)))
5553       (set-buffer-modified-p t))))
5554
5555 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5556   "Go through the HEADERS list and add all Xrefs to a hash table.
5557 The resulting hash table is returned, or nil if no Xrefs were found."
5558   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5559          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5560          (xref-hashtb (gnus-make-hashtable))
5561          start group entry number xrefs header)
5562     (while headers
5563       (setq header (pop headers))
5564       (when (and (setq xrefs (mail-header-xref header))
5565                  (not (memq (setq number (mail-header-number header))
5566                             unreads)))
5567         (setq start 0)
5568         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5569           (setq start (match-end 0))
5570           (setq group (if prefix
5571                           (concat prefix (substring xrefs (match-beginning 1)
5572                                                     (match-end 1)))
5573                         (substring xrefs (match-beginning 1) (match-end 1))))
5574           (setq number
5575                 (string-to-int (substring xrefs (match-beginning 2)
5576                                           (match-end 2))))
5577           (if (setq entry (gnus-gethash group xref-hashtb))
5578               (setcdr entry (cons number (cdr entry)))
5579             (gnus-sethash group (cons number nil) xref-hashtb)))))
5580     (and start xref-hashtb)))
5581
5582 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5583   "Look through all the headers and mark the Xrefs as read."
5584   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5585         name entry info xref-hashtb idlist method nth4)
5586     (save-excursion
5587       (set-buffer gnus-group-buffer)
5588       (when (setq xref-hashtb
5589                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5590         (mapatoms
5591          (lambda (group)
5592            (unless (string= from-newsgroup (setq name (symbol-name group)))
5593              (setq idlist (symbol-value group))
5594              ;; Dead groups are not updated.
5595              (and (prog1
5596                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5597                             info (nth 2 entry))
5598                     (when (stringp (setq nth4 (gnus-info-method info)))
5599                       (setq nth4 (gnus-server-to-method nth4))))
5600                   ;; Only do the xrefs if the group has the same
5601                   ;; select method as the group we have just read.
5602                   (or (gnus-methods-equal-p
5603                        nth4 (gnus-find-method-for-group from-newsgroup))
5604                       virtual
5605                       (equal nth4 (setq method (gnus-find-method-for-group
5606                                                 from-newsgroup)))
5607                       (and (equal (car nth4) (car method))
5608                            (equal (nth 1 nth4) (nth 1 method))))
5609                   gnus-use-cross-reference
5610                   (or (not (eq gnus-use-cross-reference t))
5611                       virtual
5612                       ;; Only do cross-references on subscribed
5613                       ;; groups, if that is what is wanted.
5614                       (<= (gnus-info-level info) gnus-level-subscribed))
5615                   (gnus-group-make-articles-read name idlist))))
5616          xref-hashtb)))))
5617
5618 (defun gnus-compute-read-articles (group articles)
5619   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5620          (info (nth 2 entry))
5621          (active (gnus-active group))
5622          ninfo)
5623     (when entry
5624       ;; First peel off all invalid article numbers.
5625       (when active
5626         (let ((ids articles)
5627               id first)
5628           (while (setq id (pop ids))
5629             (when (and first (> id (cdr active)))
5630               ;; We'll end up in this situation in one particular
5631               ;; obscure situation.  If you re-scan a group and get
5632               ;; a new article that is cross-posted to a different
5633               ;; group that has not been re-scanned, you might get
5634               ;; crossposted article that has a higher number than
5635               ;; Gnus believes possible.  So we re-activate this
5636               ;; group as well.  This might mean doing the
5637               ;; crossposting thingy will *increase* the number
5638               ;; of articles in some groups.  Tsk, tsk.
5639               (setq active (or (gnus-activate-group group) active)))
5640             (when (or (> id (cdr active))
5641                       (< id (car active)))
5642               (setq articles (delq id articles))))))
5643       ;; If the read list is nil, we init it.
5644       (if (and active
5645                (null (gnus-info-read info))
5646                (> (car active) 1))
5647           (setq ninfo (cons 1 (1- (car active))))
5648         (setq ninfo (gnus-info-read info)))
5649       ;; Then we add the read articles to the range.
5650       (gnus-add-to-range
5651        ninfo (setq articles (sort articles '<))))))
5652
5653 (defun gnus-group-make-articles-read (group articles)
5654   "Update the info of GROUP to say that ARTICLES are read."
5655   (let* ((num 0)
5656          (entry (gnus-gethash group gnus-newsrc-hashtb))
5657          (info (nth 2 entry))
5658          (active (gnus-active group))
5659          range)
5660     (when entry
5661       (setq range (gnus-compute-read-articles group articles))
5662       (save-excursion
5663         (set-buffer gnus-group-buffer)
5664         (gnus-undo-register
5665           `(progn
5666              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5667              (gnus-info-set-read ',info ',(gnus-info-read info))
5668              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5669              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5670              (gnus-group-update-group ,group t))))
5671       ;; Add the read articles to the range.
5672       (gnus-info-set-read info range)
5673       (gnus-request-set-mark group (list (list range 'add '(read))))
5674       ;; Then we have to re-compute how many unread
5675       ;; articles there are in this group.
5676       (when active
5677         (cond
5678          ((not range)
5679           (setq num (- (1+ (cdr active)) (car active))))
5680          ((not (listp (cdr range)))
5681           (setq num (- (cdr active) (- (1+ (cdr range))
5682                                        (car range)))))
5683          (t
5684           (while range
5685             (if (numberp (car range))
5686                 (setq num (1+ num))
5687               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5688             (setq range (cdr range)))
5689           (setq num (- (cdr active) num))))
5690         ;; Update the number of unread articles.
5691         (setcar entry num)
5692         ;; Update the group buffer.
5693         (unless (gnus-ephemeral-group-p group)
5694           (gnus-group-update-group group t))))))
5695
5696 (defvar gnus-newsgroup-none-id 0)
5697
5698 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5699   (let ((cur nntp-server-buffer)
5700         (dependencies
5701          (or dependencies
5702              (save-excursion (set-buffer gnus-summary-buffer)
5703                              gnus-newsgroup-dependencies)))
5704         headers id end ref
5705         (mail-parse-charset gnus-newsgroup-charset)
5706         (mail-parse-ignored-charsets
5707          (save-excursion (condition-case nil
5708                              (set-buffer gnus-summary-buffer)
5709                            (error))
5710                          gnus-newsgroup-ignored-charsets)))
5711     (save-excursion
5712       (set-buffer nntp-server-buffer)
5713       ;; Translate all TAB characters into SPACE characters.
5714       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5715       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5716       (gnus-run-hooks 'gnus-parse-headers-hook)
5717       (let ((case-fold-search t)
5718             in-reply-to header p lines chars)
5719         (goto-char (point-min))
5720         ;; Search to the beginning of the next header.  Error messages
5721         ;; do not begin with 2 or 3.
5722         (while (re-search-forward "^[23][0-9]+ " nil t)
5723           (setq id nil
5724                 ref nil)
5725           ;; This implementation of this function, with nine
5726           ;; search-forwards instead of the one re-search-forward and
5727           ;; a case (which basically was the old function) is actually
5728           ;; about twice as fast, even though it looks messier.  You
5729           ;; can't have everything, I guess.  Speed and elegance
5730           ;; doesn't always go hand in hand.
5731           (setq
5732            header
5733            (vector
5734             ;; Number.
5735             (prog1
5736                 (read cur)
5737               (end-of-line)
5738               (setq p (point))
5739               (narrow-to-region (point)
5740                                 (or (and (search-forward "\n.\n" nil t)
5741                                          (- (point) 2))
5742                                     (point))))
5743             ;; Subject.
5744             (progn
5745               (goto-char p)
5746               (if (search-forward "\nsubject:" nil t)
5747                   (funcall gnus-decode-encoded-word-function
5748                            (nnheader-header-value))
5749                 "(none)"))
5750             ;; From.
5751             (progn
5752               (goto-char p)
5753               (if (search-forward "\nfrom:" nil t)
5754                   (funcall gnus-decode-encoded-word-function
5755                            (nnheader-header-value))
5756                 "(nobody)"))
5757             ;; Date.
5758             (progn
5759               (goto-char p)
5760               (if (search-forward "\ndate:" nil t)
5761                   (nnheader-header-value) ""))
5762             ;; Message-ID.
5763             (progn
5764               (goto-char p)
5765               (setq id (if (re-search-forward
5766                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5767                            ;; We do it this way to make sure the Message-ID
5768                            ;; is (somewhat) syntactically valid.
5769                            (buffer-substring (match-beginning 1)
5770                                              (match-end 1))
5771                          ;; If there was no message-id, we just fake one
5772                          ;; to make subsequent routines simpler.
5773                          (nnheader-generate-fake-message-id))))
5774             ;; References.
5775             (progn
5776               (goto-char p)
5777               (if (search-forward "\nreferences:" nil t)
5778                   (progn
5779                     (setq end (point))
5780                     (prog1
5781                         (nnheader-header-value)
5782                       (setq ref
5783                             (buffer-substring
5784                              (progn
5785                                (end-of-line)
5786                                (search-backward ">" end t)
5787                                (1+ (point)))
5788                              (progn
5789                                (search-backward "<" end t)
5790                                (point))))))
5791                 ;; Get the references from the in-reply-to header if there
5792                 ;; were no references and the in-reply-to header looks
5793                 ;; promising.
5794                 (if (and (search-forward "\nin-reply-to:" nil t)
5795                          (setq in-reply-to (nnheader-header-value))
5796                          (string-match "<[^>]+>" in-reply-to))
5797                     (let (ref2)
5798                       (setq ref (substring in-reply-to (match-beginning 0)
5799                                            (match-end 0)))
5800                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5801                         (setq ref2 (substring in-reply-to (match-beginning 0)
5802                                               (match-end 0)))
5803                         (when (> (length ref2) (length ref))
5804                           (setq ref ref2)))
5805                       ref)
5806                   (setq ref nil))))
5807             ;; Chars.
5808             (progn
5809               (goto-char p)
5810               (if (search-forward "\nchars: " nil t)
5811                   (if (numberp (setq chars (ignore-errors (read cur))))
5812                       chars -1)
5813                 -1))
5814             ;; Lines.
5815             (progn
5816               (goto-char p)
5817               (if (search-forward "\nlines: " nil t)
5818                   (if (numberp (setq lines (ignore-errors (read cur))))
5819                       lines -1)
5820                 -1))
5821             ;; Xref.
5822             (progn
5823               (goto-char p)
5824               (and (search-forward "\nxref:" nil t)
5825                    (nnheader-header-value)))
5826             ;; Extra.
5827             (when gnus-extra-headers
5828               (let ((extra gnus-extra-headers)
5829                     out)
5830                 (while extra
5831                   (goto-char p)
5832                   (when (search-forward
5833                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5834                     (push (cons (car extra) (nnheader-header-value))
5835                           out))
5836                   (pop extra))
5837                 out))))
5838           (when (equal id ref)
5839             (setq ref nil))
5840
5841           (when gnus-alter-header-function
5842             (funcall gnus-alter-header-function header)
5843             (setq id (mail-header-id header)
5844                   ref (gnus-parent-id (mail-header-references header))))
5845
5846           (when (setq header
5847                       (gnus-dependencies-add-header
5848                        header dependencies force-new))
5849             (push header headers))
5850           (goto-char (point-max))
5851           (widen))
5852         (nreverse headers)))))
5853
5854 ;; Goes through the xover lines and returns a list of vectors
5855 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5856                                                   force-new dependencies
5857                                                   group also-fetch-heads)
5858   "Parse the news overview data in the server buffer.
5859 Return a list of headers that match SEQUENCE (see
5860 `nntp-retrieve-headers')."
5861   ;; Get the Xref when the users reads the articles since most/some
5862   ;; NNTP servers do not include Xrefs when using XOVER.
5863   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5864   (let ((mail-parse-charset gnus-newsgroup-charset)
5865         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5866         (cur nntp-server-buffer)
5867         (dependencies (or dependencies gnus-newsgroup-dependencies))
5868         (allp (cond
5869                ((eq gnus-read-all-available-headers t)
5870                 t)
5871                ((stringp gnus-read-all-available-headers)
5872                 (string-match gnus-read-all-available-headers group))
5873                (t
5874                 nil)))
5875         number headers header)
5876     (save-excursion
5877       (set-buffer nntp-server-buffer)
5878       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5879       ;; Allow the user to mangle the headers before parsing them.
5880       (gnus-run-hooks 'gnus-parse-headers-hook)
5881       (goto-char (point-min))
5882       (gnus-parse-without-error
5883         (while (and (or sequence allp)
5884                     (not (eobp)))
5885           (setq number (read cur))
5886           (when (not allp)
5887             (while (and sequence
5888                         (< (car sequence) number))
5889               (setq sequence (cdr sequence))))
5890           (when (and (or allp
5891                          (and sequence
5892                               (eq number (car sequence))))
5893                      (progn
5894                        (setq sequence (cdr sequence))
5895                        (setq header (inline
5896                                       (gnus-nov-parse-line
5897                                        number dependencies force-new)))))
5898             (push header headers))
5899           (forward-line 1)))
5900       ;; A common bug in inn is that if you have posted an article and
5901       ;; then retrieves the active file, it will answer correctly --
5902       ;; the new article is included.  However, a NOV entry for the
5903       ;; article may not have been generated yet, so this may fail.
5904       ;; We work around this problem by retrieving the last few
5905       ;; headers using HEAD.
5906       (if (or (not also-fetch-heads)
5907               (not sequence))
5908           ;; We (probably) got all the headers.
5909           (nreverse headers)
5910         (let ((gnus-nov-is-evil t))
5911           (nconc
5912            (nreverse headers)
5913            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5914              (gnus-get-newsgroup-headers))))))))
5915
5916 (defun gnus-article-get-xrefs ()
5917   "Fill in the Xref value in `gnus-current-headers', if necessary.
5918 This is meant to be called in `gnus-article-internal-prepare-hook'."
5919   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5920                                  gnus-current-headers)))
5921     (or (not gnus-use-cross-reference)
5922         (not headers)
5923         (and (mail-header-xref headers)
5924              (not (string= (mail-header-xref headers) "")))
5925         (let ((case-fold-search t)
5926               xref)
5927           (save-restriction
5928             (nnheader-narrow-to-headers)
5929             (goto-char (point-min))
5930             (when (or (and (not (eobp))
5931                            (eq (downcase (char-after)) ?x)
5932                            (looking-at "Xref:"))
5933                       (search-forward "\nXref:" nil t))
5934               (goto-char (1+ (match-end 0)))
5935               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5936               (mail-header-set-xref headers xref)))))))
5937
5938 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5939   "Find article ID and insert the summary line for that article.
5940 OLD-HEADER can either be a header or a line number to insert
5941 the subject line on."
5942   (let* ((line (and (numberp old-header) old-header))
5943          (old-header (and (vectorp old-header) old-header))
5944          (header (cond ((and old-header use-old-header)
5945                         old-header)
5946                        ((and (numberp id)
5947                              (gnus-number-to-header id))
5948                         (gnus-number-to-header id))
5949                        (t
5950                         (gnus-read-header id))))
5951          (number (and (numberp id) id))
5952          d)
5953     (when header
5954       ;; Rebuild the thread that this article is part of and go to the
5955       ;; article we have fetched.
5956       (when (and (not gnus-show-threads)
5957                  old-header)
5958         (when (and number
5959                    (setq d (gnus-data-find (mail-header-number old-header))))
5960           (goto-char (gnus-data-pos d))
5961           (gnus-data-remove
5962            number
5963            (- (gnus-point-at-bol)
5964               (prog1
5965                   (1+ (gnus-point-at-eol))
5966                 (gnus-delete-line))))))
5967       (when old-header
5968         (mail-header-set-number header (mail-header-number old-header)))
5969       (setq gnus-newsgroup-sparse
5970             (delq (setq number (mail-header-number header))
5971                   gnus-newsgroup-sparse))
5972       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5973       (push number gnus-newsgroup-limit)
5974       (gnus-rebuild-thread (mail-header-id header) line)
5975       (gnus-summary-goto-subject number nil t))
5976     (when (and (numberp number)
5977                (> number 0))
5978       ;; We have to update the boundaries even if we can't fetch the
5979       ;; article if ID is a number -- so that the next `P' or `N'
5980       ;; command will fetch the previous (or next) article even
5981       ;; if the one we tried to fetch this time has been canceled.
5982       (when (> number gnus-newsgroup-end)
5983         (setq gnus-newsgroup-end number))
5984       (when (< number gnus-newsgroup-begin)
5985         (setq gnus-newsgroup-begin number))
5986       (setq gnus-newsgroup-unselected
5987             (delq number gnus-newsgroup-unselected)))
5988     ;; Report back a success?
5989     (and header (mail-header-number header))))
5990
5991 ;;; Process/prefix in the summary buffer
5992
5993 (defun gnus-summary-work-articles (n)
5994   "Return a list of articles to be worked upon.
5995 The prefix argument, the list of process marked articles, and the
5996 current article will be taken into consideration."
5997   (save-excursion
5998     (set-buffer gnus-summary-buffer)
5999     (cond
6000      (n
6001       ;; A numerical prefix has been given.
6002       (setq n (prefix-numeric-value n))
6003       (let ((backward (< n 0))
6004             (n (abs (prefix-numeric-value n)))
6005             articles article)
6006         (save-excursion
6007           (while
6008               (and (> n 0)
6009                    (push (setq article (gnus-summary-article-number))
6010                          articles)
6011                    (if backward
6012                        (gnus-summary-find-prev nil article)
6013                      (gnus-summary-find-next nil article)))
6014             (decf n)))
6015         (nreverse articles)))
6016      ((and (gnus-region-active-p) (mark))
6017       (message "region active")
6018       ;; Work on the region between point and mark.
6019       (let ((max (max (point) (mark)))
6020             articles article)
6021         (save-excursion
6022           (goto-char (min (min (point) (mark))))
6023           (while
6024               (and
6025                (push (setq article (gnus-summary-article-number)) articles)
6026                (gnus-summary-find-next nil article)
6027                (< (point) max)))
6028           (nreverse articles))))
6029      (gnus-newsgroup-processable
6030       ;; There are process-marked articles present.
6031       ;; Save current state.
6032       (gnus-summary-save-process-mark)
6033       ;; Return the list.
6034       (reverse gnus-newsgroup-processable))
6035      (t
6036       ;; Just return the current article.
6037       (list (gnus-summary-article-number))))))
6038
6039 (defmacro gnus-summary-iterate (arg &rest forms)
6040   "Iterate over the process/prefixed articles and do FORMS.
6041 ARG is the interactive prefix given to the command.  FORMS will be
6042 executed with point over the summary line of the articles."
6043   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6044     `(let ((,articles (gnus-summary-work-articles ,arg)))
6045        (while ,articles
6046          (gnus-summary-goto-subject (car ,articles))
6047          ,@forms
6048          (pop ,articles)))))
6049
6050 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6051 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6052
6053 (defun gnus-summary-save-process-mark ()
6054   "Push the current set of process marked articles on the stack."
6055   (interactive)
6056   (push (copy-sequence gnus-newsgroup-processable)
6057         gnus-newsgroup-process-stack))
6058
6059 (defun gnus-summary-kill-process-mark ()
6060   "Push the current set of process marked articles on the stack and unmark."
6061   (interactive)
6062   (gnus-summary-save-process-mark)
6063   (gnus-summary-unmark-all-processable))
6064
6065 (defun gnus-summary-yank-process-mark ()
6066   "Pop the last process mark state off the stack and restore it."
6067   (interactive)
6068   (unless gnus-newsgroup-process-stack
6069     (error "Empty mark stack"))
6070   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6071
6072 (defun gnus-summary-process-mark-set (set)
6073   "Make SET into the current process marked articles."
6074   (gnus-summary-unmark-all-processable)
6075   (while set
6076     (gnus-summary-set-process-mark (pop set))))
6077
6078 ;;; Searching and stuff
6079
6080 (defun gnus-summary-search-group (&optional backward use-level)
6081   "Search for next unread newsgroup.
6082 If optional argument BACKWARD is non-nil, search backward instead."
6083   (save-excursion
6084     (set-buffer gnus-group-buffer)
6085     (when (gnus-group-search-forward
6086            backward nil (if use-level (gnus-group-group-level) nil))
6087       (gnus-group-group-name))))
6088
6089 (defun gnus-summary-best-group (&optional exclude-group)
6090   "Find the name of the best unread group.
6091 If EXCLUDE-GROUP, do not go to this group."
6092   (save-excursion
6093     (set-buffer gnus-group-buffer)
6094     (save-excursion
6095       (gnus-group-best-unread-group exclude-group))))
6096
6097 (defun gnus-summary-find-next (&optional unread article backward)
6098   (if backward
6099       (gnus-summary-find-prev unread article)
6100     (let* ((dummy (gnus-summary-article-intangible-p))
6101            (article (or article (gnus-summary-article-number)))
6102            (data (gnus-data-find-list article))
6103            result)
6104       (when (and (not dummy)
6105                  (or (not gnus-summary-check-current)
6106                      (not unread)
6107                      (not (gnus-data-unread-p (car data)))))
6108         (setq data (cdr data)))
6109       (when (setq result
6110                   (if unread
6111                       (progn
6112                         (while data
6113                           (unless (memq (gnus-data-number (car data)) 
6114                                         (cond
6115                                          ((eq gnus-auto-goto-ignores
6116                                               'always-undownloaded)
6117                                           gnus-newsgroup-undownloaded)
6118                                          (gnus-plugged
6119                                           nil)
6120                                          ((eq gnus-auto-goto-ignores
6121                                               'unfetched)
6122                                           gnus-newsgroup-unfetched)
6123                                          ((eq gnus-auto-goto-ignores
6124                                               'undownloaded)
6125                                           gnus-newsgroup-undownloaded)))
6126                             (when (gnus-data-unread-p (car data))
6127                               (setq result (car data)
6128                                     data nil)))
6129                           (setq data (cdr data)))
6130                         result)
6131                     (car data)))
6132         (goto-char (gnus-data-pos result))
6133         (gnus-data-number result)))))
6134
6135 (defun gnus-summary-find-prev (&optional unread article)
6136   (let* ((eobp (eobp))
6137          (article (or article (gnus-summary-article-number)))
6138          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6139          result)
6140     (when (and (not eobp)
6141                (or (not gnus-summary-check-current)
6142                    (not unread)
6143                    (not (gnus-data-unread-p (car data)))))
6144       (setq data (cdr data)))
6145     (when (setq result
6146                 (if unread
6147                     (progn
6148                       (while data
6149                         (unless (memq (gnus-data-number (car data))
6150                                       (cond
6151                                        ((eq gnus-auto-goto-ignores
6152                                             'always-undownloaded)
6153                                         gnus-newsgroup-undownloaded)
6154                                        (gnus-plugged
6155                                         nil)
6156                                        ((eq gnus-auto-goto-ignores
6157                                             'unfetched)
6158                                         gnus-newsgroup-unfetched)
6159                                        ((eq gnus-auto-goto-ignores
6160                                             'undownloaded)
6161                                         gnus-newsgroup-undownloaded)))
6162                           (when (gnus-data-unread-p (car data))
6163                             (setq result (car data)
6164                                   data nil)))
6165                         (setq data (cdr data)))
6166                       result)
6167                   (car data)))
6168       (goto-char (gnus-data-pos result))
6169       (gnus-data-number result))))
6170
6171 (defun gnus-summary-find-subject (subject &optional unread backward article)
6172   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6173          (article (or article (gnus-summary-article-number)))
6174          (articles (gnus-data-list backward))
6175          (arts (gnus-data-find-list article articles))
6176          result)
6177     (when (or (not gnus-summary-check-current)
6178               (not unread)
6179               (not (gnus-data-unread-p (car arts))))
6180       (setq arts (cdr arts)))
6181     (while arts
6182       (and (or (not unread)
6183                (gnus-data-unread-p (car arts)))
6184            (vectorp (gnus-data-header (car arts)))
6185            (gnus-subject-equal
6186             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6187            (setq result (car arts)
6188                  arts nil))
6189       (setq arts (cdr arts)))
6190     (and result
6191          (goto-char (gnus-data-pos result))
6192          (gnus-data-number result))))
6193
6194 (defun gnus-summary-search-forward (&optional unread subject backward)
6195   "Search forward for an article.
6196 If UNREAD, look for unread articles.  If SUBJECT, look for
6197 articles with that subject.  If BACKWARD, search backward instead."
6198   (cond (subject (gnus-summary-find-subject subject unread backward))
6199         (backward (gnus-summary-find-prev unread))
6200         (t (gnus-summary-find-next unread))))
6201
6202 (defun gnus-recenter (&optional n)
6203   "Center point in window and redisplay frame.
6204 Also do horizontal recentering."
6205   (interactive "P")
6206   (when (and gnus-auto-center-summary
6207              (not (eq gnus-auto-center-summary 'vertical)))
6208     (gnus-horizontal-recenter))
6209   (recenter n))
6210
6211 (defun gnus-summary-recenter ()
6212   "Center point in the summary window.
6213 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6214 displayed, no centering will be performed."
6215   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6216   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6217   (interactive)
6218   ;; The user has to want it.
6219   (when gnus-auto-center-summary
6220     (let* ((top (cond ((< (window-height) 4) 0)
6221                       ((< (window-height) 7) 1)
6222                       (t (if (numberp gnus-auto-center-summary)
6223                              gnus-auto-center-summary
6224                            2))))
6225            (height (1- (window-height)))
6226            (bottom (save-excursion (goto-char (point-max))
6227                                    (forward-line (- height))
6228                                    (point)))
6229            (window (get-buffer-window (current-buffer))))
6230       (when (get-buffer-window gnus-article-buffer)
6231         ;; Only do recentering when the article buffer is displayed,
6232         ;; Set the window start to either `bottom', which is the biggest
6233         ;; possible valid number, or the second line from the top,
6234         ;; whichever is the least.
6235         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6236           (if (> bottom top-pos)
6237               ;; Keep the second line from the top visible
6238               (set-window-start window top-pos t)
6239             ;; Try to keep the bottom line visible; if it's partially
6240             ;; obscured, either scroll one more line to make it fully
6241             ;; visible, or revert to using TOP-POS.
6242             (save-excursion
6243               (goto-char (point-max))
6244               (forward-line -1)
6245               (let ((last-line-start (point)))
6246                 (goto-char bottom)
6247                 (set-window-start window (point) t)
6248                 (when (not (pos-visible-in-window-p last-line-start window))
6249                   (forward-line 1)
6250                   (set-window-start window (min (point) top-pos) t)))))))
6251       ;; Do horizontal recentering while we're at it.
6252       (when (and (get-buffer-window (current-buffer) t)
6253                  (not (eq gnus-auto-center-summary 'vertical)))
6254         (let ((selected (selected-window)))
6255           (select-window (get-buffer-window (current-buffer) t))
6256           (gnus-summary-position-point)
6257           (gnus-horizontal-recenter)
6258           (select-window selected))))))
6259
6260 (defun gnus-summary-jump-to-group (newsgroup)
6261   "Move point to NEWSGROUP in group mode buffer."
6262   ;; Keep update point of group mode buffer if visible.
6263   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6264       (save-window-excursion
6265         ;; Take care of tree window mode.
6266         (when (get-buffer-window gnus-group-buffer)
6267           (pop-to-buffer gnus-group-buffer))
6268         (gnus-group-jump-to-group newsgroup))
6269     (save-excursion
6270       ;; Take care of tree window mode.
6271       (if (get-buffer-window gnus-group-buffer)
6272           (pop-to-buffer gnus-group-buffer)
6273         (set-buffer gnus-group-buffer))
6274       (gnus-group-jump-to-group newsgroup))))
6275
6276 ;; This function returns a list of article numbers based on the
6277 ;; difference between the ranges of read articles in this group and
6278 ;; the range of active articles.
6279 (defun gnus-list-of-unread-articles (group)
6280   (let* ((read (gnus-info-read (gnus-get-info group)))
6281          (active (or (gnus-active group) (gnus-activate-group group)))
6282          (last (cdr active))
6283          first nlast unread)
6284     ;; If none are read, then all are unread.
6285     (if (not read)
6286         (setq first (car active))
6287       ;; If the range of read articles is a single range, then the
6288       ;; first unread article is the article after the last read
6289       ;; article.  Sounds logical, doesn't it?
6290       (if (and (not (listp (cdr read)))
6291                (or (< (car read) (car active))
6292                    (progn (setq read (list read))
6293                           nil)))
6294           (setq first (max (car active) (1+ (cdr read))))
6295         ;; `read' is a list of ranges.
6296         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6297                                   (caar read)))
6298                   1)
6299           (setq first (car active)))
6300         (while read
6301           (when first
6302             (while (< first nlast)
6303               (push first unread)
6304               (setq first (1+ first))))
6305           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6306           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6307           (setq read (cdr read)))))
6308     ;; And add the last unread articles.
6309     (while (<= first last)
6310       (push first unread)
6311       (setq first (1+ first)))
6312     ;; Return the list of unread articles.
6313     (delq 0 (nreverse unread))))
6314
6315 (defun gnus-list-of-read-articles (group)
6316   "Return a list of unread, unticked and non-dormant articles."
6317   (let* ((info (gnus-get-info group))
6318          (marked (gnus-info-marks info))
6319          (active (gnus-active group)))
6320     (and info active
6321          (gnus-list-range-difference
6322           (gnus-list-range-difference
6323            (gnus-sorted-complement
6324             (gnus-uncompress-range active)
6325             (gnus-list-of-unread-articles group))
6326            (cdr (assq 'dormant marked)))
6327           (cdr (assq 'tick marked))))))
6328
6329 ;; Various summary commands
6330
6331 (defun gnus-summary-select-article-buffer ()
6332   "Reconfigure windows to show article buffer."
6333   (interactive)
6334   (if (not (gnus-buffer-live-p gnus-article-buffer))
6335       (error "There is no article buffer for this summary buffer")
6336     (gnus-configure-windows 'article)
6337     (select-window (get-buffer-window gnus-article-buffer))))
6338
6339 (defun gnus-summary-universal-argument (arg)
6340   "Perform any operation on all articles that are process/prefixed."
6341   (interactive "P")
6342   (let ((articles (gnus-summary-work-articles arg))
6343         func article)
6344     (if (eq
6345          (setq
6346           func
6347           (key-binding
6348            (read-key-sequence
6349             (substitute-command-keys
6350              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6351          'undefined)
6352         (gnus-error 1 "Undefined key")
6353       (save-excursion
6354         (while articles
6355           (gnus-summary-goto-subject (setq article (pop articles)))
6356           (let (gnus-newsgroup-processable)
6357             (command-execute func))
6358           (gnus-summary-remove-process-mark article)))))
6359   (gnus-summary-position-point))
6360
6361 (defun gnus-summary-toggle-truncation (&optional arg)
6362   "Toggle truncation of summary lines.
6363 With arg, turn line truncation on if arg is positive."
6364   (interactive "P")
6365   (setq truncate-lines
6366         (if (null arg) (not truncate-lines)
6367           (> (prefix-numeric-value arg) 0)))
6368   (redraw-display))
6369
6370 (defun gnus-summary-find-for-reselect ()
6371   "Return the number of an article to stay on across a reselect.
6372 The current article is considered, then following articles, then previous
6373 articles.  An article is sought which is not cancelled and isn't a temporary
6374 insertion from another group.  If there's no such then return a dummy 0."
6375   (let (found)
6376     (dolist (rev '(nil t))
6377       (unless found      ; don't demand the reverse list if we don't need it
6378         (let ((data (gnus-data-find-list
6379                      (gnus-summary-article-number) (gnus-data-list rev))))
6380           (while (and data (not found))
6381             (if (and (< 0 (gnus-data-number (car data)))
6382                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6383                 (setq found (gnus-data-number (car data))))
6384             (setq data (cdr data))))))
6385     (or found 0)))
6386
6387 (defun gnus-summary-reselect-current-group (&optional all rescan)
6388   "Exit and then reselect the current newsgroup.
6389 The prefix argument ALL means to select all articles."
6390   (interactive "P")
6391   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6392     (error "Ephemeral groups can't be reselected"))
6393   (let ((current-subject (gnus-summary-find-for-reselect))
6394         (group gnus-newsgroup-name))
6395     (setq gnus-newsgroup-begin nil)
6396     (gnus-summary-exit nil 'leave-hidden)
6397     ;; We have to adjust the point of group mode buffer because
6398     ;; point was moved to the next unread newsgroup by exiting.
6399     (gnus-summary-jump-to-group group)
6400     (when rescan
6401       (save-excursion
6402         (gnus-group-get-new-news-this-group 1)))
6403     (gnus-group-read-group all t)
6404     (gnus-summary-goto-subject current-subject nil t)))
6405
6406 (defun gnus-summary-rescan-group (&optional all)
6407   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6408   (interactive "P")
6409   (gnus-summary-reselect-current-group all t))
6410
6411 (defun gnus-summary-update-info (&optional non-destructive)
6412   (save-excursion
6413     (let ((group gnus-newsgroup-name))
6414       (when group
6415         (when gnus-newsgroup-kill-headers
6416           (setq gnus-newsgroup-killed
6417                 (gnus-compress-sequence
6418                  (gnus-sorted-union
6419                   (gnus-list-range-intersection
6420                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6421                   gnus-newsgroup-unreads)
6422                  t)))
6423         (unless (listp (cdr gnus-newsgroup-killed))
6424           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6425         (let ((headers gnus-newsgroup-headers))
6426           ;; Set the new ranges of read articles.
6427           (save-excursion
6428             (set-buffer gnus-group-buffer)
6429             (gnus-undo-force-boundary))
6430           (gnus-update-read-articles
6431            group (gnus-sorted-union
6432                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6433           ;; Set the current article marks.
6434           (let ((gnus-newsgroup-scored
6435                  (if (and (not gnus-save-score)
6436                           (not non-destructive))
6437                      nil
6438                    gnus-newsgroup-scored)))
6439             (save-excursion
6440               (gnus-update-marks)))
6441           ;; Do the cross-ref thing.
6442           (when gnus-use-cross-reference
6443             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6444           ;; Do not switch windows but change the buffer to work.
6445           (set-buffer gnus-group-buffer)
6446           (unless (gnus-ephemeral-group-p group)
6447             (gnus-group-update-group group)))))))
6448
6449 (defun gnus-summary-save-newsrc (&optional force)
6450   "Save the current number of read/marked articles in the dribble buffer.
6451 The dribble buffer will then be saved.
6452 If FORCE (the prefix), also save the .newsrc file(s)."
6453   (interactive "P")
6454   (gnus-summary-update-info t)
6455   (if force
6456       (gnus-save-newsrc-file)
6457     (gnus-dribble-save)))
6458
6459 (defun gnus-summary-exit (&optional temporary leave-hidden)
6460   "Exit reading current newsgroup, and then return to group selection mode.
6461 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6462   (interactive)
6463   (gnus-set-global-variables)
6464   (when (gnus-buffer-live-p gnus-article-buffer)
6465     (save-excursion
6466       (set-buffer gnus-article-buffer)
6467       (mm-destroy-parts gnus-article-mime-handles)
6468       ;; Set it to nil for safety reason.
6469       (setq gnus-article-mime-handle-alist nil)
6470       (setq gnus-article-mime-handles nil)))
6471   (gnus-kill-save-kill-buffer)
6472   (gnus-async-halt-prefetch)
6473   (let* ((group gnus-newsgroup-name)
6474          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6475          (gnus-group-is-exiting-p t)
6476          (mode major-mode)
6477          (group-point nil)
6478          (buf (current-buffer)))
6479     (unless quit-config
6480       ;; Do adaptive scoring, and possibly save score files.
6481       (when gnus-newsgroup-adaptive
6482         (gnus-score-adaptive))
6483       (when gnus-use-scoring
6484         (gnus-score-save)))
6485     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6486     ;; If we have several article buffers, we kill them at exit.
6487     (unless gnus-single-article-buffer
6488       (gnus-kill-buffer gnus-original-article-buffer)
6489       (setq gnus-article-current nil))
6490     (when gnus-use-cache
6491       (gnus-cache-possibly-remove-articles)
6492       (gnus-cache-save-buffers))
6493     (gnus-async-prefetch-remove-group group)
6494     (when gnus-suppress-duplicates
6495       (gnus-dup-enter-articles))
6496     (when gnus-use-trees
6497       (gnus-tree-close group))
6498     (when gnus-use-cache
6499       (gnus-cache-write-active))
6500     ;; Remove entries for this group.
6501     (nnmail-purge-split-history (gnus-group-real-name group))
6502     ;; Make all changes in this group permanent.
6503     (unless quit-config
6504       (gnus-run-hooks 'gnus-exit-group-hook)
6505       (gnus-summary-update-info))
6506     (gnus-close-group group)
6507     ;; Make sure where we were, and go to next newsgroup.
6508     (set-buffer gnus-group-buffer)
6509     (unless quit-config
6510       (gnus-group-jump-to-group group))
6511     (gnus-run-hooks 'gnus-summary-exit-hook)
6512     (unless (or quit-config
6513                 ;; If this group has disappeared from the summary
6514                 ;; buffer, don't skip forwards.
6515                 (not (string= group (gnus-group-group-name))))
6516       (gnus-group-next-unread-group 1))
6517     (setq group-point (point))
6518     (if temporary
6519         nil                             ;Nothing to do.
6520       ;; If we have several article buffers, we kill them at exit.
6521       (unless gnus-single-article-buffer
6522         (gnus-kill-buffer gnus-article-buffer)
6523         (gnus-kill-buffer gnus-original-article-buffer)
6524         (setq gnus-article-current nil))
6525       (set-buffer buf)
6526       (if (not gnus-kill-summary-on-exit)
6527           (progn
6528             (gnus-deaden-summary)
6529             (setq mode nil))
6530         ;; We set all buffer-local variables to nil.  It is unclear why
6531         ;; this is needed, but if we don't, buffer-local variables are
6532         ;; not garbage-collected, it seems.  This would the lead to en
6533         ;; ever-growing Emacs.
6534         (gnus-summary-clear-local-variables)
6535         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6536           (gnus-summary-clear-local-variables))
6537         (when (get-buffer gnus-article-buffer)
6538           (bury-buffer gnus-article-buffer))
6539         ;; We clear the global counterparts of the buffer-local
6540         ;; variables as well, just to be on the safe side.
6541         (set-buffer gnus-group-buffer)
6542         (gnus-summary-clear-local-variables)
6543         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6544           (gnus-summary-clear-local-variables))
6545         ;; Return to group mode buffer.
6546         (when (eq mode 'gnus-summary-mode)
6547           (gnus-kill-buffer buf)))
6548       (setq gnus-current-select-method gnus-select-method)
6549       (if leave-hidden
6550           (set-buffer gnus-group-buffer)
6551         (pop-to-buffer gnus-group-buffer))
6552       (if (not quit-config)
6553           (progn
6554             (goto-char group-point)
6555             (unless leave-hidden
6556               (gnus-configure-windows 'group 'force)))
6557         (gnus-handle-ephemeral-exit quit-config))
6558       ;; Clear the current group name.
6559       (unless quit-config
6560         (setq gnus-newsgroup-name nil)))))
6561
6562 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6563 (defun gnus-summary-exit-no-update (&optional no-questions)
6564   "Quit reading current newsgroup without updating read article info."
6565   (interactive)
6566   (let* ((group gnus-newsgroup-name)
6567          (gnus-group-is-exiting-p t)
6568          (gnus-group-is-exiting-without-update-p t)
6569          (quit-config (gnus-group-quit-config group)))
6570     (when (or no-questions
6571               gnus-expert-user
6572               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6573       (gnus-async-halt-prefetch)
6574       (run-hooks 'gnus-summary-prepare-exit-hook)
6575       (when (gnus-buffer-live-p gnus-article-buffer)
6576         (save-excursion
6577           (set-buffer gnus-article-buffer)
6578           (mm-destroy-parts gnus-article-mime-handles)
6579           ;; Set it to nil for safety reason.
6580           (setq gnus-article-mime-handle-alist nil)
6581           (setq gnus-article-mime-handles nil)))
6582       ;; If we have several article buffers, we kill them at exit.
6583       (unless gnus-single-article-buffer
6584         (gnus-kill-buffer gnus-article-buffer)
6585         (gnus-kill-buffer gnus-original-article-buffer)
6586         (setq gnus-article-current nil))
6587       (if (not gnus-kill-summary-on-exit)
6588           (gnus-deaden-summary)
6589         (gnus-close-group group)
6590         (gnus-summary-clear-local-variables)
6591         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6592           (gnus-summary-clear-local-variables))
6593         (set-buffer gnus-group-buffer)
6594         (gnus-summary-clear-local-variables)
6595         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6596           (gnus-summary-clear-local-variables))
6597         (gnus-kill-buffer gnus-summary-buffer))
6598       (unless gnus-single-article-buffer
6599         (setq gnus-article-current nil))
6600       (when gnus-use-trees
6601         (gnus-tree-close group))
6602       (gnus-async-prefetch-remove-group group)
6603       (when (get-buffer gnus-article-buffer)
6604         (bury-buffer gnus-article-buffer))
6605       ;; Return to the group buffer.
6606       (gnus-configure-windows 'group 'force)
6607       ;; Clear the current group name.
6608       (setq gnus-newsgroup-name nil)
6609       (unless (gnus-ephemeral-group-p group)
6610         (gnus-group-update-group group))
6611       (when (equal (gnus-group-group-name) group)
6612         (gnus-group-next-unread-group 1))
6613       (when quit-config
6614         (gnus-handle-ephemeral-exit quit-config)))))
6615
6616 (defun gnus-handle-ephemeral-exit (quit-config)
6617   "Handle movement when leaving an ephemeral group.
6618 The state which existed when entering the ephemeral is reset."
6619   (if (not (buffer-name (car quit-config)))
6620       (gnus-configure-windows 'group 'force)
6621     (set-buffer (car quit-config))
6622     (cond ((eq major-mode 'gnus-summary-mode)
6623            (gnus-set-global-variables))
6624           ((eq major-mode 'gnus-article-mode)
6625            (save-excursion
6626              ;; The `gnus-summary-buffer' variable may point
6627              ;; to the old summary buffer when using a single
6628              ;; article buffer.
6629              (unless (gnus-buffer-live-p gnus-summary-buffer)
6630                (set-buffer gnus-group-buffer))
6631              (set-buffer gnus-summary-buffer)
6632              (gnus-set-global-variables))))
6633     (if (or (eq (cdr quit-config) 'article)
6634             (eq (cdr quit-config) 'pick))
6635         (progn
6636           ;; The current article may be from the ephemeral group
6637           ;; thus it is best that we reload this article
6638           ;;
6639           ;; If we're exiting from a large digest, this can be
6640           ;; extremely slow.  So, it's better not to reload it. -- jh.
6641           ;;(gnus-summary-show-article)
6642           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6643               (gnus-configure-windows 'pick 'force)
6644             (gnus-configure-windows (cdr quit-config) 'force)))
6645       (gnus-configure-windows (cdr quit-config) 'force))
6646     (when (eq major-mode 'gnus-summary-mode)
6647       (gnus-summary-next-subject 1 nil t)
6648       (gnus-summary-recenter)
6649       (gnus-summary-position-point))))
6650
6651 ;;; Dead summaries.
6652
6653 (defvar gnus-dead-summary-mode-map nil)
6654
6655 (unless gnus-dead-summary-mode-map
6656   (setq gnus-dead-summary-mode-map (make-keymap))
6657   (suppress-keymap gnus-dead-summary-mode-map)
6658   (substitute-key-definition
6659    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6660   (dolist (key '("\C-d" "\r" "\177" [delete]))
6661     (define-key gnus-dead-summary-mode-map
6662       key 'gnus-summary-wake-up-the-dead))
6663   (dolist (key '("q" "Q"))
6664     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6665
6666 (defvar gnus-dead-summary-mode nil
6667   "Minor mode for Gnus summary buffers.")
6668
6669 (defun gnus-dead-summary-mode (&optional arg)
6670   "Minor mode for Gnus summary buffers."
6671   (interactive "P")
6672   (when (eq major-mode 'gnus-summary-mode)
6673     (make-local-variable 'gnus-dead-summary-mode)
6674     (setq gnus-dead-summary-mode
6675           (if (null arg) (not gnus-dead-summary-mode)
6676             (> (prefix-numeric-value arg) 0)))
6677     (when gnus-dead-summary-mode
6678       (gnus-add-minor-mode
6679        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6680
6681 (defun gnus-deaden-summary ()
6682   "Make the current summary buffer into a dead summary buffer."
6683   ;; Kill any previous dead summary buffer.
6684   (when (and gnus-dead-summary
6685              (buffer-name gnus-dead-summary))
6686     (save-excursion
6687       (set-buffer gnus-dead-summary)
6688       (when gnus-dead-summary-mode
6689         (kill-buffer (current-buffer)))))
6690   ;; Make this the current dead summary.
6691   (setq gnus-dead-summary (current-buffer))
6692   (gnus-dead-summary-mode 1)
6693   (let ((name (buffer-name)))
6694     (when (string-match "Summary" name)
6695       (rename-buffer
6696        (concat (substring name 0 (match-beginning 0)) "Dead "
6697                (substring name (match-beginning 0)))
6698        t)
6699       (bury-buffer))))
6700
6701 (defun gnus-kill-or-deaden-summary (buffer)
6702   "Kill or deaden the summary BUFFER."
6703   (save-excursion
6704     (when (and (buffer-name buffer)
6705                (not gnus-single-article-buffer))
6706       (save-excursion
6707         (set-buffer buffer)
6708         (gnus-kill-buffer gnus-article-buffer)
6709         (gnus-kill-buffer gnus-original-article-buffer)))
6710     (cond
6711      ;; Kill the buffer.
6712      (gnus-kill-summary-on-exit
6713       (when (and gnus-use-trees
6714                  (gnus-buffer-exists-p buffer))
6715         (save-excursion
6716           (set-buffer buffer)
6717           (gnus-tree-close gnus-newsgroup-name)))
6718       (gnus-kill-buffer buffer))
6719      ;; Deaden the buffer.
6720      ((gnus-buffer-exists-p buffer)
6721       (save-excursion
6722         (set-buffer buffer)
6723         (gnus-deaden-summary))))))
6724
6725 (defun gnus-summary-wake-up-the-dead (&rest args)
6726   "Wake up the dead summary buffer."
6727   (interactive)
6728   (gnus-dead-summary-mode -1)
6729   (let ((name (buffer-name)))
6730     (when (string-match "Dead " name)
6731       (rename-buffer
6732        (concat (substring name 0 (match-beginning 0))
6733                (substring name (match-end 0)))
6734        t)))
6735   (gnus-message 3 "This dead summary is now alive again"))
6736
6737 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6738 (defun gnus-summary-fetch-faq (&optional faq-dir)
6739   "Fetch the FAQ for the current group.
6740 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6741 in."
6742   (interactive
6743    (list
6744     (when current-prefix-arg
6745       (completing-read
6746        "FAQ dir: " (and (listp gnus-group-faq-directory)
6747                         (mapcar (lambda (file) (list file))
6748                                 gnus-group-faq-directory))))))
6749   (let (gnus-faq-buffer)
6750     (when (setq gnus-faq-buffer
6751                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6752       (gnus-configure-windows 'summary-faq))))
6753
6754 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6755 (defun gnus-summary-describe-group (&optional force)
6756   "Describe the current newsgroup."
6757   (interactive "P")
6758   (gnus-group-describe-group force gnus-newsgroup-name))
6759
6760 (defun gnus-summary-describe-briefly ()
6761   "Describe summary mode commands briefly."
6762   (interactive)
6763   (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")))
6764
6765 ;; Walking around group mode buffer from summary mode.
6766
6767 (defun gnus-summary-next-group (&optional no-article target-group backward)
6768   "Exit current newsgroup and then select next unread newsgroup.
6769 If prefix argument NO-ARTICLE is non-nil, no article is selected
6770 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6771 previous group instead."
6772   (interactive "P")
6773   ;; Stop pre-fetching.
6774   (gnus-async-halt-prefetch)
6775   (let ((current-group gnus-newsgroup-name)
6776         (current-buffer (current-buffer))
6777         entered)
6778     ;; First we semi-exit this group to update Xrefs and all variables.
6779     ;; We can't do a real exit, because the window conf must remain
6780     ;; the same in case the user is prompted for info, and we don't
6781     ;; want the window conf to change before that...
6782     (gnus-summary-exit t)
6783     (while (not entered)
6784       ;; Then we find what group we are supposed to enter.
6785       (set-buffer gnus-group-buffer)
6786       (gnus-group-jump-to-group current-group)
6787       (setq target-group
6788             (or target-group
6789                 (if (eq gnus-keep-same-level 'best)
6790                     (gnus-summary-best-group gnus-newsgroup-name)
6791                   (gnus-summary-search-group backward gnus-keep-same-level))))
6792       (if (not target-group)
6793           ;; There are no further groups, so we return to the group
6794           ;; buffer.
6795           (progn
6796             (gnus-message 5 "Returning to the group buffer")
6797             (setq entered t)
6798             (when (gnus-buffer-live-p current-buffer)
6799               (set-buffer current-buffer)
6800               (gnus-summary-exit))
6801             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6802         ;; We try to enter the target group.
6803         (gnus-group-jump-to-group target-group)
6804         (let ((unreads (gnus-group-group-unread)))
6805           (if (and (or (eq t unreads)
6806                        (and unreads (not (zerop unreads))))
6807                    (gnus-summary-read-group
6808                     target-group nil no-article
6809                     (and (buffer-name current-buffer) current-buffer)
6810                     nil backward))
6811               (setq entered t)
6812             (setq current-group target-group
6813                   target-group nil)))))))
6814
6815 (defun gnus-summary-prev-group (&optional no-article)
6816   "Exit current newsgroup and then select previous unread newsgroup.
6817 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6818   (interactive "P")
6819   (gnus-summary-next-group no-article nil t))
6820
6821 ;; Walking around summary lines.
6822
6823 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6824   "Go to the first subject satisfying any non-nil constraint.
6825 If UNREAD is non-nil, the article should be unread.
6826 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6827 If UNSEED is non-nil, the article should be unseen.
6828 Returns the article selected or nil if there are no matching articles."
6829   (interactive "P")
6830   (cond
6831    ;; Empty summary.
6832    ((null gnus-newsgroup-data)
6833     (gnus-message 3 "No articles in the group")
6834     nil)
6835    ;; Pick the first article.
6836    ((not (or unread undownloaded unseen))
6837     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6838     (gnus-data-number (car gnus-newsgroup-data)))
6839    ;; Find the first unread article.
6840    (t
6841     (let ((data gnus-newsgroup-data))
6842       (while (and data
6843                   (let ((num (gnus-data-number (car data))))
6844                     (or (memq num gnus-newsgroup-unfetched)
6845                         (not (or (and unread
6846                                       (memq num gnus-newsgroup-unreads))
6847                                  (and undownloaded
6848                                       (memq num gnus-newsgroup-undownloaded))
6849                                  (and unseen
6850                                       (memq num gnus-newsgroup-unseen)))))))
6851         (setq data (cdr data)))
6852       (prog1 
6853           (if data
6854               (progn
6855                 (goto-char (gnus-data-pos (car data)))
6856                 (gnus-data-number (car data)))
6857             (gnus-message 3 "No more%s articles"
6858                           (let* ((r (when unread " unread"))
6859                                  (d (when undownloaded " undownloaded"))
6860                                  (s (when unseen " unseen"))
6861                                  (l (delq nil (list r d s))))
6862                             (cond ((= 3 (length l))
6863                                    (concat r "," d ", or" s))
6864                                   ((= 2 (length l))
6865                                    (concat (car l) ", or" (cadr l)))
6866                                   ((= 1 (length l))
6867                                    (car l))
6868                                   (t
6869                                    ""))))
6870             nil
6871             )
6872         (gnus-summary-position-point))))))
6873
6874 (defun gnus-summary-next-subject (n &optional unread dont-display)
6875   "Go to next N'th summary line.
6876 If N is negative, go to the previous N'th subject line.
6877 If UNREAD is non-nil, only unread articles are selected.
6878 The difference between N and the actual number of steps taken is
6879 returned."
6880   (interactive "p")
6881   (let ((backward (< n 0))
6882         (n (abs n)))
6883     (while (and (> n 0)
6884                 (if backward
6885                     (gnus-summary-find-prev unread)
6886                   (gnus-summary-find-next unread)))
6887       (unless (zerop (setq n (1- n)))
6888         (gnus-summary-show-thread)))
6889     (when (/= 0 n)
6890       (gnus-message 7 "No more%s articles"
6891                     (if unread " unread" "")))
6892     (unless dont-display
6893       (gnus-summary-recenter)
6894       (gnus-summary-position-point))
6895     n))
6896
6897 (defun gnus-summary-next-unread-subject (n)
6898   "Go to next N'th unread summary line."
6899   (interactive "p")
6900   (gnus-summary-next-subject n t))
6901
6902 (defun gnus-summary-prev-subject (n &optional unread)
6903   "Go to previous N'th summary line.
6904 If optional argument UNREAD is non-nil, only unread article is selected."
6905   (interactive "p")
6906   (gnus-summary-next-subject (- n) unread))
6907
6908 (defun gnus-summary-prev-unread-subject (n)
6909   "Go to previous N'th unread summary line."
6910   (interactive "p")
6911   (gnus-summary-next-subject (- n) t))
6912
6913 (defun gnus-summary-goto-subjects (articles)
6914   "Insert the subject header for ARTICLES in the current buffer."
6915   (save-excursion
6916     (dolist (article articles)
6917       (gnus-summary-goto-subject article t)))
6918   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6919   (gnus-summary-position-point))
6920  
6921 (defun gnus-summary-goto-subject (article &optional force silent)
6922   "Go the subject line of ARTICLE.
6923 If FORCE, also allow jumping to articles not currently shown."
6924   (interactive "nArticle number: ")
6925   (unless (numberp article)
6926     (error "Article %s is not a number" article))
6927   (let ((b (point))
6928         (data (gnus-data-find article)))
6929     ;; We read in the article if we have to.
6930     (and (not data)
6931          force
6932          (gnus-summary-insert-subject
6933           article
6934           (if (or (numberp force) (vectorp force)) force)
6935           t)
6936          (setq data (gnus-data-find article)))
6937     (goto-char b)
6938     (if (not data)
6939         (progn
6940           (unless silent
6941             (gnus-message 3 "Can't find article %d" article))
6942           nil)
6943       (let ((pt (gnus-data-pos data)))
6944         (goto-char pt)
6945         (gnus-summary-set-article-display-arrow pt))
6946       (gnus-summary-position-point)
6947       article)))
6948
6949 ;; Walking around summary lines with displaying articles.
6950
6951 (defun gnus-summary-expand-window (&optional arg)
6952   "Make the summary buffer take up the entire Emacs frame.
6953 Given a prefix, will force an `article' buffer configuration."
6954   (interactive "P")
6955   (if arg
6956       (gnus-configure-windows 'article 'force)
6957     (gnus-configure-windows 'summary 'force)))
6958
6959 (defun gnus-summary-display-article (article &optional all-header)
6960   "Display ARTICLE in article buffer."
6961   (when (gnus-buffer-live-p gnus-article-buffer)
6962     (with-current-buffer gnus-article-buffer
6963       (mm-enable-multibyte)))
6964   (gnus-set-global-variables)
6965   (when (gnus-buffer-live-p gnus-article-buffer)
6966     (with-current-buffer gnus-article-buffer
6967       (setq gnus-article-charset gnus-newsgroup-charset)
6968       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6969       (mm-enable-multibyte)))
6970   (if (null article)
6971       nil
6972     (prog1
6973         (if gnus-summary-display-article-function
6974             (funcall gnus-summary-display-article-function article all-header)
6975           (gnus-article-prepare article all-header))
6976       (gnus-run-hooks 'gnus-select-article-hook)
6977       (when (and gnus-current-article
6978                  (not (zerop gnus-current-article)))
6979         (gnus-summary-goto-subject gnus-current-article))
6980       (gnus-summary-recenter)
6981       (when (and gnus-use-trees gnus-show-threads)
6982         (gnus-possibly-generate-tree article)
6983         (gnus-highlight-selected-tree article))
6984       ;; Successfully display article.
6985       (gnus-article-set-window-start
6986        (cdr (assq article gnus-newsgroup-bookmarks))))))
6987
6988 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6989   "Select the current article.
6990 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6991 non-nil, the article will be re-fetched even if it already present in
6992 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6993 be displayed."
6994   ;; Make sure we are in the summary buffer to work around bbdb bug.
6995   (unless (eq major-mode 'gnus-summary-mode)
6996     (set-buffer gnus-summary-buffer))
6997   (let ((article (or article (gnus-summary-article-number)))
6998         (all-headers (not (not all-headers))) ;Must be t or nil.
6999         gnus-summary-display-article-function)
7000     (and (not pseudo)
7001          (gnus-summary-article-pseudo-p article)
7002          (error "This is a pseudo-article"))
7003     (save-excursion
7004       (set-buffer gnus-summary-buffer)
7005       (if (or (and gnus-single-article-buffer
7006                    (or (null gnus-current-article)
7007                        (null gnus-article-current)
7008                        (null (get-buffer gnus-article-buffer))
7009                        (not (eq article (cdr gnus-article-current)))
7010                        (not (equal (car gnus-article-current)
7011                                    gnus-newsgroup-name))))
7012               (and (not gnus-single-article-buffer)
7013                    (or (null gnus-current-article)
7014                        (not (eq gnus-current-article article))))
7015               force)
7016           ;; The requested article is different from the current article.
7017           (progn
7018             (gnus-summary-display-article article all-headers)
7019             (when (gnus-buffer-live-p gnus-article-buffer)
7020               (with-current-buffer gnus-article-buffer
7021                 (if (not gnus-article-decoded-p) ;; a local variable
7022                     (mm-disable-multibyte))))
7023             (gnus-article-set-window-start
7024              (cdr (assq article gnus-newsgroup-bookmarks)))
7025             article)
7026         'old))))
7027
7028 (defun gnus-summary-force-verify-and-decrypt ()
7029   "Display buttons for signed/encrypted parts and verify/decrypt them."
7030   (interactive)
7031   (let ((mm-verify-option 'known)
7032         (mm-decrypt-option 'known)
7033         (gnus-buttonized-mime-types (append (list "multipart/signed"
7034                                                   "multipart/encrypted")
7035                                             gnus-buttonized-mime-types)))
7036     (gnus-summary-select-article nil 'force)))
7037
7038 (defun gnus-summary-set-current-mark (&optional current-mark)
7039   "Obsolete function."
7040   nil)
7041
7042 (defun gnus-summary-next-article (&optional unread subject backward push)
7043   "Select the next article.
7044 If UNREAD, only unread articles are selected.
7045 If SUBJECT, only articles with SUBJECT are selected.
7046 If BACKWARD, the previous article is selected instead of the next."
7047   (interactive "P")
7048   (cond
7049    ;; Is there such an article?
7050    ((and (gnus-summary-search-forward unread subject backward)
7051          (or (gnus-summary-display-article (gnus-summary-article-number))
7052              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7053     (gnus-summary-position-point))
7054    ;; If not, we try the first unread, if that is wanted.
7055    ((and subject
7056          gnus-auto-select-same
7057          (gnus-summary-first-unread-article))
7058     (gnus-summary-position-point)
7059     (gnus-message 6 "Wrapped"))
7060    ;; Try to get next/previous article not displayed in this group.
7061    ((and gnus-auto-extend-newsgroup
7062          (not unread) (not subject))
7063     (gnus-summary-goto-article
7064      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7065      nil (count-lines (point-min) (point))))
7066    ;; Go to next/previous group.
7067    (t
7068     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7069       (gnus-summary-jump-to-group gnus-newsgroup-name))
7070     (let ((cmd last-command-char)
7071           (point
7072            (save-excursion
7073              (set-buffer gnus-group-buffer)
7074              (point)))
7075           (group
7076            (if (eq gnus-keep-same-level 'best)
7077                (gnus-summary-best-group gnus-newsgroup-name)
7078              (gnus-summary-search-group backward gnus-keep-same-level))))
7079       ;; For some reason, the group window gets selected.  We change
7080       ;; it back.
7081       (select-window (get-buffer-window (current-buffer)))
7082       ;; Select next unread newsgroup automagically.
7083       (cond
7084        ((or (not gnus-auto-select-next)
7085             (not cmd))
7086         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7087        ((or (eq gnus-auto-select-next 'quietly)
7088             (and (eq gnus-auto-select-next 'slightly-quietly)
7089                  push)
7090             (and (eq gnus-auto-select-next 'almost-quietly)
7091                  (gnus-summary-last-article-p)))
7092         ;; Select quietly.
7093         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7094             (gnus-summary-exit)
7095           (gnus-message 7 "No more%s articles (%s)..."
7096                         (if unread " unread" "")
7097                         (if group (concat "selecting " group)
7098                           "exiting"))
7099           (gnus-summary-next-group nil group backward)))
7100        (t
7101         (when (gnus-key-press-event-p last-input-event)
7102           (gnus-summary-walk-group-buffer
7103            gnus-newsgroup-name cmd unread backward point))))))))
7104
7105 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7106   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7107                       (?\C-p (gnus-group-prev-unread-group 1))))
7108         (cursor-in-echo-area t)
7109         keve key group ended prompt)
7110     (save-excursion
7111       (set-buffer gnus-group-buffer)
7112       (goto-char start)
7113       (setq group
7114             (if (eq gnus-keep-same-level 'best)
7115                 (gnus-summary-best-group gnus-newsgroup-name)
7116               (gnus-summary-search-group backward gnus-keep-same-level))))
7117     (while (not ended)
7118       (setq prompt
7119             (format
7120              "No more%s articles%s " (if unread " unread" "")
7121              (if (and group
7122                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7123                  (format " (Type %s for %s [%s])"
7124                          (single-key-description cmd) group
7125                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7126                (format " (Type %s to exit %s)"
7127                        (single-key-description cmd)
7128                        gnus-newsgroup-name))))
7129       ;; Confirm auto selection.
7130       (setq key (car (setq keve (gnus-read-event-char prompt)))
7131             ended t)
7132       (cond
7133        ((assq key keystrokes)
7134         (let ((obuf (current-buffer)))
7135           (switch-to-buffer gnus-group-buffer)
7136           (when group
7137             (gnus-group-jump-to-group group))
7138           (eval (cadr (assq key keystrokes)))
7139           (setq group (gnus-group-group-name))
7140           (switch-to-buffer obuf))
7141         (setq ended nil))
7142        ((equal key cmd)
7143         (if (or (not group)
7144                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7145             (gnus-summary-exit)
7146           (gnus-summary-next-group nil group backward)))
7147        (t
7148         (push (cdr keve) unread-command-events))))))
7149
7150 (defun gnus-summary-next-unread-article ()
7151   "Select unread article after current one."
7152   (interactive)
7153   (gnus-summary-next-article
7154    (or (not (eq gnus-summary-goto-unread 'never))
7155        (gnus-summary-last-article-p (gnus-summary-article-number)))
7156    (and gnus-auto-select-same
7157         (gnus-summary-article-subject))))
7158
7159 (defun gnus-summary-prev-article (&optional unread subject)
7160   "Select the article after the current one.
7161 If UNREAD is non-nil, only unread articles are selected."
7162   (interactive "P")
7163   (gnus-summary-next-article unread subject t))
7164
7165 (defun gnus-summary-prev-unread-article ()
7166   "Select unread article before current one."
7167   (interactive)
7168   (gnus-summary-prev-article
7169    (or (not (eq gnus-summary-goto-unread 'never))
7170        (gnus-summary-first-article-p (gnus-summary-article-number)))
7171    (and gnus-auto-select-same
7172         (gnus-summary-article-subject))))
7173
7174 (defun gnus-summary-next-page (&optional lines circular stop)
7175   "Show next page of the selected article.
7176 If at the end of the current article, select the next article.
7177 LINES says how many lines should be scrolled up.
7178
7179 If CIRCULAR is non-nil, go to the start of the article instead of
7180 selecting the next article when reaching the end of the current
7181 article.
7182
7183 If STOP is non-nil, just stop when reaching the end of the message.
7184
7185 Also see the variable `gnus-article-skip-boring'."
7186   (interactive "P")
7187   (setq gnus-summary-buffer (current-buffer))
7188   (gnus-set-global-variables)
7189   (let ((article (gnus-summary-article-number))
7190         (article-window (get-buffer-window gnus-article-buffer t))
7191         endp)
7192     ;; If the buffer is empty, we have no article.
7193     (unless article
7194       (error "No article to select"))
7195     (gnus-configure-windows 'article)
7196     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7197         (if (and (eq gnus-summary-goto-unread 'never)
7198                  (not (gnus-summary-last-article-p article)))
7199             (gnus-summary-next-article)
7200           (gnus-summary-next-unread-article))
7201       (if (or (null gnus-current-article)
7202               (null gnus-article-current)
7203               (/= article (cdr gnus-article-current))
7204               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7205           ;; Selected subject is different from current article's.
7206           (gnus-summary-display-article article)
7207         (when article-window
7208           (gnus-eval-in-buffer-window gnus-article-buffer
7209             (setq endp (or (gnus-article-next-page lines)
7210                            (gnus-article-only-boring-p))))
7211           (when endp
7212             (cond (stop
7213                    (gnus-message 3 "End of message"))
7214                   (circular
7215                    (gnus-summary-beginning-of-article))
7216                   (lines
7217                    (gnus-message 3 "End of message"))
7218                   ((null lines)
7219                    (if (and (eq gnus-summary-goto-unread 'never)
7220                             (not (gnus-summary-last-article-p article)))
7221                        (gnus-summary-next-article)
7222                      (gnus-summary-next-unread-article))))))))
7223     (gnus-summary-recenter)
7224     (gnus-summary-position-point)))
7225
7226 (defun gnus-summary-prev-page (&optional lines move)
7227   "Show previous page of selected article.
7228 Argument LINES specifies lines to be scrolled down.
7229 If MOVE, move to the previous unread article if point is at
7230 the beginning of the buffer."
7231   (interactive "P")
7232   (let ((article (gnus-summary-article-number))
7233         (article-window (get-buffer-window gnus-article-buffer t))
7234         endp)
7235     (gnus-configure-windows 'article)
7236     (if (or (null gnus-current-article)
7237             (null gnus-article-current)
7238             (/= article (cdr gnus-article-current))
7239             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7240         ;; Selected subject is different from current article's.
7241         (gnus-summary-display-article article)
7242       (gnus-summary-recenter)
7243       (when article-window
7244         (gnus-eval-in-buffer-window gnus-article-buffer
7245           (setq endp (gnus-article-prev-page lines)))
7246         (when (and move endp)
7247           (cond (lines
7248                  (gnus-message 3 "Beginning of message"))
7249                 ((null lines)
7250                  (if (and (eq gnus-summary-goto-unread 'never)
7251                           (not (gnus-summary-first-article-p article)))
7252                      (gnus-summary-prev-article)
7253                    (gnus-summary-prev-unread-article))))))))
7254   (gnus-summary-position-point))
7255
7256 (defun gnus-summary-prev-page-or-article (&optional lines)
7257   "Show previous page of selected article.
7258 Argument LINES specifies lines to be scrolled down.
7259 If at the beginning of the article, go to the next article."
7260   (interactive "P")
7261   (gnus-summary-prev-page lines t))
7262
7263 (defun gnus-summary-scroll-up (lines)
7264   "Scroll up (or down) one line current article.
7265 Argument LINES specifies lines to be scrolled up (or down if negative)."
7266   (interactive "p")
7267   (gnus-configure-windows 'article)
7268   (gnus-summary-show-thread)
7269   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7270     (gnus-eval-in-buffer-window gnus-article-buffer
7271       (cond ((> lines 0)
7272              (when (gnus-article-next-page lines)
7273                (gnus-message 3 "End of message")))
7274             ((< lines 0)
7275              (gnus-article-prev-page (- lines))))))
7276   (gnus-summary-recenter)
7277   (gnus-summary-position-point))
7278
7279 (defun gnus-summary-scroll-down (lines)
7280   "Scroll down (or up) one line current article.
7281 Argument LINES specifies lines to be scrolled down (or up if negative)."
7282   (interactive "p")
7283   (gnus-summary-scroll-up (- lines)))
7284
7285 (defun gnus-summary-next-same-subject ()
7286   "Select next article which has the same subject as current one."
7287   (interactive)
7288   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7289
7290 (defun gnus-summary-prev-same-subject ()
7291   "Select previous article which has the same subject as current one."
7292   (interactive)
7293   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7294
7295 (defun gnus-summary-next-unread-same-subject ()
7296   "Select next unread article which has the same subject as current one."
7297   (interactive)
7298   (gnus-summary-next-article t (gnus-summary-article-subject)))
7299
7300 (defun gnus-summary-prev-unread-same-subject ()
7301   "Select previous unread article which has the same subject as current one."
7302   (interactive)
7303   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7304
7305 (defun gnus-summary-first-unread-article ()
7306   "Select the first unread article.
7307 Return nil if there are no unread articles."
7308   (interactive)
7309   (prog1
7310       (when (gnus-summary-first-subject t)
7311         (gnus-summary-show-thread)
7312         (gnus-summary-first-subject t)
7313         (gnus-summary-display-article (gnus-summary-article-number)))
7314     (gnus-summary-position-point)))
7315
7316 (defun gnus-summary-first-unread-subject ()
7317   "Place the point on the subject line of the first unread article.
7318 Return nil if there are no unread articles."
7319   (interactive)
7320   (prog1
7321       (when (gnus-summary-first-subject t)
7322         (gnus-summary-show-thread)
7323         (gnus-summary-first-subject t))
7324     (gnus-summary-position-point)))
7325
7326 (defun gnus-summary-first-unseen-subject ()
7327   "Place the point on the subject line of the first unseen article.
7328 Return nil if there are no unseen articles."
7329   (interactive)
7330   (prog1
7331       (when (gnus-summary-first-subject nil nil t)
7332         (gnus-summary-show-thread)
7333         (gnus-summary-first-subject nil nil t))
7334     (gnus-summary-position-point)))
7335
7336 (defun gnus-summary-first-unseen-or-unread-subject ()
7337   "Place the point on the subject line of the first unseen article or,
7338 if all article have been seen, on the subject line of the first unread
7339 article."
7340   (interactive)
7341   (prog1
7342       (unless (when (gnus-summary-first-subject nil nil t)
7343                 (gnus-summary-show-thread)
7344                 (gnus-summary-first-subject nil nil t))
7345         (when (gnus-summary-first-subject t)
7346           (gnus-summary-show-thread)
7347           (gnus-summary-first-subject t)))
7348     (gnus-summary-position-point)))
7349
7350 (defun gnus-summary-first-article ()
7351   "Select the first article.
7352 Return nil if there are no articles."
7353   (interactive)
7354   (prog1
7355       (when (gnus-summary-first-subject)
7356         (gnus-summary-show-thread)
7357         (gnus-summary-first-subject)
7358         (gnus-summary-display-article (gnus-summary-article-number)))
7359     (gnus-summary-position-point)))
7360
7361 (defun gnus-summary-best-unread-article (&optional arg)
7362   "Select the unread article with the highest score.
7363 If given a prefix argument, select the next unread article that has a
7364 score higher than the default score."
7365   (interactive "P")
7366   (let ((article (if arg
7367                      (gnus-summary-better-unread-subject)
7368                    (gnus-summary-best-unread-subject))))
7369     (if article
7370         (gnus-summary-goto-article article)
7371       (error "No unread articles"))))
7372
7373 (defun gnus-summary-best-unread-subject ()
7374   "Select the unread subject with the highest score."
7375   (interactive)
7376   (let ((best -1000000)
7377         (data gnus-newsgroup-data)
7378         article score)
7379     (while data
7380       (and (gnus-data-unread-p (car data))
7381            (> (setq score
7382                     (gnus-summary-article-score (gnus-data-number (car data))))
7383               best)
7384            (setq best score
7385                  article (gnus-data-number (car data))))
7386       (setq data (cdr data)))
7387     (when article
7388       (gnus-summary-goto-subject article))
7389     (gnus-summary-position-point)
7390     article))
7391
7392 (defun gnus-summary-better-unread-subject ()
7393   "Select the first unread subject that has a score over the default score."
7394   (interactive)
7395   (let ((data gnus-newsgroup-data)
7396         article score)
7397     (while (and (setq article (gnus-data-number (car data)))
7398                 (or (gnus-data-read-p (car data))
7399                     (not (> (gnus-summary-article-score article)
7400                             gnus-summary-default-score))))
7401       (setq data (cdr data)))
7402     (when article
7403       (gnus-summary-goto-subject article))
7404     (gnus-summary-position-point)
7405     article))
7406
7407 (defun gnus-summary-last-subject ()
7408   "Go to the last displayed subject line in the group."
7409   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7410     (when article
7411       (gnus-summary-goto-subject article))))
7412
7413 (defun gnus-summary-goto-article (article &optional all-headers force)
7414   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7415 If ALL-HEADERS is non-nil, no header lines are hidden.
7416 If FORCE, go to the article even if it isn't displayed.  If FORCE
7417 is a number, it is the line the article is to be displayed on."
7418   (interactive
7419    (list
7420     (completing-read
7421      "Article number or Message-ID: "
7422      (mapcar (lambda (number) (list (int-to-string number)))
7423              gnus-newsgroup-limit))
7424     current-prefix-arg
7425     t))
7426   (prog1
7427       (if (and (stringp article)
7428                (string-match "@" article))
7429           (gnus-summary-refer-article article)
7430         (when (stringp article)
7431           (setq article (string-to-number article)))
7432         (if (gnus-summary-goto-subject article force)
7433             (gnus-summary-display-article article all-headers)
7434           (gnus-message 4 "Couldn't go to article %s" article) nil))
7435     (gnus-summary-position-point)))
7436
7437 (defun gnus-summary-goto-last-article ()
7438   "Go to the previously read article."
7439   (interactive)
7440   (prog1
7441       (when gnus-last-article
7442         (gnus-summary-goto-article gnus-last-article nil t))
7443     (gnus-summary-position-point)))
7444
7445 (defun gnus-summary-pop-article (number)
7446   "Pop one article off the history and go to the previous.
7447 NUMBER articles will be popped off."
7448   (interactive "p")
7449   (let (to)
7450     (setq gnus-newsgroup-history
7451           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7452     (if to
7453         (gnus-summary-goto-article (car to) nil t)
7454       (error "Article history empty")))
7455   (gnus-summary-position-point))
7456
7457 ;; Summary commands and functions for limiting the summary buffer.
7458
7459 (defun gnus-summary-limit-to-articles (n)
7460   "Limit the summary buffer to the next N articles.
7461 If not given a prefix, use the process marked articles instead."
7462   (interactive "P")
7463   (prog1
7464       (let ((articles (gnus-summary-work-articles n)))
7465         (setq gnus-newsgroup-processable nil)
7466         (gnus-summary-limit articles))
7467     (gnus-summary-position-point)))
7468
7469 (defun gnus-summary-pop-limit (&optional total)
7470   "Restore the previous limit.
7471 If given a prefix, remove all limits."
7472   (interactive "P")
7473   (when total
7474     (setq gnus-newsgroup-limits
7475           (list (mapcar (lambda (h) (mail-header-number h))
7476                         gnus-newsgroup-headers))))
7477   (unless gnus-newsgroup-limits
7478     (error "No limit to pop"))
7479   (prog1
7480       (gnus-summary-limit nil 'pop)
7481     (gnus-summary-position-point)))
7482
7483 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7484   "Limit the summary buffer to articles that have subjects that match a regexp.
7485 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7486   (interactive
7487    (list (read-string (if current-prefix-arg
7488                           "Exclude subject (regexp): "
7489                         "Limit to subject (regexp): "))
7490          nil current-prefix-arg))
7491   (unless header
7492     (setq header "subject"))
7493   (when (not (equal "" subject))
7494     (prog1
7495         (let ((articles (gnus-summary-find-matching
7496                          (or header "subject") subject 'all nil nil
7497                          not-matching)))
7498           (unless articles
7499             (error "Found no matches for \"%s\"" subject))
7500           (gnus-summary-limit articles))
7501       (gnus-summary-position-point))))
7502
7503 (defun gnus-summary-limit-to-author (from &optional not-matching)
7504   "Limit the summary buffer to articles that have authors that match a regexp.
7505 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7506   (interactive
7507    (list (read-string (if current-prefix-arg
7508                           "Exclude author (regexp): "
7509                         "Limit to author (regexp): "))
7510          current-prefix-arg))
7511   (gnus-summary-limit-to-subject from "from" not-matching))
7512
7513 (defun gnus-summary-limit-to-age (age &optional younger-p)
7514   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7515 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7516 articles that are younger than AGE days."
7517   (interactive
7518    (let ((younger current-prefix-arg)
7519          (days-got nil)
7520          days)
7521      (while (not days-got)
7522        (setq days (if younger
7523                       (read-string "Limit to articles younger than (in days, older when negative): ")
7524                     (read-string
7525                      "Limit to articles older than (in days, younger when negative): ")))
7526        (when (> (length days) 0)
7527          (setq days (read days)))
7528        (if (numberp days)
7529            (progn
7530              (setq days-got t)
7531              (if (< days 0)
7532                  (progn
7533                    (setq younger (not younger))
7534                    (setq days (* days -1)))))
7535          (message "Please enter a number.")
7536          (sleep-for 1)))
7537      (list days younger)))
7538   (prog1
7539       (let ((data gnus-newsgroup-data)
7540             (cutoff (days-to-time age))
7541             articles d date is-younger)
7542         (while (setq d (pop data))
7543           (when (and (vectorp (gnus-data-header d))
7544                      (setq date (mail-header-date (gnus-data-header d))))
7545             (setq is-younger (time-less-p
7546                               (time-since (condition-case ()
7547                                               (date-to-time date)
7548                                             (error '(0 0))))
7549                               cutoff))
7550             (when (if younger-p
7551                       is-younger
7552                     (not is-younger))
7553               (push (gnus-data-number d) articles))))
7554         (gnus-summary-limit (nreverse articles)))
7555     (gnus-summary-position-point)))
7556
7557 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7558   "Limit the summary buffer to articles that match an 'extra' header."
7559   (interactive
7560    (let ((header
7561           (intern
7562            (gnus-completing-read-with-default
7563             (symbol-name (car gnus-extra-headers))
7564             (if current-prefix-arg
7565                 "Exclude extra header:"
7566               "Limit extra header:")
7567             (mapcar (lambda (x)
7568                       (cons (symbol-name x) x))
7569                     gnus-extra-headers)
7570             nil
7571             t))))
7572      (list header
7573            (read-string (format "%s header %s (regexp): "
7574                                 (if current-prefix-arg "Exclude" "Limit to")
7575                                 header))
7576            current-prefix-arg)))
7577   (when (not (equal "" regexp))
7578     (prog1
7579         (let ((articles (gnus-summary-find-matching
7580                          (cons 'extra header) regexp 'all nil nil
7581                          not-matching)))
7582           (unless articles
7583             (error "Found no matches for \"%s\"" regexp))
7584           (gnus-summary-limit articles))
7585       (gnus-summary-position-point))))
7586
7587 (defun gnus-summary-limit-to-display-predicate ()
7588   "Limit the summary buffer to the predicated in the `display' group parameter."
7589   (interactive)
7590   (unless gnus-newsgroup-display
7591     (error "There is no `display' group parameter"))
7592   (let (articles)
7593     (dolist (number gnus-newsgroup-articles)
7594       (when (funcall gnus-newsgroup-display)
7595         (push number articles)))
7596     (gnus-summary-limit articles))
7597   (gnus-summary-position-point))
7598
7599 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7600 (make-obsolete
7601  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7602
7603 (defun gnus-summary-limit-to-unread (&optional all)
7604   "Limit the summary buffer to articles that are not marked as read.
7605 If ALL is non-nil, limit strictly to unread articles."
7606   (interactive "P")
7607   (if all
7608       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7609     (gnus-summary-limit-to-marks
7610      ;; Concat all the marks that say that an article is read and have
7611      ;; those removed.
7612      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7613            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7614            gnus-low-score-mark gnus-expirable-mark
7615            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7616            gnus-duplicate-mark gnus-souped-mark)
7617      'reverse)))
7618
7619 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7620 (make-obsolete 'gnus-summary-delete-marked-with
7621                'gnus-summary-limit-exclude-marks)
7622
7623 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7624   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7625 If REVERSE, limit the summary buffer to articles that are marked
7626 with MARKS.  MARKS can either be a string of marks or a list of marks.
7627 Returns how many articles were removed."
7628   (interactive "sMarks: ")
7629   (gnus-summary-limit-to-marks marks t))
7630
7631 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7632   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7633 If REVERSE (the prefix), limit the summary buffer to articles that are
7634 not marked with MARKS.  MARKS can either be a string of marks or a
7635 list of marks.
7636 Returns how many articles were removed."
7637   (interactive "sMarks: \nP")
7638   (prog1
7639       (let ((data gnus-newsgroup-data)
7640             (marks (if (listp marks) marks
7641                      (append marks nil))) ; Transform to list.
7642             articles)
7643         (while data
7644           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7645                   (memq (gnus-data-mark (car data)) marks))
7646             (push (gnus-data-number (car data)) articles))
7647           (setq data (cdr data)))
7648         (gnus-summary-limit articles))
7649     (gnus-summary-position-point)))
7650
7651 (defun gnus-summary-limit-to-score (score)
7652   "Limit to articles with score at or above SCORE."
7653   (interactive "NLimit to articles with score of at least: ")
7654   (let ((data gnus-newsgroup-data)
7655         articles)
7656     (while data
7657       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7658                 score)
7659         (push (gnus-data-number (car data)) articles))
7660       (setq data (cdr data)))
7661     (prog1
7662         (gnus-summary-limit articles)
7663       (gnus-summary-position-point))))
7664
7665 (defun gnus-summary-limit-to-unseen ()
7666   "Limit to unseen articles."
7667   (interactive)
7668   (prog1
7669       (gnus-summary-limit gnus-newsgroup-unseen)
7670     (gnus-summary-position-point)))
7671
7672 (defun gnus-summary-limit-include-thread (id)
7673   "Display all the hidden articles that is in the thread with ID in it.
7674 When called interactively, ID is the Message-ID of the current
7675 article."
7676   (interactive (list (mail-header-id (gnus-summary-article-header))))
7677   (let ((articles (gnus-articles-in-thread
7678                    (gnus-id-to-thread (gnus-root-id id)))))
7679     (prog1
7680         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7681       (gnus-summary-limit-include-matching-articles
7682        "subject"
7683        (regexp-quote (gnus-simplify-subject-re
7684                       (mail-header-subject (gnus-id-to-header id)))))
7685       (gnus-summary-position-point))))
7686
7687 (defun gnus-summary-limit-include-matching-articles (header regexp)
7688   "Display all the hidden articles that have HEADERs that match REGEXP."
7689   (interactive (list (read-string "Match on header: ")
7690                      (read-string "Regexp: ")))
7691   (let ((articles (gnus-find-matching-articles header regexp)))
7692     (prog1
7693         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7694       (gnus-summary-position-point))))
7695
7696 (defun gnus-summary-insert-dormant-articles ()
7697   "Insert all the dormat articles for this group into the current buffer."
7698   (interactive)
7699   (let ((gnus-verbose (max 6 gnus-verbose)))
7700     (if (not gnus-newsgroup-dormant)
7701         (gnus-message 3 "No cached articles for this group")
7702       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7703
7704 (defun gnus-summary-limit-include-dormant ()
7705   "Display all the hidden articles that are marked as dormant.
7706 Note that this command only works on a subset of the articles currently
7707 fetched for this group."
7708   (interactive)
7709   (unless gnus-newsgroup-dormant
7710     (error "There are no dormant articles in this group"))
7711   (prog1
7712       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7713     (gnus-summary-position-point)))
7714
7715 (defun gnus-summary-limit-exclude-dormant ()
7716   "Hide all dormant articles."
7717   (interactive)
7718   (prog1
7719       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7720     (gnus-summary-position-point)))
7721
7722 (defun gnus-summary-limit-exclude-childless-dormant ()
7723   "Hide all dormant articles that have no children."
7724   (interactive)
7725   (let ((data (gnus-data-list t))
7726         articles d children)
7727     ;; Find all articles that are either not dormant or have
7728     ;; children.
7729     (while (setq d (pop data))
7730       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7731                 (and (setq children
7732                            (gnus-article-children (gnus-data-number d)))
7733                      (let (found)
7734                        (while children
7735                          (when (memq (car children) articles)
7736                            (setq children nil
7737                                  found t))
7738                          (pop children))
7739                        found)))
7740         (push (gnus-data-number d) articles)))
7741     ;; Do the limiting.
7742     (prog1
7743         (gnus-summary-limit articles)
7744       (gnus-summary-position-point))))
7745
7746 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7747   "Mark all unread excluded articles as read.
7748 If ALL, mark even excluded ticked and dormants as read."
7749   (interactive "P")
7750   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7751   (let ((articles (gnus-sorted-ndifference
7752                    (sort
7753                     (mapcar (lambda (h) (mail-header-number h))
7754                             gnus-newsgroup-headers)
7755                     '<)
7756                    gnus-newsgroup-limit))
7757         article)
7758     (setq gnus-newsgroup-unreads
7759           (gnus-sorted-intersection gnus-newsgroup-unreads
7760                                     gnus-newsgroup-limit))
7761     (if all
7762         (setq gnus-newsgroup-dormant nil
7763               gnus-newsgroup-marked nil
7764               gnus-newsgroup-reads
7765               (nconc
7766                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7767                gnus-newsgroup-reads))
7768       (while (setq article (pop articles))
7769         (unless (or (memq article gnus-newsgroup-dormant)
7770                     (memq article gnus-newsgroup-marked))
7771           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7772
7773 (defun gnus-summary-limit (articles &optional pop)
7774   (if pop
7775       ;; We pop the previous limit off the stack and use that.
7776       (setq articles (car gnus-newsgroup-limits)
7777             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7778     ;; We use the new limit, so we push the old limit on the stack.
7779     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7780   ;; Set the limit.
7781   (setq gnus-newsgroup-limit articles)
7782   (let ((total (length gnus-newsgroup-data))
7783         (data (gnus-data-find-list (gnus-summary-article-number)))
7784         (gnus-summary-mark-below nil)   ; Inhibit this.
7785         found)
7786     ;; This will do all the work of generating the new summary buffer
7787     ;; according to the new limit.
7788     (gnus-summary-prepare)
7789     ;; Hide any threads, possibly.
7790     (gnus-summary-maybe-hide-threads)
7791     ;; Try to return to the article you were at, or one in the
7792     ;; neighborhood.
7793     (when data
7794       ;; We try to find some article after the current one.
7795       (while data
7796         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7797           (setq data nil
7798                 found t))
7799         (setq data (cdr data))))
7800     (unless found
7801       ;; If there is no data, that means that we were after the last
7802       ;; article.  The same goes when we can't find any articles
7803       ;; after the current one.
7804       (goto-char (point-max))
7805       (gnus-summary-find-prev))
7806     (gnus-set-mode-line 'summary)
7807     ;; We return how many articles were removed from the summary
7808     ;; buffer as a result of the new limit.
7809     (- total (length gnus-newsgroup-data))))
7810
7811 (defsubst gnus-invisible-cut-children (threads)
7812   (let ((num 0))
7813     (while threads
7814       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7815         (incf num))
7816       (pop threads))
7817     (< num 2)))
7818
7819 (defsubst gnus-cut-thread (thread)
7820   "Go forwards in the thread until we find an article that we want to display."
7821   (when (or (eq gnus-fetch-old-headers 'some)
7822             (eq gnus-fetch-old-headers 'invisible)
7823             (numberp gnus-fetch-old-headers)
7824             (eq gnus-build-sparse-threads 'some)
7825             (eq gnus-build-sparse-threads 'more))
7826     ;; Deal with old-fetched headers and sparse threads.
7827     (while (and
7828             thread
7829             (or
7830              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7831              (gnus-summary-article-ancient-p
7832               (mail-header-number (car thread))))
7833             (if (or (<= (length (cdr thread)) 1)
7834                     (eq gnus-fetch-old-headers 'invisible))
7835                 (setq gnus-newsgroup-limit
7836                       (delq (mail-header-number (car thread))
7837                             gnus-newsgroup-limit)
7838                       thread (cadr thread))
7839               (when (gnus-invisible-cut-children (cdr thread))
7840                 (let ((th (cdr thread)))
7841                   (while th
7842                     (if (memq (mail-header-number (caar th))
7843                               gnus-newsgroup-limit)
7844                         (setq thread (car th)
7845                               th nil)
7846                       (setq th (cdr th))))))))))
7847   thread)
7848
7849 (defun gnus-cut-threads (threads)
7850   "Cut off all uninteresting articles from the beginning of threads."
7851   (when (or (eq gnus-fetch-old-headers 'some)
7852             (eq gnus-fetch-old-headers 'invisible)
7853             (numberp gnus-fetch-old-headers)
7854             (eq gnus-build-sparse-threads 'some)
7855             (eq gnus-build-sparse-threads 'more))
7856     (let ((th threads))
7857       (while th
7858         (setcar th (gnus-cut-thread (car th)))
7859         (setq th (cdr th)))))
7860   ;; Remove nixed out threads.
7861   (delq nil threads))
7862
7863 (defun gnus-summary-initial-limit (&optional show-if-empty)
7864   "Figure out what the initial limit is supposed to be on group entry.
7865 This entails weeding out unwanted dormants, low-scored articles,
7866 fetch-old-headers verbiage, and so on."
7867   ;; Most groups have nothing to remove.
7868   (if (or gnus-inhibit-limiting
7869           (and (null gnus-newsgroup-dormant)
7870                (eq gnus-newsgroup-display 'gnus-not-ignore)
7871                (not (eq gnus-fetch-old-headers 'some))
7872                (not (numberp gnus-fetch-old-headers))
7873                (not (eq gnus-fetch-old-headers 'invisible))
7874                (null gnus-summary-expunge-below)
7875                (not (eq gnus-build-sparse-threads 'some))
7876                (not (eq gnus-build-sparse-threads 'more))
7877                (null gnus-thread-expunge-below)
7878                (not gnus-use-nocem)))
7879       ()                                ; Do nothing.
7880     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7881     (setq gnus-newsgroup-limit nil)
7882     (mapatoms
7883      (lambda (node)
7884        (unless (car (symbol-value node))
7885          ;; These threads have no parents -- they are roots.
7886          (let ((nodes (cdr (symbol-value node)))
7887                thread)
7888            (while nodes
7889              (if (and gnus-thread-expunge-below
7890                       (< (gnus-thread-total-score (car nodes))
7891                          gnus-thread-expunge-below))
7892                  (gnus-expunge-thread (pop nodes))
7893                (setq thread (pop nodes))
7894                (gnus-summary-limit-children thread))))))
7895      gnus-newsgroup-dependencies)
7896     ;; If this limitation resulted in an empty group, we might
7897     ;; pop the previous limit and use it instead.
7898     (when (and (not gnus-newsgroup-limit)
7899                show-if-empty)
7900       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7901     gnus-newsgroup-limit))
7902
7903 (defun gnus-summary-limit-children (thread)
7904   "Return 1 if this subthread is visible and 0 if it is not."
7905   ;; First we get the number of visible children to this thread.  This
7906   ;; is done by recursing down the thread using this function, so this
7907   ;; will really go down to a leaf article first, before slowly
7908   ;; working its way up towards the root.
7909   (when thread
7910     (let* ((max-lisp-eval-depth 5000)
7911            (children
7912            (if (cdr thread)
7913                (apply '+ (mapcar 'gnus-summary-limit-children
7914                                  (cdr thread)))
7915              0))
7916           (number (mail-header-number (car thread)))
7917           score)
7918       (if (and
7919            (not (memq number gnus-newsgroup-marked))
7920            (or
7921             ;; If this article is dormant and has absolutely no visible
7922             ;; children, then this article isn't visible.
7923             (and (memq number gnus-newsgroup-dormant)
7924                  (zerop children))
7925             ;; If this is "fetch-old-headered" and there is no
7926             ;; visible children, then we don't want this article.
7927             (and (or (eq gnus-fetch-old-headers 'some)
7928                      (numberp gnus-fetch-old-headers))
7929                  (gnus-summary-article-ancient-p number)
7930                  (zerop children))
7931             ;; If this is "fetch-old-headered" and `invisible', then
7932             ;; we don't want this article.
7933             (and (eq gnus-fetch-old-headers 'invisible)
7934                  (gnus-summary-article-ancient-p number))
7935             ;; If this is a sparsely inserted article with no children,
7936             ;; we don't want it.
7937             (and (eq gnus-build-sparse-threads 'some)
7938                  (gnus-summary-article-sparse-p number)
7939                  (zerop children))
7940             ;; If we use expunging, and this article is really
7941             ;; low-scored, then we don't want this article.
7942             (when (and gnus-summary-expunge-below
7943                        (< (setq score
7944                                 (or (cdr (assq number gnus-newsgroup-scored))
7945                                     gnus-summary-default-score))
7946                           gnus-summary-expunge-below))
7947               ;; We increase the expunge-tally here, but that has
7948               ;; nothing to do with the limits, really.
7949               (incf gnus-newsgroup-expunged-tally)
7950               ;; We also mark as read here, if that's wanted.
7951               (when (and gnus-summary-mark-below
7952                          (< score gnus-summary-mark-below))
7953                 (setq gnus-newsgroup-unreads
7954                       (delq number gnus-newsgroup-unreads))
7955                 (if gnus-newsgroup-auto-expire
7956                     (push number gnus-newsgroup-expirable)
7957                   (push (cons number gnus-low-score-mark)
7958                         gnus-newsgroup-reads)))
7959               t)
7960             ;; Do the `display' group parameter.
7961             (and gnus-newsgroup-display
7962                  (not (funcall gnus-newsgroup-display)))
7963             ;; Check NoCeM things.
7964             (if (and gnus-use-nocem
7965                      (gnus-nocem-unwanted-article-p
7966                       (mail-header-id (car thread))))
7967                 (progn
7968                   (setq gnus-newsgroup-unreads
7969                         (delq number gnus-newsgroup-unreads))
7970                   t))))
7971           ;; Nope, invisible article.
7972           0
7973         ;; Ok, this article is to be visible, so we add it to the limit
7974         ;; and return 1.
7975         (push number gnus-newsgroup-limit)
7976         1))))
7977
7978 (defun gnus-expunge-thread (thread)
7979   "Mark all articles in THREAD as read."
7980   (let* ((number (mail-header-number (car thread))))
7981     (incf gnus-newsgroup-expunged-tally)
7982     ;; We also mark as read here, if that's wanted.
7983     (setq gnus-newsgroup-unreads
7984           (delq number gnus-newsgroup-unreads))
7985     (if gnus-newsgroup-auto-expire
7986         (push number gnus-newsgroup-expirable)
7987       (push (cons number gnus-low-score-mark)
7988             gnus-newsgroup-reads)))
7989   ;; Go recursively through all subthreads.
7990   (mapcar 'gnus-expunge-thread (cdr thread)))
7991
7992 ;; Summary article oriented commands
7993
7994 (defun gnus-summary-refer-parent-article (n)
7995   "Refer parent article N times.
7996 If N is negative, go to ancestor -N instead.
7997 The difference between N and the number of articles fetched is returned."
7998   (interactive "p")
7999   (let ((skip 1)
8000         error header ref)
8001     (when (not (natnump n))
8002       (setq skip (abs n)
8003             n 1))
8004     (while (and (> n 0)
8005                 (not error))
8006       (setq header (gnus-summary-article-header))
8007       (if (and (eq (mail-header-number header)
8008                    (cdr gnus-article-current))
8009                (equal gnus-newsgroup-name
8010                       (car gnus-article-current)))
8011           ;; If we try to find the parent of the currently
8012           ;; displayed article, then we take a look at the actual
8013           ;; References header, since this is slightly more
8014           ;; reliable than the References field we got from the
8015           ;; server.
8016           (save-excursion
8017             (set-buffer gnus-original-article-buffer)
8018             (nnheader-narrow-to-headers)
8019             (unless (setq ref (message-fetch-field "references"))
8020               (setq ref (message-fetch-field "in-reply-to")))
8021             (widen))
8022         (setq ref
8023               ;; It's not the current article, so we take a bet on
8024               ;; the value we got from the server.
8025               (mail-header-references header)))
8026       (if (and ref
8027                (not (equal ref "")))
8028           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8029             (gnus-message 1 "Couldn't find parent"))
8030         (gnus-message 1 "No references in article %d"
8031                       (gnus-summary-article-number))
8032         (setq error t))
8033       (decf n))
8034     (gnus-summary-position-point)
8035     n))
8036
8037 (defun gnus-summary-refer-references ()
8038   "Fetch all articles mentioned in the References header.
8039 Return the number of articles fetched."
8040   (interactive)
8041   (let ((ref (mail-header-references (gnus-summary-article-header)))
8042         (current (gnus-summary-article-number))
8043         (n 0))
8044     (if (or (not ref)
8045             (equal ref ""))
8046         (error "No References in the current article")
8047       ;; For each Message-ID in the References header...
8048       (while (string-match "<[^>]*>" ref)
8049         (incf n)
8050         ;; ... fetch that article.
8051         (gnus-summary-refer-article
8052          (prog1 (match-string 0 ref)
8053            (setq ref (substring ref (match-end 0))))))
8054       (gnus-summary-goto-subject current)
8055       (gnus-summary-position-point)
8056       n)))
8057
8058 (defun gnus-summary-refer-thread (&optional limit)
8059   "Fetch all articles in the current thread.
8060 If LIMIT (the numerical prefix), fetch that many old headers instead
8061 of what's specified by the `gnus-refer-thread-limit' variable."
8062   (interactive "P")
8063   (let ((id (mail-header-id (gnus-summary-article-header)))
8064         (limit (if limit (prefix-numeric-value limit)
8065                  gnus-refer-thread-limit)))
8066     (unless (eq gnus-fetch-old-headers 'invisible)
8067       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8068       ;; Retrieve the headers and read them in.
8069       (if (eq (if (numberp limit)
8070                   (gnus-retrieve-headers
8071                    (list (min
8072                           (+ (mail-header-number
8073                               (gnus-summary-article-header))
8074                              limit)
8075                           gnus-newsgroup-end))
8076                    gnus-newsgroup-name (* limit 2))
8077                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8078                 ;; headers.
8079                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8080                                        gnus-newsgroup-name limit))
8081               'nov)
8082           (gnus-build-all-threads)
8083         (error "Can't fetch thread from backends that don't support NOV"))
8084       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8085     (gnus-summary-limit-include-thread id)))
8086
8087 (defun gnus-summary-refer-article (message-id)
8088   "Fetch an article specified by MESSAGE-ID."
8089   (interactive "sMessage-ID: ")
8090   (when (and (stringp message-id)
8091              (not (zerop (length message-id))))
8092     ;; Construct the correct Message-ID if necessary.
8093     ;; Suggested by tale@pawl.rpi.edu.
8094     (unless (string-match "^<" message-id)
8095       (setq message-id (concat "<" message-id)))
8096     (unless (string-match ">$" message-id)
8097       (setq message-id (concat message-id ">")))
8098     (let* ((header (gnus-id-to-header message-id))
8099            (sparse (and header
8100                         (gnus-summary-article-sparse-p
8101                          (mail-header-number header))
8102                         (memq (mail-header-number header)
8103                               gnus-newsgroup-limit)))
8104            number)
8105       (cond
8106        ;; If the article is present in the buffer we just go to it.
8107        ((and header
8108              (or (not (gnus-summary-article-sparse-p
8109                        (mail-header-number header)))
8110                  sparse))
8111         (prog1
8112             (gnus-summary-goto-article
8113              (mail-header-number header) nil t)
8114           (when sparse
8115             (gnus-summary-update-article (mail-header-number header)))))
8116        (t
8117         ;; We fetch the article.
8118         (catch 'found
8119           (dolist (gnus-override-method (gnus-refer-article-methods))
8120             (when (and (gnus-check-server gnus-override-method)
8121                        ;; Fetch the header,
8122                        (setq number (gnus-summary-insert-subject message-id)))
8123               ;; and display the article.
8124               (gnus-summary-select-article nil nil nil number)
8125               (throw 'found t)))
8126           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8127
8128 (defun gnus-refer-article-methods ()
8129   "Return a list of referable methods."
8130   (cond
8131    ;; No method, so we default to current and native.
8132    ((null gnus-refer-article-method)
8133     (list gnus-current-select-method gnus-select-method))
8134    ;; Current.
8135    ((eq 'current gnus-refer-article-method)
8136     (list gnus-current-select-method))
8137    ;; List of select methods.
8138    ((not (and (symbolp (car gnus-refer-article-method))
8139               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8140     (let (out)
8141       (dolist (method gnus-refer-article-method)
8142         (push (if (eq 'current method)
8143                   gnus-current-select-method
8144                 method)
8145               out))
8146       (nreverse out)))
8147    ;; One single select method.
8148    (t
8149     (list gnus-refer-article-method))))
8150
8151 (defun gnus-summary-edit-parameters ()
8152   "Edit the group parameters of the current group."
8153   (interactive)
8154   (gnus-group-edit-group gnus-newsgroup-name 'params))
8155
8156 (defun gnus-summary-customize-parameters ()
8157   "Customize the group parameters of the current group."
8158   (interactive)
8159   (gnus-group-customize gnus-newsgroup-name))
8160
8161 (defun gnus-summary-enter-digest-group (&optional force)
8162   "Enter an nndoc group based on the current article.
8163 If FORCE, force a digest interpretation.  If not, try
8164 to guess what the document format is."
8165   (interactive "P")
8166   (let ((conf gnus-current-window-configuration))
8167     (save-window-excursion
8168       (save-excursion
8169         (let (gnus-article-prepare-hook
8170               gnus-display-mime-function
8171               gnus-break-pages)
8172           (gnus-summary-select-article))))
8173     (setq gnus-current-window-configuration conf)
8174     (let* ((name (format "%s-%d"
8175                          (gnus-group-prefixed-name
8176                           gnus-newsgroup-name (list 'nndoc ""))
8177                          (save-excursion
8178                            (set-buffer gnus-summary-buffer)
8179                            gnus-current-article)))
8180            (ogroup gnus-newsgroup-name)
8181            (params (append (gnus-info-params (gnus-get-info ogroup))
8182                            (list (cons 'to-group ogroup))
8183                            (list (cons 'save-article-group ogroup))))
8184            (case-fold-search t)
8185            (buf (current-buffer))
8186            dig to-address)
8187       (save-excursion
8188         (set-buffer gnus-original-article-buffer)
8189         ;; Have the digest group inherit the main mail address of
8190         ;; the parent article.
8191         (when (setq to-address (or (gnus-fetch-field "reply-to")
8192                                    (gnus-fetch-field "from")))
8193           (setq params (append
8194                         (list (cons 'to-address
8195                                     (funcall gnus-decode-encoded-word-function
8196                                              to-address))))))
8197         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8198         (insert-buffer-substring gnus-original-article-buffer)
8199         ;; Remove lines that may lead nndoc to misinterpret the
8200         ;; document type.
8201         (narrow-to-region
8202          (goto-char (point-min))
8203          (or (search-forward "\n\n" nil t) (point)))
8204         (goto-char (point-min))
8205         (delete-matching-lines "^Path:\\|^From ")
8206         (widen))
8207       (unwind-protect
8208           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8209                     (gnus-newsgroup-ephemeral-ignored-charsets
8210                      gnus-newsgroup-ignored-charsets))
8211                 (gnus-group-read-ephemeral-group
8212                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8213                               (nndoc-article-type
8214                                ,(if force 'mbox 'guess)))
8215                  t nil nil nil
8216                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8217                                                         "ADAPT")))))
8218               ;; Make all postings to this group go to the parent group.
8219               (nconc (gnus-info-params (gnus-get-info name))
8220                      params)
8221             ;; Couldn't select this doc group.
8222             (switch-to-buffer buf)
8223             (gnus-set-global-variables)
8224             (gnus-configure-windows 'summary)
8225             (gnus-message 3 "Article couldn't be entered?"))
8226         (kill-buffer dig)))))
8227
8228 (defun gnus-summary-read-document (n)
8229   "Open a new group based on the current article(s).
8230 This will allow you to read digests and other similar
8231 documents as newsgroups.
8232 Obeys the standard process/prefix convention."
8233   (interactive "P")
8234   (let* ((articles (gnus-summary-work-articles n))
8235          (ogroup gnus-newsgroup-name)
8236          (params (append (gnus-info-params (gnus-get-info ogroup))
8237                          (list (cons 'to-group ogroup))))
8238          article group egroup groups vgroup)
8239     (while (setq article (pop articles))
8240       (setq group (format "%s-%d" gnus-newsgroup-name article))
8241       (gnus-summary-remove-process-mark article)
8242       (when (gnus-summary-display-article article)
8243         (save-excursion
8244           (with-temp-buffer
8245             (insert-buffer-substring gnus-original-article-buffer)
8246             ;; Remove some headers that may lead nndoc to make
8247             ;; the wrong guess.
8248             (message-narrow-to-head)
8249             (goto-char (point-min))
8250             (delete-matching-lines "^\\(Path\\):\\|^From ")
8251             (widen)
8252             (if (setq egroup
8253                       (gnus-group-read-ephemeral-group
8254                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8255                                      (nndoc-article-type guess))
8256                        t nil t))
8257                 (progn
8258             ;; Make all postings to this group go to the parent group.
8259                   (nconc (gnus-info-params (gnus-get-info egroup))
8260                          params)
8261                   (push egroup groups))
8262               ;; Couldn't select this doc group.
8263               (gnus-error 3 "Article couldn't be entered"))))))
8264     ;; Now we have selected all the documents.
8265     (cond
8266      ((not groups)
8267       (error "None of the articles could be interpreted as documents"))
8268      ((gnus-group-read-ephemeral-group
8269        (setq vgroup (format
8270                      "nnvirtual:%s-%s" gnus-newsgroup-name
8271                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8272        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8273        t
8274        (cons (current-buffer) 'summary)))
8275      (t
8276       (error "Couldn't select virtual nndoc group")))))
8277
8278 (defun gnus-summary-isearch-article (&optional regexp-p)
8279   "Do incremental search forward on the current article.
8280 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8281   (interactive "P")
8282   (gnus-summary-select-article)
8283   (gnus-configure-windows 'article)
8284   (gnus-eval-in-buffer-window gnus-article-buffer
8285     (save-restriction
8286       (widen)
8287       (isearch-forward regexp-p))))
8288
8289 (defun gnus-summary-search-article-forward (regexp &optional backward)
8290   "Search for an article containing REGEXP forward.
8291 If BACKWARD, search backward instead."
8292   (interactive
8293    (list (read-string
8294           (format "Search article %s (regexp%s): "
8295                   (if current-prefix-arg "backward" "forward")
8296                   (if gnus-last-search-regexp
8297                       (concat ", default " gnus-last-search-regexp)
8298                     "")))
8299          current-prefix-arg))
8300   (if (string-equal regexp "")
8301       (setq regexp (or gnus-last-search-regexp ""))
8302     (setq gnus-last-search-regexp regexp)
8303     (setq gnus-article-before-search gnus-current-article))
8304   ;; Intentionally set gnus-last-article.
8305   (setq gnus-last-article gnus-article-before-search)
8306   (let ((gnus-last-article gnus-last-article))
8307     (if (gnus-summary-search-article regexp backward)
8308         (gnus-summary-show-thread)
8309       (error "Search failed: \"%s\"" regexp))))
8310
8311 (defun gnus-summary-search-article-backward (regexp)
8312   "Search for an article containing REGEXP backward."
8313   (interactive
8314    (list (read-string
8315           (format "Search article backward (regexp%s): "
8316                   (if gnus-last-search-regexp
8317                       (concat ", default " gnus-last-search-regexp)
8318                     "")))))
8319   (gnus-summary-search-article-forward regexp 'backward))
8320
8321 (defun gnus-summary-search-article (regexp &optional backward)
8322   "Search for an article containing REGEXP.
8323 Optional argument BACKWARD means do search for backward.
8324 `gnus-select-article-hook' is not called during the search."
8325   ;; We have to require this here to make sure that the following
8326   ;; dynamic binding isn't shadowed by autoloading.
8327   (require 'gnus-async)
8328   (require 'gnus-art)
8329   (let ((gnus-select-article-hook nil)  ;Disable hook.
8330         (gnus-article-prepare-hook nil)
8331         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8332         (gnus-use-article-prefetch nil)
8333         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8334         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8335         (gnus-visual nil)
8336         (gnus-keep-backlog nil)
8337         (gnus-break-pages nil)
8338         (gnus-summary-display-arrow nil)
8339         (gnus-updated-mode-lines nil)
8340         (gnus-auto-center-summary nil)
8341         (sum (current-buffer))
8342         (gnus-display-mime-function nil)
8343         (found nil)
8344         point)
8345     (gnus-save-hidden-threads
8346       (gnus-summary-select-article)
8347       (set-buffer gnus-article-buffer)
8348       (goto-char (window-point (get-buffer-window (current-buffer))))
8349       (when backward
8350         (forward-line -1))
8351       (while (not found)
8352         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8353         (if (if backward
8354                 (re-search-backward regexp nil t)
8355               (re-search-forward regexp nil t))
8356             ;; We found the regexp.
8357             (progn
8358               (setq found 'found)
8359               (beginning-of-line)
8360               (set-window-start
8361                (get-buffer-window (current-buffer))
8362                (point))
8363               (forward-line 1)
8364               (set-window-point
8365                (get-buffer-window (current-buffer))
8366                (point))
8367               (set-buffer sum)
8368               (setq point (point)))
8369           ;; We didn't find it, so we go to the next article.
8370           (set-buffer sum)
8371           (setq found 'not)
8372           (while (eq found 'not)
8373             (if (not (if backward (gnus-summary-find-prev)
8374                        (gnus-summary-find-next)))
8375                 ;; No more articles.
8376                 (setq found t)
8377               ;; Select the next article and adjust point.
8378               (unless (gnus-summary-article-sparse-p
8379                        (gnus-summary-article-number))
8380                 (setq found nil)
8381                 (gnus-summary-select-article)
8382                 (set-buffer gnus-article-buffer)
8383                 (widen)
8384                 (goto-char (if backward (point-max) (point-min))))))))
8385       (gnus-message 7 ""))
8386     ;; Return whether we found the regexp.
8387     (when (eq found 'found)
8388       (goto-char point)
8389       (gnus-summary-show-thread)
8390       (gnus-summary-goto-subject gnus-current-article)
8391       (gnus-summary-position-point)
8392       t)))
8393
8394 (defun gnus-find-matching-articles (header regexp)
8395   "Return a list of all articles that match REGEXP on HEADER.
8396 This search includes all articles in the current group that Gnus has
8397 fetched headers for, whether they are displayed or not."
8398   (let ((articles nil)
8399         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8400         (case-fold-search t))
8401     (dolist (header gnus-newsgroup-headers)
8402       (when (string-match regexp (funcall func header))
8403         (push (mail-header-number header) articles)))
8404     (nreverse articles)))
8405
8406 (defun gnus-summary-find-matching (header regexp &optional backward unread
8407                                           not-case-fold not-matching)
8408   "Return a list of all articles that match REGEXP on HEADER.
8409 The search stars on the current article and goes forwards unless
8410 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8411 If UNREAD is non-nil, only unread articles will
8412 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8413 in the comparisons. If NOT-MATCHING, return a list of all articles that
8414 not match REGEXP on HEADER."
8415   (let ((case-fold-search (not not-case-fold))
8416         articles d func)
8417     (if (consp header)
8418         (if (eq (car header) 'extra)
8419             (setq func
8420                   `(lambda (h)
8421                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8422                          "")))
8423           (error "%s is an invalid header" header))
8424       (unless (fboundp (intern (concat "mail-header-" header)))
8425         (error "%s is not a valid header" header))
8426       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8427     (dolist (d (if (eq backward 'all)
8428                    gnus-newsgroup-data
8429                  (gnus-data-find-list
8430                   (gnus-summary-article-number)
8431                   (gnus-data-list backward))))
8432       (when (and (or (not unread)       ; We want all articles...
8433                      (gnus-data-unread-p d)) ; Or just unreads.
8434                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8435                  (if not-matching
8436                      (not (string-match
8437                            regexp
8438                            (funcall func (gnus-data-header d))))
8439                    (string-match regexp
8440                                  (funcall func (gnus-data-header d)))))
8441         (push (gnus-data-number d) articles))) ; Success!
8442     (nreverse articles)))
8443
8444 (defun gnus-summary-execute-command (header regexp command &optional backward)
8445   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8446 If HEADER is an empty string (or nil), the match is done on the entire
8447 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8448   (interactive
8449    (list (let ((completion-ignore-case t))
8450            (completing-read
8451             "Header name: "
8452             (mapcar (lambda (header) (list (format "%s" header)))
8453                     (append
8454                      '("Number" "Subject" "From" "Lines" "Date"
8455                        "Message-ID" "Xref" "References" "Body")
8456                      gnus-extra-headers))
8457             nil 'require-match))
8458          (read-string "Regexp: ")
8459          (read-key-sequence "Command: ")
8460          current-prefix-arg))
8461   (when (equal header "Body")
8462     (setq header ""))
8463   ;; Hidden thread subtrees must be searched as well.
8464   (gnus-summary-show-all-threads)
8465   ;; We don't want to change current point nor window configuration.
8466   (save-excursion
8467     (save-window-excursion
8468       (let (gnus-visual
8469             gnus-treat-strip-trailing-blank-lines
8470             gnus-treat-strip-leading-blank-lines
8471             gnus-treat-strip-multiple-blank-lines
8472             gnus-treat-hide-boring-headers
8473             gnus-treat-fold-newsgroups
8474             gnus-article-prepare-hook)
8475         (gnus-message 6 "Executing %s..." (key-description command))
8476         ;; We'd like to execute COMMAND interactively so as to give arguments.
8477         (gnus-execute header regexp
8478                       `(call-interactively ',(key-binding command))
8479                       backward)
8480         (gnus-message 6 "Executing %s...done" (key-description command))))))
8481
8482 (defun gnus-summary-beginning-of-article ()
8483   "Scroll the article back to the beginning."
8484   (interactive)
8485   (gnus-summary-select-article)
8486   (gnus-configure-windows 'article)
8487   (gnus-eval-in-buffer-window gnus-article-buffer
8488     (widen)
8489     (goto-char (point-min))
8490     (when gnus-page-broken
8491       (gnus-narrow-to-page))))
8492
8493 (defun gnus-summary-end-of-article ()
8494   "Scroll to the end of the article."
8495   (interactive)
8496   (gnus-summary-select-article)
8497   (gnus-configure-windows 'article)
8498   (gnus-eval-in-buffer-window gnus-article-buffer
8499     (widen)
8500     (goto-char (point-max))
8501     (recenter -3)
8502     (when gnus-page-broken
8503       (gnus-narrow-to-page))))
8504
8505 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8506   "Truncate to LEN and quote all \"(\"'s in STRING."
8507   (gnus-replace-in-string (if (and len (> (length string) len))
8508                               (substring string 0 len)
8509                             string)
8510                           "[()]" "\\\\\\&"))
8511
8512 (defun gnus-summary-print-article (&optional filename n)
8513   "Generate and print a PostScript image of the N next (mail) articles.
8514
8515 If N is negative, print the N previous articles.  If N is nil and articles
8516 have been marked with the process mark, print these instead.
8517
8518 If the optional first argument FILENAME is nil, send the image to the
8519 printer.  If FILENAME is a string, save the PostScript image in a file with
8520 that name.  If FILENAME is a number, prompt the user for the name of the file
8521 to save in."
8522   (interactive (list (ps-print-preprint current-prefix-arg)))
8523   (dolist (article (gnus-summary-work-articles n))
8524     (gnus-summary-select-article nil nil 'pseudo article)
8525     (gnus-eval-in-buffer-window gnus-article-buffer
8526       (gnus-print-buffer))
8527     (gnus-summary-remove-process-mark article))
8528   (ps-despool filename))
8529
8530 (defun gnus-print-buffer ()
8531   (let ((buffer (generate-new-buffer " *print*")))
8532     (unwind-protect
8533         (progn
8534           (copy-to-buffer buffer (point-min) (point-max))
8535           (set-buffer buffer)
8536           (gnus-article-delete-invisible-text)
8537           (gnus-remove-text-with-property 'gnus-decoration)
8538           (when (gnus-visual-p 'article-highlight 'highlight)
8539             ;; Copy-to-buffer doesn't copy overlay.  So redo
8540             ;; highlight.
8541             (let ((gnus-article-buffer buffer))
8542               (gnus-article-highlight-citation t)
8543               (gnus-article-highlight-signature)))
8544           (let ((ps-left-header
8545                  (list
8546                   (concat "("
8547                           (gnus-summary-print-truncate-and-quote
8548                            (mail-header-subject gnus-current-headers)
8549                            66) ")")
8550                   (concat "("
8551                           (gnus-summary-print-truncate-and-quote
8552                            (mail-header-from gnus-current-headers)
8553                            45) ")")))
8554                 (ps-right-header
8555                  (list
8556                   "/pagenumberstring load"
8557                   (concat "("
8558                           (mail-header-date gnus-current-headers) ")"))))
8559             (gnus-run-hooks 'gnus-ps-print-hook)
8560             (save-excursion
8561               (if window-system
8562                   (ps-spool-buffer-with-faces)
8563                 (ps-spool-buffer)))))
8564       (kill-buffer buffer))))
8565
8566 (defun gnus-summary-show-article (&optional arg)
8567   "Force redisplaying of the current article.
8568 If ARG (the prefix) is a number, show the article with the charset
8569 defined in `gnus-summary-show-article-charset-alist', or the charset
8570 input.
8571 If ARG (the prefix) is non-nil and not a number, show the raw article
8572 without any article massaging functions being run.  Normally, the key
8573 strokes are `C-u g'."
8574   (interactive "P")
8575   (cond
8576    ((numberp arg)
8577     (gnus-summary-show-article t)
8578     (let ((gnus-newsgroup-charset
8579            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8580                (mm-read-coding-system
8581                 "View as charset: " ;; actually it is coding system.
8582                 (save-excursion
8583                   (set-buffer gnus-article-buffer)
8584                   (mm-detect-coding-region (point) (point-max))))))
8585           (gnus-newsgroup-ignored-charsets 'gnus-all))
8586       (gnus-summary-select-article nil 'force)
8587       (let ((deps gnus-newsgroup-dependencies)
8588             head header lines)
8589         (save-excursion
8590           (set-buffer gnus-original-article-buffer)
8591           (save-restriction
8592             (message-narrow-to-head)
8593             (setq head (buffer-string))
8594             (goto-char (point-min))
8595             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8596               (goto-char (point-max))
8597               (widen)
8598               (setq lines (1- (count-lines (point) (point-max))))))
8599           (with-temp-buffer
8600             (insert (format "211 %d Article retrieved.\n"
8601                             (cdr gnus-article-current)))
8602             (insert head)
8603             (if lines (insert (format "Lines: %d\n" lines)))
8604             (insert ".\n")
8605             (let ((nntp-server-buffer (current-buffer)))
8606               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8607         (gnus-data-set-header
8608          (gnus-data-find (cdr gnus-article-current))
8609          header)
8610         (gnus-summary-update-article-line
8611          (cdr gnus-article-current) header)
8612         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8613           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8614    ((not arg)
8615     ;; Select the article the normal way.
8616     (gnus-summary-select-article nil 'force))
8617    (t
8618     ;; We have to require this here to make sure that the following
8619     ;; dynamic binding isn't shadowed by autoloading.
8620     (require 'gnus-async)
8621     (require 'gnus-art)
8622     ;; Bind the article treatment functions to nil.
8623     (let ((gnus-have-all-headers t)
8624           gnus-article-prepare-hook
8625           gnus-article-decode-hook
8626           gnus-display-mime-function
8627           gnus-break-pages)
8628       ;; Destroy any MIME parts.
8629       (when (gnus-buffer-live-p gnus-article-buffer)
8630         (save-excursion
8631           (set-buffer gnus-article-buffer)
8632           (mm-destroy-parts gnus-article-mime-handles)
8633           ;; Set it to nil for safety reason.
8634           (setq gnus-article-mime-handle-alist nil)
8635           (setq gnus-article-mime-handles nil)))
8636       (gnus-summary-select-article nil 'force))))
8637   (gnus-summary-goto-subject gnus-current-article)
8638   (gnus-summary-position-point))
8639
8640 (defun gnus-summary-show-raw-article ()
8641   "Show the raw article without any article massaging functions being run."
8642   (interactive)
8643   (gnus-summary-show-article t))
8644
8645 (defun gnus-summary-verbose-headers (&optional arg)
8646   "Toggle permanent full header display.
8647 If ARG is a positive number, turn header display on.
8648 If ARG is a negative number, turn header display off."
8649   (interactive "P")
8650   (setq gnus-show-all-headers
8651         (cond ((or (not (numberp arg))
8652                    (zerop arg))
8653                (not gnus-show-all-headers))
8654               ((natnump arg)
8655                t)))
8656   (gnus-summary-show-article))
8657
8658 (defun gnus-summary-toggle-header (&optional arg)
8659   "Show the headers if they are hidden, or hide them if they are shown.
8660 If ARG is a positive number, show the entire header.
8661 If ARG is a negative number, hide the unwanted header lines."
8662   (interactive "P")
8663   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8664                      (get-buffer-window gnus-article-buffer t))))
8665     (with-current-buffer gnus-article-buffer
8666       (widen)
8667       (article-narrow-to-head)
8668       (let* ((buffer-read-only nil)
8669              (inhibit-point-motion-hooks t)
8670              (hidden (if (numberp arg)
8671                          (>= arg 0)
8672                        (gnus-article-hidden-text-p 'headers)))
8673              s e)
8674         (delete-region (point-min) (point-max))
8675         (with-current-buffer gnus-original-article-buffer
8676           (goto-char (setq s (point-min)))
8677           (setq e (if (search-forward "\n\n" nil t)
8678                       (1- (point))
8679                     (point-max))))
8680         (insert-buffer-substring gnus-original-article-buffer s e)
8681         (run-hooks 'gnus-article-decode-hook)
8682         (if hidden
8683             (let ((gnus-treat-hide-headers nil)
8684                   (gnus-treat-hide-boring-headers nil))
8685               (gnus-delete-wash-type 'headers)
8686               (gnus-treat-article 'head))
8687           (gnus-treat-article 'head))
8688         (widen)
8689         (if window
8690             (set-window-start window (goto-char (point-min))))
8691         (setq gnus-page-broken
8692               (when gnus-break-pages
8693                 (gnus-narrow-to-page)
8694                 t))
8695         (gnus-set-mode-line 'article)))))
8696
8697 (defun gnus-summary-show-all-headers ()
8698   "Make all header lines visible."
8699   (interactive)
8700   (gnus-summary-toggle-header 1))
8701
8702 (defun gnus-summary-caesar-message (&optional arg)
8703   "Caesar rotate the current article by 13.
8704 The numerical prefix specifies how many places to rotate each letter
8705 forward."
8706   (interactive "P")
8707   (gnus-summary-select-article)
8708   (let ((mail-header-separator ""))
8709     (gnus-eval-in-buffer-window gnus-article-buffer
8710       (save-restriction
8711         (widen)
8712         (let ((start (window-start))
8713               buffer-read-only)
8714           (message-caesar-buffer-body arg)
8715           (set-window-start (get-buffer-window (current-buffer)) start))))))
8716
8717 (autoload 'unmorse-region "morse"
8718   "Convert morse coded text in region to ordinary ASCII text."
8719   t)
8720
8721 (defun gnus-summary-morse-message (&optional arg)
8722   "Morse decode the current article."
8723   (interactive "P")
8724   (gnus-summary-select-article)
8725   (let ((mail-header-separator ""))
8726     (gnus-eval-in-buffer-window gnus-article-buffer
8727       (save-excursion
8728         (save-restriction
8729           (widen)
8730           (let ((pos (window-start))
8731                 buffer-read-only)
8732             (goto-char (point-min))
8733             (when (message-goto-body)
8734               (gnus-narrow-to-body))
8735             (goto-char (point-min))
8736             (while (re-search-forward "·" (point-max) t)
8737               (replace-match "."))
8738             (unmorse-region (point-min) (point-max))
8739             (widen)
8740             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8741
8742 (defun gnus-summary-stop-page-breaking ()
8743   "Stop page breaking in the current article."
8744   (interactive)
8745   (gnus-summary-select-article)
8746   (gnus-eval-in-buffer-window gnus-article-buffer
8747     (widen)
8748     (when (gnus-visual-p 'page-marker)
8749       (let ((buffer-read-only nil))
8750         (gnus-remove-text-with-property 'gnus-prev)
8751         (gnus-remove-text-with-property 'gnus-next))
8752       (setq gnus-page-broken nil))))
8753
8754 (defun gnus-summary-move-article (&optional n to-newsgroup
8755                                             select-method action)
8756   "Move the current article to a different newsgroup.
8757 If N is a positive number, move the N next articles.
8758 If N is a negative number, move the N previous articles.
8759 If N is nil and any articles have been marked with the process mark,
8760 move those articles instead.
8761 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8762 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8763 re-spool using this method.
8764
8765 When called interactively with TO-NEWSGROUP being nil, the value of
8766 the variable `gnus-move-split-methods' is used for finding a default
8767 for the target newsgroup.
8768
8769 For this function to work, both the current newsgroup and the
8770 newsgroup that you want to move to have to support the `request-move'
8771 and `request-accept' functions.
8772
8773 ACTION can be either `move' (the default), `crosspost' or `copy'."
8774   (interactive "P")
8775   (unless action
8776     (setq action 'move))
8777   ;; Check whether the source group supports the required functions.
8778   (cond ((and (eq action 'move)
8779               (not (gnus-check-backend-function
8780                     'request-move-article gnus-newsgroup-name)))
8781          (error "The current group does not support article moving"))
8782         ((and (eq action 'crosspost)
8783               (not (gnus-check-backend-function
8784                     'request-replace-article gnus-newsgroup-name)))
8785          (error "The current group does not support article editing")))
8786   (let ((articles (gnus-summary-work-articles n))
8787         (prefix (if (gnus-check-backend-function
8788                      'request-move-article gnus-newsgroup-name)
8789                     (gnus-group-real-prefix gnus-newsgroup-name)
8790                   ""))
8791         (names '((move "Move" "Moving")
8792                  (copy "Copy" "Copying")
8793                  (crosspost "Crosspost" "Crossposting")))
8794         (copy-buf (save-excursion
8795                     (nnheader-set-temp-buffer " *copy article*")))
8796         art-group to-method new-xref article to-groups)
8797     (unless (assq action names)
8798       (error "Unknown action %s" action))
8799     ;; Read the newsgroup name.
8800     (when (and (not to-newsgroup)
8801                (not select-method))
8802       (if (and gnus-move-split-methods
8803                (not
8804                 (and (memq gnus-current-article articles)
8805                      (gnus-buffer-live-p gnus-original-article-buffer))))
8806           ;; When `gnus-move-split-methods' is non-nil, we have to
8807           ;; select an article to give `gnus-read-move-group-name' an
8808           ;; opportunity to suggest an appropriate default.  However,
8809           ;; we needn't render or mark the article.
8810           (let ((gnus-display-mime-function nil)
8811                 (gnus-article-prepare-hook nil)
8812                 (gnus-mark-article-hook nil))
8813             (gnus-summary-select-article nil nil nil (car articles))))
8814       (setq to-newsgroup
8815             (gnus-read-move-group-name
8816              (cadr (assq action names))
8817              (symbol-value (intern (format "gnus-current-%s-group" action)))
8818              articles prefix))
8819       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8820     (setq to-method (or select-method
8821                         (gnus-server-to-method
8822                          (gnus-group-method to-newsgroup))))
8823     ;; Check the method we are to move this article to...
8824     (unless (gnus-check-backend-function
8825              'request-accept-article (car to-method))
8826       (error "%s does not support article copying" (car to-method)))
8827     (unless (gnus-check-server to-method)
8828       (error "Can't open server %s" (car to-method)))
8829     (gnus-message 6 "%s to %s: %s..."
8830                   (caddr (assq action names))
8831                   (or (car select-method) to-newsgroup) articles)
8832     (while articles
8833       (setq article (pop articles))
8834       (setq
8835        art-group
8836        (cond
8837         ;; Move the article.
8838         ((eq action 'move)
8839          ;; Remove this article from future suppression.
8840          (gnus-dup-unsuppress-article article)
8841          (gnus-request-move-article
8842           article                       ; Article to move
8843           gnus-newsgroup-name           ; From newsgroup
8844           (nth 1 (gnus-find-method-for-group
8845                   gnus-newsgroup-name)) ; Server
8846           (list 'gnus-request-accept-article
8847                 to-newsgroup (list 'quote select-method)
8848                 (not articles) t)       ; Accept form
8849           (not articles)))              ; Only save nov last time
8850         ;; Copy the article.
8851         ((eq action 'copy)
8852          (save-excursion
8853            (set-buffer copy-buf)
8854            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8855              (gnus-request-accept-article
8856               to-newsgroup select-method (not articles) t))))
8857         ;; Crosspost the article.
8858         ((eq action 'crosspost)
8859          (let ((xref (message-tokenize-header
8860                       (mail-header-xref (gnus-summary-article-header article))
8861                       " ")))
8862            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8863                                   ":" (number-to-string article)))
8864            (unless xref
8865              (setq xref (list (system-name))))
8866            (setq new-xref
8867                  (concat
8868                   (mapconcat 'identity
8869                              (delete "Xref:" (delete new-xref xref))
8870                              " ")
8871                   " " new-xref))
8872            (save-excursion
8873              (set-buffer copy-buf)
8874              ;; First put the article in the destination group.
8875              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8876              (when (consp (setq art-group
8877                                 (gnus-request-accept-article
8878                                  to-newsgroup select-method (not articles))))
8879                (setq new-xref (concat new-xref " " (car art-group)
8880                                       ":"
8881                                       (number-to-string (cdr art-group))))
8882                ;; Now we have the new Xrefs header, so we insert
8883                ;; it and replace the new article.
8884                (nnheader-replace-header "Xref" new-xref)
8885                (gnus-request-replace-article
8886                 (cdr art-group) to-newsgroup (current-buffer))
8887                art-group))))))
8888       (cond
8889        ((not art-group)
8890         (gnus-message 1 "Couldn't %s article %s: %s"
8891                       (cadr (assq action names)) article
8892                       (nnheader-get-report (car to-method))))
8893        ((eq art-group 'junk)
8894         (when (eq action 'move)
8895           (gnus-summary-mark-article article gnus-canceled-mark)
8896           (gnus-message 4 "Deleted article %s" article)
8897           ;; run the delete hook
8898           (run-hook-with-args 'gnus-summary-article-delete-hook
8899                               action
8900                               (gnus-data-header
8901                                (assoc article (gnus-data-list nil)))
8902                               gnus-newsgroup-name nil
8903                               select-method)))
8904        (t
8905         (let* ((pto-group (gnus-group-prefixed-name
8906                            (car art-group) to-method))
8907                (entry
8908                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8909                (info (nth 2 entry))
8910                (to-group (gnus-info-group info))
8911                to-marks)
8912           ;; Update the group that has been moved to.
8913           (when (and info
8914                      (memq action '(move copy)))
8915             (unless (member to-group to-groups)
8916               (push to-group to-groups))
8917
8918             (unless (memq article gnus-newsgroup-unreads)
8919               (push 'read to-marks)
8920               (gnus-info-set-read
8921                info (gnus-add-to-range (gnus-info-read info)
8922                                        (list (cdr art-group)))))
8923
8924             ;; See whether the article is to be put in the cache.
8925             (let ((marks gnus-article-mark-lists)
8926                   (to-article (cdr art-group)))
8927
8928               ;; Enter the article into the cache in the new group,
8929               ;; if that is required.
8930               (when gnus-use-cache
8931                 (gnus-cache-possibly-enter-article
8932                  to-group to-article
8933                  (memq article gnus-newsgroup-marked)
8934                  (memq article gnus-newsgroup-dormant)
8935                  (memq article gnus-newsgroup-unreads)))
8936
8937               (when gnus-preserve-marks
8938                 ;; Copy any marks over to the new group.
8939                 (when (and (equal to-group gnus-newsgroup-name)
8940                            (not (memq article gnus-newsgroup-unreads)))
8941                   ;; Mark this article as read in this group.
8942                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8943                   (setcdr (gnus-active to-group) to-article)
8944                   (setcdr gnus-newsgroup-active to-article))
8945
8946                 (while marks
8947                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8948                     (when (memq article (symbol-value
8949                                          (intern (format "gnus-newsgroup-%s"
8950                                                          (caar marks)))))
8951                       (push (cdar marks) to-marks)
8952                       ;; If the other group is the same as this group,
8953                       ;; then we have to add the mark to the list.
8954                       (when (equal to-group gnus-newsgroup-name)
8955                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8956                              (cons to-article
8957                                    (symbol-value
8958                                     (intern (format "gnus-newsgroup-%s"
8959                                                     (caar marks)))))))
8960                       ;; Copy the marks to other group.
8961                       (gnus-add-marked-articles
8962                        to-group (cdar marks) (list to-article) info)))
8963                   (setq marks (cdr marks)))
8964
8965                 (gnus-request-set-mark
8966                  to-group (list (list (list to-article) 'add to-marks))))
8967
8968               (gnus-dribble-enter
8969                (concat "(gnus-group-set-info '"
8970                        (gnus-prin1-to-string (gnus-get-info to-group))
8971                        ")"))))
8972
8973           ;; Update the Xref header in this article to point to
8974           ;; the new crossposted article we have just created.
8975           (when (eq action 'crosspost)
8976             (save-excursion
8977               (set-buffer copy-buf)
8978               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8979               (nnheader-replace-header "Xref" new-xref)
8980               (gnus-request-replace-article
8981                article gnus-newsgroup-name (current-buffer))))
8982
8983           ;; run the move/copy/crosspost/respool hook
8984           (run-hook-with-args 'gnus-summary-article-move-hook 
8985                               action
8986                               (gnus-data-header 
8987                                (assoc article (gnus-data-list nil)))
8988                               gnus-newsgroup-name
8989                               to-newsgroup
8990                               select-method))
8991
8992         ;;;!!!Why is this necessary?
8993         (set-buffer gnus-summary-buffer)
8994         
8995         (gnus-summary-goto-subject article)
8996         (when (eq action 'move)
8997           (gnus-summary-mark-article article gnus-canceled-mark))))
8998       (gnus-summary-remove-process-mark article))
8999     ;; Re-activate all groups that have been moved to.
9000     (save-excursion
9001       (set-buffer gnus-group-buffer)
9002       (let ((gnus-group-marked to-groups))
9003         (gnus-group-get-new-news-this-group nil t)))
9004
9005     (gnus-kill-buffer copy-buf)
9006     (gnus-summary-position-point)
9007     (gnus-set-mode-line 'summary)))
9008
9009 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9010   "Move the current article to a different newsgroup.
9011 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9012 When called interactively, if TO-NEWSGROUP is nil, use the value of
9013 the variable `gnus-move-split-methods' for finding a default target
9014 newsgroup.
9015 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9016 re-spool using this method."
9017   (interactive "P")
9018   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9019
9020 (defun gnus-summary-crosspost-article (&optional n)
9021   "Crosspost the current article to some other group."
9022   (interactive "P")
9023   (gnus-summary-move-article n nil nil 'crosspost))
9024
9025 (defcustom gnus-summary-respool-default-method nil
9026   "Default method type for respooling an article.
9027 If nil, use to the current newsgroup method."
9028   :type 'symbol
9029   :group 'gnus-summary-mail)
9030
9031 (defcustom gnus-summary-display-while-building nil
9032   "If non-nil, show and update the summary buffer as it's being built.
9033 If the value is t, update the buffer after every line is inserted.  If
9034 the value is an integer (N), update the display every N lines."
9035   :group 'gnus-thread
9036   :type '(choice (const :tag "off" nil)
9037                  number
9038                  (const :tag "frequently" t)))
9039
9040 (defun gnus-summary-respool-article (&optional n method)
9041   "Respool the current article.
9042 The article will be squeezed through the mail spooling process again,
9043 which means that it will be put in some mail newsgroup or other
9044 depending on `nnmail-split-methods'.
9045 If N is a positive number, respool the N next articles.
9046 If N is a negative number, respool the N previous articles.
9047 If N is nil and any articles have been marked with the process mark,
9048 respool those articles instead.
9049
9050 Respooling can be done both from mail groups and \"real\" newsgroups.
9051 In the former case, the articles in question will be moved from the
9052 current group into whatever groups they are destined to.  In the
9053 latter case, they will be copied into the relevant groups."
9054   (interactive
9055    (list current-prefix-arg
9056          (let* ((methods (gnus-methods-using 'respool))
9057                 (methname
9058                  (symbol-name (or gnus-summary-respool-default-method
9059                                   (car (gnus-find-method-for-group
9060                                         gnus-newsgroup-name)))))
9061                 (method
9062                  (gnus-completing-read-with-default
9063                   methname "What backend do you want to use when respooling?"
9064                   methods nil t nil 'gnus-mail-method-history))
9065                 ms)
9066            (cond
9067             ((zerop (length (setq ms (gnus-servers-using-backend
9068                                       (intern method)))))
9069              (list (intern method) ""))
9070             ((= 1 (length ms))
9071              (car ms))
9072             (t
9073              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9074                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9075                            ms-alist))))))))
9076   (unless method
9077     (error "No method given for respooling"))
9078   (if (assoc (symbol-name
9079               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9080              (gnus-methods-using 'respool))
9081       (gnus-summary-move-article n nil method)
9082     (gnus-summary-copy-article n nil method)))
9083
9084 (defun gnus-summary-import-article (file &optional edit)
9085   "Import an arbitrary file into a mail newsgroup."
9086   (interactive "fImport file: \nP")
9087   (let ((group gnus-newsgroup-name)
9088         (now (current-time))
9089         atts lines group-art)
9090     (unless (gnus-check-backend-function 'request-accept-article group)
9091       (error "%s does not support article importing" group))
9092     (or (file-readable-p file)
9093         (not (file-regular-p file))
9094         (error "Can't read %s" file))
9095     (save-excursion
9096       (set-buffer (gnus-get-buffer-create " *import file*"))
9097       (erase-buffer)
9098       (nnheader-insert-file-contents file)
9099       (goto-char (point-min))
9100       (if (nnheader-article-p)
9101           (save-restriction
9102             (goto-char (point-min))
9103             (search-forward "\n\n" nil t)
9104             (narrow-to-region (point-min) (1- (point)))
9105             (goto-char (point-min))
9106             (unless (re-search-forward "^date:" nil t)
9107               (goto-char (point-max))
9108               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9109        ;; This doesn't look like an article, so we fudge some headers.
9110         (setq atts (file-attributes file)
9111               lines (count-lines (point-min) (point-max)))
9112         (insert "From: " (read-string "From: ") "\n"
9113                 "Subject: " (read-string "Subject: ") "\n"
9114                 "Date: " (message-make-date (nth 5 atts)) "\n"
9115                 "Message-ID: " (message-make-message-id) "\n"
9116                 "Lines: " (int-to-string lines) "\n"
9117                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9118       (setq group-art (gnus-request-accept-article group nil t))
9119       (kill-buffer (current-buffer)))
9120     (setq gnus-newsgroup-active (gnus-activate-group group))
9121     (forward-line 1)
9122     (gnus-summary-goto-article (cdr group-art) nil t)
9123     (when edit
9124       (gnus-summary-edit-article))))
9125
9126 (defun gnus-summary-create-article ()
9127   "Create an article in a mail newsgroup."
9128   (interactive)
9129   (let ((group gnus-newsgroup-name)
9130         (now (current-time))
9131         group-art)
9132     (unless (gnus-check-backend-function 'request-accept-article group)
9133       (error "%s does not support article importing" group))
9134     (save-excursion
9135       (set-buffer (gnus-get-buffer-create " *import file*"))
9136       (erase-buffer)
9137       (goto-char (point-min))
9138       ;; This doesn't look like an article, so we fudge some headers.
9139       (insert "From: " (read-string "From: ") "\n"
9140               "Subject: " (read-string "Subject: ") "\n"
9141               "Date: " (message-make-date now) "\n"
9142               "Message-ID: " (message-make-message-id) "\n")
9143       (setq group-art (gnus-request-accept-article group nil t))
9144       (kill-buffer (current-buffer)))
9145     (setq gnus-newsgroup-active (gnus-activate-group group))
9146     (forward-line 1)
9147     (gnus-summary-goto-article (cdr group-art) nil t)
9148     (gnus-summary-edit-article)))
9149
9150 (defun gnus-summary-article-posted-p ()
9151   "Say whether the current (mail) article is available from news as well.
9152 This will be the case if the article has both been mailed and posted."
9153   (interactive)
9154   (let ((id (mail-header-references (gnus-summary-article-header)))
9155         (gnus-override-method (car (gnus-refer-article-methods))))
9156     (if (gnus-request-head id "")
9157         (gnus-message 2 "The current message was found on %s"
9158                       gnus-override-method)
9159       (gnus-message 2 "The current message couldn't be found on %s"
9160                     gnus-override-method)
9161       nil)))
9162
9163 (defun gnus-summary-expire-articles (&optional now)
9164   "Expire all articles that are marked as expirable in the current group."
9165   (interactive)
9166   (when (and (not gnus-group-is-exiting-without-update-p)
9167              (gnus-check-backend-function
9168               'request-expire-articles gnus-newsgroup-name))
9169     ;; This backend supports expiry.
9170     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9171            (expirable (if total
9172                           (progn
9173                             ;; We need to update the info for
9174                             ;; this group for `gnus-list-of-read-articles'
9175                             ;; to give us the right answer.
9176                             (gnus-run-hooks 'gnus-exit-group-hook)
9177                             (gnus-summary-update-info)
9178                             (gnus-list-of-read-articles gnus-newsgroup-name))
9179                         (setq gnus-newsgroup-expirable
9180                               (sort gnus-newsgroup-expirable '<))))
9181            (expiry-wait (if now 'immediate
9182                           (gnus-group-find-parameter
9183                            gnus-newsgroup-name 'expiry-wait)))
9184            (nnmail-expiry-target
9185             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9186                 nnmail-expiry-target))
9187            es)
9188       (when expirable
9189         ;; There are expirable articles in this group, so we run them
9190         ;; through the expiry process.
9191         (gnus-message 6 "Expiring articles...")
9192         (unless (gnus-check-group gnus-newsgroup-name)
9193           (error "Can't open server for %s" gnus-newsgroup-name))
9194         ;; The list of articles that weren't expired is returned.
9195         (save-excursion
9196           (if expiry-wait
9197               (let ((nnmail-expiry-wait-function nil)
9198                     (nnmail-expiry-wait expiry-wait))
9199                 (setq es (gnus-request-expire-articles
9200                           expirable gnus-newsgroup-name)))
9201             (setq es (gnus-request-expire-articles
9202                       expirable gnus-newsgroup-name)))
9203           (unless total
9204             (setq gnus-newsgroup-expirable es))
9205           ;; We go through the old list of expirable, and mark all
9206           ;; really expired articles as nonexistent.
9207           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9208             (let ((gnus-use-cache nil))
9209               (dolist (article expirable)
9210                 (when (and (not (memq article es))
9211                            (gnus-data-find article))
9212                   (gnus-summary-mark-article article gnus-canceled-mark)
9213                   (run-hook-with-args 'gnus-summary-article-expire-hook
9214                                       'delete
9215                                       (gnus-data-header
9216                                        (assoc article (gnus-data-list nil)))
9217                                       gnus-newsgroup-name
9218                                       nil
9219                                       nil))))))
9220         (gnus-message 6 "Expiring articles...done")))))
9221
9222 (defun gnus-summary-expire-articles-now ()
9223   "Expunge all expirable articles in the current group.
9224 This means that *all* articles that are marked as expirable will be
9225 deleted forever, right now."
9226   (interactive)
9227   (or gnus-expert-user
9228       (gnus-yes-or-no-p
9229        "Are you really, really, really sure you want to delete all these messages? ")
9230       (error "Phew!"))
9231   (gnus-summary-expire-articles t))
9232
9233 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9234 (defun gnus-summary-delete-article (&optional n)
9235   "Delete the N next (mail) articles.
9236 This command actually deletes articles.  This is not a marking
9237 command.  The article will disappear forever from your life, never to
9238 return.
9239
9240 If N is negative, delete backwards.
9241 If N is nil and articles have been marked with the process mark,
9242 delete these instead.
9243
9244 If `gnus-novice-user' is non-nil you will be asked for
9245 confirmation before the articles are deleted."
9246   (interactive "P")
9247   (unless (gnus-check-backend-function 'request-expire-articles
9248                                        gnus-newsgroup-name)
9249     (error "The current newsgroup does not support article deletion"))
9250   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9251     (error "Couldn't open server"))
9252   ;; Compute the list of articles to delete.
9253   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9254         (nnmail-expiry-target 'delete)
9255         not-deleted)
9256     (if (and gnus-novice-user
9257              (not (gnus-yes-or-no-p
9258                    (format "Do you really want to delete %s forever? "
9259                            (if (> (length articles) 1)
9260                                (format "these %s articles" (length articles))
9261                              "this article")))))
9262         ()
9263       ;; Delete the articles.
9264       (setq not-deleted (gnus-request-expire-articles
9265                          articles gnus-newsgroup-name 'force))
9266       (while articles
9267         (gnus-summary-remove-process-mark (car articles))
9268         ;; The backend might not have been able to delete the article
9269         ;; after all.
9270         (unless (memq (car articles) not-deleted)
9271           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9272         (let* ((article (car articles))
9273                (id (mail-header-id (gnus-data-header 
9274                                     (assoc article (gnus-data-list nil))))))
9275           (run-hook-with-args 'gnus-summary-article-delete-hook
9276                               'delete id gnus-newsgroup-name nil
9277                               nil))
9278         (setq articles (cdr articles)))
9279       (when not-deleted
9280         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9281     (gnus-summary-position-point)
9282     (gnus-set-mode-line 'summary)
9283     not-deleted))
9284
9285 (defun gnus-summary-edit-article (&optional arg)
9286   "Edit the current article.
9287 This will have permanent effect only in mail groups.
9288 If ARG is nil, edit the decoded articles.
9289 If ARG is 1, edit the raw articles.
9290 If ARG is 2, edit the raw articles even in read-only groups.
9291 If ARG is 3, edit the articles with the current handles.
9292 Otherwise, allow editing of articles even in read-only
9293 groups."
9294   (interactive "P")
9295   (let (force raw current-handles)
9296     (cond
9297      ((null arg))
9298      ((eq arg 1)
9299       (setq raw t))
9300      ((eq arg 2)
9301       (setq raw t
9302             force t))
9303      ((eq arg 3)
9304       (setq current-handles
9305             (and (gnus-buffer-live-p gnus-article-buffer)
9306                  (with-current-buffer gnus-article-buffer
9307                    (prog1
9308                        gnus-article-mime-handles
9309                      (setq gnus-article-mime-handles nil))))))
9310      (t
9311       (setq force t)))
9312     (when (and raw (not force)
9313                (member gnus-newsgroup-name '("nndraft:delayed"
9314                                              "nndraft:drafts"
9315                                              "nndraft:queue")))
9316       (error "Can't edit the raw article in group %s"
9317              gnus-newsgroup-name))
9318     (save-excursion
9319       (set-buffer gnus-summary-buffer)
9320       (let ((mail-parse-charset gnus-newsgroup-charset)
9321             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9322         (gnus-set-global-variables)
9323         (when (and (not force)
9324                    (gnus-group-read-only-p))
9325           (error "The current newsgroup does not support article editing"))
9326         (gnus-summary-show-article t)
9327         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9328           (with-current-buffer gnus-article-buffer
9329             (mm-enable-multibyte)))
9330         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9331             (setq raw t))
9332         (gnus-article-edit-article
9333          (if raw 'ignore
9334            `(lambda ()
9335               (let ((mbl mml-buffer-list))
9336                 (setq mml-buffer-list nil)
9337                 (mime-to-mml ,'current-handles)
9338                 (let ((mbl1 mml-buffer-list))
9339                   (setq mml-buffer-list mbl)
9340                   (set (make-local-variable 'mml-buffer-list) mbl1))
9341                 (gnus-make-local-hook 'kill-buffer-hook)
9342                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9343          `(lambda (no-highlight)
9344             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9345                   (message-options message-options)
9346                   (message-options-set-recipient)
9347                   (mail-parse-ignored-charsets
9348                    ',gnus-newsgroup-ignored-charsets))
9349               ,(if (not raw) '(progn
9350                                 (mml-to-mime)
9351                                 (mml-destroy-buffers)
9352                                 (remove-hook 'kill-buffer-hook
9353                                              'mml-destroy-buffers t)
9354                                 (kill-local-variable 'mml-buffer-list)))
9355               (gnus-summary-edit-article-done
9356                ,(or (mail-header-references gnus-current-headers) "")
9357                ,(gnus-group-read-only-p)
9358                ,gnus-summary-buffer no-highlight))))))))
9359
9360 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9361
9362 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9363                                                  no-highlight)
9364   "Make edits to the current article permanent."
9365   (interactive)
9366   (save-excursion
9367    ;; The buffer restriction contains the entire article if it exists.
9368     (when (article-goto-body)
9369       (let ((lines (count-lines (point) (point-max)))
9370             (length (- (point-max) (point)))
9371             (case-fold-search t)
9372             (body (copy-marker (point))))
9373         (goto-char (point-min))
9374         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9375           (delete-region (match-beginning 1) (match-end 1))
9376           (insert (number-to-string length)))
9377         (goto-char (point-min))
9378         (when (re-search-forward
9379                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9380           (delete-region (match-beginning 1) (match-end 1))
9381           (insert (number-to-string length)))
9382         (goto-char (point-min))
9383         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9384           (delete-region (match-beginning 1) (match-end 1))
9385           (insert (number-to-string lines))))))
9386   ;; Replace the article.
9387   (let ((buf (current-buffer)))
9388     (with-temp-buffer
9389       (insert-buffer-substring buf)
9390
9391       (if (and (not read-only)
9392                (not (gnus-request-replace-article
9393                      (cdr gnus-article-current) (car gnus-article-current)
9394                      (current-buffer) t)))
9395           (error "Couldn't replace article")
9396         ;; Update the summary buffer.
9397         (if (and references
9398                  (equal (message-tokenize-header references " ")
9399                         (message-tokenize-header
9400                          (or (message-fetch-field "references") "") " ")))
9401             ;; We only have to update this line.
9402             (save-excursion
9403               (save-restriction
9404                 (message-narrow-to-head)
9405                 (let ((head (buffer-string))
9406                       header)
9407                   (with-temp-buffer
9408                     (insert (format "211 %d Article retrieved.\n"
9409                                     (cdr gnus-article-current)))
9410                     (insert head)
9411                     (insert ".\n")
9412                     (let ((nntp-server-buffer (current-buffer)))
9413                       (setq header (car (gnus-get-newsgroup-headers
9414                                          nil t))))
9415                     (save-excursion
9416                       (set-buffer gnus-summary-buffer)
9417                       (gnus-data-set-header
9418                        (gnus-data-find (cdr gnus-article-current))
9419                        header)
9420                       (gnus-summary-update-article-line
9421                        (cdr gnus-article-current) header)
9422                       (if (gnus-summary-goto-subject
9423                            (cdr gnus-article-current) nil t)
9424                           (gnus-summary-update-secondary-mark
9425                            (cdr gnus-article-current))))))))
9426           ;; Update threads.
9427           (set-buffer (or buffer gnus-summary-buffer))
9428           (gnus-summary-update-article (cdr gnus-article-current))
9429           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9430               (gnus-summary-update-secondary-mark
9431                (cdr gnus-article-current))))
9432         ;; Prettify the article buffer again.
9433         (unless no-highlight
9434           (save-excursion
9435             (set-buffer gnus-article-buffer)
9436             ;;;!!! Fix this -- article should be rehighlighted.
9437             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9438             (set-buffer gnus-original-article-buffer)
9439             (gnus-request-article
9440              (cdr gnus-article-current)
9441              (car gnus-article-current) (current-buffer))))
9442         ;; Prettify the summary buffer line.
9443         (when (gnus-visual-p 'summary-highlight 'highlight)
9444           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9445
9446 (defun gnus-summary-edit-wash (key)
9447   "Perform editing command KEY in the article buffer."
9448   (interactive
9449    (list
9450     (progn
9451       (message "%s" (concat (this-command-keys) "- "))
9452       (read-char))))
9453   (message "")
9454   (gnus-summary-edit-article)
9455   (execute-kbd-macro (concat (this-command-keys) key))
9456   (gnus-article-edit-done))
9457
9458 ;;; Respooling
9459
9460 (defun gnus-summary-respool-query (&optional silent trace)
9461   "Query where the respool algorithm would put this article."
9462   (interactive)
9463   (let (gnus-mark-article-hook)
9464     (gnus-summary-select-article)
9465     (save-excursion
9466       (set-buffer gnus-original-article-buffer)
9467       (save-restriction
9468         (message-narrow-to-head)
9469         (let ((groups (nnmail-article-group 'identity trace)))
9470           (unless silent
9471             (if groups
9472                 (message "This message would go to %s"
9473                          (mapconcat 'car groups ", "))
9474               (message "This message would go to no groups"))
9475             groups))))))
9476
9477 (defun gnus-summary-respool-trace ()
9478   "Trace where the respool algorithm would put this article.
9479 Display a buffer showing all fancy splitting patterns which matched."
9480   (interactive)
9481   (gnus-summary-respool-query nil t))
9482
9483 ;; Summary marking commands.
9484
9485 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9486   "Mark articles which has the same subject as read, and then select the next.
9487 If UNMARK is positive, remove any kind of mark.
9488 If UNMARK is negative, tick articles."
9489   (interactive "P")
9490   (when unmark
9491     (setq unmark (prefix-numeric-value unmark)))
9492   (let ((count
9493          (gnus-summary-mark-same-subject
9494           (gnus-summary-article-subject) unmark)))
9495     ;; Select next unread article.  If auto-select-same mode, should
9496     ;; select the first unread article.
9497     (gnus-summary-next-article t (and gnus-auto-select-same
9498                                       (gnus-summary-article-subject)))
9499     (gnus-message 7 "%d article%s marked as %s"
9500                   count (if (= count 1) " is" "s are")
9501                   (if unmark "unread" "read"))))
9502
9503 (defun gnus-summary-kill-same-subject (&optional unmark)
9504   "Mark articles which has the same subject as read.
9505 If UNMARK is positive, remove any kind of mark.
9506 If UNMARK is negative, tick articles."
9507   (interactive "P")
9508   (when unmark
9509     (setq unmark (prefix-numeric-value unmark)))
9510   (let ((count
9511          (gnus-summary-mark-same-subject
9512           (gnus-summary-article-subject) unmark)))
9513     ;; If marked as read, go to next unread subject.
9514     (when (null unmark)
9515       ;; Go to next unread subject.
9516       (gnus-summary-next-subject 1 t))
9517     (gnus-message 7 "%d articles are marked as %s"
9518                   count (if unmark "unread" "read"))))
9519
9520 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9521   "Mark articles with same SUBJECT as read, and return marked number.
9522 If optional argument UNMARK is positive, remove any kinds of marks.
9523 If optional argument UNMARK is negative, mark articles as unread instead."
9524   (let ((count 1))
9525     (save-excursion
9526       (cond
9527        ((null unmark)                   ; Mark as read.
9528         (while (and
9529                 (progn
9530                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9531                   (gnus-summary-show-thread) t)
9532                 (gnus-summary-find-subject subject))
9533           (setq count (1+ count))))
9534        ((> unmark 0)                    ; Tick.
9535         (while (and
9536                 (progn
9537                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9538                   (gnus-summary-show-thread) t)
9539                 (gnus-summary-find-subject subject))
9540           (setq count (1+ count))))
9541        (t                               ; Mark as unread.
9542         (while (and
9543                 (progn
9544                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9545                   (gnus-summary-show-thread) t)
9546                 (gnus-summary-find-subject subject))
9547           (setq count (1+ count)))))
9548       (gnus-set-mode-line 'summary)
9549       ;; Return the number of marked articles.
9550       count)))
9551
9552 (defun gnus-summary-mark-as-processable (n &optional unmark)
9553   "Set the process mark on the next N articles.
9554 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9555 the process mark instead.  The difference between N and the actual
9556 number of articles marked is returned."
9557   (interactive "P")
9558   (if (and (null n) (gnus-region-active-p))
9559       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9560     (setq n (prefix-numeric-value n))
9561     (let ((backward (< n 0))
9562           (n (abs n)))
9563       (while (and
9564               (> n 0)
9565               (if unmark
9566                   (gnus-summary-remove-process-mark
9567                    (gnus-summary-article-number))
9568                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9569               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9570         (setq n (1- n)))
9571       (when (/= 0 n)
9572         (gnus-message 7 "No more articles"))
9573       (gnus-summary-recenter)
9574       (gnus-summary-position-point)
9575       n)))
9576
9577 (defun gnus-summary-unmark-as-processable (n)
9578   "Remove the process mark from the next N articles.
9579 If N is negative, unmark backward instead.  The difference between N and
9580 the actual number of articles unmarked is returned."
9581   (interactive "P")
9582   (gnus-summary-mark-as-processable n t))
9583
9584 (defun gnus-summary-unmark-all-processable ()
9585   "Remove the process mark from all articles."
9586   (interactive)
9587   (save-excursion
9588     (while gnus-newsgroup-processable
9589       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9590   (gnus-summary-position-point))
9591
9592 (defun gnus-summary-add-mark (article type)
9593   "Mark ARTICLE with a mark of TYPE."
9594   (let ((vtype (car (assq type gnus-article-mark-lists)))
9595         var)
9596     (if (not vtype)
9597         (error "No such mark type: %s" type)
9598       (setq var (intern (format "gnus-newsgroup-%s" type)))
9599       (set var (cons article (symbol-value var)))
9600       (if (memq type '(processable cached replied forwarded recent saved))
9601           (gnus-summary-update-secondary-mark article)
9602         ;;; !!! This is bogus.  We should find out what primary
9603         ;;; !!! mark we want to set.
9604         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9605
9606 (defun gnus-summary-mark-as-expirable (n)
9607   "Mark N articles forward as expirable.
9608 If N is negative, mark backward instead.  The difference between N and
9609 the actual number of articles marked is returned."
9610   (interactive "p")
9611   (gnus-summary-mark-forward n gnus-expirable-mark))
9612
9613 (defun gnus-summary-mark-as-spam (n)
9614   "Mark N articles forward as spam.
9615 If N is negative, mark backward instead.  The difference between N and
9616 the actual number of articles marked is returned."
9617   (interactive "p")
9618   (gnus-summary-mark-forward n gnus-spam-mark))
9619
9620 (defun gnus-summary-mark-article-as-replied (article)
9621   "Mark ARTICLE as replied to and update the summary line.
9622 ARTICLE can also be a list of articles."
9623   (interactive (list (gnus-summary-article-number)))
9624   (let ((articles (if (listp article) article (list article))))
9625     (dolist (article articles)
9626       (unless (numberp article)
9627         (error "%s is not a number" article))
9628       (push article gnus-newsgroup-replied)
9629       (let ((buffer-read-only nil))
9630         (when (gnus-summary-goto-subject article nil t)
9631           (gnus-summary-update-secondary-mark article))))))
9632
9633 (defun gnus-summary-mark-article-as-forwarded (article)
9634   "Mark ARTICLE as forwarded and update the summary line.
9635 ARTICLE can also be a list of articles."
9636   (let ((articles (if (listp article) article (list article))))
9637     (dolist (article articles)
9638       (push article gnus-newsgroup-forwarded)
9639       (let ((buffer-read-only nil))
9640         (when (gnus-summary-goto-subject article nil t)
9641           (gnus-summary-update-secondary-mark article))))))
9642
9643 (defun gnus-summary-set-bookmark (article)
9644   "Set a bookmark in current article."
9645   (interactive (list (gnus-summary-article-number)))
9646   (when (or (not (get-buffer gnus-article-buffer))
9647             (not gnus-current-article)
9648             (not gnus-article-current)
9649             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9650     (error "No current article selected"))
9651   ;; Remove old bookmark, if one exists.
9652   (gnus-pull article gnus-newsgroup-bookmarks)
9653   ;; Set the new bookmark, which is on the form
9654   ;; (article-number . line-number-in-body).
9655   (push
9656    (cons article
9657          (save-excursion
9658            (set-buffer gnus-article-buffer)
9659            (count-lines
9660             (min (point)
9661                  (save-excursion
9662                    (article-goto-body)
9663                    (point)))
9664             (point))))
9665    gnus-newsgroup-bookmarks)
9666   (gnus-message 6 "A bookmark has been added to the current article."))
9667
9668 (defun gnus-summary-remove-bookmark (article)
9669   "Remove the bookmark from the current article."
9670   (interactive (list (gnus-summary-article-number)))
9671   ;; Remove old bookmark, if one exists.
9672   (if (not (assq article gnus-newsgroup-bookmarks))
9673       (gnus-message 6 "No bookmark in current article.")
9674     (gnus-pull article gnus-newsgroup-bookmarks)
9675     (gnus-message 6 "Removed bookmark.")))
9676
9677 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9678 (defun gnus-summary-mark-as-dormant (n)
9679   "Mark N articles forward as dormant.
9680 If N is negative, mark backward instead.  The difference between N and
9681 the actual number of articles marked is returned."
9682   (interactive "p")
9683   (gnus-summary-mark-forward n gnus-dormant-mark))
9684
9685 (defun gnus-summary-set-process-mark (article)
9686   "Set the process mark on ARTICLE and update the summary line."
9687   (setq gnus-newsgroup-processable
9688         (cons article
9689               (delq article gnus-newsgroup-processable)))
9690   (when (gnus-summary-goto-subject article)
9691     (gnus-summary-show-thread)
9692     (gnus-summary-goto-subject article)
9693     (gnus-summary-update-secondary-mark article)))
9694
9695 (defun gnus-summary-remove-process-mark (article)
9696   "Remove the process mark from ARTICLE and update the summary line."
9697   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9698   (when (gnus-summary-goto-subject article)
9699     (gnus-summary-show-thread)
9700     (gnus-summary-goto-subject article)
9701     (gnus-summary-update-secondary-mark article)))
9702
9703 (defun gnus-summary-set-saved-mark (article)
9704   "Set the process mark on ARTICLE and update the summary line."
9705   (push article gnus-newsgroup-saved)
9706   (when (gnus-summary-goto-subject article)
9707     (gnus-summary-update-secondary-mark article)))
9708
9709 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9710   "Mark N articles as read forwards.
9711 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9712 The difference between N and the actual number of articles marked is
9713 returned.
9714 If NO-EXPIRE, auto-expiry will be inhibited."
9715   (interactive "p")
9716   (gnus-summary-show-thread)
9717   (let ((backward (< n 0))
9718         (gnus-summary-goto-unread
9719          (and gnus-summary-goto-unread
9720               (not (eq gnus-summary-goto-unread 'never))
9721               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9722                                     gnus-ticked-mark gnus-dormant-mark)))))
9723         (n (abs n))
9724         (mark (or mark gnus-del-mark)))
9725     (while (and (> n 0)
9726                 (gnus-summary-mark-article nil mark no-expire)
9727                 (zerop (gnus-summary-next-subject
9728                         (if backward -1 1)
9729                         (and gnus-summary-goto-unread
9730                              (not (eq gnus-summary-goto-unread 'never)))
9731                         t)))
9732       (setq n (1- n)))
9733     (when (/= 0 n)
9734       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9735     (gnus-summary-recenter)
9736     (gnus-summary-position-point)
9737     (gnus-set-mode-line 'summary)
9738     n))
9739
9740 (defun gnus-summary-mark-article-as-read (mark)
9741   "Mark the current article quickly as read with MARK."
9742   (let ((article (gnus-summary-article-number)))
9743     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9744     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9745     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9746     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9747     (push (cons article mark) gnus-newsgroup-reads)
9748     ;; Possibly remove from cache, if that is used.
9749     (when gnus-use-cache
9750       (gnus-cache-enter-remove-article article))
9751     ;; Allow the backend to change the mark.
9752     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9753     ;; Check for auto-expiry.
9754     (when (and gnus-newsgroup-auto-expire
9755                (memq mark gnus-auto-expirable-marks))
9756       (setq mark gnus-expirable-mark)
9757       ;; Let the backend know about the mark change.
9758       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9759       (push article gnus-newsgroup-expirable))
9760     ;; Set the mark in the buffer.
9761     (gnus-summary-update-mark mark 'unread)
9762     t))
9763
9764 (defun gnus-summary-mark-article-as-unread (mark)
9765   "Mark the current article quickly as unread with MARK."
9766   (let* ((article (gnus-summary-article-number))
9767          (old-mark (gnus-summary-article-mark article)))
9768     ;; Allow the backend to change the mark.
9769     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9770     (if (eq mark old-mark)
9771         t
9772       (if (<= article 0)
9773           (progn
9774             (gnus-error 1 "Can't mark negative article numbers")
9775             nil)
9776         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9777         (setq gnus-newsgroup-spam-marked
9778               (delq article gnus-newsgroup-spam-marked))
9779         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9780         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9781         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9782         (cond ((= mark gnus-ticked-mark)
9783                (setq gnus-newsgroup-marked
9784                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9785                                               article)))
9786               ((= mark gnus-spam-mark)
9787                (setq gnus-newsgroup-spam-marked
9788                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9789                                               article)))
9790               ((= mark gnus-dormant-mark)
9791                (setq gnus-newsgroup-dormant
9792                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9793                                               article)))
9794               (t
9795                (setq gnus-newsgroup-unreads
9796                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9797                                               article))))
9798         (gnus-pull article gnus-newsgroup-reads)
9799
9800         ;; See whether the article is to be put in the cache.
9801         (and gnus-use-cache
9802              (vectorp (gnus-summary-article-header article))
9803              (save-excursion
9804                (gnus-cache-possibly-enter-article
9805                 gnus-newsgroup-name article
9806                 (= mark gnus-ticked-mark)
9807                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9808
9809         ;; Fix the mark.
9810         (gnus-summary-update-mark mark 'unread)
9811         t))))
9812
9813 (defun gnus-summary-mark-article (&optional article mark no-expire)
9814   "Mark ARTICLE with MARK.  MARK can be any character.
9815 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9816 `??' (dormant) and `?E' (expirable).
9817 If MARK is nil, then the default character `?r' is used.
9818 If ARTICLE is nil, then the article on the current line will be
9819 marked.
9820 If NO-EXPIRE, auto-expiry will be inhibited."
9821   ;; The mark might be a string.
9822   (when (stringp mark)
9823     (setq mark (aref mark 0)))
9824   ;; If no mark is given, then we check auto-expiring.
9825   (when (null mark)
9826     (setq mark gnus-del-mark))
9827   (when (and (not no-expire)
9828              gnus-newsgroup-auto-expire
9829              (memq mark gnus-auto-expirable-marks))
9830     (setq mark gnus-expirable-mark))
9831   (let ((article (or article (gnus-summary-article-number)))
9832         (old-mark (gnus-summary-article-mark article)))
9833     ;; Allow the backend to change the mark.
9834     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9835     (if (eq mark old-mark)
9836         t
9837       (unless article
9838         (error "No article on current line"))
9839       (if (not (if (or (= mark gnus-unread-mark)
9840                        (= mark gnus-ticked-mark)
9841                        (= mark gnus-spam-mark)
9842                        (= mark gnus-dormant-mark))
9843                    (gnus-mark-article-as-unread article mark)
9844                  (gnus-mark-article-as-read article mark)))
9845           t
9846         ;; See whether the article is to be put in the cache.
9847         (and gnus-use-cache
9848              (not (= mark gnus-canceled-mark))
9849              (vectorp (gnus-summary-article-header article))
9850              (save-excursion
9851                (gnus-cache-possibly-enter-article
9852                 gnus-newsgroup-name article
9853                 (= mark gnus-ticked-mark)
9854                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9855
9856         (when (gnus-summary-goto-subject article nil t)
9857           (let ((buffer-read-only nil))
9858             (gnus-summary-show-thread)
9859             ;; Fix the mark.
9860             (gnus-summary-update-mark mark 'unread)
9861             t))))))
9862
9863 (defun gnus-summary-update-secondary-mark (article)
9864   "Update the secondary (read, process, cache) mark."
9865   (gnus-summary-update-mark
9866    (cond ((memq article gnus-newsgroup-processable)
9867           gnus-process-mark)
9868          ((memq article gnus-newsgroup-cached)
9869           gnus-cached-mark)
9870          ((memq article gnus-newsgroup-replied)
9871           gnus-replied-mark)
9872          ((memq article gnus-newsgroup-forwarded)
9873           gnus-forwarded-mark)
9874          ((memq article gnus-newsgroup-saved)
9875           gnus-saved-mark)
9876          ((memq article gnus-newsgroup-recent)
9877           gnus-recent-mark)
9878          ((memq article gnus-newsgroup-unseen)
9879           gnus-unseen-mark)
9880          (t gnus-no-mark))
9881    'replied)
9882   (when (gnus-visual-p 'summary-highlight 'highlight)
9883     (gnus-run-hooks 'gnus-summary-update-hook))
9884   t)
9885
9886 (defun gnus-summary-update-download-mark (article)
9887   "Update the download mark."
9888   (gnus-summary-update-mark
9889    (cond ((memq article gnus-newsgroup-undownloaded) 
9890           gnus-undownloaded-mark)
9891          (gnus-newsgroup-agentized
9892           gnus-downloaded-mark)
9893          (t
9894           gnus-no-mark))
9895    'download)
9896   (gnus-summary-update-line t)
9897   t)
9898
9899 (defun gnus-summary-update-mark (mark type)
9900   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9901         (buffer-read-only nil))
9902     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9903     (when forward
9904       (when (looking-at "\r")
9905         (incf forward))
9906       (when (<= (+ forward (point)) (point-max))
9907         ;; Go to the right position on the line.
9908         (goto-char (+ forward (point)))
9909         ;; Replace the old mark with the new mark.
9910         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9911         ;; Optionally update the marks by some user rule.
9912         (when (eq type 'unread)
9913           (gnus-data-set-mark
9914            (gnus-data-find (gnus-summary-article-number)) mark)
9915           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9916
9917 (defun gnus-mark-article-as-read (article &optional mark)
9918   "Enter ARTICLE in the pertinent lists and remove it from others."
9919   ;; Make the article expirable.
9920   (let ((mark (or mark gnus-del-mark)))
9921     (setq gnus-newsgroup-expirable
9922           (if (= mark gnus-expirable-mark)
9923               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9924             (delq article gnus-newsgroup-expirable)))
9925     ;; Remove from unread and marked lists.
9926     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9927     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9928     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9929     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9930     (push (cons article mark) gnus-newsgroup-reads)
9931     ;; Possibly remove from cache, if that is used.
9932     (when gnus-use-cache
9933       (gnus-cache-enter-remove-article article))
9934     t))
9935
9936 (defun gnus-mark-article-as-unread (article &optional mark)
9937   "Enter ARTICLE in the pertinent lists and remove it from others."
9938   (let ((mark (or mark gnus-ticked-mark)))
9939     (if (<= article 0)
9940         (progn
9941           (gnus-error 1 "Can't mark negative article numbers")
9942           nil)
9943       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9944             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9945             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9946             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9947             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9948
9949       ;; Unsuppress duplicates?
9950       (when gnus-suppress-duplicates
9951         (gnus-dup-unsuppress-article article))
9952
9953       (cond ((= mark gnus-ticked-mark)
9954              (setq gnus-newsgroup-marked
9955                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9956             ((= mark gnus-spam-mark)
9957              (setq gnus-newsgroup-spam-marked
9958                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9959                                             article)))
9960             ((= mark gnus-dormant-mark)
9961              (setq gnus-newsgroup-dormant
9962                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9963             (t
9964              (setq gnus-newsgroup-unreads
9965                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9966       (gnus-pull article gnus-newsgroup-reads)
9967       t)))
9968
9969 (defalias 'gnus-summary-mark-as-unread-forward
9970   'gnus-summary-tick-article-forward)
9971 (make-obsolete 'gnus-summary-mark-as-unread-forward
9972                'gnus-summary-tick-article-forward)
9973 (defun gnus-summary-tick-article-forward (n)
9974   "Tick N articles forwards.
9975 If N is negative, tick backwards instead.
9976 The difference between N and the number of articles ticked is returned."
9977   (interactive "p")
9978   (gnus-summary-mark-forward n gnus-ticked-mark))
9979
9980 (defalias 'gnus-summary-mark-as-unread-backward
9981   'gnus-summary-tick-article-backward)
9982 (make-obsolete 'gnus-summary-mark-as-unread-backward
9983                'gnus-summary-tick-article-backward)
9984 (defun gnus-summary-tick-article-backward (n)
9985   "Tick N articles backwards.
9986 The difference between N and the number of articles ticked is returned."
9987   (interactive "p")
9988   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9989
9990 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9991 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9992 (defun gnus-summary-tick-article (&optional article clear-mark)
9993   "Mark current article as unread.
9994 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9995 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9996   (interactive)
9997   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9998                                        gnus-ticked-mark)))
9999
10000 (defun gnus-summary-mark-as-read-forward (n)
10001   "Mark N articles as read forwards.
10002 If N is negative, mark backwards instead.
10003 The difference between N and the actual number of articles marked is
10004 returned."
10005   (interactive "p")
10006   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10007
10008 (defun gnus-summary-mark-as-read-backward (n)
10009   "Mark the N articles as read backwards.
10010 The difference between N and the actual number of articles marked is
10011 returned."
10012   (interactive "p")
10013   (gnus-summary-mark-forward
10014    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10015
10016 (defun gnus-summary-mark-as-read (&optional article mark)
10017   "Mark current article as read.
10018 ARTICLE specifies the article to be marked as read.
10019 MARK specifies a string to be inserted at the beginning of the line."
10020   (gnus-summary-mark-article article mark))
10021
10022 (defun gnus-summary-clear-mark-forward (n)
10023   "Clear marks from N articles forward.
10024 If N is negative, clear backward instead.
10025 The difference between N and the number of marks cleared is returned."
10026   (interactive "p")
10027   (gnus-summary-mark-forward n gnus-unread-mark))
10028
10029 (defun gnus-summary-clear-mark-backward (n)
10030   "Clear marks from N articles backward.
10031 The difference between N and the number of marks cleared is returned."
10032   (interactive "p")
10033   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10034
10035 (defun gnus-summary-mark-unread-as-read ()
10036   "Intended to be used by `gnus-summary-mark-article-hook'."
10037   (when (memq gnus-current-article gnus-newsgroup-unreads)
10038     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10039
10040 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10041   "Intended to be used by `gnus-summary-mark-article-hook'."
10042   (let ((mark (gnus-summary-article-mark)))
10043     (when (or (gnus-unread-mark-p mark)
10044               (gnus-read-mark-p mark))
10045       (gnus-summary-mark-article gnus-current-article
10046                                  (or new-mark gnus-read-mark)))))
10047
10048 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10049   "Intended to be used by `gnus-summary-mark-article-hook'."
10050   (let ((mark (gnus-summary-article-mark)))
10051     (when (or (gnus-unread-mark-p mark)
10052               (gnus-read-mark-p mark))
10053       (gnus-summary-mark-article (gnus-summary-article-number)
10054                                  (or new-mark gnus-read-mark)))))
10055
10056 (defun gnus-summary-mark-unread-as-ticked ()
10057   "Intended to be used by `gnus-summary-mark-article-hook'."
10058   (when (memq gnus-current-article gnus-newsgroup-unreads)
10059     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10060
10061 (defun gnus-summary-mark-region-as-read (point mark all)
10062   "Mark all unread articles between point and mark as read.
10063 If given a prefix, mark all articles between point and mark as read,
10064 even ticked and dormant ones."
10065   (interactive "r\nP")
10066   (save-excursion
10067     (let (article)
10068       (goto-char point)
10069       (beginning-of-line)
10070       (while (and
10071               (< (point) mark)
10072               (progn
10073                 (when (or all
10074                           (memq (setq article (gnus-summary-article-number))
10075                                 gnus-newsgroup-unreads))
10076                   (gnus-summary-mark-article article gnus-del-mark))
10077                 t)
10078               (gnus-summary-find-next))))))
10079
10080 (defun gnus-summary-mark-below (score mark)
10081   "Mark articles with score less than SCORE with MARK."
10082   (interactive "P\ncMark: ")
10083   (setq score (if score
10084                   (prefix-numeric-value score)
10085                 (or gnus-summary-default-score 0)))
10086   (save-excursion
10087     (set-buffer gnus-summary-buffer)
10088     (goto-char (point-min))
10089     (while
10090         (progn
10091           (and (< (gnus-summary-article-score) score)
10092                (gnus-summary-mark-article nil mark))
10093           (gnus-summary-find-next)))))
10094
10095 (defun gnus-summary-kill-below (&optional score)
10096   "Mark articles with score below SCORE as read."
10097   (interactive "P")
10098   (gnus-summary-mark-below score gnus-killed-mark))
10099
10100 (defun gnus-summary-clear-above (&optional score)
10101   "Clear all marks from articles with score above SCORE."
10102   (interactive "P")
10103   (gnus-summary-mark-above score gnus-unread-mark))
10104
10105 (defun gnus-summary-tick-above (&optional score)
10106   "Tick all articles with score above SCORE."
10107   (interactive "P")
10108   (gnus-summary-mark-above score gnus-ticked-mark))
10109
10110 (defun gnus-summary-mark-above (score mark)
10111   "Mark articles with score over SCORE with MARK."
10112   (interactive "P\ncMark: ")
10113   (setq score (if score
10114                   (prefix-numeric-value score)
10115                 (or gnus-summary-default-score 0)))
10116   (save-excursion
10117     (set-buffer gnus-summary-buffer)
10118     (goto-char (point-min))
10119     (while (and (progn
10120                   (when (> (gnus-summary-article-score) score)
10121                     (gnus-summary-mark-article nil mark))
10122                   t)
10123                 (gnus-summary-find-next)))))
10124
10125 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10126 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10127 (defun gnus-summary-limit-include-expunged (&optional no-error)
10128   "Display all the hidden articles that were expunged for low scores."
10129   (interactive)
10130   (let ((buffer-read-only nil))
10131     (let ((scored gnus-newsgroup-scored)
10132           headers h)
10133       (while scored
10134         (unless (gnus-summary-article-header (caar scored))
10135           (and (setq h (gnus-number-to-header (caar scored)))
10136                (< (cdar scored) gnus-summary-expunge-below)
10137                (push h headers)))
10138         (setq scored (cdr scored)))
10139       (if (not headers)
10140           (when (not no-error)
10141             (error "No expunged articles hidden"))
10142         (goto-char (point-min))
10143         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10144         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10145         (mapcar (lambda (x) (push (mail-header-number x)
10146                                   gnus-newsgroup-limit))
10147                 headers)
10148         (gnus-summary-prepare-unthreaded (nreverse headers))
10149         (goto-char (point-min))
10150         (gnus-summary-position-point)
10151         t))))
10152
10153 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10154   "Mark all unread articles in this newsgroup as read.
10155 If prefix argument ALL is non-nil, ticked and dormant articles will
10156 also be marked as read.
10157 If QUIETLY is non-nil, no questions will be asked.
10158
10159 If TO-HERE is non-nil, it should be a point in the buffer.  All
10160 articles before (after, if REVERSE is set) this point will be marked
10161 as read.
10162
10163 Note that this function will only catch up the unread article
10164 in the current summary buffer limitation.
10165
10166 The number of articles marked as read is returned."
10167   (interactive "P")
10168   (prog1
10169       (save-excursion
10170         (when (or quietly
10171                   (not gnus-interactive-catchup) ;Without confirmation?
10172                   gnus-expert-user
10173                   (gnus-y-or-n-p
10174                    (if all
10175                        "Mark absolutely all articles as read? "
10176                      "Mark all unread articles as read? ")))
10177           (if (and not-mark
10178                    (not gnus-newsgroup-adaptive)
10179                    (not gnus-newsgroup-auto-expire)
10180                    (not gnus-suppress-duplicates)
10181                    (or (not gnus-use-cache)
10182                        (eq gnus-use-cache 'passive)))
10183               (progn
10184                 (when all
10185                   (setq gnus-newsgroup-marked nil
10186                         gnus-newsgroup-spam-marked nil
10187                         gnus-newsgroup-dormant nil))
10188                 (setq gnus-newsgroup-unreads
10189                       (gnus-sorted-nunion
10190                        (gnus-intersection gnus-newsgroup-unreads
10191                                           gnus-newsgroup-downloadable)
10192                        gnus-newsgroup-unfetched)))
10193             ;; We actually mark all articles as canceled, which we
10194             ;; have to do when using auto-expiry or adaptive scoring.
10195             (gnus-summary-show-all-threads)
10196             (if (and to-here reverse)
10197                 (progn
10198                   (goto-char to-here)
10199                   (gnus-summary-mark-current-read-and-unread-as-read
10200                    gnus-catchup-mark)
10201                   (while (gnus-summary-find-next (not all))
10202                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10203               (when (gnus-summary-first-subject (not all))
10204                 (while (and
10205                         (if to-here (< (point) to-here) t)
10206                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10207                         (gnus-summary-find-next (not all))))))
10208             (gnus-set-mode-line 'summary))
10209           t))
10210     (gnus-summary-position-point)))
10211
10212 (defun gnus-summary-catchup-to-here (&optional all)
10213   "Mark all unticked articles before the current one as read.
10214 If ALL is non-nil, also mark ticked and dormant articles as read."
10215   (interactive "P")
10216   (save-excursion
10217     (gnus-save-hidden-threads
10218       (let ((beg (point)))
10219         ;; We check that there are unread articles.
10220         (when (or all (gnus-summary-find-prev))
10221           (gnus-summary-catchup all t beg)))))
10222   (gnus-summary-position-point))
10223
10224 (defun gnus-summary-catchup-from-here (&optional all)
10225   "Mark all unticked articles after (and including) the current one as read.
10226 If ALL is non-nil, also mark ticked and dormant articles as read."
10227   (interactive "P")
10228   (save-excursion
10229     (gnus-save-hidden-threads
10230       (let ((beg (point)))
10231         ;; We check that there are unread articles.
10232         (when (or all (gnus-summary-find-next))
10233           (gnus-summary-catchup all t beg nil t)))))
10234   (gnus-summary-position-point))
10235
10236 (defun gnus-summary-catchup-all (&optional quietly)
10237   "Mark all articles in this newsgroup as read.
10238 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10239 instead, which marks only unread articles as read."
10240   (interactive "P")
10241   (gnus-summary-catchup t quietly))
10242
10243 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10244   "Mark all unread articles in this group as read, then exit.
10245 If prefix argument ALL is non-nil, all articles are marked as read.
10246 If QUIETLY is non-nil, no questions will be asked."
10247   (interactive "P")
10248   (when (gnus-summary-catchup all quietly nil 'fast)
10249     ;; Select next newsgroup or exit.
10250     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10251              (eq gnus-auto-select-next 'quietly))
10252         (gnus-summary-next-group nil)
10253       (gnus-summary-exit))))
10254
10255 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10256   "Mark all articles in this newsgroup as read, and then exit.
10257 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10258 instead, which marks only unread articles as read."
10259   (interactive "P")
10260   (gnus-summary-catchup-and-exit t quietly))
10261
10262 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10263   "Mark all articles in this group as read and select the next group.
10264 If given a prefix, mark all articles, unread as well as ticked, as
10265 read."
10266   (interactive "P")
10267   (save-excursion
10268     (gnus-summary-catchup all))
10269   (gnus-summary-next-group))
10270
10271 ;;;
10272 ;;; with article
10273 ;;;
10274
10275 (defmacro gnus-with-article (article &rest forms)
10276   "Select ARTICLE and perform FORMS in the original article buffer.
10277 Then replace the article with the result."
10278   `(progn
10279      ;; We don't want the article to be marked as read.
10280      (let (gnus-mark-article-hook)
10281        (gnus-summary-select-article t t nil ,article))
10282      (set-buffer gnus-original-article-buffer)
10283      ,@forms
10284      (if (not (gnus-check-backend-function
10285                'request-replace-article (car gnus-article-current)))
10286          (gnus-message 5 "Read-only group; not replacing")
10287        (unless (gnus-request-replace-article
10288                 ,article (car gnus-article-current)
10289                 (current-buffer) t)
10290          (error "Couldn't replace article")))
10291      ;; The cache and backlog have to be flushed somewhat.
10292      (when gnus-keep-backlog
10293        (gnus-backlog-remove-article
10294         (car gnus-article-current) (cdr gnus-article-current)))
10295      (when gnus-use-cache
10296        (gnus-cache-update-article
10297         (car gnus-article-current) (cdr gnus-article-current)))))
10298
10299 (put 'gnus-with-article 'lisp-indent-function 1)
10300 (put 'gnus-with-article 'edebug-form-spec '(form body))
10301
10302 ;; Thread-based commands.
10303
10304 (defun gnus-summary-articles-in-thread (&optional article)
10305   "Return a list of all articles in the current thread.
10306 If ARTICLE is non-nil, return all articles in the thread that starts
10307 with that article."
10308   (let* ((article (or article (gnus-summary-article-number)))
10309          (data (gnus-data-find-list article))
10310          (top-level (gnus-data-level (car data)))
10311          (top-subject
10312           (cond ((null gnus-thread-operation-ignore-subject)
10313                  (gnus-simplify-subject-re
10314                   (mail-header-subject (gnus-data-header (car data)))))
10315                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10316                  (gnus-simplify-subject-fuzzy
10317                   (mail-header-subject (gnus-data-header (car data)))))
10318                 (t nil)))
10319          (end-point (save-excursion
10320                       (if (gnus-summary-go-to-next-thread)
10321                           (point) (point-max))))
10322          articles)
10323     (while (and data
10324                 (< (gnus-data-pos (car data)) end-point))
10325       (when (or (not top-subject)
10326                 (string= top-subject
10327                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10328                              (gnus-simplify-subject-fuzzy
10329                               (mail-header-subject
10330                                (gnus-data-header (car data))))
10331                            (gnus-simplify-subject-re
10332                             (mail-header-subject
10333                              (gnus-data-header (car data)))))))
10334         (push (gnus-data-number (car data)) articles))
10335       (unless (and (setq data (cdr data))
10336                    (> (gnus-data-level (car data)) top-level))
10337         (setq data nil)))
10338     ;; Return the list of articles.
10339     (nreverse articles)))
10340
10341 (defun gnus-summary-rethread-current ()
10342   "Rethread the thread the current article is part of."
10343   (interactive)
10344   (let* ((gnus-show-threads t)
10345          (article (gnus-summary-article-number))
10346          (id (mail-header-id (gnus-summary-article-header)))
10347          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10348     (unless id
10349       (error "No article on the current line"))
10350     (gnus-rebuild-thread id)
10351     (gnus-summary-goto-subject article)))
10352
10353 (defun gnus-summary-reparent-thread ()
10354   "Make the current article child of the marked (or previous) article.
10355
10356 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10357 is non-nil or the Subject: of both articles are the same."
10358   (interactive)
10359   (unless (not (gnus-group-read-only-p))
10360     (error "The current newsgroup does not support article editing"))
10361   (unless (<= (length gnus-newsgroup-processable) 1)
10362     (error "No more than one article may be marked"))
10363   (save-window-excursion
10364     (let ((gnus-article-buffer " *reparent*")
10365           (current-article (gnus-summary-article-number))
10366           ;; First grab the marked article, otherwise one line up.
10367           (parent-article (if (not (null gnus-newsgroup-processable))
10368                               (car gnus-newsgroup-processable)
10369                             (save-excursion
10370                               (if (eq (forward-line -1) 0)
10371                                   (gnus-summary-article-number)
10372                                 (error "Beginning of summary buffer"))))))
10373       (unless (not (eq current-article parent-article))
10374         (error "An article may not be self-referential"))
10375       (let ((message-id (mail-header-id
10376                          (gnus-summary-article-header parent-article))))
10377         (unless (and message-id (not (equal message-id "")))
10378           (error "No message-id in desired parent"))
10379         (gnus-with-article current-article
10380           (save-restriction
10381             (goto-char (point-min))
10382             (message-narrow-to-head)
10383             (if (re-search-forward "^References: " nil t)
10384                 (progn
10385                   (re-search-forward "^[^ \t]" nil t)
10386                   (forward-line -1)
10387                   (end-of-line)
10388                   (insert " " message-id))
10389               (insert "References: " message-id "\n"))))
10390         (set-buffer gnus-summary-buffer)
10391         (gnus-summary-unmark-all-processable)
10392         (gnus-summary-update-article current-article)
10393         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10394             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10395         (gnus-summary-rethread-current)
10396         (gnus-message 3 "Article %d is now the child of article %d"
10397                       current-article parent-article)))))
10398
10399 (defun gnus-summary-toggle-threads (&optional arg)
10400   "Toggle showing conversation threads.
10401 If ARG is positive number, turn showing conversation threads on."
10402   (interactive "P")
10403   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10404     (setq gnus-show-threads
10405           (if (null arg) (not gnus-show-threads)
10406             (> (prefix-numeric-value arg) 0)))
10407     (gnus-summary-prepare)
10408     (gnus-summary-goto-subject current)
10409     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10410     (gnus-summary-position-point)))
10411
10412 (defun gnus-summary-show-all-threads ()
10413   "Show all threads."
10414   (interactive)
10415   (save-excursion
10416     (let ((buffer-read-only nil))
10417       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10418   (gnus-summary-position-point))
10419
10420 (defun gnus-summary-show-thread ()
10421   "Show thread subtrees.
10422 Returns nil if no thread was there to be shown."
10423   (interactive)
10424   (let ((buffer-read-only nil)
10425         (orig (point))
10426         (end (gnus-point-at-eol))
10427         ;; Leave point at bol
10428         (beg (progn (beginning-of-line) (point))))
10429     (prog1
10430         ;; Any hidden lines here?
10431         (search-forward "\r" end t)
10432       (subst-char-in-region beg end ?\^M ?\n t)
10433       (goto-char orig)
10434       (gnus-summary-position-point))))
10435
10436 (defun gnus-summary-maybe-hide-threads ()
10437   "If requested, hide the threads that should be hidden."
10438   (when (and gnus-show-threads
10439              gnus-thread-hide-subtree)
10440     (gnus-summary-hide-all-threads
10441      (if (or (consp gnus-thread-hide-subtree)
10442              (functionp gnus-thread-hide-subtree))
10443          (gnus-make-predicate gnus-thread-hide-subtree)
10444        nil))))
10445
10446 ;;; Hiding predicates.
10447
10448 (defun gnus-article-unread-p (header)
10449   (memq (mail-header-number header) gnus-newsgroup-unreads))
10450
10451 (defun gnus-article-unseen-p (header)
10452   (memq (mail-header-number header) gnus-newsgroup-unseen))
10453
10454 (defun gnus-map-articles (predicate articles)
10455   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10456   (apply 'gnus-or (mapcar predicate
10457                           (mapcar 'gnus-summary-article-header articles))))
10458
10459 (defun gnus-summary-hide-all-threads (&optional predicate)
10460   "Hide all thread subtrees.
10461 If PREDICATE is supplied, threads that satisfy this predicate
10462 will not be hidden."
10463   (interactive)
10464   (save-excursion
10465     (goto-char (point-min))
10466     (let ((end nil))
10467       (while (not end)
10468         (when (or (not predicate)
10469                   (gnus-map-articles
10470                    predicate (gnus-summary-article-children)))
10471             (gnus-summary-hide-thread))
10472         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10473   (gnus-summary-position-point))
10474
10475 (defun gnus-summary-hide-thread ()
10476   "Hide thread subtrees.
10477 If PREDICATE is supplied, threads that satisfy this predicate
10478 will not be hidden.
10479 Returns nil if no threads were there to be hidden."
10480   (interactive)
10481   (let ((buffer-read-only nil)
10482         (start (point))
10483         (article (gnus-summary-article-number)))
10484     (goto-char start)
10485     ;; Go forward until either the buffer ends or the subthread
10486     ;; ends.
10487     (when (and (not (eobp))
10488                (or (zerop (gnus-summary-next-thread 1 t))
10489                    (goto-char (point-max))))
10490       (prog1
10491           (if (and (> (point) start)
10492                    (search-backward "\n" start t))
10493               (progn
10494                 (subst-char-in-region start (point) ?\n ?\^M)
10495                 (gnus-summary-goto-subject article))
10496             (goto-char start)
10497             nil)))))
10498
10499 (defun gnus-summary-go-to-next-thread (&optional previous)
10500   "Go to the same level (or less) next thread.
10501 If PREVIOUS is non-nil, go to previous thread instead.
10502 Return the article number moved to, or nil if moving was impossible."
10503   (let ((level (gnus-summary-thread-level))
10504         (way (if previous -1 1))
10505         (beg (point)))
10506     (forward-line way)
10507     (while (and (not (eobp))
10508                 (< level (gnus-summary-thread-level)))
10509       (forward-line way))
10510     (if (eobp)
10511         (progn
10512           (goto-char beg)
10513           nil)
10514       (setq beg (point))
10515       (prog1
10516           (gnus-summary-article-number)
10517         (goto-char beg)))))
10518
10519 (defun gnus-summary-next-thread (n &optional silent)
10520   "Go to the same level next N'th thread.
10521 If N is negative, search backward instead.
10522 Returns the difference between N and the number of skips actually
10523 done.
10524
10525 If SILENT, don't output messages."
10526   (interactive "p")
10527   (let ((backward (< n 0))
10528         (n (abs n)))
10529     (while (and (> n 0)
10530                 (gnus-summary-go-to-next-thread backward))
10531       (decf n))
10532     (unless silent
10533       (gnus-summary-position-point))
10534     (when (and (not silent) (/= 0 n))
10535       (gnus-message 7 "No more threads"))
10536     n))
10537
10538 (defun gnus-summary-prev-thread (n)
10539   "Go to the same level previous N'th thread.
10540 Returns the difference between N and the number of skips actually
10541 done."
10542   (interactive "p")
10543   (gnus-summary-next-thread (- n)))
10544
10545 (defun gnus-summary-go-down-thread ()
10546   "Go down one level in the current thread."
10547   (let ((children (gnus-summary-article-children)))
10548     (when children
10549       (gnus-summary-goto-subject (car children)))))
10550
10551 (defun gnus-summary-go-up-thread ()
10552   "Go up one level in the current thread."
10553   (let ((parent (gnus-summary-article-parent)))
10554     (when parent
10555       (gnus-summary-goto-subject parent))))
10556
10557 (defun gnus-summary-down-thread (n)
10558   "Go down thread N steps.
10559 If N is negative, go up instead.
10560 Returns the difference between N and how many steps down that were
10561 taken."
10562   (interactive "p")
10563   (let ((up (< n 0))
10564         (n (abs n)))
10565     (while (and (> n 0)
10566                 (if up (gnus-summary-go-up-thread)
10567                   (gnus-summary-go-down-thread)))
10568       (setq n (1- n)))
10569     (gnus-summary-position-point)
10570     (when (/= 0 n)
10571       (gnus-message 7 "Can't go further"))
10572     n))
10573
10574 (defun gnus-summary-up-thread (n)
10575   "Go up thread N steps.
10576 If N is negative, go down instead.
10577 Returns the difference between N and how many steps down that were
10578 taken."
10579   (interactive "p")
10580   (gnus-summary-down-thread (- n)))
10581
10582 (defun gnus-summary-top-thread ()
10583   "Go to the top of the thread."
10584   (interactive)
10585   (while (gnus-summary-go-up-thread))
10586   (gnus-summary-article-number))
10587
10588 (defun gnus-summary-kill-thread (&optional unmark)
10589   "Mark articles under current thread as read.
10590 If the prefix argument is positive, remove any kinds of marks.
10591 If the prefix argument is negative, tick articles instead."
10592   (interactive "P")
10593   (when unmark
10594     (setq unmark (prefix-numeric-value unmark)))
10595   (let ((articles (gnus-summary-articles-in-thread)))
10596     (save-excursion
10597       ;; Expand the thread.
10598       (gnus-summary-show-thread)
10599       ;; Mark all the articles.
10600       (while articles
10601         (gnus-summary-goto-subject (car articles))
10602         (cond ((null unmark)
10603                (gnus-summary-mark-article-as-read gnus-killed-mark))
10604               ((> unmark 0)
10605                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10606               (t
10607                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10608         (setq articles (cdr articles))))
10609     ;; Hide killed subtrees.
10610     (and (null unmark)
10611          gnus-thread-hide-killed
10612          (gnus-summary-hide-thread))
10613     ;; If marked as read, go to next unread subject.
10614     (when (null unmark)
10615       ;; Go to next unread subject.
10616       (gnus-summary-next-subject 1 t)))
10617   (gnus-set-mode-line 'summary))
10618
10619 ;; Summary sorting commands
10620
10621 (defun gnus-summary-sort-by-number (&optional reverse)
10622   "Sort the summary buffer by article number.
10623 Argument REVERSE means reverse order."
10624   (interactive "P")
10625   (gnus-summary-sort 'number reverse))
10626
10627 (defun gnus-summary-sort-by-random (&optional reverse)
10628   "Randomize the order in the summary buffer.
10629 Argument REVERSE means to randomize in reverse order."
10630   (interactive "P")
10631   (gnus-summary-sort 'random reverse))
10632
10633 (defun gnus-summary-sort-by-author (&optional reverse)
10634   "Sort the summary buffer by author name alphabetically.
10635 If `case-fold-search' is non-nil, case of letters is ignored.
10636 Argument REVERSE means reverse order."
10637   (interactive "P")
10638   (gnus-summary-sort 'author reverse))
10639
10640 (defun gnus-summary-sort-by-subject (&optional reverse)
10641   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10642 If `case-fold-search' is non-nil, case of letters is ignored.
10643 Argument REVERSE means reverse order."
10644   (interactive "P")
10645   (gnus-summary-sort 'subject reverse))
10646
10647 (defun gnus-summary-sort-by-date (&optional reverse)
10648   "Sort the summary buffer by date.
10649 Argument REVERSE means reverse order."
10650   (interactive "P")
10651   (gnus-summary-sort 'date reverse))
10652
10653 (defun gnus-summary-sort-by-score (&optional reverse)
10654   "Sort the summary buffer by score.
10655 Argument REVERSE means reverse order."
10656   (interactive "P")
10657   (gnus-summary-sort 'score reverse))
10658
10659 (defun gnus-summary-sort-by-lines (&optional reverse)
10660   "Sort the summary buffer by the number of lines.
10661 Argument REVERSE means reverse order."
10662   (interactive "P")
10663   (gnus-summary-sort 'lines reverse))
10664
10665 (defun gnus-summary-sort-by-chars (&optional reverse)
10666   "Sort the summary buffer by article length.
10667 Argument REVERSE means reverse order."
10668   (interactive "P")
10669   (gnus-summary-sort 'chars reverse))
10670
10671 (defun gnus-summary-sort-by-original (&optional reverse)
10672   "Sort the summary buffer using the default sorting method.
10673 Argument REVERSE means reverse order."
10674   (interactive "P")
10675   (let* ((buffer-read-only)
10676          (gnus-summary-prepare-hook nil))
10677     ;; We do the sorting by regenerating the threads.
10678     (gnus-summary-prepare)
10679     ;; Hide subthreads if needed.
10680     (gnus-summary-maybe-hide-threads)))
10681
10682 (defun gnus-summary-sort (predicate reverse)
10683   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10684   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10685          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10686          (gnus-thread-sort-functions
10687           (if (not reverse)
10688               thread
10689             `(lambda (t1 t2)
10690                (,thread t2 t1))))
10691          (gnus-sort-gathered-threads-function
10692           gnus-thread-sort-functions)
10693          (gnus-article-sort-functions
10694           (if (not reverse)
10695               article
10696             `(lambda (t1 t2)
10697                (,article t2 t1))))
10698          (buffer-read-only)
10699          (gnus-summary-prepare-hook nil))
10700     ;; We do the sorting by regenerating the threads.
10701     (gnus-summary-prepare)
10702     ;; Hide subthreads if needed.
10703     (gnus-summary-maybe-hide-threads)))
10704
10705 ;; Summary saving commands.
10706
10707 (defun gnus-summary-save-article (&optional n not-saved)
10708   "Save the current article using the default saver function.
10709 If N is a positive number, save the N next articles.
10710 If N is a negative number, save the N previous articles.
10711 If N is nil and any articles have been marked with the process mark,
10712 save those articles instead.
10713 The variable `gnus-default-article-saver' specifies the saver function."
10714   (interactive "P")
10715   (let* ((articles (gnus-summary-work-articles n))
10716          (save-buffer (save-excursion
10717                         (nnheader-set-temp-buffer " *Gnus Save*")))
10718          (num (length articles))
10719          header file)
10720     (dolist (article articles)
10721       (setq header (gnus-summary-article-header article))
10722       (if (not (vectorp header))
10723           ;; This is a pseudo-article.
10724           (if (assq 'name header)
10725               (gnus-copy-file (cdr (assq 'name header)))
10726             (gnus-message 1 "Article %d is unsaveable" article))
10727         ;; This is a real article.
10728         (save-window-excursion
10729           (let ((gnus-display-mime-function nil)
10730                 (gnus-article-prepare-hook nil))
10731             (gnus-summary-select-article t nil nil article)))
10732         (save-excursion
10733           (set-buffer save-buffer)
10734           (erase-buffer)
10735           (insert-buffer-substring gnus-original-article-buffer))
10736         (setq file (gnus-article-save save-buffer file num))
10737         (gnus-summary-remove-process-mark article)
10738         (unless not-saved
10739           (gnus-summary-set-saved-mark article))))
10740     (gnus-kill-buffer save-buffer)
10741     (gnus-summary-position-point)
10742     (gnus-set-mode-line 'summary)
10743     n))
10744
10745 (defun gnus-summary-pipe-output (&optional arg headers)
10746   "Pipe the current article to a subprocess.
10747 If N is a positive number, pipe the N next articles.
10748 If N is a negative number, pipe the N previous articles.
10749 If N is nil and any articles have been marked with the process mark,
10750 pipe those articles instead.
10751 If HEADERS (the symbolic prefix), include the headers, too."
10752   (interactive (gnus-interactive "P\ny"))
10753   (require 'gnus-art)
10754   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10755         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10756     (gnus-summary-save-article arg t))
10757   (let ((buffer (get-buffer "*Shell Command Output*")))
10758     (when (and buffer
10759                (not (zerop (buffer-size buffer))))
10760       (gnus-configure-windows 'pipe))))
10761
10762 (defun gnus-summary-save-article-mail (&optional arg)
10763   "Append the current article to an mail file.
10764 If N is a positive number, save the N next articles.
10765 If N is a negative number, save the N previous articles.
10766 If N is nil and any articles have been marked with the process mark,
10767 save those articles instead."
10768   (interactive "P")
10769   (require 'gnus-art)
10770   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10771     (gnus-summary-save-article arg)))
10772
10773 (defun gnus-summary-save-article-rmail (&optional arg)
10774   "Append the current article to an rmail file.
10775 If N is a positive number, save the N next articles.
10776 If N is a negative number, save the N previous articles.
10777 If N is nil and any articles have been marked with the process mark,
10778 save those articles instead."
10779   (interactive "P")
10780   (require 'gnus-art)
10781   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10782     (gnus-summary-save-article arg)))
10783
10784 (defun gnus-summary-save-article-file (&optional arg)
10785   "Append the current article to a file.
10786 If N is a positive number, save the N next articles.
10787 If N is a negative number, save the N previous articles.
10788 If N is nil and any articles have been marked with the process mark,
10789 save those articles instead."
10790   (interactive "P")
10791   (require 'gnus-art)
10792   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10793     (gnus-summary-save-article arg)))
10794
10795 (defun gnus-summary-write-article-file (&optional arg)
10796   "Write the current article to a file, deleting the previous file.
10797 If N is a positive number, save the N next articles.
10798 If N is a negative number, save the N previous articles.
10799 If N is nil and any articles have been marked with the process mark,
10800 save those articles instead."
10801   (interactive "P")
10802   (require 'gnus-art)
10803   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10804     (gnus-summary-save-article arg)))
10805
10806 (defun gnus-summary-save-article-body-file (&optional arg)
10807   "Append the current article body to a file.
10808 If N is a positive number, save the N next articles.
10809 If N is a negative number, save the N previous articles.
10810 If N is nil and any articles have been marked with the process mark,
10811 save those articles instead."
10812   (interactive "P")
10813   (require 'gnus-art)
10814   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10815     (gnus-summary-save-article arg)))
10816
10817 (defun gnus-summary-muttprint (&optional arg)
10818   "Print the current article using Muttprint.
10819 If N is a positive number, save the N next articles.
10820 If N is a negative number, save the N previous articles.
10821 If N is nil and any articles have been marked with the process mark,
10822 save those articles instead."
10823   (interactive "P")
10824   (require 'gnus-art)
10825   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10826     (gnus-summary-save-article arg t)))
10827
10828 (defun gnus-summary-pipe-message (program)
10829   "Pipe the current article through PROGRAM."
10830   (interactive "sProgram: ")
10831   (gnus-summary-select-article)
10832   (let ((mail-header-separator ""))
10833     (gnus-eval-in-buffer-window gnus-article-buffer
10834       (save-restriction
10835         (widen)
10836         (let ((start (window-start))
10837               buffer-read-only)
10838           (message-pipe-buffer-body program)
10839           (set-window-start (get-buffer-window (current-buffer)) start))))))
10840
10841 (defun gnus-get-split-value (methods)
10842   "Return a value based on the split METHODS."
10843   (let (split-name method result match)
10844     (when methods
10845       (save-excursion
10846         (set-buffer gnus-original-article-buffer)
10847         (save-restriction
10848           (nnheader-narrow-to-headers)
10849           (while (and methods (not split-name))
10850             (goto-char (point-min))
10851             (setq method (pop methods))
10852             (setq match (car method))
10853             (when (cond
10854                    ((stringp match)
10855                     ;; Regular expression.
10856                     (ignore-errors
10857                       (re-search-forward match nil t)))
10858                    ((functionp match)
10859                     ;; Function.
10860                     (save-restriction
10861                       (widen)
10862                       (setq result (funcall match gnus-newsgroup-name))))
10863                    ((consp match)
10864                     ;; Form.
10865                     (save-restriction
10866                       (widen)
10867                       (setq result (eval match)))))
10868               (setq split-name (cdr method))
10869               (cond ((stringp result)
10870                      (push (expand-file-name
10871                             result gnus-article-save-directory)
10872                            split-name))
10873                     ((consp result)
10874                      (setq split-name (append result split-name)))))))))
10875     (nreverse split-name)))
10876
10877 (defun gnus-valid-move-group-p (group)
10878   (and (boundp group)
10879        (symbol-name group)
10880        (symbol-value group)
10881        (gnus-get-function (gnus-find-method-for-group
10882                            (symbol-name group)) 'request-accept-article t)))
10883
10884 (defun gnus-read-move-group-name (prompt default articles prefix)
10885   "Read a group name."
10886   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10887          (minibuffer-confirm-incomplete nil) ; XEmacs
10888          (prom
10889           (format "%s %s to:"
10890                   prompt
10891                   (if (> (length articles) 1)
10892                       (format "these %d articles" (length articles))
10893                     "this article")))
10894          (to-newsgroup
10895           (cond
10896            ((null split-name)
10897             (gnus-completing-read-with-default
10898              default prom
10899              gnus-active-hashtb
10900              'gnus-valid-move-group-p
10901              nil prefix
10902              'gnus-group-history))
10903            ((= 1 (length split-name))
10904             (gnus-completing-read-with-default
10905              (car split-name) prom
10906              gnus-active-hashtb
10907              'gnus-valid-move-group-p
10908              nil nil
10909              'gnus-group-history))
10910            (t
10911             (gnus-completing-read-with-default
10912              nil prom
10913              (mapcar (lambda (el) (list el))
10914                      (nreverse split-name))
10915              nil nil nil
10916              'gnus-group-history))))
10917          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10918     (when to-newsgroup
10919       (if (or (string= to-newsgroup "")
10920               (string= to-newsgroup prefix))
10921           (setq to-newsgroup default))
10922       (unless to-newsgroup
10923         (error "No group name entered"))
10924       (or (gnus-active to-newsgroup)
10925           (gnus-activate-group to-newsgroup nil nil to-method)
10926           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10927                                      to-newsgroup))
10928               (or (and (gnus-request-create-group to-newsgroup to-method)
10929                        (gnus-activate-group
10930                         to-newsgroup nil nil to-method)
10931                        (gnus-subscribe-group to-newsgroup))
10932                   (error "Couldn't create group %s" to-newsgroup)))
10933           (error "No such group: %s" to-newsgroup)))
10934     to-newsgroup))
10935
10936 (defun gnus-summary-save-parts (type dir n &optional reverse)
10937   "Save parts matching TYPE to DIR.
10938 If REVERSE, save parts that do not match TYPE."
10939   (interactive
10940    (list (read-string "Save parts of type: "
10941                       (or (car gnus-summary-save-parts-type-history)
10942                           gnus-summary-save-parts-default-mime)
10943                       'gnus-summary-save-parts-type-history)
10944          (setq gnus-summary-save-parts-last-directory
10945                (read-file-name "Save to directory: "
10946                                gnus-summary-save-parts-last-directory
10947                                nil t))
10948          current-prefix-arg))
10949   (gnus-summary-iterate n
10950     (let ((gnus-display-mime-function nil)
10951           (gnus-inhibit-treatment t))
10952       (gnus-summary-select-article))
10953     (save-excursion
10954       (set-buffer gnus-article-buffer)
10955       (let ((handles (or gnus-article-mime-handles
10956                          (mm-dissect-buffer nil gnus-article-loose-mime)
10957                          (and gnus-article-emulate-mime
10958                               (mm-uu-dissect)))))
10959         (when handles
10960           (gnus-summary-save-parts-1 type dir handles reverse)
10961           (unless gnus-article-mime-handles ;; Don't destroy this case.
10962             (mm-destroy-parts handles)))))))
10963
10964 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10965   (if (stringp (car handle))
10966       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10967               (cdr handle))
10968     (when (if reverse
10969               (not (string-match type (mm-handle-media-type handle)))
10970             (string-match type (mm-handle-media-type handle)))
10971       (let ((file (expand-file-name
10972                    (file-name-nondirectory
10973                     (or
10974                      (mail-content-type-get
10975                       (mm-handle-disposition handle) 'filename)
10976                      (concat gnus-newsgroup-name
10977                              "." (number-to-string
10978                                   (cdr gnus-article-current)))))
10979                    dir)))
10980         (unless (file-exists-p file)
10981           (mm-save-part-to-file handle file))))))
10982
10983 ;; Summary extract commands
10984
10985 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10986   (let ((buffer-read-only nil)
10987         (article (gnus-summary-article-number))
10988         after-article b e)
10989     (unless (gnus-summary-goto-subject article)
10990       (error "No such article: %d" article))
10991     (gnus-summary-position-point)
10992     ;; If all commands are to be bunched up on one line, we collect
10993     ;; them here.
10994     (unless gnus-view-pseudos-separately
10995       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10996             files action)
10997         (while ps
10998           (setq action (cdr (assq 'action (car ps))))
10999           (setq files (list (cdr (assq 'name (car ps)))))
11000           (while (and ps (cdr ps)
11001                       (string= (or action "1")
11002                                (or (cdr (assq 'action (cadr ps))) "2")))
11003             (push (cdr (assq 'name (cadr ps))) files)
11004             (setcdr ps (cddr ps)))
11005           (when files
11006             (when (not (string-match "%s" action))
11007               (push " " files))
11008             (push " " files)
11009             (when (assq 'execute (car ps))
11010               (setcdr (assq 'execute (car ps))
11011                       (funcall (if (string-match "%s" action)
11012                                    'format 'concat)
11013                                action
11014                                (mapconcat
11015                                 (lambda (f)
11016                                   (if (equal f " ")
11017                                       f
11018                                     (mm-quote-arg f)))
11019                                 files " ")))))
11020           (setq ps (cdr ps)))))
11021     (if (and gnus-view-pseudos (not not-view))
11022         (while pslist
11023           (when (assq 'execute (car pslist))
11024             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11025                                   (eq gnus-view-pseudos 'not-confirm)))
11026           (setq pslist (cdr pslist)))
11027       (save-excursion
11028         (while pslist
11029           (setq after-article (or (cdr (assq 'article (car pslist)))
11030                                   (gnus-summary-article-number)))
11031           (gnus-summary-goto-subject after-article)
11032           (forward-line 1)
11033           (setq b (point))
11034           (insert "    " (file-name-nondirectory
11035                           (cdr (assq 'name (car pslist))))
11036                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11037           (setq e (point))
11038           (forward-line -1)             ; back to `b'
11039           (gnus-add-text-properties
11040            b (1- e) (list 'gnus-number gnus-reffed-article-number
11041                           gnus-mouse-face-prop gnus-mouse-face))
11042           (gnus-data-enter
11043            after-article gnus-reffed-article-number
11044            gnus-unread-mark b (car pslist) 0 (- e b))
11045           (setq gnus-newsgroup-unreads
11046                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11047                                          gnus-reffed-article-number))
11048           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11049           (setq pslist (cdr pslist)))))))
11050
11051 (defun gnus-pseudos< (p1 p2)
11052   (let ((c1 (cdr (assq 'action p1)))
11053         (c2 (cdr (assq 'action p2))))
11054     (and c1 c2 (string< c1 c2))))
11055
11056 (defun gnus-request-pseudo-article (props)
11057   (cond ((assq 'execute props)
11058          (gnus-execute-command (cdr (assq 'execute props)))))
11059   (let ((gnus-current-article (gnus-summary-article-number)))
11060     (gnus-run-hooks 'gnus-mark-article-hook)))
11061
11062 (defun gnus-execute-command (command &optional automatic)
11063   (save-excursion
11064     (gnus-article-setup-buffer)
11065     (set-buffer gnus-article-buffer)
11066     (setq buffer-read-only nil)
11067     (let ((command (if automatic command
11068                      (read-string "Command: " (cons command 0)))))
11069       (erase-buffer)
11070       (insert "$ " command "\n\n")
11071       (if gnus-view-pseudo-asynchronously
11072           (start-process "gnus-execute" (current-buffer) shell-file-name
11073                          shell-command-switch command)
11074         (call-process shell-file-name nil t nil
11075                       shell-command-switch command)))))
11076
11077 ;; Summary kill commands.
11078
11079 (defun gnus-summary-edit-global-kill (article)
11080   "Edit the \"global\" kill file."
11081   (interactive (list (gnus-summary-article-number)))
11082   (gnus-group-edit-global-kill article))
11083
11084 (defun gnus-summary-edit-local-kill ()
11085   "Edit a local kill file applied to the current newsgroup."
11086   (interactive)
11087   (setq gnus-current-headers (gnus-summary-article-header))
11088   (gnus-group-edit-local-kill
11089    (gnus-summary-article-number) gnus-newsgroup-name))
11090
11091 ;;; Header reading.
11092
11093 (defun gnus-read-header (id &optional header)
11094   "Read the headers of article ID and enter them into the Gnus system."
11095   (let ((group gnus-newsgroup-name)
11096         (gnus-override-method
11097          (or
11098           gnus-override-method
11099           (and (gnus-news-group-p gnus-newsgroup-name)
11100                (car (gnus-refer-article-methods)))))
11101         where)
11102     ;; First we check to see whether the header in question is already
11103     ;; fetched.
11104     (if (stringp id)
11105         ;; This is a Message-ID.
11106         (setq header (or header (gnus-id-to-header id)))
11107       ;; This is an article number.
11108       (setq header (or header (gnus-summary-article-header id))))
11109     (if (and header
11110              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11111         ;; We have found the header.
11112         header
11113       ;; If this is a sparse article, we have to nix out its
11114       ;; previous entry in the thread hashtb.
11115       (when (and header
11116                  (gnus-summary-article-sparse-p (mail-header-number header)))
11117         (let* ((parent (gnus-parent-id (mail-header-references header)))
11118                (thread (and parent (gnus-id-to-thread parent))))
11119           (when thread
11120             (delq (assq header thread) thread))))
11121       ;; We have to really fetch the header to this article.
11122       (save-excursion
11123         (set-buffer nntp-server-buffer)
11124         (when (setq where (gnus-request-head id group))
11125           (nnheader-fold-continuation-lines)
11126           (goto-char (point-max))
11127           (insert ".\n")
11128           (goto-char (point-min))
11129           (insert "211 ")
11130           (princ (cond
11131                   ((numberp id) id)
11132                   ((cdr where) (cdr where))
11133                   (header (mail-header-number header))
11134                   (t gnus-reffed-article-number))
11135                  (current-buffer))
11136           (insert " Article retrieved.\n"))
11137         (if (or (not where)
11138                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11139             ()                          ; Malformed head.
11140           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11141             (when (and (stringp id)
11142                        (not (string= (gnus-group-real-name group)
11143                                      (car where))))
11144               ;; If we fetched by Message-ID and the article came
11145               ;; from a different group, we fudge some bogus article
11146               ;; numbers for this article.
11147               (mail-header-set-number header gnus-reffed-article-number))
11148             (save-excursion
11149               (set-buffer gnus-summary-buffer)
11150               (decf gnus-reffed-article-number)
11151               (gnus-remove-header (mail-header-number header))
11152               (push header gnus-newsgroup-headers)
11153               (setq gnus-current-headers header)
11154               (push (mail-header-number header) gnus-newsgroup-limit)))
11155           header)))))
11156
11157 (defun gnus-remove-header (number)
11158   "Remove header NUMBER from `gnus-newsgroup-headers'."
11159   (if (and gnus-newsgroup-headers
11160            (= number (mail-header-number (car gnus-newsgroup-headers))))
11161       (pop gnus-newsgroup-headers)
11162     (let ((headers gnus-newsgroup-headers))
11163       (while (and (cdr headers)
11164                   (not (= number (mail-header-number (cadr headers)))))
11165         (pop headers))
11166       (when (cdr headers)
11167         (setcdr headers (cddr headers))))))
11168
11169 ;;;
11170 ;;; summary highlights
11171 ;;;
11172
11173 (defun gnus-highlight-selected-summary ()
11174   "Highlight selected article in summary buffer."
11175   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11176   (when gnus-summary-selected-face
11177     (save-excursion
11178       (let* ((beg (gnus-point-at-bol))
11179              (end (gnus-point-at-eol))
11180              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11181              (from (if (get-text-property beg gnus-mouse-face-prop)
11182                        beg
11183                      (or (next-single-property-change
11184                           beg gnus-mouse-face-prop nil end)
11185                          beg)))
11186              (to
11187               (if (= from end)
11188                   (- from 2)
11189                 (or (next-single-property-change
11190                      from gnus-mouse-face-prop nil end)
11191                     end))))
11192         ;; If no mouse-face prop on line we will have to = from = end,
11193         ;; so we highlight the entire line instead.
11194         (when (= (+ to 2) from)
11195           (setq from beg)
11196           (setq to end))
11197         (if gnus-newsgroup-selected-overlay
11198             ;; Move old overlay.
11199             (gnus-move-overlay
11200              gnus-newsgroup-selected-overlay from to (current-buffer))
11201           ;; Create new overlay.
11202           (gnus-overlay-put
11203            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11204            'face gnus-summary-selected-face))))))
11205
11206 (defvar gnus-summary-highlight-line-cached nil)
11207 (defvar gnus-summary-highlight-line-trigger nil)
11208
11209 (defun gnus-summary-highlight-line-0 ()
11210   (if (and (eq gnus-summary-highlight-line-trigger 
11211                gnus-summary-highlight)
11212            gnus-summary-highlight-line-cached)
11213       gnus-summary-highlight-line-cached
11214     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11215           gnus-summary-highlight-line-cached
11216           (let* ((cond (list 'cond))
11217                  (c cond)
11218                  (list gnus-summary-highlight))
11219             (while list
11220               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11221                               nil))
11222               (setq c (cdr c)
11223                     list (cdr list)))
11224             (gnus-byte-compile (list 'lambda nil cond))))))
11225
11226 (defun gnus-summary-highlight-line ()
11227   "Highlight current line according to `gnus-summary-highlight'."
11228   (let* ((beg (gnus-point-at-bol))
11229          (article (or (gnus-summary-article-number) gnus-current-article))
11230          (score (or (cdr (assq article
11231                                gnus-newsgroup-scored))
11232                     gnus-summary-default-score 0))
11233          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11234          (inhibit-read-only t)
11235          (default gnus-summary-default-score)
11236          (default-high gnus-summary-default-high-score)
11237          (default-low gnus-summary-default-low-score)
11238          (uncached (and gnus-summary-use-undownloaded-faces
11239                         (memq article gnus-newsgroup-undownloaded))))
11240     (let ((face (funcall (gnus-summary-highlight-line-0))))
11241       (unless (eq face (get-text-property beg 'face))
11242         (gnus-put-text-property-excluding-characters-with-faces
11243          beg (gnus-point-at-eol) 'face
11244          (setq face (if (boundp face) (symbol-value face) face)))
11245         (when gnus-summary-highlight-line-function
11246           (funcall gnus-summary-highlight-line-function article face))))))
11247
11248 (defun gnus-update-read-articles (group unread &optional compute)
11249   "Update the list of read articles in GROUP.
11250 UNREAD is a sorted list."
11251   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11252          (entry (gnus-gethash group gnus-newsrc-hashtb))
11253          (info (nth 2 entry))
11254          (prev 1)
11255          read)
11256     (if (or (not info) (not active))
11257         ;; There is no info on this group if it was, in fact,
11258         ;; killed.  Gnus stores no information on killed groups, so
11259         ;; there's nothing to be done.
11260         ;; One could store the information somewhere temporarily,
11261         ;; perhaps...  Hmmm...
11262         ()
11263       ;; Remove any negative articles numbers.
11264       (while (and unread (< (car unread) 0))
11265         (setq unread (cdr unread)))
11266       ;; Remove any expired article numbers
11267       (while (and unread (< (car unread) (car active)))
11268         (setq unread (cdr unread)))
11269       ;; Compute the ranges of read articles by looking at the list of
11270       ;; unread articles.
11271       (while unread
11272         (when (/= (car unread) prev)
11273           (push (if (= prev (1- (car unread))) prev
11274                   (cons prev (1- (car unread))))
11275                 read))
11276         (setq prev (1+ (car unread)))
11277         (setq unread (cdr unread)))
11278       (when (<= prev (cdr active))
11279         (push (cons prev (cdr active)) read))
11280       (setq read (if (> (length read) 1) (nreverse read) read))
11281       (if compute
11282           read
11283         (save-excursion
11284           (let (setmarkundo)
11285             ;; Propagate the read marks to the backend.
11286             (when (gnus-check-backend-function 'request-set-mark group)
11287               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11288                     (add (gnus-remove-from-range read (gnus-info-read info))))
11289                 (when (or add del)
11290                   (unless (gnus-check-group group)
11291                     (error "Can't open server for %s" group))
11292                   (gnus-request-set-mark
11293                    group (delq nil (list (if add (list add 'add '(read)))
11294                                          (if del (list del 'del '(read))))))
11295                   (setq setmarkundo
11296                         `(gnus-request-set-mark
11297                           ,group
11298                           ',(delq nil (list
11299                                        (if del (list del 'add '(read)))
11300                                        (if add (list add 'del '(read))))))))))
11301             (set-buffer gnus-group-buffer)
11302             (gnus-undo-register
11303               `(progn
11304                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11305                  (gnus-info-set-read ',info ',(gnus-info-read info))
11306                  (gnus-get-unread-articles-in-group ',info
11307                                                     (gnus-active ,group))
11308                  (gnus-group-update-group ,group t)
11309                  ,setmarkundo))))
11310         ;; Enter this list into the group info.
11311         (gnus-info-set-read info read)
11312         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11313         (gnus-get-unread-articles-in-group info (gnus-active group))
11314         t))))
11315
11316 (defun gnus-offer-save-summaries ()
11317   "Offer to save all active summary buffers."
11318   (let (buffers)
11319     ;; Go through all buffers and find all summaries.
11320     (dolist (buffer (buffer-list))
11321       (when (and (setq buffer (buffer-name buffer))
11322                  (string-match "Summary" buffer)
11323                  (save-excursion
11324                    (set-buffer buffer)
11325                    ;; We check that this is, indeed, a summary buffer.
11326                    (and (eq major-mode 'gnus-summary-mode)
11327                         ;; Also make sure this isn't bogus.
11328                         gnus-newsgroup-prepared
11329                         ;; Also make sure that this isn't a
11330                         ;; dead summary buffer.
11331                         (not gnus-dead-summary-mode))))
11332         (push buffer buffers)))
11333     ;; Go through all these summary buffers and offer to save them.
11334     (when buffers
11335       (save-excursion
11336         (map-y-or-n-p
11337          "Update summary buffer %s? "
11338          (lambda (buf)
11339            (switch-to-buffer buf)
11340            (gnus-summary-exit))
11341          buffers)))))
11342
11343 (defun gnus-summary-setup-default-charset ()
11344   "Setup newsgroup default charset."
11345   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11346       (setq gnus-newsgroup-charset nil)
11347     (let* ((ignored-charsets
11348             (or gnus-newsgroup-ephemeral-ignored-charsets
11349                 (append
11350                  (and gnus-newsgroup-name
11351                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11352                  gnus-newsgroup-ignored-charsets))))
11353       (setq gnus-newsgroup-charset
11354             (or gnus-newsgroup-ephemeral-charset
11355                 (and gnus-newsgroup-name
11356                      (gnus-parameter-charset gnus-newsgroup-name))
11357                 gnus-default-charset))
11358       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11359            ignored-charsets))))
11360
11361 ;;;
11362 ;;; Mime Commands
11363 ;;;
11364
11365 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11366   "Display the current article buffer fully MIME-buttonized.
11367 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11368 treated as multipart/mixed."
11369   (interactive "P")
11370   (require 'gnus-art)
11371   (let ((gnus-unbuttonized-mime-types nil)
11372         (gnus-mime-display-multipart-as-mixed show-all-parts))
11373     (gnus-summary-show-article)))
11374
11375 (defun gnus-summary-repair-multipart (article)
11376   "Add a Content-Type header to a multipart article without one."
11377   (interactive (list (gnus-summary-article-number)))
11378   (gnus-with-article article
11379     (message-narrow-to-head)
11380     (message-remove-header "Mime-Version")
11381     (goto-char (point-max))
11382     (insert "Mime-Version: 1.0\n")
11383     (widen)
11384     (when (search-forward "\n--" nil t)
11385       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11386         (message-narrow-to-head)
11387         (message-remove-header "Content-Type")
11388         (goto-char (point-max))
11389         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11390                         separator))
11391         (widen))))
11392   (let (gnus-mark-article-hook)
11393     (gnus-summary-select-article t t nil article)))
11394
11395 (defun gnus-summary-toggle-display-buttonized ()
11396   "Toggle the buttonizing of the article buffer."
11397   (interactive)
11398   (require 'gnus-art)
11399   (if (setq gnus-inhibit-mime-unbuttonizing
11400             (not gnus-inhibit-mime-unbuttonizing))
11401       (let ((gnus-unbuttonized-mime-types nil))
11402         (gnus-summary-show-article))
11403     (gnus-summary-show-article)))
11404
11405 ;;;
11406 ;;; Generic summary marking commands
11407 ;;;
11408
11409 (defvar gnus-summary-marking-alist
11410   '((read gnus-del-mark "d")
11411     (unread gnus-unread-mark "u")
11412     (ticked gnus-ticked-mark "!")
11413     (dormant gnus-dormant-mark "?")
11414     (expirable gnus-expirable-mark "e"))
11415   "An alist of names/marks/keystrokes.")
11416
11417 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11418 (defvar gnus-summary-mark-map)
11419
11420 (defun gnus-summary-make-all-marking-commands ()
11421   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11422   (dolist (elem gnus-summary-marking-alist)
11423     (apply 'gnus-summary-make-marking-command elem)))
11424
11425 (defun gnus-summary-make-marking-command (name mark keystroke)
11426   (let ((map (make-sparse-keymap)))
11427     (define-key gnus-summary-generic-mark-map keystroke map)
11428     (dolist (lway `((next "next" next nil "n")
11429                     (next-unread "next unread" next t "N")
11430                     (prev "previous" prev nil "p")
11431                     (prev-unread "previous unread" prev t "P")
11432                     (nomove "" nil nil ,keystroke)))
11433       (let ((func (gnus-summary-make-marking-command-1
11434                    mark (car lway) lway name)))
11435         (setq func (eval func))
11436         (define-key map (nth 4 lway) func)))))
11437
11438 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11439   `(defun ,(intern
11440             (format "gnus-summary-put-mark-as-%s%s"
11441                     name (if (eq way 'nomove)
11442                              ""
11443                            (concat "-" (symbol-name way)))))
11444      (n)
11445      ,(format
11446        "Mark the current article as %s%s.
11447 If N, the prefix, then repeat N times.
11448 If N is negative, move in reverse order.
11449 The difference between N and the actual number of articles marked is
11450 returned."
11451        name (cadr lway))
11452      (interactive "p")
11453      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11454
11455 (defun gnus-summary-generic-mark (n mark move unread)
11456   "Mark N articles with MARK."
11457   (unless (eq major-mode 'gnus-summary-mode)
11458     (error "This command can only be used in the summary buffer"))
11459   (gnus-summary-show-thread)
11460   (let ((nummove
11461          (cond
11462           ((eq move 'next) 1)
11463           ((eq move 'prev) -1)
11464           (t 0))))
11465     (if (zerop nummove)
11466         (setq n 1)
11467       (when (< n 0)
11468         (setq n (abs n)
11469               nummove (* -1 nummove))))
11470     (while (and (> n 0)
11471                 (gnus-summary-mark-article nil mark)
11472                 (zerop (gnus-summary-next-subject nummove unread t)))
11473       (setq n (1- n)))
11474     (when (/= 0 n)
11475       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11476     (gnus-summary-recenter)
11477     (gnus-summary-position-point)
11478     (gnus-set-mode-line 'summary)
11479     n))
11480
11481 (defun gnus-summary-insert-articles (articles)
11482   (when (setq articles
11483               (gnus-sorted-difference articles
11484                                       (mapcar (lambda (h)
11485                                                 (mail-header-number h))
11486                                               gnus-newsgroup-headers)))
11487     (setq gnus-newsgroup-headers
11488           (gnus-merge 'list
11489                       gnus-newsgroup-headers
11490                       (gnus-fetch-headers articles)
11491                       'gnus-article-sort-by-number))
11492     ;; Suppress duplicates?
11493     (when gnus-suppress-duplicates
11494       (gnus-dup-suppress-articles))
11495
11496     ;; We might want to build some more threads first.
11497     (when (and gnus-fetch-old-headers
11498                (eq gnus-headers-retrieved-by 'nov))
11499       (if (eq gnus-fetch-old-headers 'invisible)
11500           (gnus-build-all-threads)
11501         (gnus-build-old-threads)))
11502     ;; Let the Gnus agent mark articles as read.
11503     (when gnus-agent
11504       (gnus-agent-get-undownloaded-list))
11505     ;; Remove list identifiers from subject
11506     (when gnus-list-identifiers
11507       (gnus-summary-remove-list-identifiers))
11508     ;; First and last article in this newsgroup.
11509     (when gnus-newsgroup-headers
11510       (setq gnus-newsgroup-begin
11511             (mail-header-number (car gnus-newsgroup-headers))
11512             gnus-newsgroup-end
11513             (mail-header-number
11514              (gnus-last-element gnus-newsgroup-headers))))
11515     (when gnus-use-scoring
11516       (gnus-possibly-score-headers))))
11517
11518 (defun gnus-summary-insert-old-articles (&optional all)
11519   "Insert all old articles in this group.
11520 If ALL is non-nil, already read articles become readable.
11521 If ALL is a number, fetch this number of articles."
11522   (interactive "P")
11523   (prog1
11524       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11525             older len)
11526         (setq older
11527               ;; Some nntp servers lie about their active range.  When
11528               ;; this happens, the active range can be in the millions.
11529               ;; Use a compressed range to avoid creating a huge list.
11530               (gnus-range-difference (list gnus-newsgroup-active) old))
11531         (setq len (gnus-range-length older))
11532         (cond
11533          ((null older) nil)
11534          ((numberp all)
11535           (if (< all len)
11536               (let ((older-range (nreverse older)))
11537                 (setq older nil)
11538
11539                 (while (> all 0)
11540                   (let* ((r (pop older-range))
11541                          (min (if (numberp r) r (car r)))
11542                          (max (if (numberp r) r (cdr r))))
11543                     (while (and (<= min max)
11544                                 (> all 0))
11545                       (push max older)
11546                       (setq all (1- all)
11547                             max (1- max))))))
11548             (setq older (gnus-uncompress-range older))))
11549          (all
11550           (setq older (gnus-uncompress-range older)))
11551          (t
11552           (when (and (numberp gnus-large-newsgroup)
11553                    (> len gnus-large-newsgroup))
11554               (let* ((cursor-in-echo-area nil)
11555                      (initial (gnus-parameter-large-newsgroup-initial
11556                                gnus-newsgroup-name))
11557                      (input
11558                       (read-string
11559                        (format
11560                         "How many articles from %s (%s %d): "
11561                         (gnus-limit-string
11562                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11563                         (if initial "max" "default")
11564                         len)
11565                        (if initial
11566                            (cons (number-to-string initial)
11567                                  0)))))
11568                 (unless (string-match "^[ \t]*$" input)
11569                   (setq all (string-to-number input))
11570                   (if (< all len)
11571                       (let ((older-range (nreverse older)))
11572                         (setq older nil)
11573
11574                         (while (> all 0)
11575                           (let* ((r (pop older-range))
11576                                  (min (if (numberp r) r (car r)))
11577                                  (max (if (numberp r) r (cdr r))))
11578                             (while (and (<= min max)
11579                                         (> all 0))
11580                               (push max older)
11581                               (setq all (1- all)
11582                                     max (1- max))))))))))
11583           (setq older (gnus-uncompress-range older))))
11584         (if (not older)
11585             (message "No old news.")
11586           (gnus-summary-insert-articles older)
11587           (gnus-summary-limit (gnus-sorted-nunion old older))))
11588     (gnus-summary-position-point)))
11589
11590 (defun gnus-summary-insert-new-articles ()
11591   "Insert all new articles in this group."
11592   (interactive)
11593   (prog1
11594       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11595             (old-active gnus-newsgroup-active)
11596             (nnmail-fetched-sources (list t))
11597             i new)
11598         (setq gnus-newsgroup-active
11599               (gnus-activate-group gnus-newsgroup-name 'scan))
11600         (setq i (cdr gnus-newsgroup-active))
11601         (while (> i (cdr old-active))
11602           (push i new)
11603           (decf i))
11604         (if (not new)
11605             (message "No gnus is bad news.")
11606           (gnus-summary-insert-articles new)
11607           (setq gnus-newsgroup-unreads
11608                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11609           (gnus-summary-limit (gnus-sorted-nunion old new))))
11610     (gnus-summary-position-point)))
11611
11612 (gnus-summary-make-all-marking-commands)
11613
11614 (gnus-ems-redefine)
11615
11616 (provide 'gnus-sum)
11617
11618 (run-hooks 'gnus-sum-load-hook)
11619
11620 ;; Local Variables:
11621 ;; coding: iso-8859-1
11622 ;; End:
11623
11624 ;;; gnus-sum.el ends here