Import Oort Gnus v0.16.
[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 (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
50 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
51 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
52
53 (defcustom gnus-kill-summary-on-exit t
54   "*If non-nil, kill the summary buffer when you exit from it.
55 If nil, the summary will become a \"*Dead Summary*\" buffer, and
56 it will be killed sometime later."
57   :group 'gnus-summary-exit
58   :type 'boolean)
59
60 (defcustom gnus-fetch-old-headers nil
61   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
62 If an unread article in the group refers to an older, already read (or
63 just marked as read) article, the old article will not normally be
64 displayed in the Summary buffer.  If this variable is non-nil, Gnus
65 will attempt to grab the headers to the old articles, and thereby
66 build complete threads.  If it has the value `some', only enough
67 headers to connect otherwise loose threads will be displayed.  This
68 variable can also be a number.  In that case, no more than that number
69 of old headers will be fetched.  If it has the value `invisible', all
70 old headers will be fetched, but none will be displayed.
71
72 The server has to support NOV for any of this to work."
73   :group 'gnus-thread
74   :type '(choice (const :tag "off" nil)
75                  (const some)
76                  number
77                  (sexp :menu-tag "other" t)))
78
79 (defcustom gnus-refer-thread-limit 200
80   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
81 If t, fetch all the available old headers."
82   :group 'gnus-thread
83   :type '(choice number
84                  (sexp :menu-tag "other" t)))
85
86 (defcustom gnus-summary-make-false-root 'adopt
87   "*nil means that Gnus won't gather loose threads.
88 If the root of a thread has expired or been read in a previous
89 session, the information necessary to build a complete thread has been
90 lost.  Instead of having many small sub-threads from this original thread
91 scattered all over the summary buffer, Gnus can gather them.
92
93 If non-nil, Gnus will try to gather all loose sub-threads from an
94 original thread into one large thread.
95
96 If this variable is non-nil, it should be one of `none', `adopt',
97 `dummy' or `empty'.
98
99 If this variable is `none', Gnus will not make a false root, but just
100 present the sub-threads after another.
101 If this variable is `dummy', Gnus will create a dummy root that will
102 have all the sub-threads as children.
103 If this variable is `adopt', Gnus will make one of the \"children\"
104 the parent and mark all the step-children as such.
105 If this variable is `empty', the \"children\" are printed with empty
106 subject fields.  (Or rather, they will be printed with a string
107 given by the `gnus-summary-same-subject' variable.)"
108   :group 'gnus-thread
109   :type '(choice (const :tag "off" nil)
110                  (const none)
111                  (const dummy)
112                  (const adopt)
113                  (const empty)))
114
115 (defcustom gnus-summary-make-false-root-always nil
116   "Always make a false dummy root."
117   :group 'gnus-thread
118   :type 'boolean)
119
120 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
121   "*A regexp to match subjects to be excluded from loose thread gathering.
122 As loose thread gathering is done on subjects only, that means that
123 there can be many false gatherings performed.  By rooting out certain
124 common subjects, gathering might become saner."
125   :group 'gnus-thread
126   :type 'regexp)
127
128 (defcustom gnus-summary-gather-subject-limit nil
129   "*Maximum length of subject comparisons when gathering loose threads.
130 Use nil to compare full subjects.  Setting this variable to a low
131 number will help gather threads that have been corrupted by
132 newsreaders chopping off subject lines, but it might also mean that
133 unrelated articles that have subject that happen to begin with the
134 same few characters will be incorrectly gathered.
135
136 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
137 comparing subjects."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  (const fuzzy)
141                  (sexp :menu-tag "on" t)))
142
143 (defcustom gnus-simplify-subject-functions nil
144   "List of functions taking a string argument that simplify subjects.
145 The functions are applied recursively.
146
147 Useful functions to put in this list include:
148 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
149 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
150   :group 'gnus-thread
151   :type '(repeat function))
152
153 (defcustom gnus-simplify-ignored-prefixes nil
154   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
155   :group 'gnus-thread
156   :type '(choice (const :tag "off" nil)
157                  regexp))
158
159 (defcustom gnus-build-sparse-threads nil
160   "*If non-nil, fill in the gaps in threads.
161 If `some', only fill in the gaps that are needed to tie loose threads
162 together.  If `more', fill in all leaf nodes that Gnus can find.  If
163 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
164   :group 'gnus-thread
165   :type '(choice (const :tag "off" nil)
166                  (const some)
167                  (const more)
168                  (sexp :menu-tag "all" t)))
169
170 (defcustom gnus-summary-thread-gathering-function
171   'gnus-gather-threads-by-subject
172   "*Function used for gathering loose threads.
173 There are two pre-defined functions: `gnus-gather-threads-by-subject',
174 which only takes Subjects into consideration; and
175 `gnus-gather-threads-by-references', which compared the References
176 headers of the articles to find matches."
177   :group 'gnus-thread
178   :type '(radio (function-item gnus-gather-threads-by-subject)
179                 (function-item gnus-gather-threads-by-references)
180                 (function :tag "other")))
181
182 (defcustom gnus-summary-same-subject ""
183   "*String indicating that the current article has the same subject as the previous.
184 This variable will only be used if the value of
185 `gnus-summary-make-false-root' is `empty'."
186   :group 'gnus-summary-format
187   :type 'string)
188
189 (defcustom gnus-summary-goto-unread t
190   "*If t, many commands will go to the next unread article.
191 This applies to marking commands as well as other commands that
192 \"naturally\" select the next article, like, for instance, `SPC' at
193 the end of an article.
194
195 If nil, the marking commands do NOT go to the next unread article
196 \(they go to the next article instead).  If `never', commands that
197 usually go to the next unread article, will go to the next article,
198 whether it is read or not."
199   :group 'gnus-summary-marks
200   :link '(custom-manual "(gnus)Setting Marks")
201   :type '(choice (const :tag "off" nil)
202                  (const never)
203                  (sexp :menu-tag "on" t)))
204
205 (defcustom gnus-summary-default-score 0
206   "*Default article score level.
207 All scores generated by the score files will be added to this score.
208 If this variable is nil, scoring will be disabled."
209   :group 'gnus-score-default
210   :type '(choice (const :tag "disable")
211                  integer))
212
213 (defcustom gnus-summary-default-high-score 0
214   "*Default threshold for a high scored article.
215 An article will be highlighted as high scored if its score is greater
216 than this score."
217   :group 'gnus-score-default
218   :type 'integer)
219
220 (defcustom gnus-summary-default-low-score 0
221   "*Default threshold for a low scored article.
222 An article will be highlighted as low scored if its score is smaller
223 than this score."
224   :group 'gnus-score-default
225   :type 'integer)
226
227 (defcustom gnus-summary-zcore-fuzz 0
228   "*Fuzziness factor for the zcore in the summary buffer.
229 Articles with scores closer than this to `gnus-summary-default-score'
230 will not be marked."
231   :group 'gnus-summary-format
232   :type 'integer)
233
234 (defcustom gnus-simplify-subject-fuzzy-regexp nil
235   "*Strings to be removed when doing fuzzy matches.
236 This can either be a regular expression or list of regular expressions
237 that will be removed from subject strings if fuzzy subject
238 simplification is selected."
239   :group 'gnus-thread
240   :type '(repeat regexp))
241
242 (defcustom gnus-show-threads t
243   "*If non-nil, display threads in summary mode."
244   :group 'gnus-thread
245   :type 'boolean)
246
247 (defcustom gnus-thread-hide-subtree nil
248   "*If non-nil, hide all threads initially.
249 This can be a predicate specifier which says which threads to hide.
250 If threads are hidden, you have to run the command
251 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
252 to expose hidden threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-killed t
257   "*If non-nil, hide killed threads automatically."
258   :group 'gnus-thread
259   :type 'boolean)
260
261 (defcustom gnus-thread-ignore-subject t
262   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
263 If nil, articles that have different subjects from their parents will
264 start separate threads."
265   :group 'gnus-thread
266   :type 'boolean)
267
268 (defcustom gnus-thread-operation-ignore-subject t
269   "*If non-nil, subjects will be ignored when doing thread commands.
270 This affects commands like `gnus-summary-kill-thread' and
271 `gnus-summary-lower-thread'.
272
273 If this variable is nil, articles in the same thread with different
274 subjects will not be included in the operation in question.  If this
275 variable is `fuzzy', only articles that have subjects that are fuzzily
276 equal will be included."
277   :group 'gnus-thread
278   :type '(choice (const :tag "off" nil)
279                  (const fuzzy)
280                  (sexp :tag "on" t)))
281
282 (defcustom gnus-thread-indent-level 4
283   "*Number that says how much each sub-thread should be indented."
284   :group 'gnus-thread
285   :type 'integer)
286
287 (defcustom gnus-auto-extend-newsgroup t
288   "*If non-nil, extend newsgroup forward and backward when requested."
289   :group 'gnus-summary-choose
290   :type 'boolean)
291
292 (defcustom gnus-auto-select-first t
293   "*If non-nil, select the article under point.
294 Which article this is is controlled by the `gnus-auto-select-subject'
295 variable.
296
297 If you want to prevent automatic selection of articles in some
298 newsgroups, set the variable to nil in `gnus-select-group-hook'."
299   :group 'gnus-group-select
300   :type '(choice (const :tag "none" nil)
301                  (sexp :menu-tag "first" t)))
302
303 (defcustom gnus-auto-select-subject 'unread
304   "*Says what subject to place under point when entering a group.
305
306 This variable can either be the symbols `first' (place point on the
307 first subject), `unread' (place point on the subject line of the first
308 unread article), `best' (place point on the subject line of the
309 higest-scored article), `unseen' (place point on the subject line of
310 the first unseen article), 'unseen-or-unread' (place point on the subject
311 line of the first unseen article or, if all article have been seen, on the
312 subject line of the first unread article), or a function to be called to
313 place point on some subject line."
314   :group 'gnus-group-select
315   :type '(choice (const best)
316                  (const unread)
317                  (const first)
318                  (const unseen)
319                  (const unseen-or-unread)))
320
321 (defcustom gnus-auto-select-next t
322   "*If non-nil, offer to go to the next group from the end of the previous.
323 If the value is t and the next newsgroup is empty, Gnus will exit
324 summary mode and go back to group mode.  If the value is neither nil
325 nor t, Gnus will select the following unread newsgroup.  In
326 particular, if the value is the symbol `quietly', the next unread
327 newsgroup will be selected without any confirmation, and if it is
328 `almost-quietly', the next group will be selected without any
329 confirmation if you are located on the last article in the group.
330 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
331 will go to the next group without confirmation."
332   :group 'gnus-summary-maneuvering
333   :type '(choice (const :tag "off" nil)
334                  (const quietly)
335                  (const almost-quietly)
336                  (const slightly-quietly)
337                  (sexp :menu-tag "on" t)))
338
339 (defcustom gnus-auto-select-same nil
340   "*If non-nil, select the next article with the same subject.
341 If there are no more articles with the same subject, go to
342 the first unread article."
343   :group 'gnus-summary-maneuvering
344   :type 'boolean)
345
346 (defcustom gnus-auto-goto-ignores 'unfetched
347   "*Says how to handle unfetched articles when maneuvering.
348
349 This variable can either be the symbols `nil' (maneuver to any
350 article), `undownloaded' (maneuvering while unplugged ignores articles
351 that have not been fetched), `always-undownloaded' (maneuvering always
352 ignores articles that have not been fetched), `unfetched' (maneuvering
353 ignores articles whose headers have not been fetched).
354
355 NOTE: The list of unfetched articles will always be nil when plugged
356 and, when unplugged, a subset of the undownloaded article list."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "None" nil)
359                  (const :tag "Undownloaded when unplugged" undownloaded)
360                  (const :tag "Undownloaded" always-undownloaded)
361                  (const :tag "Unfetched" unfetched)))
362
363 (defcustom gnus-summary-check-current nil
364   "*If non-nil, consider the current article when moving.
365 The \"unread\" movement commands will stay on the same line if the
366 current article is unread."
367   :group 'gnus-summary-maneuvering
368   :type 'boolean)
369
370 (defcustom gnus-auto-center-summary t
371   "*If non-nil, always center the current summary buffer.
372 In particular, if `vertical' do only vertical recentering.  If non-nil
373 and non-`vertical', do both horizontal and vertical recentering."
374   :group 'gnus-summary-maneuvering
375   :type '(choice (const :tag "none" nil)
376                  (const vertical)
377                  (integer :tag "height")
378                  (sexp :menu-tag "both" t)))
379
380 (defvar gnus-auto-center-group t
381   "*If non-nil, always center the group buffer.")
382
383 (defcustom gnus-show-all-headers nil
384   "*If non-nil, don't hide any headers."
385   :group 'gnus-article-hiding
386   :group 'gnus-article-headers
387   :type 'boolean)
388
389 (defcustom gnus-summary-ignore-duplicates nil
390   "*If non-nil, ignore articles with identical Message-ID headers."
391   :group 'gnus-summary
392   :type 'boolean)
393
394 (defcustom gnus-single-article-buffer t
395   "*If non-nil, display all articles in the same buffer.
396 If nil, each group will get its own article buffer."
397   :group 'gnus-article-various
398   :type 'boolean)
399
400 (defcustom gnus-break-pages t
401   "*If non-nil, do page breaking on articles.
402 The page delimiter is specified by the `gnus-page-delimiter'
403 variable."
404   :group 'gnus-article-various
405   :type 'boolean)
406
407 (defcustom gnus-move-split-methods nil
408   "*Variable used to suggest where articles are to be moved to.
409 It uses the same syntax as the `gnus-split-methods' variable.
410 However, whereas `gnus-split-methods' specifies file names as targets,
411 this variable specifies group names."
412   :group 'gnus-summary-mail
413   :type '(repeat (choice (list :value (fun) function)
414                          (cons :value ("" "") regexp (repeat string))
415                          (sexp :value nil))))
416
417 (defcustom gnus-unread-mark ?           ;Whitespace
418   "*Mark used for unread articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-ticked-mark ?!
423   "*Mark used for ticked articles."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-dormant-mark ??
428   "*Mark used for dormant articles."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-del-mark ?r
433   "*Mark used for del'd articles."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-read-mark ?R
438   "*Mark used for read articles."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-expirable-mark ?E
443   "*Mark used for expirable articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-killed-mark ?K
448   "*Mark used for killed articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-spam-mark ?$
453   "*Mark used for spam articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-souped-mark ?F
458   "*Mark used for souped articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-kill-file-mark ?X
463   "*Mark used for articles killed by kill files."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-low-score-mark ?Y
468   "*Mark used for articles with a low score."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-catchup-mark ?C
473   "*Mark used for articles that are caught up."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-replied-mark ?A
478   "*Mark used for articles that have been replied to."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-forwarded-mark ?F
483   "*Mark used for articles that have been forwarded."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-recent-mark ?N
488   "*Mark used for articles that are recent."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-cached-mark ?*
493   "*Mark used for articles that are in the cache."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-saved-mark ?S
498   "*Mark used for articles that have been saved."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-unseen-mark ?.
503   "*Mark used for articles that haven't been seen."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-no-mark ?               ;Whitespace
508   "*Mark used for articles that have no other secondary mark."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-ancient-mark ?O
513   "*Mark used for ancient articles."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-sparse-mark ?Q
518   "*Mark used for sparsely reffed articles."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-canceled-mark ?G
523   "*Mark used for canceled articles."
524   :group 'gnus-summary-marks
525   :type 'character)
526
527 (defcustom gnus-duplicate-mark ?M
528   "*Mark used for duplicate articles."
529   :group 'gnus-summary-marks
530   :type 'character)
531
532 (defcustom gnus-undownloaded-mark ?-
533   "*Mark used for articles that weren't downloaded."
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-downloaded-mark ?+
538   "*Mark used for articles that were downloaded."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-downloadable-mark ?%
543   "*Mark used for articles that are to be downloaded."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-unsendable-mark ?=
548   "*Mark used for articles that won't be sent."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-score-over-mark ?+
553   "*Score mark used for articles with high scores."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-score-below-mark ?-
558   "*Score mark used for articles with low scores."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-empty-thread-mark ?     ;Whitespace
563   "*There is no thread under the article."
564   :group 'gnus-summary-marks
565   :type 'character)
566
567 (defcustom gnus-not-empty-thread-mark ?=
568   "*There is a thread under the article."
569   :group 'gnus-summary-marks
570   :type 'character)
571
572 (defcustom gnus-view-pseudo-asynchronously nil
573   "*If non-nil, Gnus will view pseudo-articles asynchronously."
574   :group 'gnus-extract-view
575   :type 'boolean)
576
577 (defcustom gnus-auto-expirable-marks
578   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
579         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
580         gnus-souped-mark gnus-duplicate-mark)
581   "*The list of marks converted into expiration if a group is auto-expirable."
582   :version "21.1"
583   :group 'gnus-summary
584   :type '(repeat character))
585
586 (defcustom gnus-inhibit-user-auto-expire t
587   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
588   :version "21.1"
589   :group 'gnus-summary
590   :type 'boolean)
591
592 (defcustom gnus-view-pseudos nil
593   "*If `automatic', pseudo-articles will be viewed automatically.
594 If `not-confirm', pseudos will be viewed automatically, and the user
595 will not be asked to confirm the command."
596   :group 'gnus-extract-view
597   :type '(choice (const :tag "off" nil)
598                  (const automatic)
599                  (const not-confirm)))
600
601 (defcustom gnus-view-pseudos-separately t
602   "*If non-nil, one pseudo-article will be created for each file to be viewed.
603 If nil, all files that use the same viewing command will be given as a
604 list of parameters to that command."
605   :group 'gnus-extract-view
606   :type 'boolean)
607
608 (defcustom gnus-insert-pseudo-articles t
609   "*If non-nil, insert pseudo-articles when decoding articles."
610   :group 'gnus-extract-view
611   :type 'boolean)
612
613 (defcustom gnus-summary-dummy-line-format
614   "   %(:                             :%) %S\n"
615   "*The format specification for the dummy roots in the summary buffer.
616 It works along the same lines as a normal formatting string,
617 with some simple extensions.
618
619 %S  The subject
620
621 General format specifiers can also be used.
622 See `(gnus)Formatting Variables'."
623   :link '(custom-manual "(gnus)Formatting Variables")
624   :group 'gnus-threading
625   :type 'string)
626
627 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
628   "*The format specification for the summary mode line.
629 It works along the same lines as a normal formatting string,
630 with some simple extensions:
631
632 %G  Group name
633 %p  Unprefixed group name
634 %A  Current article number
635 %z  Current article score
636 %V  Gnus version
637 %U  Number of unread articles in the group
638 %e  Number of unselected articles in the group
639 %Z  A string with unread/unselected article counts
640 %g  Shortish group name
641 %S  Subject of the current article
642 %u  User-defined spec
643 %s  Current score file name
644 %d  Number of dormant articles
645 %r  Number of articles that have been marked as read in this session
646 %E  Number of articles expunged by the score files"
647   :group 'gnus-summary-format
648   :type 'string)
649
650 (defcustom gnus-list-identifiers nil
651   "Regexp that matches list identifiers to be removed from subject.
652 This can also be a list of regexps."
653   :version "21.1"
654   :group 'gnus-summary-format
655   :group 'gnus-article-hiding
656   :type '(choice (const :tag "none" nil)
657                  (regexp :value ".*")
658                  (repeat :value (".*") regexp)))
659
660 (defcustom gnus-summary-mark-below 0
661   "*Mark all articles with a score below this variable as read.
662 This variable is local to each summary buffer and usually set by the
663 score file."
664   :group 'gnus-score-default
665   :type 'integer)
666
667 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
668   "*List of functions used for sorting articles in the summary buffer.
669
670 Each function takes two articles and returns non-nil if the first
671 article should be sorted before the other.  If you use more than one
672 function, the primary sort function should be the last.  You should
673 probably always include `gnus-article-sort-by-number' in the list of
674 sorting functions -- preferably first.  Also note that sorting by date
675 is often much slower than sorting by number, and the sorting order is
676 very similar.  (Sorting by date means sorting by the time the message
677 was sent, sorting by number means sorting by arrival time.)
678
679 Ready-made functions include `gnus-article-sort-by-number',
680 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
681 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
682 and `gnus-article-sort-by-score'.
683
684 When threading is turned on, the variable `gnus-thread-sort-functions'
685 controls how articles are sorted."
686   :group 'gnus-summary-sort
687   :type '(repeat (choice (function-item gnus-article-sort-by-number)
688                          (function-item gnus-article-sort-by-author)
689                          (function-item gnus-article-sort-by-subject)
690                          (function-item gnus-article-sort-by-date)
691                          (function-item gnus-article-sort-by-score)
692                          (function-item gnus-article-sort-by-random)
693                          (function :tag "other"))))
694
695 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
696   "*List of functions used for sorting threads in the summary buffer.
697 By default, threads are sorted by article number.
698
699 Each function takes two threads and returns non-nil if the first
700 thread should be sorted before the other.  If you use more than one
701 function, the primary sort function should be the last.  You should
702 probably always include `gnus-thread-sort-by-number' in the list of
703 sorting functions -- preferably first.  Also note that sorting by date
704 is often much slower than sorting by number, and the sorting order is
705 very similar.  (Sorting by date means sorting by the time the message
706 was sent, sorting by number means sorting by arrival time.)
707
708 Ready-made functions include `gnus-thread-sort-by-number',
709 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
710 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
711 `gnus-thread-sort-by-most-recent-number',
712 `gnus-thread-sort-by-most-recent-date',
713 `gnus-thread-sort-by-random', and
714 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
715
716 When threading is turned off, the variable
717 `gnus-article-sort-functions' controls how articles are sorted."
718   :group 'gnus-summary-sort
719   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
720                          (function-item gnus-thread-sort-by-author)
721                          (function-item gnus-thread-sort-by-subject)
722                          (function-item gnus-thread-sort-by-date)
723                          (function-item gnus-thread-sort-by-score)
724                          (function-item gnus-thread-sort-by-total-score)
725                          (function-item gnus-thread-sort-by-random)
726                          (function :tag "other"))))
727
728 (defcustom gnus-thread-score-function '+
729   "*Function used for calculating the total score of a thread.
730
731 The function is called with the scores of the article and each
732 subthread and should then return the score of the thread.
733
734 Some functions you can use are `+', `max', or `min'."
735   :group 'gnus-summary-sort
736   :type 'function)
737
738 (defcustom gnus-summary-expunge-below nil
739   "All articles that have a score less than this variable will be expunged.
740 This variable is local to the summary buffers."
741   :group 'gnus-score-default
742   :type '(choice (const :tag "off" nil)
743                  integer))
744
745 (defcustom gnus-thread-expunge-below nil
746   "All threads that have a total score less than this variable will be expunged.
747 See `gnus-thread-score-function' for en explanation of what a
748 \"thread score\" is.
749
750 This variable is local to the summary buffers."
751   :group 'gnus-threading
752   :group 'gnus-score-default
753   :type '(choice (const :tag "off" nil)
754                  integer))
755
756 (defcustom gnus-summary-mode-hook nil
757   "*A hook for Gnus summary mode.
758 This hook is run before any variables are set in the summary buffer."
759   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
760   :group 'gnus-summary-various
761   :type 'hook)
762
763 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
764 (when (featurep 'xemacs)
765   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
766   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
767   (add-hook 'gnus-summary-mode-hook
768             'gnus-xmas-switch-horizontal-scrollbar-off))
769
770 (defcustom gnus-summary-menu-hook nil
771   "*Hook run after the creation of the summary mode menu."
772   :group 'gnus-summary-visual
773   :type 'hook)
774
775 (defcustom gnus-summary-exit-hook nil
776   "*A hook called on exit from the summary buffer.
777 It will be called with point in the group buffer."
778   :group 'gnus-summary-exit
779   :type 'hook)
780
781 (defcustom gnus-summary-prepare-hook nil
782   "*A hook called after the summary buffer has been generated.
783 If you want to modify the summary buffer, you can use this hook."
784   :group 'gnus-summary-various
785   :type 'hook)
786
787 (defcustom gnus-summary-prepared-hook nil
788   "*A hook called as the last thing after the summary buffer has been generated."
789   :group 'gnus-summary-various
790   :type 'hook)
791
792 (defcustom gnus-summary-generate-hook nil
793   "*A hook run just before generating the summary buffer.
794 This hook is commonly used to customize threading variables and the
795 like."
796   :group 'gnus-summary-various
797   :type 'hook)
798
799 (defcustom gnus-select-group-hook nil
800   "*A hook called when a newsgroup is selected.
801
802 If you'd like to simplify subjects like the
803 `gnus-summary-next-same-subject' command does, you can use the
804 following hook:
805
806  (add-hook gnus-select-group-hook
807            (lambda ()
808              (mapcar (lambda (header)
809                        (mail-header-set-subject
810                         header
811                         (gnus-simplify-subject
812                          (mail-header-subject header) 're-only)))
813                      gnus-newsgroup-headers)))"
814   :group 'gnus-group-select
815   :type 'hook)
816
817 (defcustom gnus-select-article-hook nil
818   "*A hook called when an article is selected."
819   :group 'gnus-summary-choose
820   :options '(gnus-agent-fetch-selected-article)
821   :type 'hook)
822
823 (defcustom gnus-visual-mark-article-hook
824   (list 'gnus-highlight-selected-summary)
825   "*Hook run after selecting an article in the summary buffer.
826 It is meant to be used for highlighting the article in some way.  It
827 is not run if `gnus-visual' is nil."
828   :group 'gnus-summary-visual
829   :type 'hook)
830
831 (defcustom gnus-parse-headers-hook nil
832   "*A hook called before parsing the headers."
833   :group 'gnus-various
834   :type 'hook)
835
836 (defcustom gnus-exit-group-hook nil
837   "*A hook called when exiting summary mode.
838 This hook is not called from the non-updating exit commands like `Q'."
839   :group 'gnus-various
840   :type 'hook)
841
842 (defcustom gnus-summary-update-hook
843   (list 'gnus-summary-highlight-line)
844   "*A hook called when a summary line is changed.
845 The hook will not be called if `gnus-visual' is nil.
846
847 The default function `gnus-summary-highlight-line' will
848 highlight the line according to the `gnus-summary-highlight'
849 variable."
850   :group 'gnus-summary-visual
851   :type 'hook)
852
853 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
854   "*A hook called when an article is selected for the first time.
855 The hook is intended to mark an article as read (or unread)
856 automatically when it is selected."
857   :group 'gnus-summary-choose
858   :type 'hook)
859
860 (defcustom gnus-group-no-more-groups-hook nil
861   "*A hook run when returning to group mode having no more (unread) groups."
862   :group 'gnus-group-select
863   :type 'hook)
864
865 (defcustom gnus-ps-print-hook nil
866   "*A hook run before ps-printing something from Gnus."
867   :group 'gnus-summary
868   :type 'hook)
869
870 (defcustom gnus-summary-article-move-hook nil
871   "*A hook called after an article is moved, copied, respooled, or crossposted."
872   :group 'gnus-summary
873   :type 'hook)
874
875 (defcustom gnus-summary-article-delete-hook nil
876   "*A hook called after an article is deleted."
877   :group 'gnus-summary
878   :type 'hook)
879
880 (defcustom gnus-summary-article-expire-hook nil
881   "*A hook called after an article is expired."
882   :group 'gnus-summary
883   :type 'hook)
884
885 (defcustom gnus-summary-display-arrow
886   (and (fboundp 'display-graphic-p)
887        (display-graphic-p))
888   "*If non-nil, display an arrow highlighting the current article."
889   :version "21.1"
890   :group 'gnus-summary
891   :type 'boolean)
892
893 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
894   "Face used for highlighting the current article in the summary buffer."
895   :group 'gnus-summary-visual
896   :type 'face)
897
898 (defvar gnus-tmp-downloaded nil)
899
900 (defcustom gnus-summary-highlight
901   '(((eq mark gnus-canceled-mark)
902      . gnus-summary-cancelled-face)
903     ((and uncached (> score default-high))
904      . gnus-summary-high-undownloaded-face)
905     ((and uncached (< score default-low))
906      . gnus-summary-low-undownloaded-face)
907     (uncached
908      . gnus-summary-normal-undownloaded-face)
909     ((and (> score default-high)
910           (or (eq mark gnus-dormant-mark)
911               (eq mark gnus-ticked-mark)))
912      . gnus-summary-high-ticked-face)
913     ((and (< score default-low)
914           (or (eq mark gnus-dormant-mark)
915               (eq mark gnus-ticked-mark)))
916      . gnus-summary-low-ticked-face)
917     ((or (eq mark gnus-dormant-mark)
918          (eq mark gnus-ticked-mark))
919      . gnus-summary-normal-ticked-face)
920     ((and (> score default-high) (eq mark gnus-ancient-mark))
921      . gnus-summary-high-ancient-face)
922     ((and (< score default-low) (eq mark gnus-ancient-mark))
923      . gnus-summary-low-ancient-face)
924     ((eq mark gnus-ancient-mark)
925      . gnus-summary-normal-ancient-face)
926     ((and (> score default-high) (eq mark gnus-unread-mark))
927      . gnus-summary-high-unread-face)
928     ((and (< score default-low) (eq mark gnus-unread-mark))
929      . gnus-summary-low-unread-face)
930     ((eq mark gnus-unread-mark)
931      . gnus-summary-normal-unread-face)
932     ((> score default-high)
933      . gnus-summary-high-read-face)
934     ((< score default-low)
935      . gnus-summary-low-read-face)
936     (t
937      . gnus-summary-normal-read-face))
938   "*Controls the highlighting of summary buffer lines.
939
940 A list of (FORM . FACE) pairs.  When deciding how a a particular
941 summary line should be displayed, each form is evaluated.  The content
942 of the face field after the first true form is used.  You can change
943 how those summary lines are displayed, by editing the face field.
944
945 You can use the following variables in the FORM field.
946
947 score:        The article's score
948 default:      The default article score.
949 default-high: The default score for high scored articles.
950 default-low:  The default score for low scored articles.
951 below:        The score below which articles are automatically marked as read.
952 mark:         The articles mark."
953   :group 'gnus-summary-visual
954   :type '(repeat (cons (sexp :tag "Form" nil)
955                        face)))
956
957 (defcustom gnus-alter-header-function nil
958   "Function called to allow alteration of article header structures.
959 The function is called with one parameter, the article header vector,
960 which it may alter in any way."
961   :type '(choice (const :tag "None" nil)
962                  function)
963   :group 'gnus-summary)
964
965 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
966   "Variable that says which function should be used to decode a string with encoded words.")
967
968 (defcustom gnus-extra-headers '(To Newsgroups)
969   "*Extra headers to parse."
970   :version "21.1"
971   :group 'gnus-summary
972   :type '(repeat symbol))
973
974 (defcustom gnus-ignored-from-addresses
975   (and user-mail-address (regexp-quote user-mail-address))
976   "*Regexp of From headers that may be suppressed in favor of To headers."
977   :version "21.1"
978   :group 'gnus-summary
979   :type 'regexp)
980
981 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
982   "List of charsets that should be ignored.
983 When these charsets are used in the \"charset\" parameter, the
984 default charset will be used instead."
985   :version "21.1"
986   :type '(repeat symbol)
987   :group 'gnus-charset)
988
989 (gnus-define-group-parameter
990  ignored-charsets
991  :type list
992  :function-document
993  "Return the ignored charsets of GROUP."
994  :variable gnus-group-ignored-charsets-alist
995  :variable-default
996  '(("alt\\.chinese\\.text" iso-8859-1))
997  :variable-document
998  "Alist of regexps (to match group names) and charsets that should be ignored.
999 When these charsets are used in the \"charset\" parameter, the
1000 default charset will be used instead."
1001  :variable-group gnus-charset
1002  :variable-type '(repeat (cons (regexp :tag "Group")
1003                                (repeat symbol)))
1004  :parameter-type '(choice :tag "Ignored charsets"
1005                           :value nil
1006                           (repeat (symbol)))
1007  :parameter-document       "\
1008 List of charsets that should be ignored.
1009
1010 When these charsets are used in the \"charset\" parameter, the
1011 default charset will be used instead.")
1012
1013 (defcustom gnus-group-highlight-words-alist nil
1014   "Alist of group regexps and highlight regexps.
1015 This variable uses the same syntax as `gnus-emphasis-alist'."
1016   :version "21.1"
1017   :type '(repeat (cons (regexp :tag "Group")
1018                        (repeat (list (regexp :tag "Highlight regexp")
1019                                      (number :tag "Group for entire word" 0)
1020                                      (number :tag "Group for displayed part" 0)
1021                                      (symbol :tag "Face"
1022                                              gnus-emphasis-highlight-words)))))
1023   :group 'gnus-summary-visual)
1024
1025 (defcustom gnus-summary-show-article-charset-alist
1026   nil
1027   "Alist of number and charset.
1028 The article will be shown with the charset corresponding to the
1029 numbered argument.
1030 For example: ((1 . cn-gb-2312) (2 . big5))."
1031   :version "21.1"
1032   :type '(repeat (cons (number :tag "Argument" 1)
1033                        (symbol :tag "Charset")))
1034   :group 'gnus-charset)
1035
1036 (defcustom gnus-preserve-marks t
1037   "Whether marks are preserved when moving, copying and respooling messages."
1038   :version "21.1"
1039   :type 'boolean
1040   :group 'gnus-summary-marks)
1041
1042 (defcustom gnus-alter-articles-to-read-function nil
1043   "Function to be called to alter the list of articles to be selected."
1044   :type '(choice (const nil) function)
1045   :group 'gnus-summary)
1046
1047 (defcustom gnus-orphan-score nil
1048   "*All orphans get this score added.  Set in the score file."
1049   :group 'gnus-score-default
1050   :type '(choice (const nil)
1051                  integer))
1052
1053 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1054   "*A regexp to match MIME parts when saving multiple parts of a
1055 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1056 This regexp will be used by default when prompting the user for which
1057 type of files to save."
1058   :group 'gnus-summary
1059   :type 'regexp)
1060
1061 (defcustom gnus-read-all-available-headers nil
1062   "Whether Gnus should parse all headers made available to it.
1063 This is mostly relevant for slow backends where the user may
1064 wish to widen the summary buffer to include all headers
1065 that were fetched.  Say, for nnultimate groups."
1066   :group 'gnus-summary
1067   :type '(choice boolean regexp))
1068
1069 (defcustom gnus-summary-muttprint-program "muttprint"
1070   "Command (and optional arguments) used to run Muttprint."
1071   :version "21.3"
1072   :group 'gnus-summary
1073   :type 'string)
1074
1075 (defcustom gnus-article-loose-mime nil
1076   "If non-nil, don't require MIME-Version header.
1077 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1078 supply the MIME-Version header or deliberately strip it From the mail.
1079 Set it to non-nil, Gnus will treat some articles as MIME even if
1080 the MIME-Version header is missed."
1081   :version "21.3"
1082   :type 'boolean
1083   :group 'gnus-article)
1084
1085 (defcustom gnus-article-emulate-mime t
1086   "If non-nil, use MIME emulation for uuencode and the like.
1087 This means that Gnus will search message bodies for text that look
1088 like uuencoded bits, yEncoded bits, and so on, and present that using
1089 the normal Gnus MIME machinery."
1090   :type 'boolean
1091   :group 'gnus-article)
1092
1093 ;;; Internal variables
1094
1095 (defvar gnus-summary-display-cache nil)
1096 (defvar gnus-article-mime-handles nil)
1097 (defvar gnus-article-decoded-p nil)
1098 (defvar gnus-article-charset nil)
1099 (defvar gnus-article-ignored-charsets nil)
1100 (defvar gnus-scores-exclude-files nil)
1101 (defvar gnus-page-broken nil)
1102
1103 (defvar gnus-original-article nil)
1104 (defvar gnus-article-internal-prepare-hook nil)
1105 (defvar gnus-newsgroup-process-stack nil)
1106
1107 (defvar gnus-thread-indent-array nil)
1108 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1109 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1110   "Function called to sort the articles within a thread after it has been gathered together.")
1111
1112 (defvar gnus-summary-save-parts-type-history nil)
1113 (defvar gnus-summary-save-parts-last-directory nil)
1114
1115 ;; Avoid highlighting in kill files.
1116 (defvar gnus-summary-inhibit-highlight nil)
1117 (defvar gnus-newsgroup-selected-overlay nil)
1118 (defvar gnus-inhibit-limiting nil)
1119 (defvar gnus-newsgroup-adaptive-score-file nil)
1120 (defvar gnus-current-score-file nil)
1121 (defvar gnus-current-move-group nil)
1122 (defvar gnus-current-copy-group nil)
1123 (defvar gnus-current-crosspost-group nil)
1124 (defvar gnus-newsgroup-display nil)
1125
1126 (defvar gnus-newsgroup-dependencies nil)
1127 (defvar gnus-newsgroup-adaptive nil)
1128 (defvar gnus-summary-display-article-function nil)
1129 (defvar gnus-summary-highlight-line-function nil
1130   "Function called after highlighting a summary line.")
1131
1132 (defvar gnus-summary-line-format-alist
1133   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1134     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1135     (?s gnus-tmp-subject-or-nil ?s)
1136     (?n gnus-tmp-name ?s)
1137     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1138         ?s)
1139     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1140             gnus-tmp-from) ?s)
1141     (?F gnus-tmp-from ?s)
1142     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1143     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1144     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1145     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1146     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1147     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1148     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1149     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1150     (?L gnus-tmp-lines ?s)
1151     (?O gnus-tmp-downloaded ?c)
1152     (?I gnus-tmp-indentation ?s)
1153     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1154     (?R gnus-tmp-replied ?c)
1155     (?\[ gnus-tmp-opening-bracket ?c)
1156     (?\] gnus-tmp-closing-bracket ?c)
1157     (?\> (make-string gnus-tmp-level ? ) ?s)
1158     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1159     (?i gnus-tmp-score ?d)
1160     (?z gnus-tmp-score-char ?c)
1161     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1162     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1163     (?U gnus-tmp-unread ?c)
1164     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1165         ?s)
1166     (?t (gnus-summary-number-of-articles-in-thread
1167          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1168         ?d)
1169     (?e (gnus-summary-number-of-articles-in-thread
1170          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1171         ?c)
1172     (?u gnus-tmp-user-defined ?s)
1173     (?P (gnus-pick-line-number) ?d)
1174     (?B gnus-tmp-thread-tree-header-string ?s)
1175     (user-date (gnus-user-date
1176                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1177   "An alist of format specifications that can appear in summary lines.
1178 These are paired with what variables they correspond with, along with
1179 the type of the variable (string, integer, character, etc).")
1180
1181 (defvar gnus-summary-dummy-line-format-alist
1182   `((?S gnus-tmp-subject ?s)
1183     (?N gnus-tmp-number ?d)
1184     (?u gnus-tmp-user-defined ?s)))
1185
1186 (defvar gnus-summary-mode-line-format-alist
1187   `((?G gnus-tmp-group-name ?s)
1188     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1189     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1190     (?A gnus-tmp-article-number ?d)
1191     (?Z gnus-tmp-unread-and-unselected ?s)
1192     (?V gnus-version ?s)
1193     (?U gnus-tmp-unread-and-unticked ?d)
1194     (?S gnus-tmp-subject ?s)
1195     (?e gnus-tmp-unselected ?d)
1196     (?u gnus-tmp-user-defined ?s)
1197     (?d (length gnus-newsgroup-dormant) ?d)
1198     (?t (length gnus-newsgroup-marked) ?d)
1199     (?h (length gnus-newsgroup-spam-marked) ?d)
1200     (?r (length gnus-newsgroup-reads) ?d)
1201     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1202     (?E gnus-newsgroup-expunged-tally ?d)
1203     (?s (gnus-current-score-file-nondirectory) ?s)))
1204
1205 (defvar gnus-last-search-regexp nil
1206   "Default regexp for article search command.")
1207
1208 (defvar gnus-last-shell-command nil
1209   "Default shell command on article.")
1210
1211 (defvar gnus-newsgroup-agentized nil
1212   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1213 (defvar gnus-newsgroup-begin nil)
1214 (defvar gnus-newsgroup-end nil)
1215 (defvar gnus-newsgroup-last-rmail nil)
1216 (defvar gnus-newsgroup-last-mail nil)
1217 (defvar gnus-newsgroup-last-folder nil)
1218 (defvar gnus-newsgroup-last-file nil)
1219 (defvar gnus-newsgroup-auto-expire nil)
1220 (defvar gnus-newsgroup-active nil)
1221
1222 (defvar gnus-newsgroup-data nil)
1223 (defvar gnus-newsgroup-data-reverse nil)
1224 (defvar gnus-newsgroup-limit nil)
1225 (defvar gnus-newsgroup-limits nil)
1226
1227 (defvar gnus-newsgroup-unreads nil
1228   "Sorted list of unread articles in the current newsgroup.")
1229
1230 (defvar gnus-newsgroup-unselected nil
1231   "Sorted list of unselected unread articles in the current newsgroup.")
1232
1233 (defvar gnus-newsgroup-reads nil
1234   "Alist of read articles and article marks in the current newsgroup.")
1235
1236 (defvar gnus-newsgroup-expunged-tally nil)
1237
1238 (defvar gnus-newsgroup-marked nil
1239   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1240
1241 (defvar gnus-newsgroup-spam-marked nil
1242   "List of ranges of articles that have been marked as spam.")
1243
1244 (defvar gnus-newsgroup-killed nil
1245   "List of ranges of articles that have been through the scoring process.")
1246
1247 (defvar gnus-newsgroup-cached nil
1248   "Sorted list of articles that come from the article cache.")
1249
1250 (defvar gnus-newsgroup-saved nil
1251   "List of articles that have been saved.")
1252
1253 (defvar gnus-newsgroup-kill-headers nil)
1254
1255 (defvar gnus-newsgroup-replied nil
1256   "List of articles that have been replied to in the current newsgroup.")
1257
1258 (defvar gnus-newsgroup-forwarded nil
1259   "List of articles that have been forwarded in the current newsgroup.")
1260
1261 (defvar gnus-newsgroup-recent nil
1262   "List of articles that have are recent in the current newsgroup.")
1263
1264 (defvar gnus-newsgroup-expirable nil
1265   "Sorted list of articles in the current newsgroup that can be expired.")
1266
1267 (defvar gnus-newsgroup-processable nil
1268   "List of articles in the current newsgroup that can be processed.")
1269
1270 (defvar gnus-newsgroup-downloadable nil
1271   "Sorted list of articles in the current newsgroup that can be processed.")
1272
1273 (defvar gnus-newsgroup-unfetched nil
1274   "Sorted list of articles in the current newsgroup whose headers have
1275 not been fetched into the agent.
1276
1277 This list will always be a subset of gnus-newsgroup-undownloaded.")
1278
1279 (defvar gnus-newsgroup-undownloaded nil
1280   "List of articles in the current newsgroup that haven't been downloaded.")
1281
1282 (defvar gnus-newsgroup-unsendable nil
1283   "List of articles in the current newsgroup that won't be sent.")
1284
1285 (defvar gnus-newsgroup-bookmarks nil
1286   "List of articles in the current newsgroup that have bookmarks.")
1287
1288 (defvar gnus-newsgroup-dormant nil
1289   "Sorted list of dormant articles in the current newsgroup.")
1290
1291 (defvar gnus-newsgroup-unseen nil
1292   "List of unseen articles in the current newsgroup.")
1293
1294 (defvar gnus-newsgroup-seen nil
1295   "Range of seen articles in the current newsgroup.")
1296
1297 (defvar gnus-newsgroup-articles nil
1298   "List of articles in the current newsgroup.")
1299
1300 (defvar gnus-newsgroup-scored nil
1301   "List of scored articles in the current newsgroup.")
1302
1303 (defvar gnus-newsgroup-headers nil
1304   "List of article headers in the current newsgroup.")
1305
1306 (defvar gnus-newsgroup-threads nil)
1307
1308 (defvar gnus-newsgroup-prepared nil
1309   "Whether the current group has been prepared properly.")
1310
1311 (defvar gnus-newsgroup-ancient nil
1312   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1313
1314 (defvar gnus-newsgroup-sparse nil)
1315
1316 (defvar gnus-current-article nil)
1317 (defvar gnus-article-current nil)
1318 (defvar gnus-current-headers nil)
1319 (defvar gnus-have-all-headers nil)
1320 (defvar gnus-last-article nil)
1321 (defvar gnus-newsgroup-history nil)
1322 (defvar gnus-newsgroup-charset nil)
1323 (defvar gnus-newsgroup-ephemeral-charset nil)
1324 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1325
1326 (defvar gnus-article-before-search nil)
1327
1328 (defconst gnus-summary-local-variables
1329   '(gnus-newsgroup-name
1330     gnus-newsgroup-begin gnus-newsgroup-end
1331     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1332     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1333     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1334     gnus-newsgroup-unselected gnus-newsgroup-marked
1335     gnus-newsgroup-spam-marked
1336     gnus-newsgroup-reads gnus-newsgroup-saved
1337     gnus-newsgroup-replied gnus-newsgroup-forwarded
1338     gnus-newsgroup-recent
1339     gnus-newsgroup-expirable
1340     gnus-newsgroup-processable gnus-newsgroup-killed
1341     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1342     gnus-newsgroup-unfetched
1343     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1344     gnus-newsgroup-seen gnus-newsgroup-articles
1345     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1346     gnus-newsgroup-headers gnus-newsgroup-threads
1347     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1348     gnus-current-article gnus-current-headers gnus-have-all-headers
1349     gnus-last-article gnus-article-internal-prepare-hook
1350     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1351     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1352     gnus-thread-expunge-below
1353     gnus-score-alist gnus-current-score-file
1354     (gnus-summary-expunge-below . global)
1355     (gnus-summary-mark-below . global)
1356     (gnus-orphan-score . global)
1357     gnus-newsgroup-active gnus-scores-exclude-files
1358     gnus-newsgroup-history gnus-newsgroup-ancient
1359     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1360     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1361     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1362     (gnus-newsgroup-expunged-tally . 0)
1363     gnus-cache-removable-articles gnus-newsgroup-cached
1364     gnus-newsgroup-data gnus-newsgroup-data-reverse
1365     gnus-newsgroup-limit gnus-newsgroup-limits
1366     gnus-newsgroup-charset gnus-newsgroup-display)
1367   "Variables that are buffer-local to the summary buffers.")
1368
1369 (defvar gnus-newsgroup-variables nil
1370   "A list of variables that have separate values in different newsgroups.
1371 A list of newsgroup (summary buffer) local variables, or cons of
1372 variables and their default values (when the default values are not
1373 nil), that should be made global while the summary buffer is active.
1374 These variables can be used to set variables in the group parameters
1375 while still allowing them to affect operations done in other
1376 buffers. For example:
1377
1378 \(setq gnus-newsgroup-variables
1379      '(message-use-followup-to
1380        (gnus-visible-headers .
1381          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1382 ")
1383
1384 ;; Byte-compiler warning.
1385 ;(eval-when-compile (defvar gnus-article-mode-map))
1386 (eval-when-compile
1387   (let ((features (cons 'gnus-sum features)))
1388     (require 'gnus)
1389     (require 'gnus-agent)
1390     (require 'gnus-art)))
1391
1392 ;; MIME stuff.
1393
1394 (defvar gnus-decode-encoded-word-methods
1395   '(mail-decode-encoded-word-string)
1396   "List of methods used to decode encoded words.
1397
1398 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1399 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1400 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1401 whose names match REGEXP.
1402
1403 For example:
1404 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1405  mail-decode-encoded-word-string
1406  (\"chinese\" . rfc1843-decode-string))")
1407
1408 (defvar gnus-decode-encoded-word-methods-cache nil)
1409
1410 (defun gnus-multi-decode-encoded-word-string (string)
1411   "Apply the functions from `gnus-encoded-word-methods' that match."
1412   (unless (and gnus-decode-encoded-word-methods-cache
1413                (eq gnus-newsgroup-name
1414                    (car gnus-decode-encoded-word-methods-cache)))
1415     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1416     (mapcar (lambda (x)
1417               (if (symbolp x)
1418                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1419                 (if (and gnus-newsgroup-name
1420                          (string-match (car x) gnus-newsgroup-name))
1421                     (nconc gnus-decode-encoded-word-methods-cache
1422                            (list (cdr x))))))
1423             gnus-decode-encoded-word-methods))
1424   (let ((xlist gnus-decode-encoded-word-methods-cache))
1425     (pop xlist)
1426     (while xlist
1427       (setq string (funcall (pop xlist) string))))
1428   string)
1429
1430 ;; Subject simplification.
1431
1432 (defun gnus-simplify-whitespace (str)
1433   "Remove excessive whitespace from STR."
1434   ;; Multiple spaces.
1435   (while (string-match "[ \t][ \t]+" str)
1436     (setq str (concat (substring str 0 (match-beginning 0))
1437                         " "
1438                         (substring str (match-end 0)))))
1439   ;; Leading spaces.
1440   (when (string-match "^[ \t]+" str)
1441     (setq str (substring str (match-end 0))))
1442   ;; Trailing spaces.
1443   (when (string-match "[ \t]+$" str)
1444     (setq str (substring str 0 (match-beginning 0))))
1445   str)
1446
1447 (defun gnus-simplify-all-whitespace (str)
1448   "Remove all whitespace from STR."
1449   (while (string-match "[ \t\n]+" str)
1450     (setq str (replace-match "" nil nil str)))
1451   str)
1452
1453 (defsubst gnus-simplify-subject-re (subject)
1454   "Remove \"Re:\" from subject lines."
1455   (if (string-match message-subject-re-regexp subject)
1456       (substring subject (match-end 0))
1457     subject))
1458
1459 (defun gnus-simplify-subject (subject &optional re-only)
1460   "Remove `Re:' and words in parentheses.
1461 If RE-ONLY is non-nil, strip leading `Re:'s only."
1462   (let ((case-fold-search t))           ;Ignore case.
1463     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1464     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1465       (setq subject (substring subject (match-end 0))))
1466     ;; Remove uninteresting prefixes.
1467     (when (and (not re-only)
1468                gnus-simplify-ignored-prefixes
1469                (string-match gnus-simplify-ignored-prefixes subject))
1470       (setq subject (substring subject (match-end 0))))
1471     ;; Remove words in parentheses from end.
1472     (unless re-only
1473       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1474         (setq subject (substring subject 0 (match-beginning 0)))))
1475     ;; Return subject string.
1476     subject))
1477
1478 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1479 ;; all whitespace.
1480 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1481   (goto-char (point-min))
1482   (while (re-search-forward regexp nil t)
1483     (replace-match (or newtext ""))))
1484
1485 (defun gnus-simplify-buffer-fuzzy ()
1486   "Simplify string in the buffer fuzzily.
1487 The string in the accessible portion of the current buffer is simplified.
1488 It is assumed to be a single-line subject.
1489 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1490 matter is removed.  Additional things can be deleted by setting
1491 `gnus-simplify-subject-fuzzy-regexp'."
1492   (let ((case-fold-search t)
1493         (modified-tick))
1494     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1495
1496     (while (not (eq modified-tick (buffer-modified-tick)))
1497       (setq modified-tick (buffer-modified-tick))
1498       (cond
1499        ((listp gnus-simplify-subject-fuzzy-regexp)
1500         (mapcar 'gnus-simplify-buffer-fuzzy-step
1501                 gnus-simplify-subject-fuzzy-regexp))
1502        (gnus-simplify-subject-fuzzy-regexp
1503         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1504       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1505       (gnus-simplify-buffer-fuzzy-step
1506        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1507       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1508
1509     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1510     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1511     (gnus-simplify-buffer-fuzzy-step " $")
1512     (gnus-simplify-buffer-fuzzy-step "^ +")))
1513
1514 (defun gnus-simplify-subject-fuzzy (subject)
1515   "Simplify a subject string fuzzily.
1516 See `gnus-simplify-buffer-fuzzy' for details."
1517   (save-excursion
1518     (gnus-set-work-buffer)
1519     (let ((case-fold-search t))
1520       ;; Remove uninteresting prefixes.
1521       (when (and gnus-simplify-ignored-prefixes
1522                  (string-match gnus-simplify-ignored-prefixes subject))
1523         (setq subject (substring subject (match-end 0))))
1524       (insert subject)
1525       (inline (gnus-simplify-buffer-fuzzy))
1526       (buffer-string))))
1527
1528 (defsubst gnus-simplify-subject-fully (subject)
1529   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1530   (cond
1531    (gnus-simplify-subject-functions
1532     (gnus-map-function gnus-simplify-subject-functions subject))
1533    ((null gnus-summary-gather-subject-limit)
1534     (gnus-simplify-subject-re subject))
1535    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1536     (gnus-simplify-subject-fuzzy subject))
1537    ((numberp gnus-summary-gather-subject-limit)
1538     (gnus-limit-string (gnus-simplify-subject-re subject)
1539                        gnus-summary-gather-subject-limit))
1540    (t
1541     subject)))
1542
1543 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1544   "Check whether two subjects are equal.
1545 If optional argument SIMPLE-FIRST is t, first argument is already
1546 simplified."
1547   (cond
1548    ((null simple-first)
1549     (equal (gnus-simplify-subject-fully s1)
1550            (gnus-simplify-subject-fully s2)))
1551    (t
1552     (equal s1
1553            (gnus-simplify-subject-fully s2)))))
1554
1555 (defun gnus-summary-bubble-group ()
1556   "Increase the score of the current group.
1557 This is a handy function to add to `gnus-summary-exit-hook' to
1558 increase the score of each group you read."
1559   (gnus-group-add-score gnus-newsgroup-name))
1560
1561 \f
1562 ;;;
1563 ;;; Gnus summary mode
1564 ;;;
1565
1566 (put 'gnus-summary-mode 'mode-class 'special)
1567
1568 (defvar gnus-article-commands-menu)
1569
1570 ;; Non-orthogonal keys
1571
1572 (gnus-define-keys gnus-summary-mode-map
1573   " " gnus-summary-next-page
1574   "\177" gnus-summary-prev-page
1575   [delete] gnus-summary-prev-page
1576   [backspace] gnus-summary-prev-page
1577   "\r" gnus-summary-scroll-up
1578   "\M-\r" gnus-summary-scroll-down
1579   "n" gnus-summary-next-unread-article
1580   "p" gnus-summary-prev-unread-article
1581   "N" gnus-summary-next-article
1582   "P" gnus-summary-prev-article
1583   "\M-\C-n" gnus-summary-next-same-subject
1584   "\M-\C-p" gnus-summary-prev-same-subject
1585   "\M-n" gnus-summary-next-unread-subject
1586   "\M-p" gnus-summary-prev-unread-subject
1587   "." gnus-summary-first-unread-article
1588   "," gnus-summary-best-unread-article
1589   "\M-s" gnus-summary-search-article-forward
1590   "\M-r" gnus-summary-search-article-backward
1591   "<" gnus-summary-beginning-of-article
1592   ">" gnus-summary-end-of-article
1593   "j" gnus-summary-goto-article
1594   "^" gnus-summary-refer-parent-article
1595   "\M-^" gnus-summary-refer-article
1596   "u" gnus-summary-tick-article-forward
1597   "!" gnus-summary-tick-article-forward
1598   "U" gnus-summary-tick-article-backward
1599   "d" gnus-summary-mark-as-read-forward
1600   "D" gnus-summary-mark-as-read-backward
1601   "E" gnus-summary-mark-as-expirable
1602   "\M-u" gnus-summary-clear-mark-forward
1603   "\M-U" gnus-summary-clear-mark-backward
1604   "k" gnus-summary-kill-same-subject-and-select
1605   "\C-k" gnus-summary-kill-same-subject
1606   "\M-\C-k" gnus-summary-kill-thread
1607   "\M-\C-l" gnus-summary-lower-thread
1608   "e" gnus-summary-edit-article
1609   "#" gnus-summary-mark-as-processable
1610   "\M-#" gnus-summary-unmark-as-processable
1611   "\M-\C-t" gnus-summary-toggle-threads
1612   "\M-\C-s" gnus-summary-show-thread
1613   "\M-\C-h" gnus-summary-hide-thread
1614   "\M-\C-f" gnus-summary-next-thread
1615   "\M-\C-b" gnus-summary-prev-thread
1616   [(meta down)] gnus-summary-next-thread
1617   [(meta up)] gnus-summary-prev-thread
1618   "\M-\C-u" gnus-summary-up-thread
1619   "\M-\C-d" gnus-summary-down-thread
1620   "&" gnus-summary-execute-command
1621   "c" gnus-summary-catchup-and-exit
1622   "\C-w" gnus-summary-mark-region-as-read
1623   "\C-t" gnus-summary-toggle-truncation
1624   "?" gnus-summary-mark-as-dormant
1625   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1626   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1627   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1628   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1629   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1630   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1631   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1632   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1633   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1634   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1635   "=" gnus-summary-expand-window
1636   "\C-x\C-s" gnus-summary-reselect-current-group
1637   "\M-g" gnus-summary-rescan-group
1638   "w" gnus-summary-stop-page-breaking
1639   "\C-c\C-r" gnus-summary-caesar-message
1640   "f" gnus-summary-followup
1641   "F" gnus-summary-followup-with-original
1642   "C" gnus-summary-cancel-article
1643   "r" gnus-summary-reply
1644   "R" gnus-summary-reply-with-original
1645   "\C-c\C-f" gnus-summary-mail-forward
1646   "o" gnus-summary-save-article
1647   "\C-o" gnus-summary-save-article-mail
1648   "|" gnus-summary-pipe-output
1649   "\M-k" gnus-summary-edit-local-kill
1650   "\M-K" gnus-summary-edit-global-kill
1651   ;; "V" gnus-version
1652   "\C-c\C-d" gnus-summary-describe-group
1653   "q" gnus-summary-exit
1654   "Q" gnus-summary-exit-no-update
1655   "\C-c\C-i" gnus-info-find-node
1656   gnus-mouse-2 gnus-mouse-pick-article
1657   "m" gnus-summary-mail-other-window
1658   "a" gnus-summary-post-news
1659   "i" gnus-summary-news-other-window
1660   "x" gnus-summary-limit-to-unread
1661   "s" gnus-summary-isearch-article
1662   "t" gnus-summary-toggle-header
1663   "g" gnus-summary-show-article
1664   "l" gnus-summary-goto-last-article
1665   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1666   "\C-d" gnus-summary-enter-digest-group
1667   "\M-\C-d" gnus-summary-read-document
1668   "\M-\C-e" gnus-summary-edit-parameters
1669   "\M-\C-a" gnus-summary-customize-parameters
1670   "\C-c\C-b" gnus-bug
1671   "*" gnus-cache-enter-article
1672   "\M-*" gnus-cache-remove-article
1673   "\M-&" gnus-summary-universal-argument
1674   "\C-l" gnus-recenter
1675   "I" gnus-summary-increase-score
1676   "L" gnus-summary-lower-score
1677   "\M-i" gnus-symbolic-argument
1678   "h" gnus-summary-select-article-buffer
1679
1680   "b" gnus-article-view-part
1681   "\M-t" gnus-summary-toggle-display-buttonized
1682
1683   "V" gnus-summary-score-map
1684   "X" gnus-uu-extract-map
1685   "S" gnus-summary-send-map)
1686
1687 ;; Sort of orthogonal keymap
1688 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1689   "t" gnus-summary-tick-article-forward
1690   "!" gnus-summary-tick-article-forward
1691   "d" gnus-summary-mark-as-read-forward
1692   "r" gnus-summary-mark-as-read-forward
1693   "c" gnus-summary-clear-mark-forward
1694   " " gnus-summary-clear-mark-forward
1695   "e" gnus-summary-mark-as-expirable
1696   "x" gnus-summary-mark-as-expirable
1697   "?" gnus-summary-mark-as-dormant
1698   "b" gnus-summary-set-bookmark
1699   "B" gnus-summary-remove-bookmark
1700   "#" gnus-summary-mark-as-processable
1701   "\M-#" gnus-summary-unmark-as-processable
1702   "S" gnus-summary-limit-include-expunged
1703   "C" gnus-summary-catchup
1704   "H" gnus-summary-catchup-to-here
1705   "h" gnus-summary-catchup-from-here
1706   "\C-c" gnus-summary-catchup-all
1707   "k" gnus-summary-kill-same-subject-and-select
1708   "K" gnus-summary-kill-same-subject
1709   "P" gnus-uu-mark-map)
1710
1711 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1712   "c" gnus-summary-clear-above
1713   "u" gnus-summary-tick-above
1714   "m" gnus-summary-mark-above
1715   "k" gnus-summary-kill-below)
1716
1717 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1718   "/" gnus-summary-limit-to-subject
1719   "n" gnus-summary-limit-to-articles
1720   "w" gnus-summary-pop-limit
1721   "s" gnus-summary-limit-to-subject
1722   "a" gnus-summary-limit-to-author
1723   "u" gnus-summary-limit-to-unread
1724   "m" gnus-summary-limit-to-marks
1725   "M" gnus-summary-limit-exclude-marks
1726   "v" gnus-summary-limit-to-score
1727   "*" gnus-summary-limit-include-cached
1728   "D" gnus-summary-limit-include-dormant
1729   "T" gnus-summary-limit-include-thread
1730   "d" gnus-summary-limit-exclude-dormant
1731   "t" gnus-summary-limit-to-age
1732   "." gnus-summary-limit-to-unseen
1733   "x" gnus-summary-limit-to-extra
1734   "p" gnus-summary-limit-to-display-predicate
1735   "E" gnus-summary-limit-include-expunged
1736   "c" gnus-summary-limit-exclude-childless-dormant
1737   "C" gnus-summary-limit-mark-excluded-as-read
1738   "o" gnus-summary-insert-old-articles
1739   "N" gnus-summary-insert-new-articles)
1740
1741 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1742   "n" gnus-summary-next-unread-article
1743   "p" gnus-summary-prev-unread-article
1744   "N" gnus-summary-next-article
1745   "P" gnus-summary-prev-article
1746   "\C-n" gnus-summary-next-same-subject
1747   "\C-p" gnus-summary-prev-same-subject
1748   "\M-n" gnus-summary-next-unread-subject
1749   "\M-p" gnus-summary-prev-unread-subject
1750   "f" gnus-summary-first-unread-article
1751   "b" gnus-summary-best-unread-article
1752   "j" gnus-summary-goto-article
1753   "g" gnus-summary-goto-subject
1754   "l" gnus-summary-goto-last-article
1755   "o" gnus-summary-pop-article)
1756
1757 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1758   "k" gnus-summary-kill-thread
1759   "l" gnus-summary-lower-thread
1760   "i" gnus-summary-raise-thread
1761   "T" gnus-summary-toggle-threads
1762   "t" gnus-summary-rethread-current
1763   "^" gnus-summary-reparent-thread
1764   "s" gnus-summary-show-thread
1765   "S" gnus-summary-show-all-threads
1766   "h" gnus-summary-hide-thread
1767   "H" gnus-summary-hide-all-threads
1768   "n" gnus-summary-next-thread
1769   "p" gnus-summary-prev-thread
1770   "u" gnus-summary-up-thread
1771   "o" gnus-summary-top-thread
1772   "d" gnus-summary-down-thread
1773   "#" gnus-uu-mark-thread
1774   "\M-#" gnus-uu-unmark-thread)
1775
1776 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1777   "g" gnus-summary-prepare
1778   "c" gnus-summary-insert-cached-articles
1779   "d" gnus-summary-insert-dormant-articles)
1780
1781 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1782   "c" gnus-summary-catchup-and-exit
1783   "C" gnus-summary-catchup-all-and-exit
1784   "E" gnus-summary-exit-no-update
1785   "Q" gnus-summary-exit
1786   "Z" gnus-summary-exit
1787   "n" gnus-summary-catchup-and-goto-next-group
1788   "R" gnus-summary-reselect-current-group
1789   "G" gnus-summary-rescan-group
1790   "N" gnus-summary-next-group
1791   "s" gnus-summary-save-newsrc
1792   "P" gnus-summary-prev-group)
1793
1794 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1795   " " gnus-summary-next-page
1796   "n" gnus-summary-next-page
1797   "\177" gnus-summary-prev-page
1798   [delete] gnus-summary-prev-page
1799   "p" gnus-summary-prev-page
1800   "\r" gnus-summary-scroll-up
1801   "\M-\r" gnus-summary-scroll-down
1802   "<" gnus-summary-beginning-of-article
1803   ">" gnus-summary-end-of-article
1804   "b" gnus-summary-beginning-of-article
1805   "e" gnus-summary-end-of-article
1806   "^" gnus-summary-refer-parent-article
1807   "r" gnus-summary-refer-parent-article
1808   "D" gnus-summary-enter-digest-group
1809   "R" gnus-summary-refer-references
1810   "T" gnus-summary-refer-thread
1811   "g" gnus-summary-show-article
1812   "s" gnus-summary-isearch-article
1813   "P" gnus-summary-print-article
1814   "M" gnus-mailing-list-insinuate
1815   "t" gnus-article-babel)
1816
1817 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1818   "b" gnus-article-add-buttons
1819   "B" gnus-article-add-buttons-to-head
1820   "o" gnus-article-treat-overstrike
1821   "e" gnus-article-emphasize
1822   "w" gnus-article-fill-cited-article
1823   "Q" gnus-article-fill-long-lines
1824   "C" gnus-article-capitalize-sentences
1825   "c" gnus-article-remove-cr
1826   "q" gnus-article-de-quoted-unreadable
1827   "6" gnus-article-de-base64-unreadable
1828   "Z" gnus-article-decode-HZ
1829   "h" gnus-article-wash-html
1830   "u" gnus-article-unsplit-urls
1831   "s" gnus-summary-force-verify-and-decrypt
1832   "f" gnus-article-display-x-face
1833   "l" gnus-summary-stop-page-breaking
1834   "r" gnus-summary-caesar-message
1835   "m" gnus-summary-morse-message
1836   "t" gnus-summary-toggle-header
1837   "g" gnus-treat-smiley
1838   "v" gnus-summary-verbose-headers
1839   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1840   "p" gnus-article-verify-x-pgp-sig
1841   "d" gnus-article-treat-dumbquotes)
1842
1843 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1844   ;; mnemonic: deuglif*Y*
1845   "u" gnus-article-outlook-unwrap-lines
1846   "a" gnus-article-outlook-repair-attribution
1847   "c" gnus-article-outlook-rearrange-citation
1848   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1849
1850 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1851   "a" gnus-article-hide
1852   "h" gnus-article-hide-headers
1853   "b" gnus-article-hide-boring-headers
1854   "s" gnus-article-hide-signature
1855   "c" gnus-article-hide-citation
1856   "C" gnus-article-hide-citation-in-followups
1857   "l" gnus-article-hide-list-identifiers
1858   "B" gnus-article-strip-banner
1859   "P" gnus-article-hide-pem
1860   "\C-c" gnus-article-hide-citation-maybe)
1861
1862 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1863   "a" gnus-article-highlight
1864   "h" gnus-article-highlight-headers
1865   "c" gnus-article-highlight-citation
1866   "s" gnus-article-highlight-signature)
1867
1868 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1869   "f" gnus-article-treat-fold-headers
1870   "u" gnus-article-treat-unfold-headers
1871   "n" gnus-article-treat-fold-newsgroups)
1872
1873 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1874   "x" gnus-article-display-x-face
1875   "d" gnus-article-display-face
1876   "s" gnus-treat-smiley
1877   "D" gnus-article-remove-images
1878   "f" gnus-treat-from-picon
1879   "m" gnus-treat-mail-picon
1880   "n" gnus-treat-newsgroups-picon)
1881
1882 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1883   "w" gnus-article-decode-mime-words
1884   "c" gnus-article-decode-charset
1885   "v" gnus-mime-view-all-parts
1886   "b" gnus-article-view-part)
1887
1888 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1889   "z" gnus-article-date-ut
1890   "u" gnus-article-date-ut
1891   "l" gnus-article-date-local
1892   "p" gnus-article-date-english
1893   "e" gnus-article-date-lapsed
1894   "o" gnus-article-date-original
1895   "i" gnus-article-date-iso8601
1896   "s" gnus-article-date-user)
1897
1898 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1899   "t" gnus-article-remove-trailing-blank-lines
1900   "l" gnus-article-strip-leading-blank-lines
1901   "m" gnus-article-strip-multiple-blank-lines
1902   "a" gnus-article-strip-blank-lines
1903   "A" gnus-article-strip-all-blank-lines
1904   "s" gnus-article-strip-leading-space
1905   "e" gnus-article-strip-trailing-space
1906   "w" gnus-article-remove-leading-whitespace)
1907
1908 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1909   "v" gnus-version
1910   "f" gnus-summary-fetch-faq
1911   "d" gnus-summary-describe-group
1912   "h" gnus-summary-describe-briefly
1913   "i" gnus-info-find-node
1914   "c" gnus-group-fetch-charter
1915   "C" gnus-group-fetch-control)
1916
1917 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1918   "e" gnus-summary-expire-articles
1919   "\M-\C-e" gnus-summary-expire-articles-now
1920   "\177" gnus-summary-delete-article
1921   [delete] gnus-summary-delete-article
1922   [backspace] gnus-summary-delete-article
1923   "m" gnus-summary-move-article
1924   "r" gnus-summary-respool-article
1925   "w" gnus-summary-edit-article
1926   "c" gnus-summary-copy-article
1927   "B" gnus-summary-crosspost-article
1928   "q" gnus-summary-respool-query
1929   "t" gnus-summary-respool-trace
1930   "i" gnus-summary-import-article
1931   "I" gnus-summary-create-article
1932   "p" gnus-summary-article-posted-p)
1933
1934 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1935   "o" gnus-summary-save-article
1936   "m" gnus-summary-save-article-mail
1937   "F" gnus-summary-write-article-file
1938   "r" gnus-summary-save-article-rmail
1939   "f" gnus-summary-save-article-file
1940   "b" gnus-summary-save-article-body-file
1941   "h" gnus-summary-save-article-folder
1942   "v" gnus-summary-save-article-vm
1943   "p" gnus-summary-pipe-output
1944   "P" gnus-summary-muttprint
1945   "s" gnus-soup-add-article)
1946
1947 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1948   "b" gnus-summary-display-buttonized
1949   "m" gnus-summary-repair-multipart
1950   "v" gnus-article-view-part
1951   "o" gnus-article-save-part
1952   "c" gnus-article-copy-part
1953   "C" gnus-article-view-part-as-charset
1954   "e" gnus-article-view-part-externally
1955   "E" gnus-article-encrypt-body
1956   "i" gnus-article-inline-part
1957   "|" gnus-article-pipe-part)
1958
1959 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1960   "p" gnus-summary-mark-as-processable
1961   "u" gnus-summary-unmark-as-processable
1962   "U" gnus-summary-unmark-all-processable
1963   "v" gnus-uu-mark-over
1964   "s" gnus-uu-mark-series
1965   "r" gnus-uu-mark-region
1966   "g" gnus-uu-unmark-region
1967   "R" gnus-uu-mark-by-regexp
1968   "G" gnus-uu-unmark-by-regexp
1969   "t" gnus-uu-mark-thread
1970   "T" gnus-uu-unmark-thread
1971   "a" gnus-uu-mark-all
1972   "b" gnus-uu-mark-buffer
1973   "S" gnus-uu-mark-sparse
1974   "k" gnus-summary-kill-process-mark
1975   "y" gnus-summary-yank-process-mark
1976   "w" gnus-summary-save-process-mark
1977   "i" gnus-uu-invert-processable)
1978
1979 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1980   ;;"x" gnus-uu-extract-any
1981   "m" gnus-summary-save-parts
1982   "u" gnus-uu-decode-uu
1983   "U" gnus-uu-decode-uu-and-save
1984   "s" gnus-uu-decode-unshar
1985   "S" gnus-uu-decode-unshar-and-save
1986   "o" gnus-uu-decode-save
1987   "O" gnus-uu-decode-save
1988   "b" gnus-uu-decode-binhex
1989   "B" gnus-uu-decode-binhex
1990   "p" gnus-uu-decode-postscript
1991   "P" gnus-uu-decode-postscript-and-save)
1992
1993 (gnus-define-keys
1994     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1995   "u" gnus-uu-decode-uu-view
1996   "U" gnus-uu-decode-uu-and-save-view
1997   "s" gnus-uu-decode-unshar-view
1998   "S" gnus-uu-decode-unshar-and-save-view
1999   "o" gnus-uu-decode-save-view
2000   "O" gnus-uu-decode-save-view
2001   "b" gnus-uu-decode-binhex-view
2002   "B" gnus-uu-decode-binhex-view
2003   "p" gnus-uu-decode-postscript-view
2004   "P" gnus-uu-decode-postscript-and-save-view)
2005
2006 (defvar gnus-article-post-menu nil)
2007
2008 (defconst gnus-summary-menu-maxlen 20)
2009
2010 (defun gnus-summary-menu-split (menu)
2011   ;; If we have lots of elements, divide them into groups of 20
2012   ;; and make a pane (or submenu) for each one.
2013   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2014       (let ((menu menu) sublists next
2015             (i 1))
2016         (while menu
2017           ;; Pull off the next gnus-summary-menu-maxlen elements
2018           ;; and make them the next element of sublist.
2019           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2020           (if next
2021               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2022                       nil))
2023           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2024                                              (aref (car (last menu)) 0)) menu)
2025                                sublists))
2026           (setq i (1+ i))
2027           (setq menu next))
2028         (nreverse sublists))
2029     ;; Few elements--put them all in one pane.
2030     menu))
2031
2032 (defun gnus-summary-make-menu-bar ()
2033   (gnus-turn-off-edit-menu 'summary)
2034
2035   (unless (boundp 'gnus-summary-misc-menu)
2036
2037     (easy-menu-define
2038       gnus-summary-kill-menu gnus-summary-mode-map ""
2039       (cons
2040        "Score"
2041        (nconc
2042         (list
2043          ["Customize" gnus-score-customize t])
2044         (gnus-make-score-map 'increase)
2045         (gnus-make-score-map 'lower)
2046         '(("Mark"
2047            ["Kill below" gnus-summary-kill-below t]
2048            ["Mark above" gnus-summary-mark-above t]
2049            ["Tick above" gnus-summary-tick-above t]
2050            ["Clear above" gnus-summary-clear-above t])
2051           ["Current score" gnus-summary-current-score t]
2052           ["Set score" gnus-summary-set-score t]
2053           ["Switch current score file..." gnus-score-change-score-file t]
2054           ["Set mark below..." gnus-score-set-mark-below t]
2055           ["Set expunge below..." gnus-score-set-expunge-below t]
2056           ["Edit current score file" gnus-score-edit-current-scores t]
2057           ["Edit score file" gnus-score-edit-file t]
2058           ["Trace score" gnus-score-find-trace t]
2059           ["Find words" gnus-score-find-favourite-words t]
2060           ["Rescore buffer" gnus-summary-rescore t]
2061           ["Increase score..." gnus-summary-increase-score t]
2062           ["Lower score..." gnus-summary-lower-score t]))))
2063
2064     ;; Define both the Article menu in the summary buffer and the
2065     ;; equivalent Commands menu in the article buffer here for
2066     ;; consistency.
2067     (let ((innards
2068            `(("Hide"
2069               ["All" gnus-article-hide t]
2070               ["Headers" gnus-article-hide-headers t]
2071               ["Signature" gnus-article-hide-signature t]
2072               ["Citation" gnus-article-hide-citation t]
2073               ["List identifiers" gnus-article-hide-list-identifiers t]
2074               ["Banner" gnus-article-strip-banner t]
2075               ["Boring headers" gnus-article-hide-boring-headers t])
2076              ("Highlight"
2077               ["All" gnus-article-highlight t]
2078               ["Headers" gnus-article-highlight-headers t]
2079               ["Signature" gnus-article-highlight-signature t]
2080               ["Citation" gnus-article-highlight-citation t])
2081              ("MIME"
2082               ["Words" gnus-article-decode-mime-words t]
2083               ["Charset" gnus-article-decode-charset t]
2084               ["QP" gnus-article-de-quoted-unreadable t]
2085               ["Base64" gnus-article-de-base64-unreadable t]
2086               ["View MIME buttons" gnus-summary-display-buttonized t]
2087               ["View all" gnus-mime-view-all-parts t]
2088               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2089               ["Encrypt body" gnus-article-encrypt-body t]
2090               ["Extract all parts" gnus-summary-save-parts t]
2091               ("Multipart"
2092                ["Repair multipart" gnus-summary-repair-multipart t]
2093                ["Add buttons" gnus-summary-display-buttonized t]
2094                ["Pipe part" gnus-article-pipe-part t]
2095                ["Inline part" gnus-article-inline-part t]
2096                ["Encrypt body" gnus-article-encrypt-body t]
2097                ["View part externally" gnus-article-view-part-externally t]
2098                ["View part with charset" gnus-article-view-part-as-charset t]
2099                ["Copy part" gnus-article-copy-part t]
2100                ["Save part" gnus-article-save-part t]
2101                ["View part" gnus-article-view-part t]))
2102              ("Date"
2103               ["Local" gnus-article-date-local t]
2104               ["ISO8601" gnus-article-date-iso8601 t]
2105               ["UT" gnus-article-date-ut t]
2106               ["Original" gnus-article-date-original t]
2107               ["Lapsed" gnus-article-date-lapsed t]
2108               ["User-defined" gnus-article-date-user t])
2109              ("Display"
2110               ["Remove images" gnus-article-remove-images t]
2111               ["Toggle smiley" gnus-treat-smiley t]
2112               ["Show X-Face" gnus-article-display-x-face t]
2113               ["Show picons in From" gnus-treat-from-picon t]
2114               ["Show picons in mail headers" gnus-treat-mail-picon t]
2115               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2116               ("View as different encoding"
2117                ,@(gnus-summary-menu-split
2118                   (mapcar
2119                    (lambda (cs)
2120                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2121                      ;; forms for menu commands, we should provide intern'ed
2122                      ;; function symbols.
2123                      (let ((command (intern (format "\
2124 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2125                        (fset command
2126                              `(lambda ()
2127                                 (interactive)
2128                                 (let ((gnus-summary-show-article-charset-alist
2129                                        '((1 . ,cs))))
2130                                   (gnus-summary-show-article 1))))
2131                        `[,(symbol-name cs) ,command t]))
2132                    (sort (if (fboundp 'coding-system-list)
2133                              (coding-system-list)
2134                            (mapcar 'car mm-mime-mule-charset-alist))
2135                          'string<)))))
2136              ("Washing"
2137               ("Remove Blanks"
2138                ["Leading" gnus-article-strip-leading-blank-lines t]
2139                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2140                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2141                ["All of the above" gnus-article-strip-blank-lines t]
2142                ["All" gnus-article-strip-all-blank-lines t]
2143                ["Leading space" gnus-article-strip-leading-space t]
2144                ["Trailing space" gnus-article-strip-trailing-space t]
2145                ["Leading space in headers"
2146                 gnus-article-remove-leading-whitespace t])
2147               ["Overstrike" gnus-article-treat-overstrike t]
2148               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2149               ["Emphasis" gnus-article-emphasize t]
2150               ["Word wrap" gnus-article-fill-cited-article t]
2151               ["Fill long lines" gnus-article-fill-long-lines t]
2152               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2153               ["CR" gnus-article-remove-cr t]
2154               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2155               ["Base64" gnus-article-de-base64-unreadable t]
2156               ["Rot 13" gnus-summary-caesar-message
2157                ,@(if (featurep 'xemacs) '(t)
2158                    '(:help "\"Caesar rotate\" article by 13"))]
2159               ["Morse decode" gnus-summary-morse-message t]
2160               ["Unix pipe..." gnus-summary-pipe-message t]
2161               ["Add buttons" gnus-article-add-buttons t]
2162               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2163               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2164               ["Verbose header" gnus-summary-verbose-headers t]
2165               ["Toggle header" gnus-summary-toggle-header t]
2166               ["Unfold headers" gnus-article-treat-unfold-headers t]
2167               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2168               ["Html" gnus-article-wash-html t]
2169               ["URLs" gnus-article-unsplit-urls t]
2170               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2171               ["HZ" gnus-article-decode-HZ t]
2172               ("(Outlook) Deuglify"
2173                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2174                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2175                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2176                ["Full (Outlook) deuglify"
2177                 gnus-article-outlook-deuglify-article t])
2178               )
2179              ("Output"
2180               ["Save in default format" gnus-summary-save-article
2181                ,@(if (featurep 'xemacs) '(t)
2182                    '(:help "Save article using default method"))]
2183               ["Save in file" gnus-summary-save-article-file
2184                ,@(if (featurep 'xemacs) '(t)
2185                    '(:help "Save article in file"))]
2186               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2187               ["Save in MH folder" gnus-summary-save-article-folder t]
2188               ["Save in VM folder" gnus-summary-save-article-vm t]
2189               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2190               ["Save body in file" gnus-summary-save-article-body-file t]
2191               ["Pipe through a filter" gnus-summary-pipe-output t]
2192               ["Add to SOUP packet" gnus-soup-add-article t]
2193               ["Print with Muttprint" gnus-summary-muttprint t]
2194               ["Print" gnus-summary-print-article t])
2195              ("Backend"
2196               ["Respool article..." gnus-summary-respool-article t]
2197               ["Move article..." gnus-summary-move-article
2198                (gnus-check-backend-function
2199                 'request-move-article gnus-newsgroup-name)]
2200               ["Copy article..." gnus-summary-copy-article t]
2201               ["Crosspost article..." gnus-summary-crosspost-article
2202                (gnus-check-backend-function
2203                 'request-replace-article gnus-newsgroup-name)]
2204               ["Import file..." gnus-summary-import-article t]
2205               ["Create article..." gnus-summary-create-article t]
2206               ["Check if posted" gnus-summary-article-posted-p t]
2207               ["Edit article" gnus-summary-edit-article
2208                (not (gnus-group-read-only-p))]
2209               ["Delete article" gnus-summary-delete-article
2210                (gnus-check-backend-function
2211                 'request-expire-articles gnus-newsgroup-name)]
2212               ["Query respool" gnus-summary-respool-query t]
2213               ["Trace respool" gnus-summary-respool-trace t]
2214               ["Delete expirable articles" gnus-summary-expire-articles-now
2215                (gnus-check-backend-function
2216                 'request-expire-articles gnus-newsgroup-name)])
2217              ("Extract"
2218               ["Uudecode" gnus-uu-decode-uu
2219                ,@(if (featurep 'xemacs) '(t)
2220                    '(:help "Decode uuencoded article(s)"))]
2221               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2222               ["Unshar" gnus-uu-decode-unshar t]
2223               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2224               ["Save" gnus-uu-decode-save t]
2225               ["Binhex" gnus-uu-decode-binhex t]
2226               ["Postscript" gnus-uu-decode-postscript t]
2227               ["All MIME parts" gnus-summary-save-parts t])
2228              ("Cache"
2229               ["Enter article" gnus-cache-enter-article t]
2230               ["Remove article" gnus-cache-remove-article t])
2231              ["Translate" gnus-article-babel t]
2232              ["Select article buffer" gnus-summary-select-article-buffer t]
2233              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2234              ["Isearch article..." gnus-summary-isearch-article t]
2235              ["Beginning of the article" gnus-summary-beginning-of-article t]
2236              ["End of the article" gnus-summary-end-of-article t]
2237              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2238              ["Fetch referenced articles" gnus-summary-refer-references t]
2239              ["Fetch current thread" gnus-summary-refer-thread t]
2240              ["Fetch article with id..." gnus-summary-refer-article t]
2241              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2242              ["Redisplay" gnus-summary-show-article t]
2243              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2244       (easy-menu-define
2245         gnus-summary-article-menu gnus-summary-mode-map ""
2246         (cons "Article" innards))
2247
2248       (if (not (keymapp gnus-summary-article-menu))
2249           (easy-menu-define
2250             gnus-article-commands-menu gnus-article-mode-map ""
2251             (cons "Commands" innards))
2252         ;; in Emacs, don't share menu.
2253         (setq gnus-article-commands-menu
2254               (copy-keymap gnus-summary-article-menu))
2255         (define-key gnus-article-mode-map [menu-bar commands]
2256           (cons "Commands" gnus-article-commands-menu))))
2257
2258     (easy-menu-define
2259       gnus-summary-thread-menu gnus-summary-mode-map ""
2260       '("Threads"
2261         ["Find all messages in thread" gnus-summary-refer-thread t]
2262         ["Toggle threading" gnus-summary-toggle-threads t]
2263         ["Hide threads" gnus-summary-hide-all-threads t]
2264         ["Show threads" gnus-summary-show-all-threads t]
2265         ["Hide thread" gnus-summary-hide-thread t]
2266         ["Show thread" gnus-summary-show-thread t]
2267         ["Go to next thread" gnus-summary-next-thread t]
2268         ["Go to previous thread" gnus-summary-prev-thread t]
2269         ["Go down thread" gnus-summary-down-thread t]
2270         ["Go up thread" gnus-summary-up-thread t]
2271         ["Top of thread" gnus-summary-top-thread t]
2272         ["Mark thread as read" gnus-summary-kill-thread t]
2273         ["Lower thread score" gnus-summary-lower-thread t]
2274         ["Raise thread score" gnus-summary-raise-thread t]
2275         ["Rethread current" gnus-summary-rethread-current t]))
2276
2277     (easy-menu-define
2278       gnus-summary-post-menu gnus-summary-mode-map ""
2279       `("Post"
2280         ["Send a message (mail or news)" gnus-summary-post-news
2281          ,@(if (featurep 'xemacs) '(t)
2282              '(:help "Post an article"))]
2283         ["Followup" gnus-summary-followup
2284          ,@(if (featurep 'xemacs) '(t)
2285              '(:help "Post followup to this article"))]
2286         ["Followup and yank" gnus-summary-followup-with-original
2287          ,@(if (featurep 'xemacs) '(t)
2288              '(:help "Post followup to this article, quoting its contents"))]
2289         ["Supersede article" gnus-summary-supersede-article t]
2290         ["Cancel article" gnus-summary-cancel-article
2291          ,@(if (featurep 'xemacs) '(t)
2292              '(:help "Cancel an article you posted"))]
2293         ["Reply" gnus-summary-reply t]
2294         ["Reply and yank" gnus-summary-reply-with-original t]
2295         ["Wide reply" gnus-summary-wide-reply t]
2296         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2297          ,@(if (featurep 'xemacs) '(t)
2298              '(:help "Mail a reply, quoting this article"))]
2299         ["Very wide reply" gnus-summary-very-wide-reply t]
2300         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2301          ,@(if (featurep 'xemacs) '(t)
2302              '(:help "Mail a very wide reply, quoting this article"))]
2303         ["Mail forward" gnus-summary-mail-forward t]
2304         ["Post forward" gnus-summary-post-forward t]
2305         ["Digest and mail" gnus-uu-digest-mail-forward t]
2306         ["Digest and post" gnus-uu-digest-post-forward t]
2307         ["Resend message" gnus-summary-resend-message t]
2308         ["Resend message edit" gnus-summary-resend-message-edit t]
2309         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2310         ["Send a mail" gnus-summary-mail-other-window t]
2311         ["Create a local message" gnus-summary-news-other-window t]
2312         ["Uuencode and post" gnus-uu-post-news
2313          ,@(if (featurep 'xemacs) '(t)
2314              '(:help "Post a uuencoded article"))]
2315         ["Followup via news" gnus-summary-followup-to-mail t]
2316         ["Followup via news and yank"
2317          gnus-summary-followup-to-mail-with-original t]
2318         ;;("Draft"
2319         ;;["Send" gnus-summary-send-draft t]
2320         ;;["Send bounced" gnus-resend-bounced-mail t])
2321         ))
2322
2323     (cond
2324      ((not (keymapp gnus-summary-post-menu))
2325       (setq gnus-article-post-menu gnus-summary-post-menu))
2326      ((not gnus-article-post-menu)
2327       ;; Don't share post menu.
2328       (setq gnus-article-post-menu
2329             (copy-keymap gnus-summary-post-menu))))
2330     (define-key gnus-article-mode-map [menu-bar post]
2331       (cons "Post" gnus-article-post-menu))
2332
2333     (easy-menu-define
2334       gnus-summary-misc-menu gnus-summary-mode-map ""
2335       `("Gnus"
2336         ("Mark Read"
2337          ["Mark as read" gnus-summary-mark-as-read-forward t]
2338          ["Mark same subject and select"
2339           gnus-summary-kill-same-subject-and-select t]
2340          ["Mark same subject" gnus-summary-kill-same-subject t]
2341          ["Catchup" gnus-summary-catchup
2342           ,@(if (featurep 'xemacs) '(t)
2343               '(:help "Mark unread articles in this group as read"))]
2344          ["Catchup all" gnus-summary-catchup-all t]
2345          ["Catchup to here" gnus-summary-catchup-to-here t]
2346          ["Catchup from here" gnus-summary-catchup-from-here t]
2347          ["Catchup region" gnus-summary-mark-region-as-read 
2348           (gnus-mark-active-p)]
2349          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2350         ("Mark Various"
2351          ["Tick" gnus-summary-tick-article-forward t]
2352          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2353          ["Remove marks" gnus-summary-clear-mark-forward t]
2354          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2355          ["Set bookmark" gnus-summary-set-bookmark t]
2356          ["Remove bookmark" gnus-summary-remove-bookmark t])
2357         ("Limit to"
2358          ["Marks..." gnus-summary-limit-to-marks t]
2359          ["Subject..." gnus-summary-limit-to-subject t]
2360          ["Author..." gnus-summary-limit-to-author t]
2361          ["Age..." gnus-summary-limit-to-age t]
2362          ["Extra..." gnus-summary-limit-to-extra t]
2363          ["Score..." gnus-summary-limit-to-score t]
2364          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2365          ["Unread" gnus-summary-limit-to-unread t]
2366          ["Unseen" gnus-summary-limit-to-unseen t]
2367          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2368          ["Next articles" gnus-summary-limit-to-articles t]
2369          ["Pop limit" gnus-summary-pop-limit t]
2370          ["Show dormant" gnus-summary-limit-include-dormant t]
2371          ["Hide childless dormant"
2372           gnus-summary-limit-exclude-childless-dormant t]
2373          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2374          ["Hide marked" gnus-summary-limit-exclude-marks t]
2375          ["Show expunged" gnus-summary-limit-include-expunged t])
2376         ("Process Mark"
2377          ["Set mark" gnus-summary-mark-as-processable t]
2378          ["Remove mark" gnus-summary-unmark-as-processable t]
2379          ["Remove all marks" gnus-summary-unmark-all-processable t]
2380          ["Mark above" gnus-uu-mark-over t]
2381          ["Mark series" gnus-uu-mark-series t]
2382          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2383          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2384          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2385          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2386          ["Mark all" gnus-uu-mark-all t]
2387          ["Mark buffer" gnus-uu-mark-buffer t]
2388          ["Mark sparse" gnus-uu-mark-sparse t]
2389          ["Mark thread" gnus-uu-mark-thread t]
2390          ["Unmark thread" gnus-uu-unmark-thread t]
2391          ("Process Mark Sets"
2392           ["Kill" gnus-summary-kill-process-mark t]
2393           ["Yank" gnus-summary-yank-process-mark
2394            gnus-newsgroup-process-stack]
2395           ["Save" gnus-summary-save-process-mark t]
2396           ["Run command on marked..." gnus-summary-universal-argument t]))
2397         ("Scroll article"
2398          ["Page forward" gnus-summary-next-page
2399           ,@(if (featurep 'xemacs) '(t)
2400               '(:help "Show next page of article"))]
2401          ["Page backward" gnus-summary-prev-page
2402           ,@(if (featurep 'xemacs) '(t)
2403               '(:help "Show previous page of article"))]
2404          ["Line forward" gnus-summary-scroll-up t])
2405         ("Move"
2406          ["Next unread article" gnus-summary-next-unread-article t]
2407          ["Previous unread article" gnus-summary-prev-unread-article t]
2408          ["Next article" gnus-summary-next-article t]
2409          ["Previous article" gnus-summary-prev-article t]
2410          ["Next unread subject" gnus-summary-next-unread-subject t]
2411          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2412          ["Next article same subject" gnus-summary-next-same-subject t]
2413          ["Previous article same subject" gnus-summary-prev-same-subject t]
2414          ["First unread article" gnus-summary-first-unread-article t]
2415          ["Best unread article" gnus-summary-best-unread-article t]
2416          ["Go to subject number..." gnus-summary-goto-subject t]
2417          ["Go to article number..." gnus-summary-goto-article t]
2418          ["Go to the last article" gnus-summary-goto-last-article t]
2419          ["Pop article off history" gnus-summary-pop-article t])
2420         ("Sort"
2421          ["Sort by number" gnus-summary-sort-by-number t]
2422          ["Sort by author" gnus-summary-sort-by-author t]
2423          ["Sort by subject" gnus-summary-sort-by-subject t]
2424          ["Sort by date" gnus-summary-sort-by-date t]
2425          ["Sort by score" gnus-summary-sort-by-score t]
2426          ["Sort by lines" gnus-summary-sort-by-lines t]
2427          ["Sort by characters" gnus-summary-sort-by-chars t]
2428          ["Randomize" gnus-summary-sort-by-random t]
2429          ["Original sort" gnus-summary-sort-by-original t])
2430         ("Help"
2431          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2432          ["Describe group" gnus-summary-describe-group t]
2433          ["Fetch charter" gnus-group-fetch-charter
2434           ,@(if (featurep 'xemacs) nil
2435               '(:help "Display the charter of the current group"))]
2436          ["Fetch control message" gnus-group-fetch-control
2437           ,@(if (featurep 'xemacs) nil
2438               '(:help "Display the archived control message for the current group"))]
2439          ["Read manual" gnus-info-find-node t])
2440         ("Modes"
2441          ["Pick and read" gnus-pick-mode t]
2442          ["Binary" gnus-binary-mode t])
2443         ("Regeneration"
2444          ["Regenerate" gnus-summary-prepare t]
2445          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2446          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2447          ["Toggle threading" gnus-summary-toggle-threads t])
2448         ["See old articles" gnus-summary-insert-old-articles t]
2449         ["See new articles" gnus-summary-insert-new-articles t]
2450         ["Filter articles..." gnus-summary-execute-command t]
2451         ["Run command on articles..." gnus-summary-universal-argument t]
2452         ["Search articles forward..." gnus-summary-search-article-forward t]
2453         ["Search articles backward..." gnus-summary-search-article-backward t]
2454         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2455         ["Expand window" gnus-summary-expand-window t]
2456         ["Expire expirable articles" gnus-summary-expire-articles
2457          (gnus-check-backend-function
2458           'request-expire-articles gnus-newsgroup-name)]
2459         ["Edit local kill file" gnus-summary-edit-local-kill t]
2460         ["Edit main kill file" gnus-summary-edit-global-kill t]
2461         ["Edit group parameters" gnus-summary-edit-parameters t]
2462         ["Customize group parameters" gnus-summary-customize-parameters t]
2463         ["Send a bug report" gnus-bug t]
2464         ("Exit"
2465          ["Catchup and exit" gnus-summary-catchup-and-exit
2466           ,@(if (featurep 'xemacs) '(t)
2467               '(:help "Mark unread articles in this group as read, then exit"))]
2468          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2469          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2470          ["Exit group" gnus-summary-exit
2471           ,@(if (featurep 'xemacs) '(t)
2472               '(:help "Exit current group, return to group selection mode"))]
2473          ["Exit group without updating" gnus-summary-exit-no-update t]
2474          ["Exit and goto next group" gnus-summary-next-group t]
2475          ["Exit and goto prev group" gnus-summary-prev-group t]
2476          ["Reselect group" gnus-summary-reselect-current-group t]
2477          ["Rescan group" gnus-summary-rescan-group t]
2478          ["Update dribble" gnus-summary-save-newsrc t])))
2479
2480     (gnus-run-hooks 'gnus-summary-menu-hook)))
2481
2482 (defvar gnus-summary-tool-bar-map nil)
2483
2484 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2485 (defun gnus-summary-make-tool-bar ()
2486   (if (and (fboundp 'tool-bar-add-item-from-menu)
2487            (default-value 'tool-bar-mode)
2488            (not gnus-summary-tool-bar-map))
2489       (setq gnus-summary-tool-bar-map
2490             (let ((tool-bar-map (make-sparse-keymap))
2491                   (load-path (mm-image-load-path)))
2492               (tool-bar-add-item-from-menu
2493                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2494               (tool-bar-add-item-from-menu
2495                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2496               (tool-bar-add-item-from-menu
2497                'gnus-summary-post-news "post" gnus-summary-mode-map)
2498               (tool-bar-add-item-from-menu
2499                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2500               (tool-bar-add-item-from-menu
2501                'gnus-summary-followup "followup" gnus-summary-mode-map)
2502               (tool-bar-add-item-from-menu
2503                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2504               (tool-bar-add-item-from-menu
2505                'gnus-summary-reply "reply" gnus-summary-mode-map)
2506               (tool-bar-add-item-from-menu
2507                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2508               (tool-bar-add-item-from-menu
2509                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2510               (tool-bar-add-item-from-menu
2511                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2512               (tool-bar-add-item-from-menu
2513                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2514               (tool-bar-add-item-from-menu
2515                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2516               (tool-bar-add-item-from-menu
2517                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2518               (tool-bar-add-item-from-menu
2519                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2520               (tool-bar-add-item-from-menu
2521                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2522               tool-bar-map)))
2523   (if gnus-summary-tool-bar-map
2524       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2525
2526 (defun gnus-score-set-default (var value)
2527   "A version of set that updates the GNU Emacs menu-bar."
2528   (set var value)
2529   ;; It is the message that forces the active status to be updated.
2530   (message ""))
2531
2532 (defun gnus-make-score-map (type)
2533   "Make a summary score map of type TYPE."
2534   (if t
2535       nil
2536     (let ((headers '(("author" "from" string)
2537                      ("subject" "subject" string)
2538                      ("article body" "body" string)
2539                      ("article head" "head" string)
2540                      ("xref" "xref" string)
2541                      ("extra header" "extra" string)
2542                      ("lines" "lines" number)
2543                      ("followups to author" "followup" string)))
2544           (types '((number ("less than" <)
2545                            ("greater than" >)
2546                            ("equal" =))
2547                    (string ("substring" s)
2548                            ("exact string" e)
2549                            ("fuzzy string" f)
2550                            ("regexp" r))))
2551           (perms '(("temporary" (current-time-string))
2552                    ("permanent" nil)
2553                    ("immediate" now)))
2554           header)
2555       (list
2556        (apply
2557         'nconc
2558         (list
2559          (if (eq type 'lower)
2560              "Lower score"
2561            "Increase score"))
2562         (let (outh)
2563           (while headers
2564             (setq header (car headers))
2565             (setq outh
2566                   (cons
2567                    (apply
2568                     'nconc
2569                     (list (car header))
2570                     (let ((ts (cdr (assoc (nth 2 header) types)))
2571                           outt)
2572                       (while ts
2573                         (setq outt
2574                               (cons
2575                                (apply
2576                                 'nconc
2577                                 (list (caar ts))
2578                                 (let ((ps perms)
2579                                       outp)
2580                                   (while ps
2581                                     (setq outp
2582                                           (cons
2583                                            (vector
2584                                             (caar ps)
2585                                             (list
2586                                              'gnus-summary-score-entry
2587                                              (nth 1 header)
2588                                              (if (or (string= (nth 1 header)
2589                                                               "head")
2590                                                      (string= (nth 1 header)
2591                                                               "body"))
2592                                                  ""
2593                                                (list 'gnus-summary-header
2594                                                      (nth 1 header)))
2595                                              (list 'quote (nth 1 (car ts)))
2596                                              (list 'gnus-score-delta-default
2597                                                    nil)
2598                                              (nth 1 (car ps))
2599                                              t)
2600                                             t)
2601                                            outp))
2602                                     (setq ps (cdr ps)))
2603                                   (list (nreverse outp))))
2604                                outt))
2605                         (setq ts (cdr ts)))
2606                       (list (nreverse outt))))
2607                    outh))
2608             (setq headers (cdr headers)))
2609           (list (nreverse outh))))))))
2610
2611 \f
2612
2613 (defun gnus-summary-mode (&optional group)
2614   "Major mode for reading articles.
2615
2616 All normal editing commands are switched off.
2617 \\<gnus-summary-mode-map>
2618 Each line in this buffer represents one article.  To read an
2619 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2620 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2621 respectively.
2622
2623 You can also post articles and send mail from this buffer.  To
2624 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2625 of an article, type `\\[gnus-summary-reply]'.
2626
2627 There are approx. one gazillion commands you can execute in this
2628 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2629
2630 The following commands are available:
2631
2632 \\{gnus-summary-mode-map}"
2633   (interactive)
2634   (kill-all-local-variables)
2635   (when (gnus-visual-p 'summary-menu 'menu)
2636     (gnus-summary-make-menu-bar)
2637     (gnus-summary-make-tool-bar))
2638   (gnus-summary-make-local-variables)
2639   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2640     (gnus-summary-make-local-variables))
2641   (gnus-make-thread-indent-array)
2642   (gnus-simplify-mode-line)
2643   (setq major-mode 'gnus-summary-mode)
2644   (setq mode-name "Summary")
2645   (make-local-variable 'minor-mode-alist)
2646   (use-local-map gnus-summary-mode-map)
2647   (buffer-disable-undo)
2648   (setq buffer-read-only t)             ;Disable modification
2649   (setq truncate-lines t)
2650   (setq selective-display t)
2651   (setq selective-display-ellipses t)   ;Display `...'
2652   (gnus-summary-set-display-table)
2653   (gnus-set-default-directory)
2654   (setq gnus-newsgroup-name group)
2655   (make-local-variable 'gnus-summary-line-format)
2656   (make-local-variable 'gnus-summary-line-format-spec)
2657   (make-local-variable 'gnus-summary-dummy-line-format)
2658   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2659   (make-local-variable 'gnus-summary-mark-positions)
2660   (make-local-hook 'pre-command-hook)
2661   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2662   (gnus-run-hooks 'gnus-summary-mode-hook)
2663   (turn-on-gnus-mailing-list-mode)
2664   (mm-enable-multibyte)
2665   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2666   (gnus-update-summary-mark-positions))
2667
2668 (defun gnus-summary-make-local-variables ()
2669   "Make all the local summary buffer variables."
2670   (let (global)
2671     (dolist (local gnus-summary-local-variables)
2672       (if (consp local)
2673           (progn
2674             (if (eq (cdr local) 'global)
2675                 ;; Copy the global value of the variable.
2676                 (setq global (symbol-value (car local)))
2677               ;; Use the value from the list.
2678               (setq global (eval (cdr local))))
2679             (set (make-local-variable (car local)) global))
2680         ;; Simple nil-valued local variable.
2681         (set (make-local-variable local) nil)))))
2682
2683 (defun gnus-summary-clear-local-variables ()
2684   (let ((locals gnus-summary-local-variables))
2685     (while locals
2686       (if (consp (car locals))
2687           (and (vectorp (caar locals))
2688                (set (caar locals) nil))
2689         (and (vectorp (car locals))
2690              (set (car locals) nil)))
2691       (setq locals (cdr locals)))))
2692
2693 ;; Summary data functions.
2694
2695 (defmacro gnus-data-number (data)
2696   `(car ,data))
2697
2698 (defmacro gnus-data-set-number (data number)
2699   `(setcar ,data ,number))
2700
2701 (defmacro gnus-data-mark (data)
2702   `(nth 1 ,data))
2703
2704 (defmacro gnus-data-set-mark (data mark)
2705   `(setcar (nthcdr 1 ,data) ,mark))
2706
2707 (defmacro gnus-data-pos (data)
2708   `(nth 2 ,data))
2709
2710 (defmacro gnus-data-set-pos (data pos)
2711   `(setcar (nthcdr 2 ,data) ,pos))
2712
2713 (defmacro gnus-data-header (data)
2714   `(nth 3 ,data))
2715
2716 (defmacro gnus-data-set-header (data header)
2717   `(setf (nth 3 ,data) ,header))
2718
2719 (defmacro gnus-data-level (data)
2720   `(nth 4 ,data))
2721
2722 (defmacro gnus-data-unread-p (data)
2723   `(= (nth 1 ,data) gnus-unread-mark))
2724
2725 (defmacro gnus-data-read-p (data)
2726   `(/= (nth 1 ,data) gnus-unread-mark))
2727
2728 (defmacro gnus-data-pseudo-p (data)
2729   `(consp (nth 3 ,data)))
2730
2731 (defmacro gnus-data-find (number)
2732   `(assq ,number gnus-newsgroup-data))
2733
2734 (defmacro gnus-data-find-list (number &optional data)
2735   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2736      (memq (assq ,number bdata)
2737            bdata)))
2738
2739 (defmacro gnus-data-make (number mark pos header level)
2740   `(list ,number ,mark ,pos ,header ,level))
2741
2742 (defun gnus-data-enter (after-article number mark pos header level offset)
2743   (let ((data (gnus-data-find-list after-article)))
2744     (unless data
2745       (error "No such article: %d" after-article))
2746     (setcdr data (cons (gnus-data-make number mark pos header level)
2747                        (cdr data)))
2748     (setq gnus-newsgroup-data-reverse nil)
2749     (gnus-data-update-list (cddr data) offset)))
2750
2751 (defun gnus-data-enter-list (after-article list &optional offset)
2752   (when list
2753     (let ((data (and after-article (gnus-data-find-list after-article)))
2754           (ilist list))
2755       (if (not (or data
2756                    after-article))
2757           (let ((odata gnus-newsgroup-data))
2758             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2759             (when offset
2760               (gnus-data-update-list odata offset)))
2761       ;; Find the last element in the list to be spliced into the main
2762         ;; list.
2763         (while (cdr list)
2764           (setq list (cdr list)))
2765         (if (not data)
2766             (progn
2767               (setcdr list gnus-newsgroup-data)
2768               (setq gnus-newsgroup-data ilist)
2769               (when offset
2770                 (gnus-data-update-list (cdr list) offset)))
2771           (setcdr list (cdr data))
2772           (setcdr data ilist)
2773           (when offset
2774             (gnus-data-update-list (cdr list) offset))))
2775       (setq gnus-newsgroup-data-reverse nil))))
2776
2777 (defun gnus-data-remove (article &optional offset)
2778   (let ((data gnus-newsgroup-data))
2779     (if (= (gnus-data-number (car data)) article)
2780         (progn
2781           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2782                 gnus-newsgroup-data-reverse nil)
2783           (when offset
2784             (gnus-data-update-list gnus-newsgroup-data offset)))
2785       (while (cdr data)
2786         (when (= (gnus-data-number (cadr data)) article)
2787           (setcdr data (cddr data))
2788           (when offset
2789             (gnus-data-update-list (cdr data) offset))
2790           (setq data nil
2791                 gnus-newsgroup-data-reverse nil))
2792         (setq data (cdr data))))))
2793
2794 (defmacro gnus-data-list (backward)
2795   `(if ,backward
2796        (or gnus-newsgroup-data-reverse
2797            (setq gnus-newsgroup-data-reverse
2798                  (reverse gnus-newsgroup-data)))
2799      gnus-newsgroup-data))
2800
2801 (defun gnus-data-update-list (data offset)
2802   "Add OFFSET to the POS of all data entries in DATA."
2803   (setq gnus-newsgroup-data-reverse nil)
2804   (while data
2805     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2806     (setq data (cdr data))))
2807
2808 (defun gnus-summary-article-pseudo-p (article)
2809   "Say whether this article is a pseudo article or not."
2810   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2811
2812 (defmacro gnus-summary-article-sparse-p (article)
2813   "Say whether this article is a sparse article or not."
2814   `(memq ,article gnus-newsgroup-sparse))
2815
2816 (defmacro gnus-summary-article-ancient-p (article)
2817   "Say whether this article is a sparse article or not."
2818   `(memq ,article gnus-newsgroup-ancient))
2819
2820 (defun gnus-article-parent-p (number)
2821   "Say whether this article is a parent or not."
2822   (let ((data (gnus-data-find-list number)))
2823     (and (cdr data)              ; There has to be an article after...
2824          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2825             (gnus-data-level (nth 1 data))))))
2826
2827 (defun gnus-article-children (number)
2828   "Return a list of all children to NUMBER."
2829   (let* ((data (gnus-data-find-list number))
2830          (level (gnus-data-level (car data)))
2831          children)
2832     (setq data (cdr data))
2833     (while (and data
2834                 (= (gnus-data-level (car data)) (1+ level)))
2835       (push (gnus-data-number (car data)) children)
2836       (setq data (cdr data)))
2837     children))
2838
2839 (defmacro gnus-summary-skip-intangible ()
2840   "If the current article is intangible, then jump to a different article."
2841   '(let ((to (get-text-property (point) 'gnus-intangible)))
2842      (and to (gnus-summary-goto-subject to))))
2843
2844 (defmacro gnus-summary-article-intangible-p ()
2845   "Say whether this article is intangible or not."
2846   '(get-text-property (point) 'gnus-intangible))
2847
2848 (defun gnus-article-read-p (article)
2849   "Say whether ARTICLE is read or not."
2850   (not (or (memq article gnus-newsgroup-marked)
2851            (memq article gnus-newsgroup-spam-marked)
2852            (memq article gnus-newsgroup-unreads)
2853            (memq article gnus-newsgroup-unselected)
2854            (memq article gnus-newsgroup-dormant))))
2855
2856 ;; Some summary mode macros.
2857
2858 (defmacro gnus-summary-article-number ()
2859   "The article number of the article on the current line.
2860 If there isn't an article number here, then we return the current
2861 article number."
2862   '(progn
2863      (gnus-summary-skip-intangible)
2864      (or (get-text-property (point) 'gnus-number)
2865          (gnus-summary-last-subject))))
2866
2867 (defmacro gnus-summary-article-header (&optional number)
2868   "Return the header of article NUMBER."
2869   `(gnus-data-header (gnus-data-find
2870                       ,(or number '(gnus-summary-article-number)))))
2871
2872 (defmacro gnus-summary-thread-level (&optional number)
2873   "Return the level of thread that starts with article NUMBER."
2874   `(if (and (eq gnus-summary-make-false-root 'dummy)
2875             (get-text-property (point) 'gnus-intangible))
2876        0
2877      (gnus-data-level (gnus-data-find
2878                        ,(or number '(gnus-summary-article-number))))))
2879
2880 (defmacro gnus-summary-article-mark (&optional number)
2881   "Return the mark of article NUMBER."
2882   `(gnus-data-mark (gnus-data-find
2883                     ,(or number '(gnus-summary-article-number)))))
2884
2885 (defmacro gnus-summary-article-pos (&optional number)
2886   "Return the position of the line of article NUMBER."
2887   `(gnus-data-pos (gnus-data-find
2888                    ,(or number '(gnus-summary-article-number)))))
2889
2890 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2891 (defmacro gnus-summary-article-subject (&optional number)
2892   "Return current subject string or nil if nothing."
2893   `(let ((headers
2894           ,(if number
2895                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2896              '(gnus-data-header (assq (gnus-summary-article-number)
2897                                       gnus-newsgroup-data)))))
2898      (and headers
2899           (vectorp headers)
2900           (mail-header-subject headers))))
2901
2902 (defmacro gnus-summary-article-score (&optional number)
2903   "Return current article score."
2904   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2905                   gnus-newsgroup-scored))
2906        gnus-summary-default-score 0))
2907
2908 (defun gnus-summary-article-children (&optional number)
2909   "Return a list of article numbers that are children of article NUMBER."
2910   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2911          (level (gnus-data-level (car data)))
2912          l children)
2913     (while (and (setq data (cdr data))
2914                 (> (setq l (gnus-data-level (car data))) level))
2915       (and (= (1+ level) l)
2916            (push (gnus-data-number (car data))
2917                  children)))
2918     (nreverse children)))
2919
2920 (defun gnus-summary-article-parent (&optional number)
2921   "Return the article number of the parent of article NUMBER."
2922   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2923                                     (gnus-data-list t)))
2924          (level (gnus-data-level (car data))))
2925     (if (zerop level)
2926         ()                              ; This is a root.
2927       ;; We search until we find an article with a level less than
2928       ;; this one.  That function has to be the parent.
2929       (while (and (setq data (cdr data))
2930                   (not (< (gnus-data-level (car data)) level))))
2931       (and data (gnus-data-number (car data))))))
2932
2933 (defun gnus-unread-mark-p (mark)
2934   "Say whether MARK is the unread mark."
2935   (= mark gnus-unread-mark))
2936
2937 (defun gnus-read-mark-p (mark)
2938   "Say whether MARK is one of the marks that mark as read.
2939 This is all marks except unread, ticked, dormant, and expirable."
2940   (not (or (= mark gnus-unread-mark)
2941            (= mark gnus-ticked-mark)
2942            (= mark gnus-spam-mark)
2943            (= mark gnus-dormant-mark)
2944            (= mark gnus-expirable-mark))))
2945
2946 (defmacro gnus-article-mark (number)
2947   "Return the MARK of article NUMBER.
2948 This macro should only be used when computing the mark the \"first\"
2949 time; i.e., when generating the summary lines.  After that,
2950 `gnus-summary-article-mark' should be used to examine the
2951 marks of articles."
2952   `(cond
2953     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2954     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2955     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2956     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2957     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2958     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2959     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2960     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2961            gnus-ancient-mark))))
2962
2963 ;; Saving hidden threads.
2964
2965 (defmacro gnus-save-hidden-threads (&rest forms)
2966   "Save hidden threads, eval FORMS, and restore the hidden threads."
2967   (let ((config (make-symbol "config")))
2968     `(let ((,config (gnus-hidden-threads-configuration)))
2969        (unwind-protect
2970            (save-excursion
2971              ,@forms)
2972          (gnus-restore-hidden-threads-configuration ,config)))))
2973 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2974 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2975
2976 (defun gnus-data-compute-positions ()
2977   "Compute the positions of all articles."
2978   (setq gnus-newsgroup-data-reverse nil)
2979   (let ((data gnus-newsgroup-data))
2980     (save-excursion
2981       (gnus-save-hidden-threads
2982         (gnus-summary-show-all-threads)
2983         (goto-char (point-min))
2984         (while data
2985           (while (get-text-property (point) 'gnus-intangible)
2986             (forward-line 1))
2987           (gnus-data-set-pos (car data) (+ (point) 3))
2988           (setq data (cdr data))
2989           (forward-line 1))))))
2990
2991 (defun gnus-hidden-threads-configuration ()
2992   "Return the current hidden threads configuration."
2993   (save-excursion
2994     (let (config)
2995       (goto-char (point-min))
2996       (while (search-forward "\r" nil t)
2997         (push (1- (point)) config))
2998       config)))
2999
3000 (defun gnus-restore-hidden-threads-configuration (config)
3001   "Restore hidden threads configuration from CONFIG."
3002   (save-excursion
3003     (let (point buffer-read-only)
3004       (while (setq point (pop config))
3005         (when (and (< point (point-max))
3006                    (goto-char point)
3007                    (eq (char-after) ?\n))
3008           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3009
3010 ;; Various summary mode internalish functions.
3011
3012 (defun gnus-mouse-pick-article (e)
3013   (interactive "e")
3014   (mouse-set-point e)
3015   (gnus-summary-next-page nil t))
3016
3017 (defun gnus-summary-set-display-table ()
3018   "Change the display table.
3019 Odd characters have a tendency to mess
3020 up nicely formatted displays - we make all possible glyphs
3021 display only a single character."
3022
3023   ;; We start from the standard display table, if any.
3024   (let ((table (or (copy-sequence standard-display-table)
3025                    (make-display-table)))
3026         (i 32))
3027     ;; Nix out all the control chars...
3028     (while (>= (setq i (1- i)) 0)
3029       (aset table i [??]))
3030    ;; ... but not newline and cr, of course.  (cr is necessary for the
3031     ;; selective display).
3032     (aset table ?\n nil)
3033     (aset table ?\r nil)
3034     ;; We keep TAB as well.
3035     (aset table ?\t nil)
3036     ;; We nix out any glyphs over 126 that are not set already.
3037     (let ((i 256))
3038       (while (>= (setq i (1- i)) 127)
3039         ;; Only modify if the entry is nil.
3040         (unless (aref table i)
3041           (aset table i [??]))))
3042     (setq buffer-display-table table)))
3043
3044 (defun gnus-summary-set-article-display-arrow (pos)
3045   "Update the overlay arrow to point to line at position POS."
3046   (when (and gnus-summary-display-arrow
3047              (boundp 'overlay-arrow-position)
3048              (boundp 'overlay-arrow-string))
3049     (save-excursion
3050       (goto-char pos)
3051       (beginning-of-line)
3052       (unless overlay-arrow-position
3053         (setq overlay-arrow-position (make-marker)))
3054       (setq overlay-arrow-string "=>"
3055             overlay-arrow-position (set-marker overlay-arrow-position
3056                                                (point)
3057                                                (current-buffer))))))
3058
3059 (defun gnus-summary-setup-buffer (group)
3060   "Initialize summary buffer."
3061   (let ((buffer (gnus-summary-buffer-name group))
3062         (dead-name (concat "*Dead Summary "
3063                            (gnus-group-decoded-name group) "*")))
3064     ;; If a dead summary buffer exists, we kill it.
3065     (when (gnus-buffer-live-p dead-name)
3066       (gnus-kill-buffer dead-name))
3067     (if (get-buffer buffer)
3068         (progn
3069           (set-buffer buffer)
3070           (setq gnus-summary-buffer (current-buffer))
3071           (not gnus-newsgroup-prepared))
3072       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3073       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3074       (gnus-summary-mode group)
3075       (when gnus-carpal
3076         (gnus-carpal-setup-buffer 'summary))
3077       (unless gnus-single-article-buffer
3078         (make-local-variable 'gnus-article-buffer)
3079         (make-local-variable 'gnus-article-current)
3080         (make-local-variable 'gnus-original-article-buffer))
3081       (setq gnus-newsgroup-name group)
3082       ;; Set any local variables in the group parameters.
3083       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3084       t)))
3085
3086 (defun gnus-set-global-variables ()
3087   "Set the global equivalents of the buffer-local variables.
3088 They are set to the latest values they had.  These reflect the summary
3089 buffer that was in action when the last article was fetched."
3090   (when (eq major-mode 'gnus-summary-mode)
3091     (setq gnus-summary-buffer (current-buffer))
3092     (let ((name gnus-newsgroup-name)
3093           (marked gnus-newsgroup-marked)
3094           (spam gnus-newsgroup-spam-marked)
3095           (unread gnus-newsgroup-unreads)
3096           (headers gnus-current-headers)
3097           (data gnus-newsgroup-data)
3098           (summary gnus-summary-buffer)
3099           (article-buffer gnus-article-buffer)
3100           (original gnus-original-article-buffer)
3101           (gac gnus-article-current)
3102           (reffed gnus-reffed-article-number)
3103           (score-file gnus-current-score-file)
3104           (default-charset gnus-newsgroup-charset)
3105           vlist)
3106       (let ((locals gnus-newsgroup-variables))
3107         (while locals
3108           (if (consp (car locals))
3109               (push (eval (caar locals)) vlist)
3110             (push (eval (car locals)) vlist))
3111           (setq locals (cdr locals)))
3112         (setq vlist (nreverse vlist)))
3113       (save-excursion
3114         (set-buffer gnus-group-buffer)
3115         (setq gnus-newsgroup-name name
3116               gnus-newsgroup-marked marked
3117               gnus-newsgroup-spam-marked spam
3118               gnus-newsgroup-unreads unread
3119               gnus-current-headers headers
3120               gnus-newsgroup-data data
3121               gnus-article-current gac
3122               gnus-summary-buffer summary
3123               gnus-article-buffer article-buffer
3124               gnus-original-article-buffer original
3125               gnus-reffed-article-number reffed
3126               gnus-current-score-file score-file
3127               gnus-newsgroup-charset default-charset)
3128         (let ((locals gnus-newsgroup-variables))
3129           (while locals
3130             (if (consp (car locals))
3131                 (set (caar locals) (pop vlist))
3132               (set (car locals) (pop vlist)))
3133             (setq locals (cdr locals))))
3134         ;; The article buffer also has local variables.
3135         (when (gnus-buffer-live-p gnus-article-buffer)
3136           (set-buffer gnus-article-buffer)
3137           (setq gnus-summary-buffer summary))))))
3138
3139 (defun gnus-summary-article-unread-p (article)
3140   "Say whether ARTICLE is unread or not."
3141   (memq article gnus-newsgroup-unreads))
3142
3143 (defun gnus-summary-first-article-p (&optional article)
3144   "Return whether ARTICLE is the first article in the buffer."
3145   (if (not (setq article (or article (gnus-summary-article-number))))
3146       nil
3147     (eq article (caar gnus-newsgroup-data))))
3148
3149 (defun gnus-summary-last-article-p (&optional article)
3150   "Return whether ARTICLE is the last article in the buffer."
3151   (if (not (setq article (or article (gnus-summary-article-number))))
3152       ;; All non-existent numbers are the last article.  :-)
3153       t
3154     (not (cdr (gnus-data-find-list article)))))
3155
3156 (defun gnus-make-thread-indent-array ()
3157   (let ((n 200))
3158     (unless (and gnus-thread-indent-array
3159                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3160       (setq gnus-thread-indent-array (make-vector 201 "")
3161             gnus-thread-indent-array-level gnus-thread-indent-level)
3162       (while (>= n 0)
3163         (aset gnus-thread-indent-array n
3164               (make-string (* n gnus-thread-indent-level) ? ))
3165         (setq n (1- n))))))
3166
3167 (defun gnus-update-summary-mark-positions ()
3168   "Compute where the summary marks are to go."
3169   (save-excursion
3170     (when (gnus-buffer-exists-p gnus-summary-buffer)
3171       (set-buffer gnus-summary-buffer))
3172     (let ((gnus-replied-mark 129)
3173           (gnus-score-below-mark 130)
3174           (gnus-score-over-mark 130)
3175           (gnus-undownloaded-mark 131)
3176           (spec gnus-summary-line-format-spec)
3177           gnus-visual pos)
3178       (save-excursion
3179         (gnus-set-work-buffer)
3180         (let ((gnus-summary-line-format-spec spec)
3181               (gnus-newsgroup-downloadable '(0)))
3182           (gnus-summary-insert-line
3183            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3184            0 nil t 128 t nil "" nil 1)
3185           (goto-char (point-min))
3186           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3187                                              (- (point) (point-min) 1)))))
3188           (goto-char (point-min))
3189           (push (cons 'replied (and (search-forward "\201" nil t)
3190                                     (- (point) (point-min) 1)))
3191                 pos)
3192           (goto-char (point-min))
3193           (push (cons 'score (and (search-forward "\202" nil t)
3194                                   (- (point) (point-min) 1)))
3195                 pos)
3196           (goto-char (point-min))
3197           (push (cons 'download
3198                       (and (search-forward "\203" nil t)
3199                            (- (point) (point-min) 1)))
3200                 pos)))
3201       (setq gnus-summary-mark-positions pos))))
3202
3203 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3204   "Insert a dummy root in the summary buffer."
3205   (beginning-of-line)
3206   (gnus-add-text-properties
3207    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3208    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3209
3210 (defun gnus-summary-extract-address-component (from)
3211   (or (car (funcall gnus-extract-address-components from))
3212       from))
3213
3214 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3215   (let ((mail-parse-charset gnus-newsgroup-charset)
3216         ; Is it really necessary to do this next part for each summary line?
3217         ; Luckily, doesn't seem to slow things down much.
3218         (mail-parse-ignored-charsets
3219          (save-excursion (set-buffer gnus-summary-buffer)
3220                          gnus-newsgroup-ignored-charsets)))
3221     (or
3222      (and gnus-ignored-from-addresses
3223           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3224           (let ((extra-headers (mail-header-extra header))
3225                 to
3226                 newsgroups)
3227             (cond
3228              ((setq to (cdr (assq 'To extra-headers)))
3229               (concat "-> "
3230                       (inline
3231                         (gnus-summary-extract-address-component
3232                          (funcall gnus-decode-encoded-word-function to)))))
3233              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3234               (concat "=> " newsgroups)))))
3235      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3236
3237 (defun gnus-summary-insert-line (gnus-tmp-header
3238                                  gnus-tmp-level gnus-tmp-current
3239                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3240                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3241                                  &optional gnus-tmp-dummy gnus-tmp-score
3242                                  gnus-tmp-process)
3243   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3244          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3245          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3246          (gnus-tmp-score-char
3247           (if (or (null gnus-summary-default-score)
3248                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3249                       gnus-summary-zcore-fuzz))
3250               ?                         ;Whitespace
3251             (if (< gnus-tmp-score gnus-summary-default-score)
3252                 gnus-score-below-mark gnus-score-over-mark)))
3253          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3254          (gnus-tmp-replied
3255           (cond (gnus-tmp-process gnus-process-mark)
3256                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3257                  gnus-cached-mark)
3258                 (gnus-tmp-replied gnus-replied-mark)
3259                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3260                  gnus-forwarded-mark)
3261                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3262                  gnus-saved-mark)
3263                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3264                  gnus-recent-mark)
3265                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3266                  gnus-unseen-mark)
3267                 (t gnus-no-mark)))
3268          (gnus-tmp-downloaded
3269           (cond (undownloaded 
3270                  gnus-undownloaded-mark)
3271                 (gnus-newsgroup-agentized
3272                  gnus-downloaded-mark)
3273                 (t
3274                  gnus-no-mark)))
3275          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3276          (gnus-tmp-name
3277           (cond
3278            ((string-match "<[^>]+> *$" gnus-tmp-from)
3279             (let ((beg (match-beginning 0)))
3280               (or (and (string-match "^\".+\"" gnus-tmp-from)
3281                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3282                   (substring gnus-tmp-from 0 beg))))
3283            ((string-match "(.+)" gnus-tmp-from)
3284             (substring gnus-tmp-from
3285                        (1+ (match-beginning 0)) (1- (match-end 0))))
3286            (t gnus-tmp-from)))
3287          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3288          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3289          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3290          (buffer-read-only nil))
3291     (when (string= gnus-tmp-name "")
3292       (setq gnus-tmp-name gnus-tmp-from))
3293     (unless (numberp gnus-tmp-lines)
3294       (setq gnus-tmp-lines -1))
3295     (if (= gnus-tmp-lines -1)
3296         (setq gnus-tmp-lines "?")
3297       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines))) 
3298       (gnus-put-text-property
3299      (point)
3300      (progn (eval gnus-summary-line-format-spec) (point))
3301        'gnus-number gnus-tmp-number)
3302     (when (gnus-visual-p 'summary-highlight 'highlight)
3303       (forward-line -1)
3304       (gnus-run-hooks 'gnus-summary-update-hook)
3305       (forward-line 1))))
3306
3307 (defun gnus-summary-update-line (&optional dont-update)
3308   "Update summary line after change."
3309   (when (and gnus-summary-default-score
3310              (not gnus-summary-inhibit-highlight))
3311     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3312            (article (gnus-summary-article-number))
3313            (score (gnus-summary-article-score article)))
3314       (unless dont-update
3315         (if (and gnus-summary-mark-below
3316                  (< (gnus-summary-article-score)
3317                     gnus-summary-mark-below))
3318             ;; This article has a low score, so we mark it as read.
3319             (when (memq article gnus-newsgroup-unreads)
3320               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3321           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3322             ;; This article was previously marked as read on account
3323             ;; of a low score, but now it has risen, so we mark it as
3324             ;; unread.
3325             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3326         (gnus-summary-update-mark
3327          (if (or (null gnus-summary-default-score)
3328                  (<= (abs (- score gnus-summary-default-score))
3329                      gnus-summary-zcore-fuzz))
3330              ?                          ;Whitespace
3331            (if (< score gnus-summary-default-score)
3332                gnus-score-below-mark gnus-score-over-mark))
3333          'score))
3334       ;; Do visual highlighting.
3335       (when (gnus-visual-p 'summary-highlight 'highlight)
3336         (gnus-run-hooks 'gnus-summary-update-hook)))))
3337
3338 (defvar gnus-tmp-new-adopts nil)
3339
3340 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3341   "Return the number of articles in THREAD.
3342 This may be 0 in some cases -- if none of the articles in
3343 the thread are to be displayed."
3344   (let* ((number
3345          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3346           (cond
3347            ((not (listp thread))
3348             1)
3349            ((and (consp thread) (cdr thread))
3350             (apply
3351              '+ 1 (mapcar
3352                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3353            ((null thread)
3354             1)
3355            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3356             1)
3357            (t 0))))
3358     (when (and level (zerop level) gnus-tmp-new-adopts)
3359       (incf number
3360             (apply '+ (mapcar
3361                        'gnus-summary-number-of-articles-in-thread
3362                        gnus-tmp-new-adopts))))
3363     (if char
3364         (if (> number 1) gnus-not-empty-thread-mark
3365           gnus-empty-thread-mark)
3366       number)))
3367
3368 (defsubst gnus-summary-line-message-size (head)
3369   "Return pretty-printed version of message size.
3370 This function is intended to be used in
3371 `gnus-summary-line-format-alist', which see."
3372   (let ((c (or (mail-header-chars head) -1)))
3373     (cond ((< c 0) "n/a")               ; chars not available
3374           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3375           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3376           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3377           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3378
3379
3380 (defun gnus-summary-set-local-parameters (group)
3381   "Go through the local params of GROUP and set all variable specs in that list."
3382   (let ((params (gnus-group-find-parameter group))
3383         (vars '(quit-config))           ; Ignore quit-config.
3384         elem)
3385     (while params
3386       (setq elem (car params)
3387             params (cdr params))
3388       (and (consp elem)                 ; Has to be a cons.
3389            (consp (cdr elem))           ; The cdr has to be a list.
3390            (symbolp (car elem))         ; Has to be a symbol in there.
3391            (not (memq (car elem) vars))
3392            (ignore-errors               ; So we set it.
3393              (push (car elem) vars)
3394              (make-local-variable (car elem))
3395              (set (car elem) (eval (nth 1 elem))))))))
3396
3397 (defun gnus-summary-read-group (group &optional show-all no-article
3398                                       kill-buffer no-display backward
3399                                       select-articles)
3400   "Start reading news in newsgroup GROUP.
3401 If SHOW-ALL is non-nil, already read articles are also listed.
3402 If NO-ARTICLE is non-nil, no article is selected initially.
3403 If NO-DISPLAY, don't generate a summary buffer."
3404   (let (result)
3405     (while (and group
3406                 (null (setq result
3407                             (let ((gnus-auto-select-next nil))
3408                               (or (gnus-summary-read-group-1
3409                                    group show-all no-article
3410                                    kill-buffer no-display
3411                                    select-articles)
3412                                   (setq show-all nil
3413                                         select-articles nil)))))
3414                 (eq gnus-auto-select-next 'quietly))
3415       (set-buffer gnus-group-buffer)
3416       ;; The entry function called above goes to the next
3417       ;; group automatically, so we go two groups back
3418       ;; if we are searching for the previous group.
3419       (when backward
3420         (gnus-group-prev-unread-group 2))
3421       (if (not (equal group (gnus-group-group-name)))
3422           (setq group (gnus-group-group-name))
3423         (setq group nil)))
3424     result))
3425
3426 (defun gnus-summary-read-group-1 (group show-all no-article
3427                                         kill-buffer no-display
3428                                         &optional select-articles)
3429   ;; Killed foreign groups can't be entered.
3430   ;;  (when (and (not (gnus-group-native-p group))
3431   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3432   ;;    (error "Dead non-native groups can't be entered"))
3433   (gnus-message 5 "Retrieving newsgroup: %s..."
3434                 (gnus-group-decoded-name group))
3435   (let* ((new-group (gnus-summary-setup-buffer group))
3436          (quit-config (gnus-group-quit-config group))
3437          (did-select (and new-group (gnus-select-newsgroup
3438                                      group show-all select-articles))))
3439     (cond
3440      ;; This summary buffer exists already, so we just select it.
3441      ((not new-group)
3442       (gnus-set-global-variables)
3443       (when kill-buffer
3444         (gnus-kill-or-deaden-summary kill-buffer))
3445       (gnus-configure-windows 'summary 'force)
3446       (gnus-set-mode-line 'summary)
3447       (gnus-summary-position-point)
3448       (message "")
3449       t)
3450      ;; We couldn't select this group.
3451      ((null did-select)
3452       (when (and (eq major-mode 'gnus-summary-mode)
3453                  (not (equal (current-buffer) kill-buffer)))
3454         (kill-buffer (current-buffer))
3455         (if (not quit-config)
3456             (progn
3457               ;; Update the info -- marks might need to be removed,
3458               ;; for instance.
3459               (gnus-summary-update-info)
3460               (set-buffer gnus-group-buffer)
3461               (gnus-group-jump-to-group group)
3462               (gnus-group-next-unread-group 1))
3463           (gnus-handle-ephemeral-exit quit-config)))
3464       (let ((grpinfo (gnus-get-info group)))
3465         (if (null (gnus-info-read grpinfo))
3466             (gnus-message 3 "Group %s contains no messages"
3467                           (gnus-group-decoded-name group))
3468           (gnus-message 3 "Can't select group")))
3469       nil)
3470      ;; The user did a `C-g' while prompting for number of articles,
3471      ;; so we exit this group.
3472      ((eq did-select 'quit)
3473       (and (eq major-mode 'gnus-summary-mode)
3474            (not (equal (current-buffer) kill-buffer))
3475            (kill-buffer (current-buffer)))
3476       (when kill-buffer
3477         (gnus-kill-or-deaden-summary kill-buffer))
3478       (if (not quit-config)
3479           (progn
3480             (set-buffer gnus-group-buffer)
3481             (gnus-group-jump-to-group group)
3482             (gnus-group-next-unread-group 1)
3483             (gnus-configure-windows 'group 'force))
3484         (gnus-handle-ephemeral-exit quit-config))
3485       ;; Finally signal the quit.
3486       (signal 'quit nil))
3487      ;; The group was successfully selected.
3488      (t
3489       (gnus-set-global-variables)
3490       ;; Save the active value in effect when the group was entered.
3491       (setq gnus-newsgroup-active
3492             (gnus-copy-sequence
3493              (gnus-active gnus-newsgroup-name)))
3494       ;; You can change the summary buffer in some way with this hook.
3495       (gnus-run-hooks 'gnus-select-group-hook)
3496       (gnus-update-format-specifications
3497        nil 'summary 'summary-mode 'summary-dummy)
3498       (gnus-update-summary-mark-positions)
3499       ;; Do score processing.
3500       (when gnus-use-scoring
3501         (gnus-possibly-score-headers))
3502       ;; Check whether to fill in the gaps in the threads.
3503       (when gnus-build-sparse-threads
3504         (gnus-build-sparse-threads))
3505       ;; Find the initial limit.
3506       (if gnus-show-threads
3507           (if show-all
3508               (let ((gnus-newsgroup-dormant nil))
3509                 (gnus-summary-initial-limit show-all))
3510             (gnus-summary-initial-limit show-all))
3511         ;; When unthreaded, all articles are always shown.
3512         (setq gnus-newsgroup-limit
3513               (mapcar
3514                (lambda (header) (mail-header-number header))
3515                gnus-newsgroup-headers)))
3516       ;; Generate the summary buffer.
3517       (unless no-display
3518         (gnus-summary-prepare))
3519       (when gnus-use-trees
3520         (gnus-tree-open group)
3521         (setq gnus-summary-highlight-line-function
3522               'gnus-tree-highlight-article))
3523       ;; If the summary buffer is empty, but there are some low-scored
3524       ;; articles or some excluded dormants, we include these in the
3525       ;; buffer.
3526       (when (and (zerop (buffer-size))
3527                  (not no-display))
3528         (cond (gnus-newsgroup-dormant
3529                (gnus-summary-limit-include-dormant))
3530               ((and gnus-newsgroup-scored show-all)
3531                (gnus-summary-limit-include-expunged t))))
3532       ;; Function `gnus-apply-kill-file' must be called in this hook.
3533       (gnus-run-hooks 'gnus-apply-kill-hook)
3534       (if (and (zerop (buffer-size))
3535                (not no-display))
3536           (progn
3537             ;; This newsgroup is empty.
3538             (gnus-summary-catchup-and-exit nil t)
3539             (gnus-message 6 "No unread news")
3540             (when kill-buffer
3541               (gnus-kill-or-deaden-summary kill-buffer))
3542             ;; Return nil from this function.
3543             nil)
3544         ;; Hide conversation thread subtrees.  We cannot do this in
3545         ;; gnus-summary-prepare-hook since kill processing may not
3546         ;; work with hidden articles.
3547         (gnus-summary-maybe-hide-threads)
3548         (when kill-buffer
3549           (gnus-kill-or-deaden-summary kill-buffer))
3550         (gnus-summary-auto-select-subject)
3551         ;; Show first unread article if requested.
3552         (if (and (not no-article)
3553                  (not no-display)
3554                  gnus-newsgroup-unreads
3555                  gnus-auto-select-first)
3556             (progn
3557               (gnus-configure-windows 'summary)
3558               (let ((art (gnus-summary-article-number)))
3559                 (unless (and (not gnus-plugged)
3560                              (or (memq art gnus-newsgroup-undownloaded)
3561                                  (memq art gnus-newsgroup-downloadable)))
3562                   (gnus-summary-goto-article art))))
3563           ;; Don't select any articles.
3564           (gnus-summary-position-point)
3565           (gnus-configure-windows 'summary 'force)
3566           (gnus-set-mode-line 'summary))
3567         (when (and gnus-auto-center-group
3568                    (get-buffer-window gnus-group-buffer t))
3569           ;; Gotta use windows, because recenter does weird stuff if
3570           ;; the current buffer ain't the displayed window.
3571           (let ((owin (selected-window)))
3572             (select-window (get-buffer-window gnus-group-buffer t))
3573             (when (gnus-group-goto-group group)
3574               (recenter))
3575             (select-window owin)))
3576         ;; Mark this buffer as "prepared".
3577         (setq gnus-newsgroup-prepared t)
3578         (gnus-run-hooks 'gnus-summary-prepared-hook)
3579         (unless (gnus-ephemeral-group-p group)
3580           (gnus-group-update-group group))
3581         t)))))
3582
3583 (defun gnus-summary-auto-select-subject ()
3584   "Select the subject line on initial group entry."
3585   (goto-char (point-min))
3586   (cond
3587    ((eq gnus-auto-select-subject 'best)
3588     (gnus-summary-best-unread-subject))
3589    ((eq gnus-auto-select-subject 'unread)
3590     (gnus-summary-first-unread-subject))
3591    ((eq gnus-auto-select-subject 'unseen)
3592     (gnus-summary-first-unseen-subject))
3593    ((eq gnus-auto-select-subject 'unseen-or-unread)
3594     (gnus-summary-first-unseen-or-unread-subject))
3595    ((eq gnus-auto-select-subject 'first)
3596     ;; Do nothing.
3597     )
3598    ((gnus-functionp gnus-auto-select-subject)
3599     (funcall gnus-auto-select-subject))))
3600
3601 (defun gnus-summary-prepare ()
3602   "Generate the summary buffer."
3603   (interactive)
3604   (let ((buffer-read-only nil))
3605     (erase-buffer)
3606     (setq gnus-newsgroup-data nil
3607           gnus-newsgroup-data-reverse nil)
3608     (gnus-run-hooks 'gnus-summary-generate-hook)
3609     ;; Generate the buffer, either with threads or without.
3610     (when gnus-newsgroup-headers
3611       (gnus-summary-prepare-threads
3612        (if gnus-show-threads
3613            (gnus-sort-gathered-threads
3614             (funcall gnus-summary-thread-gathering-function
3615                      (gnus-sort-threads
3616                       (gnus-cut-threads (gnus-make-threads)))))
3617          ;; Unthreaded display.
3618          (gnus-sort-articles gnus-newsgroup-headers))))
3619     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3620     ;; Call hooks for modifying summary buffer.
3621     (goto-char (point-min))
3622     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3623
3624 (defsubst gnus-general-simplify-subject (subject)
3625   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3626   (setq subject
3627         (cond
3628          ;; Truncate the subject.
3629          (gnus-simplify-subject-functions
3630           (gnus-map-function gnus-simplify-subject-functions subject))
3631          ((numberp gnus-summary-gather-subject-limit)
3632           (setq subject (gnus-simplify-subject-re subject))
3633           (if (> (length subject) gnus-summary-gather-subject-limit)
3634               (substring subject 0 gnus-summary-gather-subject-limit)
3635             subject))
3636          ;; Fuzzily simplify it.
3637          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3638           (gnus-simplify-subject-fuzzy subject))
3639          ;; Just remove the leading "Re:".
3640          (t
3641           (gnus-simplify-subject-re subject))))
3642
3643   (if (and gnus-summary-gather-exclude-subject
3644            (string-match gnus-summary-gather-exclude-subject subject))
3645       nil                         ; This article shouldn't be gathered
3646     subject))
3647
3648 (defun gnus-summary-simplify-subject-query ()
3649   "Query where the respool algorithm would put this article."
3650   (interactive)
3651   (gnus-summary-select-article)
3652   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3653
3654 (defun gnus-gather-threads-by-subject (threads)
3655   "Gather threads by looking at Subject headers."
3656   (if (not gnus-summary-make-false-root)
3657       threads
3658     (let ((hashtb (gnus-make-hashtable 1024))
3659           (prev threads)
3660           (result threads)
3661           subject hthread whole-subject)
3662       (while threads
3663         (setq subject (gnus-general-simplify-subject
3664                        (setq whole-subject (mail-header-subject
3665                                             (caar threads)))))
3666         (when subject
3667           (if (setq hthread (gnus-gethash subject hashtb))
3668               (progn
3669                 ;; We enter a dummy root into the thread, if we
3670                 ;; haven't done that already.
3671                 (unless (stringp (caar hthread))
3672                   (setcar hthread (list whole-subject (car hthread))))
3673                 ;; We add this new gathered thread to this gathered
3674                 ;; thread.
3675                 (setcdr (car hthread)
3676                         (nconc (cdar hthread) (list (car threads))))
3677                 ;; Remove it from the list of threads.
3678                 (setcdr prev (cdr threads))
3679                 (setq threads prev))
3680             ;; Enter this thread into the hash table.
3681             (gnus-sethash subject
3682                           (if gnus-summary-make-false-root-always
3683                               (progn
3684                                 ;; If you want a dummy root above all
3685                                 ;; threads...
3686                                 (setcar threads (list whole-subject
3687                                                       (car threads)))
3688                                 threads)
3689                             threads)
3690                           hashtb)))
3691         (setq prev threads)
3692         (setq threads (cdr threads)))
3693       result)))
3694
3695 (defun gnus-gather-threads-by-references (threads)
3696   "Gather threads by looking at References headers."
3697   (let ((idhashtb (gnus-make-hashtable 1024))
3698         (thhashtb (gnus-make-hashtable 1024))
3699         (prev threads)
3700         (result threads)
3701         ids references id gthread gid entered ref)
3702     (while threads
3703       (when (setq references (mail-header-references (caar threads)))
3704         (setq id (mail-header-id (caar threads))
3705               ids (inline (gnus-split-references references))
3706               entered nil)
3707         (while (setq ref (pop ids))
3708           (setq ids (delete ref ids))
3709           (if (not (setq gid (gnus-gethash ref idhashtb)))
3710               (progn
3711                 (gnus-sethash ref id idhashtb)
3712                 (gnus-sethash id threads thhashtb))
3713             (setq gthread (gnus-gethash gid thhashtb))
3714             (unless entered
3715               ;; We enter a dummy root into the thread, if we
3716               ;; haven't done that already.
3717               (unless (stringp (caar gthread))
3718                 (setcar gthread (list (mail-header-subject (caar gthread))
3719                                       (car gthread))))
3720               ;; We add this new gathered thread to this gathered
3721               ;; thread.
3722               (setcdr (car gthread)
3723                       (nconc (cdar gthread) (list (car threads)))))
3724             ;; Add it into the thread hash table.
3725             (gnus-sethash id gthread thhashtb)
3726             (setq entered t)
3727             ;; Remove it from the list of threads.
3728             (setcdr prev (cdr threads))
3729             (setq threads prev))))
3730       (setq prev threads)
3731       (setq threads (cdr threads)))
3732     result))
3733
3734 (defun gnus-sort-gathered-threads (threads)
3735   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3736   (let ((result threads))
3737     (while threads
3738       (when (stringp (caar threads))
3739         (setcdr (car threads)
3740                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3741       (setq threads (cdr threads)))
3742     result))
3743
3744 (defun gnus-thread-loop-p (root thread)
3745   "Say whether ROOT is in THREAD."
3746   (let ((stack (list thread))
3747         (infloop 0)
3748         th)
3749     (while (setq thread (pop stack))
3750       (setq th (cdr thread))
3751       (while (and th
3752                   (not (eq (caar th) root)))
3753         (pop th))
3754       (if th
3755           ;; We have found a loop.
3756           (let (ref-dep)
3757             (setcdr thread (delq (car th) (cdr thread)))
3758             (if (boundp (setq ref-dep (intern "none"
3759                                               gnus-newsgroup-dependencies)))
3760                 (setcdr (symbol-value ref-dep)
3761                         (nconc (cdr (symbol-value ref-dep))
3762                                (list (car th))))
3763               (set ref-dep (list nil (car th))))
3764             (setq infloop 1
3765                   stack nil))
3766         ;; Push all the subthreads onto the stack.
3767         (push (cdr thread) stack)))
3768     infloop))
3769
3770 (defun gnus-make-threads ()
3771   "Go through the dependency hashtb and find the roots.  Return all threads."
3772   (let (threads)
3773     (while (catch 'infloop
3774              (mapatoms
3775               (lambda (refs)
3776                 ;; Deal with self-referencing References loops.
3777                 (when (and (car (symbol-value refs))
3778                            (not (zerop
3779                                  (apply
3780                                   '+
3781                                   (mapcar
3782                                    (lambda (thread)
3783                                      (gnus-thread-loop-p
3784                                       (car (symbol-value refs)) thread))
3785                                    (cdr (symbol-value refs)))))))
3786                   (setq threads nil)
3787                   (throw 'infloop t))
3788                 (unless (car (symbol-value refs))
3789                   ;; These threads do not refer back to any other
3790                   ;; articles, so they're roots.
3791                   (setq threads (append (cdr (symbol-value refs)) threads))))
3792               gnus-newsgroup-dependencies)))
3793     threads))
3794
3795 ;; Build the thread tree.
3796 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3797   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3798
3799 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3800 if it was already present.
3801
3802 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3803 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3804 Message-IDs will be renamed to a unique Message-ID before being
3805 entered.
3806
3807 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3808   (let* ((id (mail-header-id header))
3809          (id-dep (and id (intern id dependencies)))
3810          parent-id ref ref-dep ref-header replaced)
3811     ;; Enter this `header' in the `dependencies' table.
3812     (cond
3813      ((not id-dep)
3814       (setq header nil))
3815      ;; The first two cases do the normal part: enter a new `header'
3816      ;; in the `dependencies' table.
3817      ((not (boundp id-dep))
3818       (set id-dep (list header)))
3819      ((null (car (symbol-value id-dep)))
3820       (setcar (symbol-value id-dep) header))
3821
3822      ;; From here the `header' was already present in the
3823      ;; `dependencies' table.
3824      (force-new
3825       ;; Overrides an existing entry;
3826       ;; just set the header part of the entry.
3827       (setcar (symbol-value id-dep) header)
3828       (setq replaced t))
3829
3830      ;; Renames the existing `header' to a unique Message-ID.
3831      ((not gnus-summary-ignore-duplicates)
3832       ;; An article with this Message-ID has already been seen.
3833       ;; We rename the Message-ID.
3834       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3835            (list header))
3836       (mail-header-set-id header id))
3837
3838      ;; The last case ignores an existing entry, except it adds any
3839      ;; additional Xrefs (in case the two articles came from different
3840      ;; servers.
3841      ;; Also sets `header' to `nil' meaning that the `dependencies'
3842      ;; table was *not* modified.
3843      (t
3844       (mail-header-set-xref
3845        (car (symbol-value id-dep))
3846        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3847                    "")
3848                (or (mail-header-xref header) "")))
3849       (setq header nil)))
3850
3851     (when (and header (not replaced))
3852       ;; First check that we are not creating a References loop.
3853       (setq parent-id (gnus-parent-id (mail-header-references header)))
3854       (setq ref parent-id)
3855       (while (and ref
3856                   (setq ref-dep (intern-soft ref dependencies))
3857                   (boundp ref-dep)
3858                   (setq ref-header (car (symbol-value ref-dep))))
3859         (if (string= id ref)
3860             ;; Yuk!  This is a reference loop.  Make the article be a
3861             ;; root article.
3862             (progn
3863               (mail-header-set-references (car (symbol-value id-dep)) "none")
3864               (setq ref nil)
3865               (setq parent-id nil))
3866           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3867       (setq ref-dep (intern (or parent-id "none") dependencies))
3868       (if (boundp ref-dep)
3869           (setcdr (symbol-value ref-dep)
3870                   (nconc (cdr (symbol-value ref-dep))
3871                          (list (symbol-value id-dep))))
3872         (set ref-dep (list nil (symbol-value id-dep)))))
3873     header))
3874
3875 (defun gnus-extract-message-id-from-in-reply-to (string)
3876   (if (string-match "<[^>]+>" string)
3877       (substring string (match-beginning 0) (match-end 0))
3878     nil))
3879
3880 (defun gnus-build-sparse-threads ()
3881   (let ((headers gnus-newsgroup-headers)
3882         (mail-parse-charset gnus-newsgroup-charset)
3883         (gnus-summary-ignore-duplicates t)
3884         header references generation relations
3885         subject child end new-child date)
3886     ;; First we create an alist of generations/relations, where
3887     ;; generations is how much we trust the relation, and the relation
3888     ;; is parent/child.
3889     (gnus-message 7 "Making sparse threads...")
3890     (save-excursion
3891       (nnheader-set-temp-buffer " *gnus sparse threads*")
3892       (while (setq header (pop headers))
3893         (when (and (setq references (mail-header-references header))
3894                    (not (string= references "")))
3895           (insert references)
3896           (setq child (mail-header-id header)
3897                 subject (mail-header-subject header)
3898                 date (mail-header-date header)
3899                 generation 0)
3900           (while (search-backward ">" nil t)
3901             (setq end (1+ (point)))
3902             (when (search-backward "<" nil t)
3903               (setq new-child (buffer-substring (point) end))
3904               (push (list (incf generation)
3905                           child (setq child new-child)
3906                           subject date)
3907                     relations)))
3908           (when child
3909             (push (list (1+ generation) child nil subject) relations))
3910           (erase-buffer)))
3911       (kill-buffer (current-buffer)))
3912     ;; Sort over trustworthiness.
3913     (mapcar
3914      (lambda (relation)
3915        (when (gnus-dependencies-add-header
3916               (make-full-mail-header
3917                gnus-reffed-article-number
3918                (nth 3 relation) "" (or (nth 4 relation) "")
3919                (nth 1 relation)
3920                (or (nth 2 relation) "") 0 0 "")
3921               gnus-newsgroup-dependencies nil)
3922          (push gnus-reffed-article-number gnus-newsgroup-limit)
3923          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3924          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3925                gnus-newsgroup-reads)
3926          (decf gnus-reffed-article-number)))
3927      (sort relations 'car-less-than-car))
3928     (gnus-message 7 "Making sparse threads...done")))
3929
3930 (defun gnus-build-old-threads ()
3931   ;; Look at all the articles that refer back to old articles, and
3932   ;; fetch the headers for the articles that aren't there.  This will
3933   ;; build complete threads - if the roots haven't been expired by the
3934   ;; server, that is.
3935   (let ((mail-parse-charset gnus-newsgroup-charset)
3936         id heads)
3937     (mapatoms
3938      (lambda (refs)
3939        (when (not (car (symbol-value refs)))
3940          (setq heads (cdr (symbol-value refs)))
3941          (while heads
3942            (if (memq (mail-header-number (caar heads))
3943                      gnus-newsgroup-dormant)
3944                (setq heads (cdr heads))
3945              (setq id (symbol-name refs))
3946              (while (and (setq id (gnus-build-get-header id))
3947                          (not (car (gnus-id-to-thread id)))))
3948              (setq heads nil)))))
3949      gnus-newsgroup-dependencies)))
3950
3951 ;; This function has to be called with point after the article number
3952 ;; on the beginning of the line.
3953 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3954   (let ((eol (gnus-point-at-eol))
3955         (buffer (current-buffer))
3956         header references in-reply-to)
3957
3958     ;; overview: [num subject from date id refs chars lines misc]
3959     (unwind-protect
3960         (let (x)
3961           (narrow-to-region (point) eol)
3962           (unless (eobp)
3963             (forward-char))
3964
3965           (setq header
3966                 (make-full-mail-header
3967                  number                 ; number
3968                  (condition-case ()     ; subject
3969                      (funcall gnus-decode-encoded-word-function
3970                               (setq x (nnheader-nov-field)))
3971                    (error x))
3972                  (condition-case ()     ; from
3973                      (funcall gnus-decode-encoded-word-function
3974                               (setq x (nnheader-nov-field)))
3975                    (error x))
3976                  (nnheader-nov-field)   ; date
3977                  (nnheader-nov-read-message-id) ; id
3978                  (setq references (nnheader-nov-field)) ; refs
3979                  (nnheader-nov-read-integer) ; chars
3980                  (nnheader-nov-read-integer) ; lines
3981                  (unless (eobp)
3982                    (if (looking-at "Xref: ")
3983                        (goto-char (match-end 0)))
3984                    (nnheader-nov-field)) ; Xref
3985                  (nnheader-nov-parse-extra)))) ; extra
3986
3987       (widen))
3988
3989     (when (and (string= references "")
3990                (setq in-reply-to (mail-header-extra header))
3991                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3992       (mail-header-set-references
3993        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3994
3995     (when gnus-alter-header-function
3996       (funcall gnus-alter-header-function header))
3997     (gnus-dependencies-add-header header dependencies force-new)))
3998
3999 (defun gnus-build-get-header (id)
4000   "Look through the buffer of NOV lines and find the header to ID.
4001 Enter this line into the dependencies hash table, and return
4002 the id of the parent article (if any)."
4003   (let ((deps gnus-newsgroup-dependencies)
4004         found header)
4005     (prog1
4006         (save-excursion
4007           (set-buffer nntp-server-buffer)
4008           (let ((case-fold-search nil))
4009             (goto-char (point-min))
4010             (while (and (not found)
4011                         (search-forward id nil t))
4012               (beginning-of-line)
4013               (setq found (looking-at
4014                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4015                                    (regexp-quote id))))
4016               (or found (beginning-of-line 2)))
4017             (when found
4018               (beginning-of-line)
4019               (and
4020                (setq header (gnus-nov-parse-line
4021                              (read (current-buffer)) deps))
4022                (gnus-parent-id (mail-header-references header))))))
4023       (when header
4024         (let ((number (mail-header-number header)))
4025           (push number gnus-newsgroup-limit)
4026           (push header gnus-newsgroup-headers)
4027           (if (memq number gnus-newsgroup-unselected)
4028               (progn
4029                 (setq gnus-newsgroup-unreads
4030                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4031                                                number))
4032                 (setq gnus-newsgroup-unselected
4033                       (delq number gnus-newsgroup-unselected)))
4034             (push number gnus-newsgroup-ancient)))))))
4035
4036 (defun gnus-build-all-threads ()
4037   "Read all the headers."
4038   (let ((gnus-summary-ignore-duplicates t)
4039         (mail-parse-charset gnus-newsgroup-charset)
4040         (dependencies gnus-newsgroup-dependencies)
4041         header article)
4042     (save-excursion
4043       (set-buffer nntp-server-buffer)
4044       (let ((case-fold-search nil))
4045         (goto-char (point-min))
4046         (while (not (eobp))
4047           (ignore-errors
4048             (setq article (read (current-buffer))
4049                   header (gnus-nov-parse-line article dependencies)))
4050           (when header
4051             (save-excursion
4052               (set-buffer gnus-summary-buffer)
4053               (push header gnus-newsgroup-headers)
4054               (if (memq (setq article (mail-header-number header))
4055                         gnus-newsgroup-unselected)
4056                   (progn
4057                     (setq gnus-newsgroup-unreads
4058                           (gnus-add-to-sorted-list
4059                            gnus-newsgroup-unreads article))
4060                     (setq gnus-newsgroup-unselected
4061                           (delq article gnus-newsgroup-unselected)))
4062                 (push article gnus-newsgroup-ancient)))
4063             (forward-line 1)))))))
4064
4065 (defun gnus-summary-update-article-line (article header)
4066   "Update the line for ARTICLE using HEADERS."
4067   (let* ((id (mail-header-id header))
4068          (thread (gnus-id-to-thread id)))
4069     (unless thread
4070       (error "Article in no thread"))
4071     ;; Update the thread.
4072     (setcar thread header)
4073     (gnus-summary-goto-subject article)
4074     (let* ((datal (gnus-data-find-list article))
4075            (data (car datal))
4076            (buffer-read-only nil)
4077            (level (gnus-summary-thread-level)))
4078       (gnus-delete-line)
4079       (let ((inserted (- (point)
4080                          (progn
4081                            (gnus-summary-insert-line
4082                             header level nil 
4083                             (memq article gnus-newsgroup-undownloaded)
4084                             (gnus-article-mark article)
4085                             (memq article gnus-newsgroup-replied)
4086                             (memq article gnus-newsgroup-expirable)
4087                             ;; Only insert the Subject string when it's different
4088                             ;; from the previous Subject string.
4089                             (if (and
4090                                  gnus-show-threads
4091                                  (gnus-subject-equal
4092                                   (condition-case ()
4093                                       (mail-header-subject
4094                                        (gnus-data-header
4095                                         (cadr
4096                                          (gnus-data-find-list
4097                                           article
4098                                           (gnus-data-list t)))))
4099                                     ;; Error on the side of excessive subjects.
4100                                     (error ""))
4101                                   (mail-header-subject header)))
4102                                 ""
4103                               (mail-header-subject header))
4104                             nil (cdr (assq article gnus-newsgroup-scored))
4105                             (memq article gnus-newsgroup-processable))
4106                            (point)))))
4107         (when (cdr datal)
4108           (gnus-data-update-list
4109            (cdr datal) 
4110            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4111
4112 (defun gnus-summary-update-article (article &optional iheader)
4113   "Update ARTICLE in the summary buffer."
4114   (set-buffer gnus-summary-buffer)
4115   (let* ((header (gnus-summary-article-header article))
4116          (id (mail-header-id header))
4117          (data (gnus-data-find article))
4118          (thread (gnus-id-to-thread id))
4119          (references (mail-header-references header))
4120          (parent
4121           (gnus-id-to-thread
4122            (or (gnus-parent-id
4123                 (when (and references
4124                            (not (equal "" references)))
4125                   references))
4126                "none")))
4127          (buffer-read-only nil)
4128          (old (car thread)))
4129     (when thread
4130       (unless iheader
4131         (setcar thread nil)
4132         (when parent
4133           (delq thread parent)))
4134       (if (gnus-summary-insert-subject id header)
4135           ;; Set the (possibly) new article number in the data structure.
4136           (gnus-data-set-number data (gnus-id-to-article id))
4137         (setcar thread old)
4138         nil))))
4139
4140 (defun gnus-rebuild-thread (id &optional line)
4141   "Rebuild the thread containing ID.
4142 If LINE, insert the rebuilt thread starting on line LINE."
4143   (let ((buffer-read-only nil)
4144         old-pos current thread data)
4145     (if (not gnus-show-threads)
4146         (setq thread (list (car (gnus-id-to-thread id))))
4147       ;; Get the thread this article is part of.
4148       (setq thread (gnus-remove-thread id)))
4149     (setq old-pos (gnus-point-at-bol))
4150     (setq current (save-excursion
4151                     (and (re-search-backward "[\r\n]" nil t)
4152                          (gnus-summary-article-number))))
4153     ;; If this is a gathered thread, we have to go some re-gathering.
4154     (when (stringp (car thread))
4155       (let ((subject (car thread))
4156             roots thr)
4157         (setq thread (cdr thread))
4158         (while thread
4159           (unless (memq (setq thr (gnus-id-to-thread
4160                                    (gnus-root-id
4161                                     (mail-header-id (caar thread)))))
4162                         roots)
4163             (push thr roots))
4164           (setq thread (cdr thread)))
4165         ;; We now have all (unique) roots.
4166         (if (= (length roots) 1)
4167             ;; All the loose roots are now one solid root.
4168             (setq thread (car roots))
4169           (setq thread (cons subject (gnus-sort-threads roots))))))
4170     (let (threads)
4171       ;; We then insert this thread into the summary buffer.
4172       (when line
4173         (goto-char (point-min))
4174         (forward-line (1- line)))
4175       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4176         (if gnus-show-threads
4177             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4178           (gnus-summary-prepare-unthreaded thread))
4179         (setq data (nreverse gnus-newsgroup-data))
4180         (setq threads gnus-newsgroup-threads))
4181       ;; We splice the new data into the data structure.
4182       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4183       ;;!!! then we want to insert at the beginning of the buffer.
4184       ;;!!! That happens to be true with Gnus now, but that may
4185       ;;!!! change in the future.  Perhaps.
4186       (gnus-data-enter-list
4187        (if line nil current) data (- (point) old-pos))
4188       (setq gnus-newsgroup-threads
4189             (nconc threads gnus-newsgroup-threads))
4190       (gnus-data-compute-positions))))
4191
4192 (defun gnus-number-to-header (number)
4193   "Return the header for article NUMBER."
4194   (let ((headers gnus-newsgroup-headers))
4195     (while (and headers
4196                 (not (= number (mail-header-number (car headers)))))
4197       (pop headers))
4198     (when headers
4199       (car headers))))
4200
4201 (defun gnus-parent-headers (in-headers &optional generation)
4202   "Return the headers of the GENERATIONeth parent of HEADERS."
4203   (unless generation
4204     (setq generation 1))
4205   (let ((parent t)
4206         (headers in-headers)
4207         references)
4208     (while (and parent
4209                 (not (zerop generation))
4210                 (setq references (mail-header-references headers)))
4211       (setq headers (if (and references
4212                              (setq parent (gnus-parent-id references)))
4213                         (car (gnus-id-to-thread parent))
4214                       nil))
4215       (decf generation))
4216     (and (not (eq headers in-headers))
4217          headers)))
4218
4219 (defun gnus-id-to-thread (id)
4220   "Return the (sub-)thread where ID appears."
4221   (gnus-gethash id gnus-newsgroup-dependencies))
4222
4223 (defun gnus-id-to-article (id)
4224   "Return the article number of ID."
4225   (let ((thread (gnus-id-to-thread id)))
4226     (when (and thread
4227                (car thread))
4228       (mail-header-number (car thread)))))
4229
4230 (defun gnus-id-to-header (id)
4231   "Return the article headers of ID."
4232   (car (gnus-id-to-thread id)))
4233
4234 (defun gnus-article-displayed-root-p (article)
4235   "Say whether ARTICLE is a root(ish) article."
4236   (let ((level (gnus-summary-thread-level article))
4237         (refs (mail-header-references  (gnus-summary-article-header article)))
4238         particle)
4239     (cond
4240      ((null level) nil)
4241      ((zerop level) t)
4242      ((null refs) t)
4243      ((null (gnus-parent-id refs)) t)
4244      ((and (= 1 level)
4245            (null (setq particle (gnus-id-to-article
4246                                  (gnus-parent-id refs))))
4247            (null (gnus-summary-thread-level particle)))))))
4248
4249 (defun gnus-root-id (id)
4250   "Return the id of the root of the thread where ID appears."
4251   (let (last-id prev)
4252     (while (and id (setq prev (car (gnus-id-to-thread id))))
4253       (setq last-id id
4254             id (gnus-parent-id (mail-header-references prev))))
4255     last-id))
4256
4257 (defun gnus-articles-in-thread (thread)
4258   "Return the list of articles in THREAD."
4259   (cons (mail-header-number (car thread))
4260         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4261
4262 (defun gnus-remove-thread (id &optional dont-remove)
4263   "Remove the thread that has ID in it."
4264   (let (headers thread last-id)
4265     ;; First go up in this thread until we find the root.
4266     (setq last-id (gnus-root-id id)
4267           headers (message-flatten-list (gnus-id-to-thread last-id)))
4268     ;; We have now found the real root of this thread.  It might have
4269     ;; been gathered into some loose thread, so we have to search
4270     ;; through the threads to find the thread we wanted.
4271     (let ((threads gnus-newsgroup-threads)
4272           sub)
4273       (while threads
4274         (setq sub (car threads))
4275         (if (stringp (car sub))
4276             ;; This is a gathered thread, so we look at the roots
4277             ;; below it to find whether this article is in this
4278             ;; gathered root.
4279             (progn
4280               (setq sub (cdr sub))
4281               (while sub
4282                 (when (member (caar sub) headers)
4283                   (setq thread (car threads)
4284                         threads nil
4285                         sub nil))
4286                 (setq sub (cdr sub))))
4287           ;; It's an ordinary thread, so we check it.
4288           (when (eq (car sub) (car headers))
4289             (setq thread sub
4290                   threads nil)))
4291         (setq threads (cdr threads)))
4292       ;; If this article is in no thread, then it's a root.
4293       (if thread
4294           (unless dont-remove
4295             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4296         (setq thread (gnus-id-to-thread last-id)))
4297       (when thread
4298         (prog1
4299             thread                      ; We return this thread.
4300           (unless dont-remove
4301             (if (stringp (car thread))
4302                 (progn
4303                   ;; If we use dummy roots, then we have to remove the
4304                   ;; dummy root as well.
4305                   (when (eq gnus-summary-make-false-root 'dummy)
4306                     ;; We go to the dummy root by going to
4307                     ;; the first sub-"thread", and then one line up.
4308                     (gnus-summary-goto-article
4309                      (mail-header-number (caadr thread)))
4310                     (forward-line -1)
4311                     (gnus-delete-line)
4312                     (gnus-data-compute-positions))
4313                   (setq thread (cdr thread))
4314                   (while thread
4315                     (gnus-remove-thread-1 (car thread))
4316                     (setq thread (cdr thread))))
4317               (gnus-remove-thread-1 thread))))))))
4318
4319 (defun gnus-remove-thread-1 (thread)
4320   "Remove the thread THREAD recursively."
4321   (let ((number (mail-header-number (pop thread)))
4322         d)
4323     (setq thread (reverse thread))
4324     (while thread
4325       (gnus-remove-thread-1 (pop thread)))
4326     (when (setq d (gnus-data-find number))
4327       (goto-char (gnus-data-pos d))
4328       (gnus-summary-show-thread)
4329       (gnus-data-remove
4330        number
4331        (- (gnus-point-at-bol)
4332           (prog1
4333               (1+ (gnus-point-at-eol))
4334             (gnus-delete-line)))))))
4335
4336 (defun gnus-sort-threads-1 (threads func)
4337   (sort (mapcar (lambda (thread)
4338                   (cons (car thread)
4339                         (and (cdr thread)
4340                              (gnus-sort-threads-1 (cdr thread) func))))
4341                 threads) func))
4342
4343 (defun gnus-sort-threads (threads)
4344   "Sort THREADS."
4345   (if (not gnus-thread-sort-functions)
4346       threads
4347     (gnus-message 8 "Sorting threads...")
4348     (let ((max-lisp-eval-depth 5000))
4349       (prog1 (gnus-sort-threads-1
4350          threads
4351          (gnus-make-sort-function gnus-thread-sort-functions))
4352         (gnus-message 8 "Sorting threads...done")))))
4353
4354 (defun gnus-sort-articles (articles)
4355   "Sort ARTICLES."
4356   (when gnus-article-sort-functions
4357     (gnus-message 7 "Sorting articles...")
4358     (prog1
4359         (setq gnus-newsgroup-headers
4360               (sort articles (gnus-make-sort-function
4361                               gnus-article-sort-functions)))
4362       (gnus-message 7 "Sorting articles...done"))))
4363
4364 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4365 (defmacro gnus-thread-header (thread)
4366   "Return header of first article in THREAD.
4367 Note that THREAD must never, ever be anything else than a variable -
4368 using some other form will lead to serious barfage."
4369   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4370   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4371   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4372         (vector thread) 2))
4373
4374 (defsubst gnus-article-sort-by-number (h1 h2)
4375   "Sort articles by article number."
4376   (< (mail-header-number h1)
4377      (mail-header-number h2)))
4378
4379 (defun gnus-thread-sort-by-number (h1 h2)
4380   "Sort threads by root article number."
4381   (gnus-article-sort-by-number
4382    (gnus-thread-header h1) (gnus-thread-header h2)))
4383
4384 (defsubst gnus-article-sort-by-random (h1 h2)
4385   "Sort articles by article number."
4386   (zerop (random 2)))
4387
4388 (defun gnus-thread-sort-by-random (h1 h2)
4389   "Sort threads by root article number."
4390   (gnus-article-sort-by-random
4391    (gnus-thread-header h1) (gnus-thread-header h2)))
4392
4393 (defsubst gnus-article-sort-by-lines (h1 h2)
4394   "Sort articles by article Lines header."
4395   (< (mail-header-lines h1)
4396      (mail-header-lines h2)))
4397
4398 (defun gnus-thread-sort-by-lines (h1 h2)
4399   "Sort threads by root article Lines header."
4400   (gnus-article-sort-by-lines
4401    (gnus-thread-header h1) (gnus-thread-header h2)))
4402
4403 (defsubst gnus-article-sort-by-chars (h1 h2)
4404   "Sort articles by octet length."
4405   (< (mail-header-chars h1)
4406      (mail-header-chars h2)))
4407
4408 (defun gnus-thread-sort-by-chars (h1 h2)
4409   "Sort threads by root article octet length."
4410   (gnus-article-sort-by-chars
4411    (gnus-thread-header h1) (gnus-thread-header h2)))
4412
4413 (defsubst gnus-article-sort-by-author (h1 h2)
4414   "Sort articles by root author."
4415   (string-lessp
4416    (let ((extract (funcall
4417                    gnus-extract-address-components
4418                    (mail-header-from h1))))
4419      (or (car extract) (cadr extract) ""))
4420    (let ((extract (funcall
4421                    gnus-extract-address-components
4422                    (mail-header-from h2))))
4423      (or (car extract) (cadr extract) ""))))
4424
4425 (defun gnus-thread-sort-by-author (h1 h2)
4426   "Sort threads by root author."
4427   (gnus-article-sort-by-author
4428    (gnus-thread-header h1)  (gnus-thread-header h2)))
4429
4430 (defsubst gnus-article-sort-by-subject (h1 h2)
4431   "Sort articles by root subject."
4432   (string-lessp
4433    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4434    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4435
4436 (defun gnus-thread-sort-by-subject (h1 h2)
4437   "Sort threads by root subject."
4438   (gnus-article-sort-by-subject
4439    (gnus-thread-header h1) (gnus-thread-header h2)))
4440
4441 (defsubst gnus-article-sort-by-date (h1 h2)
4442   "Sort articles by root article date."
4443   (time-less-p
4444    (gnus-date-get-time (mail-header-date h1))
4445    (gnus-date-get-time (mail-header-date h2))))
4446
4447 (defun gnus-thread-sort-by-date (h1 h2)
4448   "Sort threads by root article date."
4449   (gnus-article-sort-by-date
4450    (gnus-thread-header h1) (gnus-thread-header h2)))
4451
4452 (defsubst gnus-article-sort-by-score (h1 h2)
4453   "Sort articles by root article score.
4454 Unscored articles will be counted as having a score of zero."
4455   (> (or (cdr (assq (mail-header-number h1)
4456                     gnus-newsgroup-scored))
4457          gnus-summary-default-score 0)
4458      (or (cdr (assq (mail-header-number h2)
4459                     gnus-newsgroup-scored))
4460          gnus-summary-default-score 0)))
4461
4462 (defun gnus-thread-sort-by-score (h1 h2)
4463   "Sort threads by root article score."
4464   (gnus-article-sort-by-score
4465    (gnus-thread-header h1) (gnus-thread-header h2)))
4466
4467 (defun gnus-thread-sort-by-total-score (h1 h2)
4468   "Sort threads by the sum of all scores in the thread.
4469 Unscored articles will be counted as having a score of zero."
4470   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4471
4472 (defun gnus-thread-total-score (thread)
4473   ;; This function find the total score of THREAD.
4474   (cond
4475    ((null thread)
4476     0)
4477    ((consp thread)
4478     (if (stringp (car thread))
4479         (apply gnus-thread-score-function 0
4480                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4481       (gnus-thread-total-score-1 thread)))
4482    (t
4483     (gnus-thread-total-score-1 (list thread)))))
4484
4485 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4486   "Sort threads such that the thread with the most recently arrived article comes first."
4487   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4488
4489 (defun gnus-thread-highest-number (thread)
4490   "Return the highest article number in THREAD."
4491   (apply 'max (mapcar (lambda (header)
4492                         (mail-header-number header))
4493                       (message-flatten-list thread))))
4494
4495 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4496   "Sort threads such that the thread with the most recently dated article comes first."
4497   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4498
4499 (defun gnus-thread-latest-date (thread)
4500   "Return the highest article date in THREAD."
4501   (let ((previous-time 0))
4502     (apply 'max
4503            (mapcar
4504             (lambda (header)
4505               (setq previous-time
4506                     (time-to-seconds
4507                      (condition-case ()
4508                          (mail-header-parse-date (mail-header-date header))
4509                        (error previous-time)))))
4510             (sort
4511              (message-flatten-list thread)
4512              (lambda (h1 h2)
4513                (< (mail-header-number h1)
4514                   (mail-header-number h2))))))))
4515
4516 (defun gnus-thread-total-score-1 (root)
4517   ;; This function find the total score of the thread below ROOT.
4518   (setq root (car root))
4519   (apply gnus-thread-score-function
4520          (or (append
4521               (mapcar 'gnus-thread-total-score
4522                       (cdr (gnus-id-to-thread (mail-header-id root))))
4523               (when (> (mail-header-number root) 0)
4524                 (list (or (cdr (assq (mail-header-number root)
4525                                      gnus-newsgroup-scored))
4526                           gnus-summary-default-score 0))))
4527              (list gnus-summary-default-score)
4528              '(0))))
4529
4530 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4531 (defvar gnus-tmp-prev-subject nil)
4532 (defvar gnus-tmp-false-parent nil)
4533 (defvar gnus-tmp-root-expunged nil)
4534 (defvar gnus-tmp-dummy-line nil)
4535
4536 (eval-when-compile (defvar gnus-tmp-header))
4537 (defun gnus-extra-header (type &optional header)
4538   "Return the extra header of TYPE."
4539   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4540       ""))
4541
4542 (defvar gnus-tmp-thread-tree-header-string "")
4543
4544 (defcustom gnus-sum-thread-tree-root "> "
4545   "With %B spec, used for the root of a thread.
4546 If nil, use subject instead."
4547   :type 'string
4548   :group 'gnus-thread)
4549 (defcustom gnus-sum-thread-tree-single-indent ""
4550   "With %B spec, used for a thread with just one message.
4551 If nil, use subject instead."
4552   :type 'string
4553   :group 'gnus-thread)
4554 (defcustom gnus-sum-thread-tree-vertical "| "
4555   "With %B spec, used for drawing a vertical line."
4556   :type 'string
4557   :group 'gnus-thread)
4558 (defcustom gnus-sum-thread-tree-indent "  "
4559   "With %B spec, used for indenting."
4560   :type 'string
4561   :group 'gnus-thread)
4562 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4563   "With %B spec, used for a leaf with brothers."
4564   :type 'string
4565   :group 'gnus-thread)
4566 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4567   "With %B spec, used for a leaf without brothers."
4568   :type 'string
4569   :group 'gnus-thread)
4570
4571 (defun gnus-summary-prepare-threads (threads)
4572   "Prepare summary buffer from THREADS and indentation LEVEL.
4573 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4574 or a straight list of headers."
4575   (gnus-message 7 "Generating summary...")
4576
4577   (setq gnus-newsgroup-threads threads)
4578   (beginning-of-line)
4579
4580   (let ((gnus-tmp-level 0)
4581         (default-score (or gnus-summary-default-score 0))
4582         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4583         (building-line-count gnus-summary-display-while-building)
4584         (building-count (integerp gnus-summary-display-while-building))
4585         thread number subject stack state gnus-tmp-gathered beg-match
4586         new-roots gnus-tmp-new-adopts thread-end simp-subject
4587         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4588         gnus-tmp-replied gnus-tmp-subject-or-nil
4589         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4590         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4591         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4592         tree-stack)
4593
4594     (setq gnus-tmp-prev-subject nil
4595           gnus-tmp-thread-tree-header-string "")
4596
4597     (if (vectorp (car threads))
4598         ;; If this is a straight (sic) list of headers, then a
4599         ;; threaded summary display isn't required, so we just create
4600         ;; an unthreaded one.
4601         (gnus-summary-prepare-unthreaded threads)
4602
4603       ;; Do the threaded display.
4604
4605       (if gnus-summary-display-while-building
4606           (switch-to-buffer (buffer-name)))
4607       (while (or threads stack gnus-tmp-new-adopts new-roots)
4608
4609         (if (and (= gnus-tmp-level 0)
4610                  (or (not stack)
4611                      (= (caar stack) 0))
4612                  (not gnus-tmp-false-parent)
4613                  (or gnus-tmp-new-adopts new-roots))
4614             (if gnus-tmp-new-adopts
4615                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4616                       thread (list (car gnus-tmp-new-adopts))
4617                       gnus-tmp-header (caar thread)
4618                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4619               (when new-roots
4620                 (setq thread (list (car new-roots))
4621                       gnus-tmp-header (caar thread)
4622                       new-roots (cdr new-roots))))
4623
4624           (if threads
4625               ;; If there are some threads, we do them before the
4626               ;; threads on the stack.
4627               (setq thread threads
4628                     gnus-tmp-header (caar thread))
4629             ;; There were no current threads, so we pop something off
4630             ;; the stack.
4631             (setq state (car stack)
4632                   gnus-tmp-level (car state)
4633                   tree-stack (cadr state)
4634                   thread (caddr state)
4635                   stack (cdr stack)
4636                   gnus-tmp-header (caar thread))))
4637
4638         (setq gnus-tmp-false-parent nil)
4639         (setq gnus-tmp-root-expunged nil)
4640         (setq thread-end nil)
4641
4642         (if (stringp gnus-tmp-header)
4643             ;; The header is a dummy root.
4644             (cond
4645              ((eq gnus-summary-make-false-root 'adopt)
4646               ;; We let the first article adopt the rest.
4647               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4648                                                (cddar thread)))
4649               (setq gnus-tmp-gathered
4650                     (nconc (mapcar
4651                             (lambda (h) (mail-header-number (car h)))
4652                             (cddar thread))
4653                            gnus-tmp-gathered))
4654               (setq thread (cons (list (caar thread)
4655                                        (cadar thread))
4656                                  (cdr thread)))
4657               (setq gnus-tmp-level -1
4658                     gnus-tmp-false-parent t))
4659              ((eq gnus-summary-make-false-root 'empty)
4660               ;; We print adopted articles with empty subject fields.
4661               (setq gnus-tmp-gathered
4662                     (nconc (mapcar
4663                             (lambda (h) (mail-header-number (car h)))
4664                             (cddar thread))
4665                            gnus-tmp-gathered))
4666               (setq gnus-tmp-level -1))
4667              ((eq gnus-summary-make-false-root 'dummy)
4668               ;; We remember that we probably want to output a dummy
4669               ;; root.
4670               (setq gnus-tmp-dummy-line gnus-tmp-header)
4671               (setq gnus-tmp-prev-subject gnus-tmp-header))
4672              (t
4673               ;; We do not make a root for the gathered
4674               ;; sub-threads at all.
4675               (setq gnus-tmp-level -1)))
4676
4677           (setq number (mail-header-number gnus-tmp-header)
4678                 subject (mail-header-subject gnus-tmp-header)
4679                 simp-subject (gnus-simplify-subject-fully subject))
4680
4681           (cond
4682            ;; If the thread has changed subject, we might want to make
4683            ;; this subthread into a root.
4684            ((and (null gnus-thread-ignore-subject)
4685                  (not (zerop gnus-tmp-level))
4686                  gnus-tmp-prev-subject
4687                  (not (string= gnus-tmp-prev-subject simp-subject)))
4688             (setq new-roots (nconc new-roots (list (car thread)))
4689                   thread-end t
4690                   gnus-tmp-header nil))
4691            ;; If the article lies outside the current limit,
4692            ;; then we do not display it.
4693            ((not (memq number gnus-newsgroup-limit))
4694             (setq gnus-tmp-gathered
4695                   (nconc (mapcar
4696                           (lambda (h) (mail-header-number (car h)))
4697                           (cdar thread))
4698                          gnus-tmp-gathered))
4699             (setq gnus-tmp-new-adopts (if (cdar thread)
4700                                           (append gnus-tmp-new-adopts
4701                                                   (cdar thread))
4702                                         gnus-tmp-new-adopts)
4703                   thread-end t
4704                   gnus-tmp-header nil)
4705             (when (zerop gnus-tmp-level)
4706               (setq gnus-tmp-root-expunged t)))
4707            ;; Perhaps this article is to be marked as read?
4708            ((and gnus-summary-mark-below
4709                  (< (or (cdr (assq number gnus-newsgroup-scored))
4710                         default-score)
4711                     gnus-summary-mark-below)
4712                  ;; Don't touch sparse articles.
4713                  (not (gnus-summary-article-sparse-p number))
4714                  (not (gnus-summary-article-ancient-p number)))
4715             (setq gnus-newsgroup-unreads
4716                   (delq number gnus-newsgroup-unreads))
4717             (if gnus-newsgroup-auto-expire
4718                 (setq gnus-newsgroup-expirable
4719                       (gnus-add-to-sorted-list
4720                        gnus-newsgroup-expirable number))
4721               (push (cons number gnus-low-score-mark)
4722                     gnus-newsgroup-reads))))
4723
4724           (when gnus-tmp-header
4725             ;; We may have an old dummy line to output before this
4726             ;; article.
4727             (when (and gnus-tmp-dummy-line
4728                        (gnus-subject-equal
4729                         gnus-tmp-dummy-line
4730                         (mail-header-subject gnus-tmp-header)))
4731               (gnus-summary-insert-dummy-line
4732                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4733               (setq gnus-tmp-dummy-line nil))
4734
4735             ;; Compute the mark.
4736             (setq gnus-tmp-unread (gnus-article-mark number))
4737
4738             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4739                                   gnus-tmp-header gnus-tmp-level)
4740                   gnus-newsgroup-data)
4741
4742             ;; Actually insert the line.
4743             (setq
4744              gnus-tmp-subject-or-nil
4745              (cond
4746               ((and gnus-thread-ignore-subject
4747                     gnus-tmp-prev-subject
4748                     (not (string= gnus-tmp-prev-subject simp-subject)))
4749                subject)
4750               ((zerop gnus-tmp-level)
4751                (if (and (eq gnus-summary-make-false-root 'empty)
4752                         (memq number gnus-tmp-gathered)
4753                         gnus-tmp-prev-subject
4754                         (string= gnus-tmp-prev-subject simp-subject))
4755                    gnus-summary-same-subject
4756                  subject))
4757               (t gnus-summary-same-subject)))
4758             (if (and (eq gnus-summary-make-false-root 'adopt)
4759                      (= gnus-tmp-level 1)
4760                      (memq number gnus-tmp-gathered))
4761                 (setq gnus-tmp-opening-bracket ?\<
4762                       gnus-tmp-closing-bracket ?\>)
4763               (setq gnus-tmp-opening-bracket ?\[
4764                     gnus-tmp-closing-bracket ?\]))
4765             (setq
4766              gnus-tmp-indentation
4767              (aref gnus-thread-indent-array gnus-tmp-level)
4768              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4769              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4770                                 gnus-summary-default-score 0)
4771              gnus-tmp-score-char
4772              (if (or (null gnus-summary-default-score)
4773                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4774                          gnus-summary-zcore-fuzz))
4775                  ?                      ;Whitespace
4776                (if (< gnus-tmp-score gnus-summary-default-score)
4777                    gnus-score-below-mark gnus-score-over-mark))
4778              gnus-tmp-replied
4779              (cond ((memq number gnus-newsgroup-processable)
4780                     gnus-process-mark)
4781                    ((memq number gnus-newsgroup-cached)
4782                     gnus-cached-mark)
4783                    ((memq number gnus-newsgroup-replied)
4784                     gnus-replied-mark)
4785                    ((memq number gnus-newsgroup-forwarded)
4786                     gnus-forwarded-mark)
4787                    ((memq number gnus-newsgroup-saved)
4788                     gnus-saved-mark)
4789                    ((memq number gnus-newsgroup-recent)
4790                     gnus-recent-mark)
4791                    ((memq number gnus-newsgroup-unseen)
4792                     gnus-unseen-mark)
4793                    (t gnus-no-mark))
4794              gnus-tmp-downloaded
4795              (cond ((memq number gnus-newsgroup-undownloaded) 
4796                     gnus-undownloaded-mark)
4797                    (gnus-newsgroup-agentized
4798                     gnus-downloaded-mark)
4799                    (t
4800                     gnus-no-mark))
4801              gnus-tmp-from (mail-header-from gnus-tmp-header)
4802              gnus-tmp-name
4803              (cond
4804               ((string-match "<[^>]+> *$" gnus-tmp-from)
4805                (setq beg-match (match-beginning 0))
4806                (or (and (string-match "^\".+\"" gnus-tmp-from)
4807                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4808                    (substring gnus-tmp-from 0 beg-match)))
4809               ((string-match "(.+)" gnus-tmp-from)
4810                (substring gnus-tmp-from
4811                           (1+ (match-beginning 0)) (1- (match-end 0))))
4812               (t gnus-tmp-from))
4813              gnus-tmp-thread-tree-header-string
4814              (cond
4815               ((not gnus-show-threads) "")
4816               ((zerop gnus-tmp-level)
4817                (if (cdar thread)
4818                    (or gnus-sum-thread-tree-root subject)
4819                  (or gnus-sum-thread-tree-single-indent subject)))
4820               (t
4821                (concat (apply 'concat
4822                               (mapcar (lambda (item)
4823                                         (if (= item 1)
4824                                             gnus-sum-thread-tree-vertical
4825                                           gnus-sum-thread-tree-indent))
4826                                       (cdr (reverse tree-stack))))
4827                        (if (nth 1 thread)
4828                            gnus-sum-thread-tree-leaf-with-other
4829                          gnus-sum-thread-tree-single-leaf)))))
4830             (when (string= gnus-tmp-name "")
4831               (setq gnus-tmp-name gnus-tmp-from))
4832             (unless (numberp gnus-tmp-lines)
4833               (setq gnus-tmp-lines -1))
4834             (if (= gnus-tmp-lines -1)
4835                 (setq gnus-tmp-lines "?")
4836               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4837               (gnus-put-text-property
4838              (point)
4839              (progn (eval gnus-summary-line-format-spec) (point))
4840                'gnus-number number)
4841             (when gnus-visual-p
4842               (forward-line -1)
4843               (gnus-run-hooks 'gnus-summary-update-hook)
4844               (forward-line 1))
4845
4846             (setq gnus-tmp-prev-subject simp-subject)))
4847
4848         (when (nth 1 thread)
4849           (push (list (max 0 gnus-tmp-level)
4850                       (copy-list tree-stack)
4851                       (nthcdr 1 thread))
4852                 stack))
4853         (push (if (nth 1 thread) 1 0) tree-stack)
4854         (incf gnus-tmp-level)
4855         (setq threads (if thread-end nil (cdar thread)))
4856         (if gnus-summary-display-while-building
4857             (if building-count
4858                 (progn
4859                   ;; use a set frequency
4860                   (setq building-line-count (1- building-line-count))
4861                   (when (= building-line-count 0)
4862                     (sit-for 0)
4863                     (setq building-line-count
4864                           gnus-summary-display-while-building)))
4865               ;; always
4866               (sit-for 0)))
4867         (unless threads
4868           (setq gnus-tmp-level 0)))))
4869   (gnus-message 7 "Generating summary...done"))
4870
4871 (defun gnus-summary-prepare-unthreaded (headers)
4872   "Generate an unthreaded summary buffer based on HEADERS."
4873   (let (header number mark)
4874
4875     (beginning-of-line)
4876
4877     (while headers
4878       ;; We may have to root out some bad articles...
4879       (when (memq (setq number (mail-header-number
4880                                 (setq header (pop headers))))
4881                   gnus-newsgroup-limit)
4882         ;; Mark article as read when it has a low score.
4883         (when (and gnus-summary-mark-below
4884                    (< (or (cdr (assq number gnus-newsgroup-scored))
4885                           gnus-summary-default-score 0)
4886                       gnus-summary-mark-below)
4887                    (not (gnus-summary-article-ancient-p number)))
4888           (setq gnus-newsgroup-unreads
4889                 (delq number gnus-newsgroup-unreads))
4890           (if gnus-newsgroup-auto-expire
4891               (push number gnus-newsgroup-expirable)
4892             (push (cons number gnus-low-score-mark)
4893                   gnus-newsgroup-reads)))
4894
4895         (setq mark (gnus-article-mark number))
4896         (push (gnus-data-make number mark (1+ (point)) header 0)
4897               gnus-newsgroup-data)
4898         (gnus-summary-insert-line
4899          header 0 number
4900          (memq number gnus-newsgroup-undownloaded)
4901          mark (memq number gnus-newsgroup-replied)
4902          (memq number gnus-newsgroup-expirable)
4903          (mail-header-subject header) nil
4904          (cdr (assq number gnus-newsgroup-scored))
4905          (memq number gnus-newsgroup-processable))))))
4906
4907 (defun gnus-summary-remove-list-identifiers ()
4908   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4909   (let ((regexp (if (consp gnus-list-identifiers)
4910                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4911                   gnus-list-identifiers))
4912         changed subject)
4913     (when regexp
4914       (dolist (header gnus-newsgroup-headers)
4915         (setq subject (mail-header-subject header)
4916               changed nil)
4917         (while (string-match
4918                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4919                 subject)
4920           (setq subject
4921                 (concat (substring subject 0 (match-beginning 2))
4922                         (substring subject (match-end 0)))
4923                 changed t))
4924         (when (and changed
4925                    (string-match
4926                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4927           (setq subject
4928                 (concat (substring subject 0 (match-beginning 1))
4929                         (substring subject (match-end 1)))))
4930         (when changed
4931           (mail-header-set-subject header subject))))))
4932
4933 (defun gnus-fetch-headers (articles)
4934   "Fetch headers of ARTICLES."
4935   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4936     (gnus-message 5 "Fetching headers for %s..." name)
4937     (prog1
4938         (if (eq 'nov
4939                 (setq gnus-headers-retrieved-by
4940                       (gnus-retrieve-headers
4941                        articles gnus-newsgroup-name
4942                        ;; We might want to fetch old headers, but
4943                        ;; not if there is only 1 article.
4944                        (and (or (and
4945                                  (not (eq gnus-fetch-old-headers 'some))
4946                                  (not (numberp gnus-fetch-old-headers)))
4947                                 (> (length articles) 1))
4948                             gnus-fetch-old-headers))))
4949             (gnus-get-newsgroup-headers-xover
4950              articles nil nil gnus-newsgroup-name t)
4951           (gnus-get-newsgroup-headers))
4952       (gnus-message 5 "Fetching headers for %s...done" name))))
4953
4954 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4955   "Select newsgroup GROUP.
4956 If READ-ALL is non-nil, all articles in the group are selected.
4957 If SELECT-ARTICLES, only select those articles from GROUP."
4958   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4959          ;;!!! Dirty hack; should be removed.
4960          (gnus-summary-ignore-duplicates
4961           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4962               t
4963             gnus-summary-ignore-duplicates))
4964          (info (nth 2 entry))
4965          articles fetched-articles cached)
4966
4967     (unless (gnus-check-server
4968              (set (make-local-variable 'gnus-current-select-method)
4969                   (gnus-find-method-for-group group)))
4970       (error "Couldn't open server"))
4971
4972     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4973         (gnus-activate-group group)     ; Or we can activate it...
4974         (progn                          ; Or we bug out.
4975           (when (equal major-mode 'gnus-summary-mode)
4976             (gnus-kill-buffer (current-buffer)))
4977           (error "Couldn't activate group %s: %s"
4978                  group (gnus-status-message group))))
4979
4980     (unless (gnus-request-group group t)
4981       (when (equal major-mode 'gnus-summary-mode)
4982         (gnus-kill-buffer (current-buffer)))
4983       (error "Couldn't request group %s: %s"
4984              group (gnus-status-message group)))
4985
4986     (setq gnus-newsgroup-name group
4987           gnus-newsgroup-unselected nil
4988           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4989
4990     (let ((display (gnus-group-find-parameter group 'display)))
4991       (setq gnus-newsgroup-display
4992             (cond
4993              ((not (zerop (or (car-safe read-all) 0)))
4994               ;; The user entered the group with C-u SPC/RET, let's show
4995               ;; all articles.
4996               'gnus-not-ignore)
4997              ((eq display 'all)
4998               'gnus-not-ignore)
4999              ((arrayp display)
5000               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5001              ((numberp display)
5002               ;; The following is probably the "correct" solution, but
5003               ;; it makes Gnus fetch all headers and then limit the
5004               ;; articles (which is slow), so instead we hack the
5005               ;; select-articles parameter instead. -- Simon Josefsson
5006               ;; <jas@kth.se>
5007               ;;
5008               ;; (gnus-byte-compile
5009               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5010               ;;                         display)))))
5011               (setq select-articles
5012                     (gnus-uncompress-range
5013                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5014                              (if (> tmp 0)
5015                                  tmp
5016                                1))
5017                            (cdr (gnus-active group)))))
5018               nil)
5019              (t
5020               nil))))
5021
5022     (gnus-summary-setup-default-charset)
5023
5024     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5025     (when (gnus-virtual-group-p group)
5026       (setq cached gnus-newsgroup-cached))
5027
5028     (setq gnus-newsgroup-unreads
5029           (gnus-sorted-ndifference
5030            (gnus-sorted-ndifference gnus-newsgroup-unreads
5031                                     gnus-newsgroup-marked)
5032            gnus-newsgroup-dormant))
5033
5034     (setq gnus-newsgroup-processable nil)
5035
5036     (gnus-update-read-articles group gnus-newsgroup-unreads)
5037
5038     ;; Adjust and set lists of article marks.
5039     (when info
5040       (gnus-adjust-marked-articles info))
5041     (if (setq articles select-articles)
5042         (setq gnus-newsgroup-unselected
5043               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5044       (setq articles (gnus-articles-to-read group read-all)))
5045
5046     (cond
5047      ((null articles)
5048       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5049       'quit)
5050      ((eq articles 0) nil)
5051      (t
5052       ;; Init the dependencies hash table.
5053       (setq gnus-newsgroup-dependencies
5054             (gnus-make-hashtable (length articles)))
5055       (gnus-set-global-variables)
5056       ;; Retrieve the headers and read them in.
5057
5058       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5059
5060       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5061       (when cached
5062         (setq gnus-newsgroup-cached cached))
5063
5064       ;; Suppress duplicates?
5065       (when gnus-suppress-duplicates
5066         (gnus-dup-suppress-articles))
5067
5068       ;; Set the initial limit.
5069       (setq gnus-newsgroup-limit (copy-sequence articles))
5070       ;; Remove canceled articles from the list of unread articles.
5071       (setq fetched-articles
5072             (mapcar (lambda (headers) (mail-header-number headers))
5073                     gnus-newsgroup-headers))
5074       (setq gnus-newsgroup-articles fetched-articles)
5075       (setq gnus-newsgroup-unreads
5076             (gnus-sorted-nintersection
5077              gnus-newsgroup-unreads fetched-articles))
5078       (gnus-compute-unseen-list)
5079
5080       ;; Removed marked articles that do not exist.
5081       (gnus-update-missing-marks
5082        (gnus-sorted-difference articles fetched-articles))
5083       ;; We might want to build some more threads first.
5084       (when (and gnus-fetch-old-headers
5085                  (eq gnus-headers-retrieved-by 'nov))
5086         (if (eq gnus-fetch-old-headers 'invisible)
5087             (gnus-build-all-threads)
5088           (gnus-build-old-threads)))
5089       ;; Let the Gnus agent mark articles as read.
5090       (when gnus-agent
5091         (gnus-agent-get-undownloaded-list))
5092       ;; Remove list identifiers from subject
5093       (when gnus-list-identifiers
5094         (gnus-summary-remove-list-identifiers))
5095       ;; Check whether auto-expire is to be done in this group.
5096       (setq gnus-newsgroup-auto-expire
5097             (gnus-group-auto-expirable-p group))
5098       ;; Set up the article buffer now, if necessary.
5099       (unless gnus-single-article-buffer
5100         (gnus-article-setup-buffer))
5101       ;; First and last article in this newsgroup.
5102       (when gnus-newsgroup-headers
5103         (setq gnus-newsgroup-begin
5104               (mail-header-number (car gnus-newsgroup-headers))
5105               gnus-newsgroup-end
5106               (mail-header-number
5107                (gnus-last-element gnus-newsgroup-headers))))
5108       ;; GROUP is successfully selected.
5109       (or gnus-newsgroup-headers t)))))
5110
5111 (defun gnus-compute-unseen-list ()
5112   ;; The `seen' marks are treated specially.
5113   (if (not gnus-newsgroup-seen)
5114       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5115     (setq gnus-newsgroup-unseen
5116           (gnus-inverse-list-range-intersection
5117            gnus-newsgroup-articles gnus-newsgroup-seen))))
5118
5119 (defun gnus-summary-display-make-predicate (display)
5120   (require 'gnus-agent)
5121   (when (= (length display) 1)
5122     (setq display (car display)))
5123   (unless gnus-summary-display-cache
5124     (dolist (elem (append '((unread . unread)
5125                             (read . read)
5126                             (unseen . unseen))
5127                           gnus-article-mark-lists))
5128       (push (cons (cdr elem)
5129                   (gnus-byte-compile
5130                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5131             gnus-summary-display-cache)))
5132   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5133         (gnus-category-predicate-cache gnus-summary-display-cache))
5134     (gnus-get-predicate display)))
5135
5136 ;; Uses the dynamically bound `number' variable.
5137 (eval-when-compile
5138   (defvar number))
5139 (defun gnus-article-marked-p (type &optional article)
5140   (let ((article (or article number)))
5141     (cond
5142      ((eq type 'tick)
5143       (memq article gnus-newsgroup-marked))
5144      ((eq type 'spam)
5145       (memq article gnus-newsgroup-spam-marked))
5146      ((eq type 'unsend)
5147       (memq article gnus-newsgroup-unsendable))
5148      ((eq type 'undownload)
5149       (memq article gnus-newsgroup-undownloaded))
5150      ((eq type 'download)
5151       (memq article gnus-newsgroup-downloadable))
5152      ((eq type 'unread)
5153       (memq article gnus-newsgroup-unreads))
5154      ((eq type 'read)
5155       (memq article gnus-newsgroup-reads))
5156      ((eq type 'dormant)
5157       (memq article gnus-newsgroup-dormant) )
5158      ((eq type 'expire)
5159       (memq article gnus-newsgroup-expirable))
5160      ((eq type 'reply)
5161       (memq article gnus-newsgroup-replied))
5162      ((eq type 'killed)
5163       (memq article gnus-newsgroup-killed))
5164      ((eq type 'bookmark)
5165       (assq article gnus-newsgroup-bookmarks))
5166      ((eq type 'score)
5167       (assq article gnus-newsgroup-scored))
5168      ((eq type 'save)
5169       (memq article gnus-newsgroup-saved))
5170      ((eq type 'cache)
5171       (memq article gnus-newsgroup-cached))
5172      ((eq type 'forward)
5173       (memq article gnus-newsgroup-forwarded))
5174      ((eq type 'seen)
5175       (not (memq article gnus-newsgroup-unseen)))
5176      ((eq type 'recent)
5177       (memq article gnus-newsgroup-recent))
5178      (t t))))
5179
5180 (defun gnus-articles-to-read (group &optional read-all)
5181   "Find out what articles the user wants to read."
5182   (let* ((display (gnus-group-find-parameter group 'display))
5183          (articles
5184           ;; Select all articles if `read-all' is non-nil, or if there
5185           ;; are no unread articles.
5186           (if (or read-all
5187                   (and (zerop (length gnus-newsgroup-marked))
5188                        (zerop (length gnus-newsgroup-unreads)))
5189                   ;; Fetch all if the predicate is non-nil.
5190                   gnus-newsgroup-display)
5191               ;; We want to select the headers for all the articles in
5192               ;; the group, so we select either all the active
5193               ;; articles in the group, or (if that's nil), the
5194               ;; articles in the cache.
5195               (or
5196                (gnus-uncompress-range (gnus-active group))
5197                (gnus-cache-articles-in-group group))
5198             ;; Select only the "normal" subset of articles.
5199             (gnus-sorted-nunion
5200              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5201              gnus-newsgroup-unreads)))
5202          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5203          (scored (length scored-list))
5204          (number (length articles))
5205          (marked (+ (length gnus-newsgroup-marked)
5206                     (length gnus-newsgroup-dormant)))
5207          (select
5208           (cond
5209            ((numberp read-all)
5210             read-all)
5211            ((numberp gnus-newsgroup-display)
5212             gnus-newsgroup-display)
5213            (t
5214             (condition-case ()
5215                 (cond
5216                  ((and (or (<= scored marked) (= scored number))
5217                        (numberp gnus-large-newsgroup)
5218                        (> number gnus-large-newsgroup))
5219                   (let* ((cursor-in-echo-area nil)
5220                          (initial (gnus-parameter-large-newsgroup-initial
5221                                    gnus-newsgroup-name))
5222                          (input
5223                           (read-string
5224                            (format
5225                             "How many articles from %s (%s %d): "
5226                             (gnus-limit-string
5227                              (gnus-group-decoded-name gnus-newsgroup-name)
5228                              35)
5229                             (if initial "max" "default")
5230                             number)
5231                            (if initial
5232                                (cons (number-to-string initial)
5233                                      0)))))
5234                     (if (string-match "^[ \t]*$" input) number input)))
5235                  ((and (> scored marked) (< scored number)
5236                        (> (- scored number) 20))
5237                   (let ((input
5238                          (read-string
5239                           (format "%s %s (%d scored, %d total): "
5240                                   "How many articles from"
5241                                   (gnus-group-decoded-name group)
5242                                   scored number))))
5243                     (if (string-match "^[ \t]*$" input)
5244                         number input)))
5245                  (t number))
5246               (quit
5247                (message "Quit getting the articles to read")
5248                nil))))))
5249     (setq select (if (stringp select) (string-to-number select) select))
5250     (if (or (null select) (zerop select))
5251         select
5252       (if (and (not (zerop scored)) (<= (abs select) scored))
5253           (progn
5254             (setq articles (sort scored-list '<))
5255             (setq number (length articles)))
5256         (setq articles (copy-sequence articles)))
5257
5258       (when (< (abs select) number)
5259         (if (< select 0)
5260             ;; Select the N oldest articles.
5261             (setcdr (nthcdr (1- (abs select)) articles) nil)
5262           ;; Select the N most recent articles.
5263           (setq articles (nthcdr (- number select) articles))))
5264       (setq gnus-newsgroup-unselected
5265             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5266       (when gnus-alter-articles-to-read-function
5267         (setq articles
5268               (sort
5269                (funcall gnus-alter-articles-to-read-function
5270                         gnus-newsgroup-name articles)
5271                '<)))
5272       articles)))
5273
5274 (defun gnus-killed-articles (killed articles)
5275   (let (out)
5276     (while articles
5277       (when (inline (gnus-member-of-range (car articles) killed))
5278         (push (car articles) out))
5279       (setq articles (cdr articles)))
5280     out))
5281
5282 (defun gnus-uncompress-marks (marks)
5283   "Uncompress the mark ranges in MARKS."
5284   (let ((uncompressed '(score bookmark))
5285         out)
5286     (while marks
5287       (if (memq (caar marks) uncompressed)
5288           (push (car marks) out)
5289         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5290       (setq marks (cdr marks)))
5291     out))
5292
5293 (defun gnus-article-mark-to-type (mark)
5294   "Return the type of MARK."
5295   (or (cadr (assq mark gnus-article-special-mark-lists))
5296       'list))
5297
5298 (defun gnus-article-unpropagatable-p (mark)
5299   "Return whether MARK should be propagated to backend."
5300   (memq mark gnus-article-unpropagated-mark-lists))
5301
5302 (defun gnus-adjust-marked-articles (info)
5303   "Set all article lists and remove all marks that are no longer valid."
5304   (let* ((marked-lists (gnus-info-marks info))
5305          (active (gnus-active (gnus-info-group info)))
5306          (min (car active))
5307          (max (cdr active))
5308          (types gnus-article-mark-lists)
5309          marks var articles article mark mark-type)
5310
5311     (dolist (marks marked-lists)
5312       (setq mark (car marks)
5313             mark-type (gnus-article-mark-to-type mark)
5314             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5315
5316       ;; We set the variable according to the type of the marks list,
5317       ;; and then adjust the marks to a subset of the active articles.
5318       (cond
5319        ;; Adjust "simple" lists.
5320        ((eq mark-type 'list)
5321         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5322         (when (memq mark '(tick dormant expire reply save))
5323           (while articles
5324             (when (or (< (setq article (pop articles)) min) (> article max))
5325               (set var (delq article (symbol-value var)))))))
5326        ;; Adjust assocs.
5327        ((eq mark-type 'tuple)
5328         (set var (setq articles (cdr marks)))
5329         (when (not (listp (cdr (symbol-value var))))
5330           (set var (list (symbol-value var))))
5331         (when (not (listp (cdr articles)))
5332           (setq articles (list articles)))
5333         (while articles
5334           (when (or (not (consp (setq article (pop articles))))
5335                     (< (car article) min)
5336                     (> (car article) max))
5337             (set var (delq article (symbol-value var))))))
5338        ;; Adjust ranges (sloppily).
5339        ((eq mark-type 'range)
5340         (cond
5341          ((eq mark 'seen)
5342           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5343           ;; It should be (seen (NUM1 . NUM2)).
5344           (when (numberp (cddr marks))
5345             (setcdr marks (list (cdr marks))))
5346           (setq articles (cdr marks))
5347           (while (and articles
5348                       (or (and (consp (car articles))
5349                                (> min (cdar articles)))
5350                           (and (numberp (car articles))
5351                                (> min (car articles)))))
5352             (pop articles))
5353           (set var articles))))))))
5354
5355 (defun gnus-update-missing-marks (missing)
5356   "Go through the list of MISSING articles and remove them from the mark lists."
5357   (when missing
5358     (let (var m)
5359       ;; Go through all types.
5360       (dolist (elem gnus-article-mark-lists)
5361         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5362           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5363           (when (symbol-value var)
5364             ;; This list has articles.  So we delete all missing
5365             ;; articles from it.
5366             (setq m missing)
5367             (while m
5368               (set var (delq (pop m) (symbol-value var))))))))))
5369
5370 (defun gnus-update-marks ()
5371   "Enter the various lists of marked articles into the newsgroup info list."
5372   (let ((types gnus-article-mark-lists)
5373         (info (gnus-get-info gnus-newsgroup-name))
5374         type list newmarked symbol delta-marks)
5375     (when info
5376       ;; Add all marks lists to the list of marks lists.
5377       (while (setq type (pop types))
5378         (setq list (symbol-value
5379                     (setq symbol
5380                           (intern (format "gnus-newsgroup-%s" (car type))))))
5381
5382         (when list
5383           ;; Get rid of the entries of the articles that have the
5384           ;; default score.
5385           (when (and (eq (cdr type) 'score)
5386                      gnus-save-score
5387                      list)
5388             (let* ((arts list)
5389                    (prev (cons nil list))
5390                    (all prev))
5391               (while arts
5392                 (if (or (not (consp (car arts)))
5393                         (= (cdar arts) gnus-summary-default-score))
5394                     (setcdr prev (cdr arts))
5395                   (setq prev arts))
5396                 (setq arts (cdr arts)))
5397               (setq list (cdr all)))))
5398
5399         (when (eq (cdr type) 'seen)
5400           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5401
5402         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5403           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5404
5405         (when (and (gnus-check-backend-function
5406                     'request-set-mark gnus-newsgroup-name)
5407                    (not (gnus-article-unpropagatable-p (cdr type))))
5408           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5409                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5410                  (add (gnus-remove-from-range
5411                        (gnus-copy-sequence list) old)))
5412             (when add
5413               (push (list add 'add (list (cdr type))) delta-marks))
5414             (when del
5415               (push (list del 'del (list (cdr type))) delta-marks))))
5416
5417         (when list
5418           (push (cons (cdr type) list) newmarked)))
5419
5420       (when delta-marks
5421         (unless (gnus-check-group gnus-newsgroup-name)
5422           (error "Can't open server for %s" gnus-newsgroup-name))
5423         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5424
5425       ;; Enter these new marks into the info of the group.
5426       (if (nthcdr 3 info)
5427           (setcar (nthcdr 3 info) newmarked)
5428         ;; Add the marks lists to the end of the info.
5429         (when newmarked
5430           (setcdr (nthcdr 2 info) (list newmarked))))
5431
5432       ;; Cut off the end of the info if there's nothing else there.
5433       (let ((i 5))
5434         (while (and (> i 2)
5435                     (not (nth i info)))
5436           (when (nthcdr (decf i) info)
5437             (setcdr (nthcdr i info) nil)))))))
5438
5439 (defun gnus-set-mode-line (where)
5440   "Set the mode line of the article or summary buffers.
5441 If WHERE is `summary', the summary mode line format will be used."
5442   ;; Is this mode line one we keep updated?
5443   (when (and (memq where gnus-updated-mode-lines)
5444              (symbol-value
5445               (intern (format "gnus-%s-mode-line-format-spec" where))))
5446     (let (mode-string)
5447       (save-excursion
5448         ;; We evaluate this in the summary buffer since these
5449         ;; variables are buffer-local to that buffer.
5450         (set-buffer gnus-summary-buffer)
5451        ;; We bind all these variables that are used in the `eval' form
5452         ;; below.
5453         (let* ((mformat (symbol-value
5454                          (intern
5455                           (format "gnus-%s-mode-line-format-spec" where))))
5456                (gnus-tmp-group-name (gnus-group-decoded-name
5457                                      gnus-newsgroup-name))
5458                (gnus-tmp-article-number (or gnus-current-article 0))
5459                (gnus-tmp-unread gnus-newsgroup-unreads)
5460                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5461                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5462                (gnus-tmp-unread-and-unselected
5463                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5464                             (zerop gnus-tmp-unselected))
5465                        "")
5466                       ((zerop gnus-tmp-unselected)
5467                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5468                       (t (format "{%d(+%d) more}"
5469                                  gnus-tmp-unread-and-unticked
5470                                  gnus-tmp-unselected))))
5471                (gnus-tmp-subject
5472                 (if (and gnus-current-headers
5473                          (vectorp gnus-current-headers))
5474                     (gnus-mode-string-quote
5475                      (mail-header-subject gnus-current-headers))
5476                   ""))
5477                bufname-length max-len
5478                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5479           (setq mode-string (eval mformat))
5480           (setq bufname-length (if (string-match "%b" mode-string)
5481                                    (- (length
5482                                        (buffer-name
5483                                         (if (eq where 'summary)
5484                                             nil
5485                                           (get-buffer gnus-article-buffer))))
5486                                       2)
5487                                  0))
5488           (setq max-len (max 4 (if gnus-mode-non-string-length
5489                                    (- (window-width)
5490                                       gnus-mode-non-string-length
5491                                       bufname-length)
5492                                  (length mode-string))))
5493           ;; We might have to chop a bit of the string off...
5494           (when (> (length mode-string) max-len)
5495             (setq mode-string
5496                   (concat (truncate-string-to-width mode-string (- max-len 3))
5497                           "...")))
5498           ;; Pad the mode string a bit.
5499           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5500       ;; Update the mode line.
5501       (setq mode-line-buffer-identification
5502             (gnus-mode-line-buffer-identification (list mode-string)))
5503       (set-buffer-modified-p t))))
5504
5505 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5506   "Go through the HEADERS list and add all Xrefs to a hash table.
5507 The resulting hash table is returned, or nil if no Xrefs were found."
5508   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5509          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5510          (xref-hashtb (gnus-make-hashtable))
5511          start group entry number xrefs header)
5512     (while headers
5513       (setq header (pop headers))
5514       (when (and (setq xrefs (mail-header-xref header))
5515                  (not (memq (setq number (mail-header-number header))
5516                             unreads)))
5517         (setq start 0)
5518         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5519           (setq start (match-end 0))
5520           (setq group (if prefix
5521                           (concat prefix (substring xrefs (match-beginning 1)
5522                                                     (match-end 1)))
5523                         (substring xrefs (match-beginning 1) (match-end 1))))
5524           (setq number
5525                 (string-to-int (substring xrefs (match-beginning 2)
5526                                           (match-end 2))))
5527           (if (setq entry (gnus-gethash group xref-hashtb))
5528               (setcdr entry (cons number (cdr entry)))
5529             (gnus-sethash group (cons number nil) xref-hashtb)))))
5530     (and start xref-hashtb)))
5531
5532 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5533   "Look through all the headers and mark the Xrefs as read."
5534   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5535         name entry info xref-hashtb idlist method nth4)
5536     (save-excursion
5537       (set-buffer gnus-group-buffer)
5538       (when (setq xref-hashtb
5539                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5540         (mapatoms
5541          (lambda (group)
5542            (unless (string= from-newsgroup (setq name (symbol-name group)))
5543              (setq idlist (symbol-value group))
5544              ;; Dead groups are not updated.
5545              (and (prog1
5546                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5547                             info (nth 2 entry))
5548                     (when (stringp (setq nth4 (gnus-info-method info)))
5549                       (setq nth4 (gnus-server-to-method nth4))))
5550                   ;; Only do the xrefs if the group has the same
5551                   ;; select method as the group we have just read.
5552                   (or (gnus-methods-equal-p
5553                        nth4 (gnus-find-method-for-group from-newsgroup))
5554                       virtual
5555                       (equal nth4 (setq method (gnus-find-method-for-group
5556                                                 from-newsgroup)))
5557                       (and (equal (car nth4) (car method))
5558                            (equal (nth 1 nth4) (nth 1 method))))
5559                   gnus-use-cross-reference
5560                   (or (not (eq gnus-use-cross-reference t))
5561                       virtual
5562                       ;; Only do cross-references on subscribed
5563                       ;; groups, if that is what is wanted.
5564                       (<= (gnus-info-level info) gnus-level-subscribed))
5565                   (gnus-group-make-articles-read name idlist))))
5566          xref-hashtb)))))
5567
5568 (defun gnus-compute-read-articles (group articles)
5569   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5570          (info (nth 2 entry))
5571          (active (gnus-active group))
5572          ninfo)
5573     (when entry
5574       ;; First peel off all invalid article numbers.
5575       (when active
5576         (let ((ids articles)
5577               id first)
5578           (while (setq id (pop ids))
5579             (when (and first (> id (cdr active)))
5580               ;; We'll end up in this situation in one particular
5581               ;; obscure situation.  If you re-scan a group and get
5582               ;; a new article that is cross-posted to a different
5583               ;; group that has not been re-scanned, you might get
5584               ;; crossposted article that has a higher number than
5585               ;; Gnus believes possible.  So we re-activate this
5586               ;; group as well.  This might mean doing the
5587               ;; crossposting thingy will *increase* the number
5588               ;; of articles in some groups.  Tsk, tsk.
5589               (setq active (or (gnus-activate-group group) active)))
5590             (when (or (> id (cdr active))
5591                       (< id (car active)))
5592               (setq articles (delq id articles))))))
5593       ;; If the read list is nil, we init it.
5594       (if (and active
5595                (null (gnus-info-read info))
5596                (> (car active) 1))
5597           (setq ninfo (cons 1 (1- (car active))))
5598         (setq ninfo (gnus-info-read info)))
5599       ;; Then we add the read articles to the range.
5600       (gnus-add-to-range
5601        ninfo (setq articles (sort articles '<))))))
5602
5603 (defun gnus-group-make-articles-read (group articles)
5604   "Update the info of GROUP to say that ARTICLES are read."
5605   (let* ((num 0)
5606          (entry (gnus-gethash group gnus-newsrc-hashtb))
5607          (info (nth 2 entry))
5608          (active (gnus-active group))
5609          range)
5610     (when entry
5611       (setq range (gnus-compute-read-articles group articles))
5612       (save-excursion
5613         (set-buffer gnus-group-buffer)
5614         (gnus-undo-register
5615           `(progn
5616              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5617              (gnus-info-set-read ',info ',(gnus-info-read info))
5618              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5619              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5620              (gnus-group-update-group ,group t))))
5621       ;; Add the read articles to the range.
5622       (gnus-info-set-read info range)
5623       (gnus-request-set-mark group (list (list range 'add '(read))))
5624       ;; Then we have to re-compute how many unread
5625       ;; articles there are in this group.
5626       (when active
5627         (cond
5628          ((not range)
5629           (setq num (- (1+ (cdr active)) (car active))))
5630          ((not (listp (cdr range)))
5631           (setq num (- (cdr active) (- (1+ (cdr range))
5632                                        (car range)))))
5633          (t
5634           (while range
5635             (if (numberp (car range))
5636                 (setq num (1+ num))
5637               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5638             (setq range (cdr range)))
5639           (setq num (- (cdr active) num))))
5640         ;; Update the number of unread articles.
5641         (setcar entry num)
5642         ;; Update the group buffer.
5643         (unless (gnus-ephemeral-group-p group)
5644           (gnus-group-update-group group t))))))
5645
5646 (defvar gnus-newsgroup-none-id 0)
5647
5648 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5649   (let ((cur nntp-server-buffer)
5650         (dependencies
5651          (or dependencies
5652              (save-excursion (set-buffer gnus-summary-buffer)
5653                              gnus-newsgroup-dependencies)))
5654         headers id end ref
5655         (mail-parse-charset gnus-newsgroup-charset)
5656         (mail-parse-ignored-charsets
5657          (save-excursion (condition-case nil
5658                              (set-buffer gnus-summary-buffer)
5659                            (error))
5660                          gnus-newsgroup-ignored-charsets)))
5661     (save-excursion
5662       (set-buffer nntp-server-buffer)
5663       ;; Translate all TAB characters into SPACE characters.
5664       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5665       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5666       (gnus-run-hooks 'gnus-parse-headers-hook)
5667       (let ((case-fold-search t)
5668             in-reply-to header p lines chars)
5669         (goto-char (point-min))
5670         ;; Search to the beginning of the next header.  Error messages
5671         ;; do not begin with 2 or 3.
5672         (while (re-search-forward "^[23][0-9]+ " nil t)
5673           (setq id nil
5674                 ref nil)
5675           ;; This implementation of this function, with nine
5676           ;; search-forwards instead of the one re-search-forward and
5677           ;; a case (which basically was the old function) is actually
5678           ;; about twice as fast, even though it looks messier.  You
5679           ;; can't have everything, I guess.  Speed and elegance
5680           ;; doesn't always go hand in hand.
5681           (setq
5682            header
5683            (vector
5684             ;; Number.
5685             (prog1
5686                 (read cur)
5687               (end-of-line)
5688               (setq p (point))
5689               (narrow-to-region (point)
5690                                 (or (and (search-forward "\n.\n" nil t)
5691                                          (- (point) 2))
5692                                     (point))))
5693             ;; Subject.
5694             (progn
5695               (goto-char p)
5696               (if (search-forward "\nsubject:" nil t)
5697                   (funcall gnus-decode-encoded-word-function
5698                            (nnheader-header-value))
5699                 "(none)"))
5700             ;; From.
5701             (progn
5702               (goto-char p)
5703               (if (search-forward "\nfrom:" nil t)
5704                   (funcall gnus-decode-encoded-word-function
5705                            (nnheader-header-value))
5706                 "(nobody)"))
5707             ;; Date.
5708             (progn
5709               (goto-char p)
5710               (if (search-forward "\ndate:" nil t)
5711                   (nnheader-header-value) ""))
5712             ;; Message-ID.
5713             (progn
5714               (goto-char p)
5715               (setq id (if (re-search-forward
5716                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5717                            ;; We do it this way to make sure the Message-ID
5718                            ;; is (somewhat) syntactically valid.
5719                            (buffer-substring (match-beginning 1)
5720                                              (match-end 1))
5721                          ;; If there was no message-id, we just fake one
5722                          ;; to make subsequent routines simpler.
5723                          (nnheader-generate-fake-message-id))))
5724             ;; References.
5725             (progn
5726               (goto-char p)
5727               (if (search-forward "\nreferences:" nil t)
5728                   (progn
5729                     (setq end (point))
5730                     (prog1
5731                         (nnheader-header-value)
5732                       (setq ref
5733                             (buffer-substring
5734                              (progn
5735                                (end-of-line)
5736                                (search-backward ">" end t)
5737                                (1+ (point)))
5738                              (progn
5739                                (search-backward "<" end t)
5740                                (point))))))
5741                 ;; Get the references from the in-reply-to header if there
5742                 ;; were no references and the in-reply-to header looks
5743                 ;; promising.
5744                 (if (and (search-forward "\nin-reply-to:" nil t)
5745                          (setq in-reply-to (nnheader-header-value))
5746                          (string-match "<[^>]+>" in-reply-to))
5747                     (let (ref2)
5748                       (setq ref (substring in-reply-to (match-beginning 0)
5749                                            (match-end 0)))
5750                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5751                         (setq ref2 (substring in-reply-to (match-beginning 0)
5752                                               (match-end 0)))
5753                         (when (> (length ref2) (length ref))
5754                           (setq ref ref2)))
5755                       ref)
5756                   (setq ref nil))))
5757             ;; Chars.
5758             (progn
5759               (goto-char p)
5760               (if (search-forward "\nchars: " nil t)
5761                   (if (numberp (setq chars (ignore-errors (read cur))))
5762                       chars -1)
5763                 -1))
5764             ;; Lines.
5765             (progn
5766               (goto-char p)
5767               (if (search-forward "\nlines: " nil t)
5768                   (if (numberp (setq lines (ignore-errors (read cur))))
5769                       lines -1)
5770                 -1))
5771             ;; Xref.
5772             (progn
5773               (goto-char p)
5774               (and (search-forward "\nxref:" nil t)
5775                    (nnheader-header-value)))
5776             ;; Extra.
5777             (when gnus-extra-headers
5778               (let ((extra gnus-extra-headers)
5779                     out)
5780                 (while extra
5781                   (goto-char p)
5782                   (when (search-forward
5783                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5784                     (push (cons (car extra) (nnheader-header-value))
5785                           out))
5786                   (pop extra))
5787                 out))))
5788           (when (equal id ref)
5789             (setq ref nil))
5790
5791           (when gnus-alter-header-function
5792             (funcall gnus-alter-header-function header)
5793             (setq id (mail-header-id header)
5794                   ref (gnus-parent-id (mail-header-references header))))
5795
5796           (when (setq header
5797                       (gnus-dependencies-add-header
5798                        header dependencies force-new))
5799             (push header headers))
5800           (goto-char (point-max))
5801           (widen))
5802         (nreverse headers)))))
5803
5804 ;; Goes through the xover lines and returns a list of vectors
5805 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5806                                                   force-new dependencies
5807                                                   group also-fetch-heads)
5808   "Parse the news overview data in the server buffer.
5809 Return a list of headers that match SEQUENCE (see
5810 `nntp-retrieve-headers')."
5811   ;; Get the Xref when the users reads the articles since most/some
5812   ;; NNTP servers do not include Xrefs when using XOVER.
5813   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5814   (let ((mail-parse-charset gnus-newsgroup-charset)
5815         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5816         (cur nntp-server-buffer)
5817         (dependencies (or dependencies gnus-newsgroup-dependencies))
5818         (allp (cond
5819                ((eq gnus-read-all-available-headers t)
5820                 t)
5821                ((stringp gnus-read-all-available-headers)
5822                 (string-match gnus-read-all-available-headers group))
5823                (t
5824                 nil)))
5825         number headers header)
5826     (save-excursion
5827       (set-buffer nntp-server-buffer)
5828       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5829       ;; Allow the user to mangle the headers before parsing them.
5830       (gnus-run-hooks 'gnus-parse-headers-hook)
5831       (goto-char (point-min))
5832       (gnus-parse-without-error
5833         (while (and (or sequence allp)
5834                     (not (eobp)))
5835           (setq number (read cur))
5836           (when (not allp)
5837             (while (and sequence
5838                         (< (car sequence) number))
5839               (setq sequence (cdr sequence))))
5840           (when (and (or allp
5841                          (and sequence
5842                               (eq number (car sequence))))
5843                      (progn
5844                        (setq sequence (cdr sequence))
5845                        (setq header (inline
5846                                       (gnus-nov-parse-line
5847                                        number dependencies force-new)))))
5848             (push header headers))
5849           (forward-line 1)))
5850       ;; A common bug in inn is that if you have posted an article and
5851       ;; then retrieves the active file, it will answer correctly --
5852       ;; the new article is included.  However, a NOV entry for the
5853       ;; article may not have been generated yet, so this may fail.
5854       ;; We work around this problem by retrieving the last few
5855       ;; headers using HEAD.
5856       (if (or (not also-fetch-heads)
5857               (not sequence))
5858           ;; We (probably) got all the headers.
5859           (nreverse headers)
5860         (let ((gnus-nov-is-evil t))
5861           (nconc
5862            (nreverse headers)
5863            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5864              (gnus-get-newsgroup-headers))))))))
5865
5866 (defun gnus-article-get-xrefs ()
5867   "Fill in the Xref value in `gnus-current-headers', if necessary.
5868 This is meant to be called in `gnus-article-internal-prepare-hook'."
5869   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5870                                  gnus-current-headers)))
5871     (or (not gnus-use-cross-reference)
5872         (not headers)
5873         (and (mail-header-xref headers)
5874              (not (string= (mail-header-xref headers) "")))
5875         (let ((case-fold-search t)
5876               xref)
5877           (save-restriction
5878             (nnheader-narrow-to-headers)
5879             (goto-char (point-min))
5880             (when (or (and (not (eobp))
5881                            (eq (downcase (char-after)) ?x)
5882                            (looking-at "Xref:"))
5883                       (search-forward "\nXref:" nil t))
5884               (goto-char (1+ (match-end 0)))
5885               (setq xref (buffer-substring (point) (gnus-point-at-eol)))
5886               (mail-header-set-xref headers xref)))))))
5887
5888 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5889   "Find article ID and insert the summary line for that article.
5890 OLD-HEADER can either be a header or a line number to insert
5891 the subject line on."
5892   (let* ((line (and (numberp old-header) old-header))
5893          (old-header (and (vectorp old-header) old-header))
5894          (header (cond ((and old-header use-old-header)
5895                         old-header)
5896                        ((and (numberp id)
5897                              (gnus-number-to-header id))
5898                         (gnus-number-to-header id))
5899                        (t
5900                         (gnus-read-header id))))
5901          (number (and (numberp id) id))
5902          d)
5903     (when header
5904       ;; Rebuild the thread that this article is part of and go to the
5905       ;; article we have fetched.
5906       (when (and (not gnus-show-threads)
5907                  old-header)
5908         (when (and number
5909                    (setq d (gnus-data-find (mail-header-number old-header))))
5910           (goto-char (gnus-data-pos d))
5911           (gnus-data-remove
5912            number
5913            (- (gnus-point-at-bol)
5914               (prog1
5915                   (1+ (gnus-point-at-eol))
5916                 (gnus-delete-line))))))
5917       (when old-header
5918         (mail-header-set-number header (mail-header-number old-header)))
5919       (setq gnus-newsgroup-sparse
5920             (delq (setq number (mail-header-number header))
5921                   gnus-newsgroup-sparse))
5922       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5923       (push number gnus-newsgroup-limit)
5924       (gnus-rebuild-thread (mail-header-id header) line)
5925       (gnus-summary-goto-subject number nil t))
5926     (when (and (numberp number)
5927                (> number 0))
5928       ;; We have to update the boundaries even if we can't fetch the
5929       ;; article if ID is a number -- so that the next `P' or `N'
5930       ;; command will fetch the previous (or next) article even
5931       ;; if the one we tried to fetch this time has been canceled.
5932       (when (> number gnus-newsgroup-end)
5933         (setq gnus-newsgroup-end number))
5934       (when (< number gnus-newsgroup-begin)
5935         (setq gnus-newsgroup-begin number))
5936       (setq gnus-newsgroup-unselected
5937             (delq number gnus-newsgroup-unselected)))
5938     ;; Report back a success?
5939     (and header (mail-header-number header))))
5940
5941 ;;; Process/prefix in the summary buffer
5942
5943 (defun gnus-summary-work-articles (n)
5944   "Return a list of articles to be worked upon.
5945 The prefix argument, the list of process marked articles, and the
5946 current article will be taken into consideration."
5947   (save-excursion
5948     (set-buffer gnus-summary-buffer)
5949     (cond
5950      (n
5951       ;; A numerical prefix has been given.
5952       (setq n (prefix-numeric-value n))
5953       (let ((backward (< n 0))
5954             (n (abs (prefix-numeric-value n)))
5955             articles article)
5956         (save-excursion
5957           (while
5958               (and (> n 0)
5959                    (push (setq article (gnus-summary-article-number))
5960                          articles)
5961                    (if backward
5962                        (gnus-summary-find-prev nil article)
5963                      (gnus-summary-find-next nil article)))
5964             (decf n)))
5965         (nreverse articles)))
5966      ((and (gnus-region-active-p) (mark))
5967       (message "region active")
5968       ;; Work on the region between point and mark.
5969       (let ((max (max (point) (mark)))
5970             articles article)
5971         (save-excursion
5972           (goto-char (min (min (point) (mark))))
5973           (while
5974               (and
5975                (push (setq article (gnus-summary-article-number)) articles)
5976                (gnus-summary-find-next nil article)
5977                (< (point) max)))
5978           (nreverse articles))))
5979      (gnus-newsgroup-processable
5980       ;; There are process-marked articles present.
5981       ;; Save current state.
5982       (gnus-summary-save-process-mark)
5983       ;; Return the list.
5984       (reverse gnus-newsgroup-processable))
5985      (t
5986       ;; Just return the current article.
5987       (list (gnus-summary-article-number))))))
5988
5989 (defmacro gnus-summary-iterate (arg &rest forms)
5990   "Iterate over the process/prefixed articles and do FORMS.
5991 ARG is the interactive prefix given to the command.  FORMS will be
5992 executed with point over the summary line of the articles."
5993   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5994     `(let ((,articles (gnus-summary-work-articles ,arg)))
5995        (while ,articles
5996          (gnus-summary-goto-subject (car ,articles))
5997          ,@forms
5998          (pop ,articles)))))
5999
6000 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6001 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6002
6003 (defun gnus-summary-save-process-mark ()
6004   "Push the current set of process marked articles on the stack."
6005   (interactive)
6006   (push (copy-sequence gnus-newsgroup-processable)
6007         gnus-newsgroup-process-stack))
6008
6009 (defun gnus-summary-kill-process-mark ()
6010   "Push the current set of process marked articles on the stack and unmark."
6011   (interactive)
6012   (gnus-summary-save-process-mark)
6013   (gnus-summary-unmark-all-processable))
6014
6015 (defun gnus-summary-yank-process-mark ()
6016   "Pop the last process mark state off the stack and restore it."
6017   (interactive)
6018   (unless gnus-newsgroup-process-stack
6019     (error "Empty mark stack"))
6020   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6021
6022 (defun gnus-summary-process-mark-set (set)
6023   "Make SET into the current process marked articles."
6024   (gnus-summary-unmark-all-processable)
6025   (while set
6026     (gnus-summary-set-process-mark (pop set))))
6027
6028 ;;; Searching and stuff
6029
6030 (defun gnus-summary-search-group (&optional backward use-level)
6031   "Search for next unread newsgroup.
6032 If optional argument BACKWARD is non-nil, search backward instead."
6033   (save-excursion
6034     (set-buffer gnus-group-buffer)
6035     (when (gnus-group-search-forward
6036            backward nil (if use-level (gnus-group-group-level) nil))
6037       (gnus-group-group-name))))
6038
6039 (defun gnus-summary-best-group (&optional exclude-group)
6040   "Find the name of the best unread group.
6041 If EXCLUDE-GROUP, do not go to this group."
6042   (save-excursion
6043     (set-buffer gnus-group-buffer)
6044     (save-excursion
6045       (gnus-group-best-unread-group exclude-group))))
6046
6047 (defun gnus-summary-find-next (&optional unread article backward)
6048   (if backward (gnus-summary-find-prev unread article)
6049     (let* ((dummy (gnus-summary-article-intangible-p))
6050            (article (or article (gnus-summary-article-number)))
6051            (data (gnus-data-find-list article))
6052            result)
6053       (when (and (not dummy)
6054                  (or (not gnus-summary-check-current)
6055                      (not unread)
6056                      (not (gnus-data-unread-p (car data)))))
6057         (setq data (cdr data)))
6058       (when (setq result
6059                   (if unread
6060                       (progn
6061                         (while data
6062                           (unless (memq (gnus-data-number (car data)) 
6063                                         (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6064                                                gnus-newsgroup-undownloaded)
6065                                               (gnus-plugged
6066                                                nil)
6067                                               ((eq gnus-auto-goto-ignores 'unfetched)
6068                                                gnus-newsgroup-unfetched)
6069                                               ((eq gnus-auto-goto-ignores 'undownloaded)
6070                                                gnus-newsgroup-undownloaded)))
6071                             (when (gnus-data-unread-p (car data))
6072                               (setq result (car data)
6073                                     data nil)))
6074                           (setq data (cdr data)))
6075                         result)
6076                     (car data)))
6077         (goto-char (gnus-data-pos result))
6078         (gnus-data-number result)))))
6079
6080 (defun gnus-summary-find-prev (&optional unread article)
6081   (let* ((eobp (eobp))
6082          (article (or article (gnus-summary-article-number)))
6083          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6084          result)
6085     (when (and (not eobp)
6086                (or (not gnus-summary-check-current)
6087                    (not unread)
6088                    (not (gnus-data-unread-p (car data)))))
6089       (setq data (cdr data)))
6090     (when (setq result
6091                 (if unread
6092                     (progn
6093                       (while data
6094                         (unless (memq (gnus-data-number (car data))
6095                                       (cond ((eq gnus-auto-goto-ignores 'always-undownloaded)
6096                                              gnus-newsgroup-undownloaded)
6097                                             (gnus-plugged
6098                                              nil)
6099                                             ((eq gnus-auto-goto-ignores 'unfetched)
6100                                              gnus-newsgroup-unfetched)
6101                                             ((eq gnus-auto-goto-ignores 'undownloaded)
6102                                              gnus-newsgroup-undownloaded)))
6103                           (when (gnus-data-unread-p (car data))
6104                             (setq result (car data)
6105                                   data nil)))
6106                         (setq data (cdr data)))
6107                       result)
6108                   (car data)))
6109       (goto-char (gnus-data-pos result))
6110       (gnus-data-number result))))
6111
6112 (defun gnus-summary-find-subject (subject &optional unread backward article)
6113   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6114          (article (or article (gnus-summary-article-number)))
6115          (articles (gnus-data-list backward))
6116          (arts (gnus-data-find-list article articles))
6117          result)
6118     (when (or (not gnus-summary-check-current)
6119               (not unread)
6120               (not (gnus-data-unread-p (car arts))))
6121       (setq arts (cdr arts)))
6122     (while arts
6123       (and (or (not unread)
6124                (gnus-data-unread-p (car arts)))
6125            (vectorp (gnus-data-header (car arts)))
6126            (gnus-subject-equal
6127             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6128            (setq result (car arts)
6129                  arts nil))
6130       (setq arts (cdr arts)))
6131     (and result
6132          (goto-char (gnus-data-pos result))
6133          (gnus-data-number result))))
6134
6135 (defun gnus-summary-search-forward (&optional unread subject backward)
6136   "Search forward for an article.
6137 If UNREAD, look for unread articles.  If SUBJECT, look for
6138 articles with that subject.  If BACKWARD, search backward instead."
6139   (cond (subject (gnus-summary-find-subject subject unread backward))
6140         (backward (gnus-summary-find-prev unread))
6141         (t (gnus-summary-find-next unread))))
6142
6143 (defun gnus-recenter (&optional n)
6144   "Center point in window and redisplay frame.
6145 Also do horizontal recentering."
6146   (interactive "P")
6147   (when (and gnus-auto-center-summary
6148              (not (eq gnus-auto-center-summary 'vertical)))
6149     (gnus-horizontal-recenter))
6150   (recenter n))
6151
6152 (defun gnus-summary-recenter ()
6153   "Center point in the summary window.
6154 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6155 displayed, no centering will be performed."
6156   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6157   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6158   (interactive)
6159   ;; The user has to want it.
6160   (when gnus-auto-center-summary
6161     (let* ((top (cond ((< (window-height) 4) 0)
6162                       ((< (window-height) 7) 1)
6163                       (t (if (numberp gnus-auto-center-summary)
6164                              gnus-auto-center-summary
6165                            2))))
6166            (height (1- (window-height)))
6167            (bottom (save-excursion (goto-char (point-max))
6168                                    (forward-line (- height))
6169                                    (point)))
6170            (window (get-buffer-window (current-buffer))))
6171       (when (get-buffer-window gnus-article-buffer)
6172         ;; Only do recentering when the article buffer is displayed,
6173         ;; Set the window start to either `bottom', which is the biggest
6174         ;; possible valid number, or the second line from the top,
6175         ;; whichever is the least.
6176         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6177           (if (> bottom top-pos)
6178               ;; Keep the second line from the top visible
6179               (set-window-start window top-pos t)
6180             ;; Try to keep the bottom line visible; if it's partially
6181             ;; obscured, either scroll one more line to make it fully
6182             ;; visible, or revert to using TOP-POS.
6183             (save-excursion
6184               (goto-char (point-max))
6185               (forward-line -1)
6186               (let ((last-line-start (point)))
6187                 (goto-char bottom)
6188                 (set-window-start window (point) t)
6189                 (when (not (pos-visible-in-window-p last-line-start window))
6190                   (forward-line 1)
6191                   (set-window-start window (min (point) top-pos) t)))))))
6192       ;; Do horizontal recentering while we're at it.
6193       (when (and (get-buffer-window (current-buffer) t)
6194                  (not (eq gnus-auto-center-summary 'vertical)))
6195         (let ((selected (selected-window)))
6196           (select-window (get-buffer-window (current-buffer) t))
6197           (gnus-summary-position-point)
6198           (gnus-horizontal-recenter)
6199           (select-window selected))))))
6200
6201 (defun gnus-summary-jump-to-group (newsgroup)
6202   "Move point to NEWSGROUP in group mode buffer."
6203   ;; Keep update point of group mode buffer if visible.
6204   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6205       (save-window-excursion
6206         ;; Take care of tree window mode.
6207         (when (get-buffer-window gnus-group-buffer)
6208           (pop-to-buffer gnus-group-buffer))
6209         (gnus-group-jump-to-group newsgroup))
6210     (save-excursion
6211       ;; Take care of tree window mode.
6212       (if (get-buffer-window gnus-group-buffer)
6213           (pop-to-buffer gnus-group-buffer)
6214         (set-buffer gnus-group-buffer))
6215       (gnus-group-jump-to-group newsgroup))))
6216
6217 ;; This function returns a list of article numbers based on the
6218 ;; difference between the ranges of read articles in this group and
6219 ;; the range of active articles.
6220 (defun gnus-list-of-unread-articles (group)
6221   (let* ((read (gnus-info-read (gnus-get-info group)))
6222          (active (or (gnus-active group) (gnus-activate-group group)))
6223          (last (cdr active))
6224          first nlast unread)
6225     ;; If none are read, then all are unread.
6226     (if (not read)
6227         (setq first (car active))
6228       ;; If the range of read articles is a single range, then the
6229       ;; first unread article is the article after the last read
6230       ;; article.  Sounds logical, doesn't it?
6231       (if (and (not (listp (cdr read)))
6232                (or (< (car read) (car active))
6233                    (progn (setq read (list read))
6234                           nil)))
6235           (setq first (max (car active) (1+ (cdr read))))
6236         ;; `read' is a list of ranges.
6237         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6238                                   (caar read)))
6239                   1)
6240           (setq first (car active)))
6241         (while read
6242           (when first
6243             (while (< first nlast)
6244               (push first unread)
6245               (setq first (1+ first))))
6246           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6247           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6248           (setq read (cdr read)))))
6249     ;; And add the last unread articles.
6250     (while (<= first last)
6251       (push first unread)
6252       (setq first (1+ first)))
6253     ;; Return the list of unread articles.
6254     (delq 0 (nreverse unread))))
6255
6256 (defun gnus-list-of-read-articles (group)
6257   "Return a list of unread, unticked and non-dormant articles."
6258   (let* ((info (gnus-get-info group))
6259          (marked (gnus-info-marks info))
6260          (active (gnus-active group)))
6261     (and info active
6262          (gnus-list-range-difference
6263           (gnus-list-range-difference
6264            (gnus-sorted-complement
6265             (gnus-uncompress-range active)
6266             (gnus-list-of-unread-articles group))
6267            (cdr (assq 'dormant marked)))
6268           (cdr (assq 'tick marked))))))
6269
6270 ;; Various summary commands
6271
6272 (defun gnus-summary-select-article-buffer ()
6273   "Reconfigure windows to show article buffer."
6274   (interactive)
6275   (if (not (gnus-buffer-live-p gnus-article-buffer))
6276       (error "There is no article buffer for this summary buffer")
6277     (gnus-configure-windows 'article)
6278     (select-window (get-buffer-window gnus-article-buffer))))
6279
6280 (defun gnus-summary-universal-argument (arg)
6281   "Perform any operation on all articles that are process/prefixed."
6282   (interactive "P")
6283   (let ((articles (gnus-summary-work-articles arg))
6284         func article)
6285     (if (eq
6286          (setq
6287           func
6288           (key-binding
6289            (read-key-sequence
6290             (substitute-command-keys
6291              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6292          'undefined)
6293         (gnus-error 1 "Undefined key")
6294       (save-excursion
6295         (while articles
6296           (gnus-summary-goto-subject (setq article (pop articles)))
6297           (let (gnus-newsgroup-processable)
6298             (command-execute func))
6299           (gnus-summary-remove-process-mark article)))))
6300   (gnus-summary-position-point))
6301
6302 (defun gnus-summary-toggle-truncation (&optional arg)
6303   "Toggle truncation of summary lines.
6304 With arg, turn line truncation on if arg is positive."
6305   (interactive "P")
6306   (setq truncate-lines
6307         (if (null arg) (not truncate-lines)
6308           (> (prefix-numeric-value arg) 0)))
6309   (redraw-display))
6310
6311 (defun gnus-summary-find-uncancelled ()
6312   "Return the number of an uncancelled article.
6313 The current article is considered, then following articles, then previous
6314 articles.  If all articles are cancelled then return a dummy 0."
6315   (let (found)
6316     (dolist (rev '(nil t))
6317       (unless found      ; don't demand the reverse list if we don't need it
6318         (let ((data (gnus-data-find-list
6319                      (gnus-summary-article-number) (gnus-data-list rev))))
6320           (while (and data (not found))
6321             (if (not (eq gnus-canceled-mark (gnus-data-mark (car data))))
6322                 (setq found (gnus-data-number (car data))))
6323             (setq data (cdr data))))))
6324     (or found 0)))
6325
6326 (defun gnus-summary-reselect-current-group (&optional all rescan)
6327   "Exit and then reselect the current newsgroup.
6328 The prefix argument ALL means to select all articles."
6329   (interactive "P")
6330   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6331     (error "Ephemeral groups can't be reselected"))
6332   (let ((current-subject (gnus-summary-find-uncancelled))
6333         (group gnus-newsgroup-name))
6334     (setq gnus-newsgroup-begin nil)
6335     (gnus-summary-exit)
6336     ;; We have to adjust the point of group mode buffer because
6337     ;; point was moved to the next unread newsgroup by exiting.
6338     (gnus-summary-jump-to-group group)
6339     (when rescan
6340       (save-excursion
6341         (gnus-group-get-new-news-this-group 1)))
6342     (gnus-group-read-group all t)
6343     (gnus-summary-goto-subject current-subject nil t)))
6344
6345 (defun gnus-summary-rescan-group (&optional all)
6346   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6347   (interactive "P")
6348   (gnus-summary-reselect-current-group all t))
6349
6350 (defun gnus-summary-update-info (&optional non-destructive)
6351   (save-excursion
6352     (let ((group gnus-newsgroup-name))
6353       (when group
6354         (when gnus-newsgroup-kill-headers
6355           (setq gnus-newsgroup-killed
6356                 (gnus-compress-sequence
6357                  (gnus-sorted-union
6358                   (gnus-list-range-intersection
6359                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6360                   gnus-newsgroup-unreads)
6361                  t)))
6362         (unless (listp (cdr gnus-newsgroup-killed))
6363           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6364         (let ((headers gnus-newsgroup-headers))
6365           ;; Set the new ranges of read articles.
6366           (save-excursion
6367             (set-buffer gnus-group-buffer)
6368             (gnus-undo-force-boundary))
6369           (gnus-update-read-articles
6370            group (gnus-sorted-union
6371                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6372           ;; Set the current article marks.
6373           (let ((gnus-newsgroup-scored
6374                  (if (and (not gnus-save-score)
6375                           (not non-destructive))
6376                      nil
6377                    gnus-newsgroup-scored)))
6378             (save-excursion
6379               (gnus-update-marks)))
6380           ;; Do the cross-ref thing.
6381           (when gnus-use-cross-reference
6382             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6383           ;; Do not switch windows but change the buffer to work.
6384           (set-buffer gnus-group-buffer)
6385           (unless (gnus-ephemeral-group-p group)
6386             (gnus-group-update-group group)))))))
6387
6388 (defun gnus-summary-save-newsrc (&optional force)
6389   "Save the current number of read/marked articles in the dribble buffer.
6390 The dribble buffer will then be saved.
6391 If FORCE (the prefix), also save the .newsrc file(s)."
6392   (interactive "P")
6393   (gnus-summary-update-info t)
6394   (if force
6395       (gnus-save-newsrc-file)
6396     (gnus-dribble-save)))
6397
6398 (defun gnus-summary-exit (&optional temporary)
6399   "Exit reading current newsgroup, and then return to group selection mode.
6400 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6401   (interactive)
6402   (gnus-set-global-variables)
6403   (when (gnus-buffer-live-p gnus-article-buffer)
6404     (save-excursion
6405       (set-buffer gnus-article-buffer)
6406       (mm-destroy-parts gnus-article-mime-handles)
6407       ;; Set it to nil for safety reason.
6408       (setq gnus-article-mime-handle-alist nil)
6409       (setq gnus-article-mime-handles nil)))
6410   (gnus-kill-save-kill-buffer)
6411   (gnus-async-halt-prefetch)
6412   (let* ((group gnus-newsgroup-name)
6413          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6414          (gnus-group-is-exiting-p t)
6415          (mode major-mode)
6416          (group-point nil)
6417          (buf (current-buffer)))
6418     (unless quit-config
6419       ;; Do adaptive scoring, and possibly save score files.
6420       (when gnus-newsgroup-adaptive
6421         (gnus-score-adaptive))
6422       (when gnus-use-scoring
6423         (gnus-score-save)))
6424     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6425     ;; If we have several article buffers, we kill them at exit.
6426     (unless gnus-single-article-buffer
6427       (gnus-kill-buffer gnus-original-article-buffer)
6428       (setq gnus-article-current nil))
6429     (when gnus-use-cache
6430       (gnus-cache-possibly-remove-articles)
6431       (gnus-cache-save-buffers))
6432     (gnus-async-prefetch-remove-group group)
6433     (when gnus-suppress-duplicates
6434       (gnus-dup-enter-articles))
6435     (when gnus-use-trees
6436       (gnus-tree-close group))
6437     (when gnus-use-cache
6438       (gnus-cache-write-active))
6439     ;; Remove entries for this group.
6440     (nnmail-purge-split-history (gnus-group-real-name group))
6441     ;; Make all changes in this group permanent.
6442     (unless quit-config
6443       (gnus-run-hooks 'gnus-exit-group-hook)
6444       (gnus-summary-update-info))
6445     (gnus-close-group group)
6446     ;; Make sure where we were, and go to next newsgroup.
6447     (set-buffer gnus-group-buffer)
6448     (unless quit-config
6449       (gnus-group-jump-to-group group))
6450     (gnus-run-hooks 'gnus-summary-exit-hook)
6451     (unless (or quit-config
6452                 ;; If this group has disappeared from the summary
6453                 ;; buffer, don't skip forwards.
6454                 (not (string= group (gnus-group-group-name))))
6455       (gnus-group-next-unread-group 1))
6456     (setq group-point (point))
6457     (if temporary
6458         nil                             ;Nothing to do.
6459       ;; If we have several article buffers, we kill them at exit.
6460       (unless gnus-single-article-buffer
6461         (gnus-kill-buffer gnus-article-buffer)
6462         (gnus-kill-buffer gnus-original-article-buffer)
6463         (setq gnus-article-current nil))
6464       (set-buffer buf)
6465       (if (not gnus-kill-summary-on-exit)
6466           (progn
6467             (gnus-deaden-summary)
6468             (setq mode nil))
6469         ;; We set all buffer-local variables to nil.  It is unclear why
6470         ;; this is needed, but if we don't, buffer-local variables are
6471         ;; not garbage-collected, it seems.  This would the lead to en
6472         ;; ever-growing Emacs.
6473         (gnus-summary-clear-local-variables)
6474         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6475           (gnus-summary-clear-local-variables))
6476         (when (get-buffer gnus-article-buffer)
6477           (bury-buffer gnus-article-buffer))
6478         ;; We clear the global counterparts of the buffer-local
6479         ;; variables as well, just to be on the safe side.
6480         (set-buffer gnus-group-buffer)
6481         (gnus-summary-clear-local-variables)
6482         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6483           (gnus-summary-clear-local-variables))
6484         ;; Return to group mode buffer.
6485         (when (eq mode 'gnus-summary-mode)
6486           (gnus-kill-buffer buf)))
6487       (setq gnus-current-select-method gnus-select-method)
6488       (pop-to-buffer gnus-group-buffer)
6489       (if (not quit-config)
6490           (progn
6491             (goto-char group-point)
6492             (gnus-configure-windows 'group 'force))
6493         (gnus-handle-ephemeral-exit quit-config))
6494       ;; Clear the current group name.
6495       (unless quit-config
6496         (setq gnus-newsgroup-name nil)))))
6497
6498 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6499 (defun gnus-summary-exit-no-update (&optional no-questions)
6500   "Quit reading current newsgroup without updating read article info."
6501   (interactive)
6502   (let* ((group gnus-newsgroup-name)
6503          (gnus-group-is-exiting-p t)
6504          (gnus-group-is-exiting-without-update-p t)
6505          (quit-config (gnus-group-quit-config group)))
6506     (when (or no-questions
6507               gnus-expert-user
6508               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6509       (gnus-async-halt-prefetch)
6510       (run-hooks 'gnus-summary-prepare-exit-hook)
6511       (when (gnus-buffer-live-p gnus-article-buffer)
6512         (save-excursion
6513           (set-buffer gnus-article-buffer)
6514           (mm-destroy-parts gnus-article-mime-handles)
6515           ;; Set it to nil for safety reason.
6516           (setq gnus-article-mime-handle-alist nil)
6517           (setq gnus-article-mime-handles nil)))
6518       ;; If we have several article buffers, we kill them at exit.
6519       (unless gnus-single-article-buffer
6520         (gnus-kill-buffer gnus-article-buffer)
6521         (gnus-kill-buffer gnus-original-article-buffer)
6522         (setq gnus-article-current nil))
6523       (if (not gnus-kill-summary-on-exit)
6524           (gnus-deaden-summary)
6525         (gnus-close-group group)
6526         (gnus-summary-clear-local-variables)
6527         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6528           (gnus-summary-clear-local-variables))
6529         (set-buffer gnus-group-buffer)
6530         (gnus-summary-clear-local-variables)
6531         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6532           (gnus-summary-clear-local-variables))
6533         (gnus-kill-buffer gnus-summary-buffer))
6534       (unless gnus-single-article-buffer
6535         (setq gnus-article-current nil))
6536       (when gnus-use-trees
6537         (gnus-tree-close group))
6538       (gnus-async-prefetch-remove-group group)
6539       (when (get-buffer gnus-article-buffer)
6540         (bury-buffer gnus-article-buffer))
6541       ;; Return to the group buffer.
6542       (gnus-configure-windows 'group 'force)
6543       ;; Clear the current group name.
6544       (setq gnus-newsgroup-name nil)
6545       (unless (gnus-ephemeral-group-p group)
6546         (gnus-group-update-group group))
6547       (when (equal (gnus-group-group-name) group)
6548         (gnus-group-next-unread-group 1))
6549       (when quit-config
6550         (gnus-handle-ephemeral-exit quit-config)))))
6551
6552 (defun gnus-handle-ephemeral-exit (quit-config)
6553   "Handle movement when leaving an ephemeral group.
6554 The state which existed when entering the ephemeral is reset."
6555   (if (not (buffer-name (car quit-config)))
6556       (gnus-configure-windows 'group 'force)
6557     (set-buffer (car quit-config))
6558     (cond ((eq major-mode 'gnus-summary-mode)
6559            (gnus-set-global-variables))
6560           ((eq major-mode 'gnus-article-mode)
6561            (save-excursion
6562              ;; The `gnus-summary-buffer' variable may point
6563              ;; to the old summary buffer when using a single
6564              ;; article buffer.
6565              (unless (gnus-buffer-live-p gnus-summary-buffer)
6566                (set-buffer gnus-group-buffer))
6567              (set-buffer gnus-summary-buffer)
6568              (gnus-set-global-variables))))
6569     (if (or (eq (cdr quit-config) 'article)
6570             (eq (cdr quit-config) 'pick))
6571         (progn
6572           ;; The current article may be from the ephemeral group
6573           ;; thus it is best that we reload this article
6574           (gnus-summary-show-article)
6575           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6576               (gnus-configure-windows 'pick 'force)
6577             (gnus-configure-windows (cdr quit-config) 'force)))
6578       (gnus-configure-windows (cdr quit-config) 'force))
6579     (when (eq major-mode 'gnus-summary-mode)
6580       (gnus-summary-next-subject 1 nil t)
6581       (gnus-summary-recenter)
6582       (gnus-summary-position-point))))
6583
6584 ;;; Dead summaries.
6585
6586 (defvar gnus-dead-summary-mode-map nil)
6587
6588 (unless gnus-dead-summary-mode-map
6589   (setq gnus-dead-summary-mode-map (make-keymap))
6590   (suppress-keymap gnus-dead-summary-mode-map)
6591   (substitute-key-definition
6592    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6593   (dolist (key '("\C-d" "\r" "\177" [delete]))
6594     (define-key gnus-dead-summary-mode-map
6595       key 'gnus-summary-wake-up-the-dead))
6596   (dolist (key '("q" "Q"))
6597     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6598
6599 (defvar gnus-dead-summary-mode nil
6600   "Minor mode for Gnus summary buffers.")
6601
6602 (defun gnus-dead-summary-mode (&optional arg)
6603   "Minor mode for Gnus summary buffers."
6604   (interactive "P")
6605   (when (eq major-mode 'gnus-summary-mode)
6606     (make-local-variable 'gnus-dead-summary-mode)
6607     (setq gnus-dead-summary-mode
6608           (if (null arg) (not gnus-dead-summary-mode)
6609             (> (prefix-numeric-value arg) 0)))
6610     (when gnus-dead-summary-mode
6611       (gnus-add-minor-mode
6612        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6613
6614 (defun gnus-deaden-summary ()
6615   "Make the current summary buffer into a dead summary buffer."
6616   ;; Kill any previous dead summary buffer.
6617   (when (and gnus-dead-summary
6618              (buffer-name gnus-dead-summary))
6619     (save-excursion
6620       (set-buffer gnus-dead-summary)
6621       (when gnus-dead-summary-mode
6622         (kill-buffer (current-buffer)))))
6623   ;; Make this the current dead summary.
6624   (setq gnus-dead-summary (current-buffer))
6625   (gnus-dead-summary-mode 1)
6626   (let ((name (buffer-name)))
6627     (when (string-match "Summary" name)
6628       (rename-buffer
6629        (concat (substring name 0 (match-beginning 0)) "Dead "
6630                (substring name (match-beginning 0)))
6631        t)
6632       (bury-buffer))))
6633
6634 (defun gnus-kill-or-deaden-summary (buffer)
6635   "Kill or deaden the summary BUFFER."
6636   (save-excursion
6637     (when (and (buffer-name buffer)
6638                (not gnus-single-article-buffer))
6639       (save-excursion
6640         (set-buffer buffer)
6641         (gnus-kill-buffer gnus-article-buffer)
6642         (gnus-kill-buffer gnus-original-article-buffer)))
6643     (cond
6644      ;; Kill the buffer.
6645      (gnus-kill-summary-on-exit
6646       (when (and gnus-use-trees
6647                  (gnus-buffer-exists-p buffer))
6648         (save-excursion
6649           (set-buffer buffer)
6650           (gnus-tree-close gnus-newsgroup-name)))
6651       (gnus-kill-buffer buffer))
6652      ;; Deaden the buffer.
6653      ((gnus-buffer-exists-p buffer)
6654       (save-excursion
6655         (set-buffer buffer)
6656         (gnus-deaden-summary))))))
6657
6658 (defun gnus-summary-wake-up-the-dead (&rest args)
6659   "Wake up the dead summary buffer."
6660   (interactive)
6661   (gnus-dead-summary-mode -1)
6662   (let ((name (buffer-name)))
6663     (when (string-match "Dead " name)
6664       (rename-buffer
6665        (concat (substring name 0 (match-beginning 0))
6666                (substring name (match-end 0)))
6667        t)))
6668   (gnus-message 3 "This dead summary is now alive again"))
6669
6670 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6671 (defun gnus-summary-fetch-faq (&optional faq-dir)
6672   "Fetch the FAQ for the current group.
6673 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6674 in."
6675   (interactive
6676    (list
6677     (when current-prefix-arg
6678       (completing-read
6679        "FAQ dir: " (and (listp gnus-group-faq-directory)
6680                         (mapcar (lambda (file) (list file))
6681                                 gnus-group-faq-directory))))))
6682   (let (gnus-faq-buffer)
6683     (when (setq gnus-faq-buffer
6684                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6685       (gnus-configure-windows 'summary-faq))))
6686
6687 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6688 (defun gnus-summary-describe-group (&optional force)
6689   "Describe the current newsgroup."
6690   (interactive "P")
6691   (gnus-group-describe-group force gnus-newsgroup-name))
6692
6693 (defun gnus-summary-describe-briefly ()
6694   "Describe summary mode commands briefly."
6695   (interactive)
6696   (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")))
6697
6698 ;; Walking around group mode buffer from summary mode.
6699
6700 (defun gnus-summary-next-group (&optional no-article target-group backward)
6701   "Exit current newsgroup and then select next unread newsgroup.
6702 If prefix argument NO-ARTICLE is non-nil, no article is selected
6703 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6704 previous group instead."
6705   (interactive "P")
6706   ;; Stop pre-fetching.
6707   (gnus-async-halt-prefetch)
6708   (let ((current-group gnus-newsgroup-name)
6709         (current-buffer (current-buffer))
6710         entered)
6711     ;; First we semi-exit this group to update Xrefs and all variables.
6712     ;; We can't do a real exit, because the window conf must remain
6713     ;; the same in case the user is prompted for info, and we don't
6714     ;; want the window conf to change before that...
6715     (gnus-summary-exit t)
6716     (while (not entered)
6717       ;; Then we find what group we are supposed to enter.
6718       (set-buffer gnus-group-buffer)
6719       (gnus-group-jump-to-group current-group)
6720       (setq target-group
6721             (or target-group
6722                 (if (eq gnus-keep-same-level 'best)
6723                     (gnus-summary-best-group gnus-newsgroup-name)
6724                   (gnus-summary-search-group backward gnus-keep-same-level))))
6725       (if (not target-group)
6726           ;; There are no further groups, so we return to the group
6727           ;; buffer.
6728           (progn
6729             (gnus-message 5 "Returning to the group buffer")
6730             (setq entered t)
6731             (when (gnus-buffer-live-p current-buffer)
6732               (set-buffer current-buffer)
6733               (gnus-summary-exit))
6734             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6735         ;; We try to enter the target group.
6736         (gnus-group-jump-to-group target-group)
6737         (let ((unreads (gnus-group-group-unread)))
6738           (if (and (or (eq t unreads)
6739                        (and unreads (not (zerop unreads))))
6740                    (gnus-summary-read-group
6741                     target-group nil no-article
6742                     (and (buffer-name current-buffer) current-buffer)
6743                     nil backward))
6744               (setq entered t)
6745             (setq current-group target-group
6746                   target-group nil)))))))
6747
6748 (defun gnus-summary-prev-group (&optional no-article)
6749   "Exit current newsgroup and then select previous unread newsgroup.
6750 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6751   (interactive "P")
6752   (gnus-summary-next-group no-article nil t))
6753
6754 ;; Walking around summary lines.
6755
6756 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6757   "Go to the first subject satisfying any non-nil constraint.
6758 If UNREAD is non-nil, the article should be unread.
6759 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6760 If UNSEED is non-nil, the article should be unseen.
6761 Returns the article selected or nil if there are no matching articles."
6762   (interactive "P")
6763   (cond
6764    ;; Empty summary.
6765    ((null gnus-newsgroup-data)
6766     (gnus-message 3 "No articles in the group")
6767     nil)
6768    ;; Pick the first article.
6769    ((not (or unread undownloaded unseen))
6770     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6771     (gnus-data-number (car gnus-newsgroup-data)))
6772    ;; Find the first unread article.
6773    (t
6774     (let ((data gnus-newsgroup-data))
6775       (while (and data
6776                   (let ((num (gnus-data-number (car data))))
6777                     (or (memq num gnus-newsgroup-unfetched)
6778                         (not (or (and unread
6779                                       (memq num gnus-newsgroup-unreads))
6780                                  (and undownloaded
6781                                       (memq num gnus-newsgroup-undownloaded))
6782                                  (and unseen
6783                                       (memq num gnus-newsgroup-unseen)))))))
6784         (setq data (cdr data)))
6785       (prog1 
6786           (if data
6787               (progn
6788                 (goto-char (gnus-data-pos (car data)))
6789                 (gnus-data-number (car data)))
6790             (gnus-message 3 "No more%s articles"
6791                           (let* ((r (when unread " unread"))
6792                                  (d (when undownloaded " undownloaded"))
6793                                  (s (when unseen " unseen"))
6794                                  (l (delq nil (list r d s))))
6795                             (cond ((= 3 (length l))
6796                                    (concat r "," d ", or" s))
6797                                   ((= 2 (length l))
6798                                    (concat (car l) ", or" (cadr l)))
6799                                   ((= 1 (length l))
6800                                    (car l))
6801                                   (t
6802                                    ""))))
6803             nil
6804             )
6805         (gnus-summary-position-point))))))
6806
6807 (defun gnus-summary-next-subject (n &optional unread dont-display)
6808   "Go to next N'th summary line.
6809 If N is negative, go to the previous N'th subject line.
6810 If UNREAD is non-nil, only unread articles are selected.
6811 The difference between N and the actual number of steps taken is
6812 returned."
6813   (interactive "p")
6814   (let ((backward (< n 0))
6815         (n (abs n)))
6816     (while (and (> n 0)
6817                 (if backward
6818                     (gnus-summary-find-prev unread)
6819                   (gnus-summary-find-next unread)))
6820       (unless (zerop (setq n (1- n)))
6821         (gnus-summary-show-thread)))
6822     (when (/= 0 n)
6823       (gnus-message 7 "No more%s articles"
6824                     (if unread " unread" "")))
6825     (unless dont-display
6826       (gnus-summary-recenter)
6827       (gnus-summary-position-point))
6828     n))
6829
6830 (defun gnus-summary-next-unread-subject (n)
6831   "Go to next N'th unread summary line."
6832   (interactive "p")
6833   (gnus-summary-next-subject n t))
6834
6835 (defun gnus-summary-prev-subject (n &optional unread)
6836   "Go to previous N'th summary line.
6837 If optional argument UNREAD is non-nil, only unread article is selected."
6838   (interactive "p")
6839   (gnus-summary-next-subject (- n) unread))
6840
6841 (defun gnus-summary-prev-unread-subject (n)
6842   "Go to previous N'th unread summary line."
6843   (interactive "p")
6844   (gnus-summary-next-subject (- n) t))
6845
6846 (defun gnus-summary-goto-subjects (articles)
6847   "Insert the subject header for ARTICLES in the current buffer."
6848   (save-excursion
6849     (dolist (article articles)
6850       (gnus-summary-goto-subject article t)))
6851   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6852   (gnus-summary-position-point))
6853  
6854 (defun gnus-summary-goto-subject (article &optional force silent)
6855   "Go the subject line of ARTICLE.
6856 If FORCE, also allow jumping to articles not currently shown."
6857   (interactive "nArticle number: ")
6858   (unless (numberp article)
6859     (error "Article %s is not a number" article))
6860   (let ((b (point))
6861         (data (gnus-data-find article)))
6862     ;; We read in the article if we have to.
6863     (and (not data)
6864          force
6865          (gnus-summary-insert-subject
6866           article
6867           (if (or (numberp force) (vectorp force)) force)
6868           t)
6869          (setq data (gnus-data-find article)))
6870     (goto-char b)
6871     (if (not data)
6872         (progn
6873           (unless silent
6874             (gnus-message 3 "Can't find article %d" article))
6875           nil)
6876       (let ((pt (gnus-data-pos data)))
6877         (goto-char pt)
6878         (gnus-summary-set-article-display-arrow pt))
6879       (gnus-summary-position-point)
6880       article)))
6881
6882 ;; Walking around summary lines with displaying articles.
6883
6884 (defun gnus-summary-expand-window (&optional arg)
6885   "Make the summary buffer take up the entire Emacs frame.
6886 Given a prefix, will force an `article' buffer configuration."
6887   (interactive "P")
6888   (if arg
6889       (gnus-configure-windows 'article 'force)
6890     (gnus-configure-windows 'summary 'force)))
6891
6892 (defun gnus-summary-display-article (article &optional all-header)
6893   "Display ARTICLE in article buffer."
6894   (when (gnus-buffer-live-p gnus-article-buffer)
6895     (with-current-buffer gnus-article-buffer
6896       (mm-enable-multibyte)))
6897   (gnus-set-global-variables)
6898   (when (gnus-buffer-live-p gnus-article-buffer)
6899     (with-current-buffer gnus-article-buffer
6900       (setq gnus-article-charset gnus-newsgroup-charset)
6901       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6902       (mm-enable-multibyte)))
6903   (if (null article)
6904       nil
6905     (prog1
6906         (if gnus-summary-display-article-function
6907             (funcall gnus-summary-display-article-function article all-header)
6908           (gnus-article-prepare article all-header))
6909       (gnus-run-hooks 'gnus-select-article-hook)
6910       (when (and gnus-current-article
6911                  (not (zerop gnus-current-article)))
6912         (gnus-summary-goto-subject gnus-current-article))
6913       (gnus-summary-recenter)
6914       (when (and gnus-use-trees gnus-show-threads)
6915         (gnus-possibly-generate-tree article)
6916         (gnus-highlight-selected-tree article))
6917       ;; Successfully display article.
6918       (gnus-article-set-window-start
6919        (cdr (assq article gnus-newsgroup-bookmarks))))))
6920
6921 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6922   "Select the current article.
6923 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6924 non-nil, the article will be re-fetched even if it already present in
6925 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6926 be displayed."
6927   ;; Make sure we are in the summary buffer to work around bbdb bug.
6928   (unless (eq major-mode 'gnus-summary-mode)
6929     (set-buffer gnus-summary-buffer))
6930   (let ((article (or article (gnus-summary-article-number)))
6931         (all-headers (not (not all-headers))) ;Must be t or nil.
6932         gnus-summary-display-article-function)
6933     (and (not pseudo)
6934          (gnus-summary-article-pseudo-p article)
6935          (error "This is a pseudo-article"))
6936     (save-excursion
6937       (set-buffer gnus-summary-buffer)
6938       (if (or (and gnus-single-article-buffer
6939                    (or (null gnus-current-article)
6940                        (null gnus-article-current)
6941                        (null (get-buffer gnus-article-buffer))
6942                        (not (eq article (cdr gnus-article-current)))
6943                        (not (equal (car gnus-article-current)
6944                                    gnus-newsgroup-name))))
6945               (and (not gnus-single-article-buffer)
6946                    (or (null gnus-current-article)
6947                        (not (eq gnus-current-article article))))
6948               force)
6949           ;; The requested article is different from the current article.
6950           (progn
6951             (gnus-summary-display-article article all-headers)
6952             (when (gnus-buffer-live-p gnus-article-buffer)
6953               (with-current-buffer gnus-article-buffer
6954                 (if (not gnus-article-decoded-p) ;; a local variable
6955                     (mm-disable-multibyte))))
6956             (gnus-article-set-window-start
6957              (cdr (assq article gnus-newsgroup-bookmarks)))
6958             article)
6959         'old))))
6960
6961 (defun gnus-summary-force-verify-and-decrypt ()
6962   "Display buttons for signed/encrypted parts and verify/decrypt them."
6963   (interactive)
6964   (let ((mm-verify-option 'known)
6965         (mm-decrypt-option 'known)
6966         (gnus-buttonized-mime-types (append (list "multipart/signed"
6967                                                   "multipart/encrypted")
6968                                             gnus-buttonized-mime-types)))
6969     (gnus-summary-select-article nil 'force)))
6970
6971 (defun gnus-summary-set-current-mark (&optional current-mark)
6972   "Obsolete function."
6973   nil)
6974
6975 (defun gnus-summary-next-article (&optional unread subject backward push)
6976   "Select the next article.
6977 If UNREAD, only unread articles are selected.
6978 If SUBJECT, only articles with SUBJECT are selected.
6979 If BACKWARD, the previous article is selected instead of the next."
6980   (interactive "P")
6981   (cond
6982    ;; Is there such an article?
6983    ((and (gnus-summary-search-forward unread subject backward)
6984          (or (gnus-summary-display-article (gnus-summary-article-number))
6985              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6986     (gnus-summary-position-point))
6987    ;; If not, we try the first unread, if that is wanted.
6988    ((and subject
6989          gnus-auto-select-same
6990          (gnus-summary-first-unread-article))
6991     (gnus-summary-position-point)
6992     (gnus-message 6 "Wrapped"))
6993    ;; Try to get next/previous article not displayed in this group.
6994    ((and gnus-auto-extend-newsgroup
6995          (not unread) (not subject))
6996     (gnus-summary-goto-article
6997      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6998      nil (count-lines (point-min) (point))))
6999    ;; Go to next/previous group.
7000    (t
7001     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7002       (gnus-summary-jump-to-group gnus-newsgroup-name))
7003     (let ((cmd last-command-char)
7004           (point
7005            (save-excursion
7006              (set-buffer gnus-group-buffer)
7007              (point)))
7008           (group
7009            (if (eq gnus-keep-same-level 'best)
7010                (gnus-summary-best-group gnus-newsgroup-name)
7011              (gnus-summary-search-group backward gnus-keep-same-level))))
7012       ;; For some reason, the group window gets selected.  We change
7013       ;; it back.
7014       (select-window (get-buffer-window (current-buffer)))
7015       ;; Select next unread newsgroup automagically.
7016       (cond
7017        ((or (not gnus-auto-select-next)
7018             (not cmd))
7019         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7020        ((or (eq gnus-auto-select-next 'quietly)
7021             (and (eq gnus-auto-select-next 'slightly-quietly)
7022                  push)
7023             (and (eq gnus-auto-select-next 'almost-quietly)
7024                  (gnus-summary-last-article-p)))
7025         ;; Select quietly.
7026         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7027             (gnus-summary-exit)
7028           (gnus-message 7 "No more%s articles (%s)..."
7029                         (if unread " unread" "")
7030                         (if group (concat "selecting " group)
7031                           "exiting"))
7032           (gnus-summary-next-group nil group backward)))
7033        (t
7034         (when (gnus-key-press-event-p last-input-event)
7035           (gnus-summary-walk-group-buffer
7036            gnus-newsgroup-name cmd unread backward point))))))))
7037
7038 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7039   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7040                       (?\C-p (gnus-group-prev-unread-group 1))))
7041         (cursor-in-echo-area t)
7042         keve key group ended prompt)
7043     (save-excursion
7044       (set-buffer gnus-group-buffer)
7045       (goto-char start)
7046       (setq group
7047             (if (eq gnus-keep-same-level 'best)
7048                 (gnus-summary-best-group gnus-newsgroup-name)
7049               (gnus-summary-search-group backward gnus-keep-same-level))))
7050     (while (not ended)
7051       (setq prompt
7052             (format
7053              "No more%s articles%s " (if unread " unread" "")
7054              (if (and group
7055                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7056                  (format " (Type %s for %s [%s])"
7057                          (single-key-description cmd) group
7058                          (car (gnus-gethash group gnus-newsrc-hashtb)))
7059                (format " (Type %s to exit %s)"
7060                        (single-key-description cmd)
7061                        gnus-newsgroup-name))))
7062       ;; Confirm auto selection.
7063       (setq key (car (setq keve (gnus-read-event-char prompt)))
7064             ended t)
7065       (cond
7066        ((assq key keystrokes)
7067         (let ((obuf (current-buffer)))
7068           (switch-to-buffer gnus-group-buffer)
7069           (when group
7070             (gnus-group-jump-to-group group))
7071           (eval (cadr (assq key keystrokes)))
7072           (setq group (gnus-group-group-name))
7073           (switch-to-buffer obuf))
7074         (setq ended nil))
7075        ((equal key cmd)
7076         (if (or (not group)
7077                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7078             (gnus-summary-exit)
7079           (gnus-summary-next-group nil group backward)))
7080        (t
7081         (push (cdr keve) unread-command-events))))))
7082
7083 (defun gnus-summary-next-unread-article ()
7084   "Select unread article after current one."
7085   (interactive)
7086   (gnus-summary-next-article
7087    (or (not (eq gnus-summary-goto-unread 'never))
7088        (gnus-summary-last-article-p (gnus-summary-article-number)))
7089    (and gnus-auto-select-same
7090         (gnus-summary-article-subject))))
7091
7092 (defun gnus-summary-prev-article (&optional unread subject)
7093   "Select the article after the current one.
7094 If UNREAD is non-nil, only unread articles are selected."
7095   (interactive "P")
7096   (gnus-summary-next-article unread subject t))
7097
7098 (defun gnus-summary-prev-unread-article ()
7099   "Select unread article before current one."
7100   (interactive)
7101   (gnus-summary-prev-article
7102    (or (not (eq gnus-summary-goto-unread 'never))
7103        (gnus-summary-first-article-p (gnus-summary-article-number)))
7104    (and gnus-auto-select-same
7105         (gnus-summary-article-subject))))
7106
7107 (defun gnus-summary-next-page (&optional lines circular stop)
7108   "Show next page of the selected article.
7109 If at the end of the current article, select the next article.
7110 LINES says how many lines should be scrolled up.
7111
7112 If CIRCULAR is non-nil, go to the start of the article instead of
7113 selecting the next article when reaching the end of the current
7114 article.
7115
7116 If STOP is non-nil, just stop when reaching the end of the message."
7117   (interactive "P")
7118   (setq gnus-summary-buffer (current-buffer))
7119   (gnus-set-global-variables)
7120   (let ((article (gnus-summary-article-number))
7121         (article-window (get-buffer-window gnus-article-buffer t))
7122         endp)
7123     ;; If the buffer is empty, we have no article.
7124     (unless article
7125       (error "No article to select"))
7126     (gnus-configure-windows 'article)
7127     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7128         (if (and (eq gnus-summary-goto-unread 'never)
7129                  (not (gnus-summary-last-article-p article)))
7130             (gnus-summary-next-article)
7131           (gnus-summary-next-unread-article))
7132       (if (or (null gnus-current-article)
7133               (null gnus-article-current)
7134               (/= article (cdr gnus-article-current))
7135               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7136           ;; Selected subject is different from current article's.
7137           (gnus-summary-display-article article)
7138         (when article-window
7139           (gnus-eval-in-buffer-window gnus-article-buffer
7140             (setq endp (or (gnus-article-next-page lines)
7141                            (gnus-article-only-boring-p))))
7142           (when endp
7143             (cond (stop
7144                    (gnus-message 3 "End of message"))
7145                   (circular
7146                    (gnus-summary-beginning-of-article))
7147                   (lines
7148                    (gnus-message 3 "End of message"))
7149                   ((null lines)
7150                    (if (and (eq gnus-summary-goto-unread 'never)
7151                             (not (gnus-summary-last-article-p article)))
7152                        (gnus-summary-next-article)
7153                      (gnus-summary-next-unread-article))))))))
7154     (gnus-summary-recenter)
7155     (gnus-summary-position-point)))
7156
7157 (defun gnus-summary-prev-page (&optional lines move)
7158   "Show previous page of selected article.
7159 Argument LINES specifies lines to be scrolled down.
7160 If MOVE, move to the previous unread article if point is at
7161 the beginning of the buffer."
7162   (interactive "P")
7163   (let ((article (gnus-summary-article-number))
7164         (article-window (get-buffer-window gnus-article-buffer t))
7165         endp)
7166     (gnus-configure-windows 'article)
7167     (if (or (null gnus-current-article)
7168             (null gnus-article-current)
7169             (/= article (cdr gnus-article-current))
7170             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7171         ;; Selected subject is different from current article's.
7172         (gnus-summary-display-article article)
7173       (gnus-summary-recenter)
7174       (when article-window
7175         (gnus-eval-in-buffer-window gnus-article-buffer
7176           (setq endp (gnus-article-prev-page lines)))
7177         (when (and move endp)
7178           (cond (lines
7179                  (gnus-message 3 "Beginning of message"))
7180                 ((null lines)
7181                  (if (and (eq gnus-summary-goto-unread 'never)
7182                           (not (gnus-summary-first-article-p article)))
7183                      (gnus-summary-prev-article)
7184                    (gnus-summary-prev-unread-article))))))))
7185   (gnus-summary-position-point))
7186
7187 (defun gnus-summary-prev-page-or-article (&optional lines)
7188   "Show previous page of selected article.
7189 Argument LINES specifies lines to be scrolled down.
7190 If at the beginning of the article, go to the next article."
7191   (interactive "P")
7192   (gnus-summary-prev-page lines t))
7193
7194 (defun gnus-summary-scroll-up (lines)
7195   "Scroll up (or down) one line current article.
7196 Argument LINES specifies lines to be scrolled up (or down if negative)."
7197   (interactive "p")
7198   (gnus-configure-windows 'article)
7199   (gnus-summary-show-thread)
7200   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7201     (gnus-eval-in-buffer-window gnus-article-buffer
7202       (cond ((> lines 0)
7203              (when (gnus-article-next-page lines)
7204                (gnus-message 3 "End of message")))
7205             ((< lines 0)
7206              (gnus-article-prev-page (- lines))))))
7207   (gnus-summary-recenter)
7208   (gnus-summary-position-point))
7209
7210 (defun gnus-summary-scroll-down (lines)
7211   "Scroll down (or up) one line current article.
7212 Argument LINES specifies lines to be scrolled down (or up if negative)."
7213   (interactive "p")
7214   (gnus-summary-scroll-up (- lines)))
7215
7216 (defun gnus-summary-next-same-subject ()
7217   "Select next article which has the same subject as current one."
7218   (interactive)
7219   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7220
7221 (defun gnus-summary-prev-same-subject ()
7222   "Select previous article which has the same subject as current one."
7223   (interactive)
7224   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7225
7226 (defun gnus-summary-next-unread-same-subject ()
7227   "Select next unread article which has the same subject as current one."
7228   (interactive)
7229   (gnus-summary-next-article t (gnus-summary-article-subject)))
7230
7231 (defun gnus-summary-prev-unread-same-subject ()
7232   "Select previous unread article which has the same subject as current one."
7233   (interactive)
7234   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7235
7236 (defun gnus-summary-first-unread-article ()
7237   "Select the first unread article.
7238 Return nil if there are no unread articles."
7239   (interactive)
7240   (prog1
7241       (when (gnus-summary-first-subject t)
7242         (gnus-summary-show-thread)
7243         (gnus-summary-first-subject t)
7244         (gnus-summary-display-article (gnus-summary-article-number)))
7245     (gnus-summary-position-point)))
7246
7247 (defun gnus-summary-first-unread-subject ()
7248   "Place the point on the subject line of the first unread article.
7249 Return nil if there are no unread articles."
7250   (interactive)
7251   (prog1
7252       (when (gnus-summary-first-subject t)
7253         (gnus-summary-show-thread)
7254         (gnus-summary-first-subject t))
7255     (gnus-summary-position-point)))
7256
7257 (defun gnus-summary-first-unseen-subject ()
7258   "Place the point on the subject line of the first unseen article.
7259 Return nil if there are no unseen articles."
7260   (interactive)
7261   (prog1
7262       (when (gnus-summary-first-subject nil nil t)
7263         (gnus-summary-show-thread)
7264         (gnus-summary-first-subject nil nil t))
7265     (gnus-summary-position-point)))
7266
7267 (defun gnus-summary-first-unseen-or-unread-subject ()
7268   "Place the point on the subject line of the first unseen article or,
7269 if all article have been seen, on the subject line of the first unread
7270 article."
7271   (interactive)
7272   (prog1
7273       (unless (when (gnus-summary-first-subject nil nil t)
7274                 (gnus-summary-show-thread)
7275                 (gnus-summary-first-subject nil nil t))
7276         (when (gnus-summary-first-subject t)
7277           (gnus-summary-show-thread)
7278           (gnus-summary-first-subject t)))
7279     (gnus-summary-position-point)))
7280
7281 (defun gnus-summary-first-article ()
7282   "Select the first article.
7283 Return nil if there are no articles."
7284   (interactive)
7285   (prog1
7286       (when (gnus-summary-first-subject)
7287         (gnus-summary-show-thread)
7288         (gnus-summary-first-subject)
7289         (gnus-summary-display-article (gnus-summary-article-number)))
7290     (gnus-summary-position-point)))
7291
7292 (defun gnus-summary-best-unread-article (&optional arg)
7293   "Select the unread article with the highest score.
7294 If given a prefix argument, select the next unread article that has a
7295 score higher than the default score."
7296   (interactive "P")
7297   (let ((article (if arg
7298                      (gnus-summary-better-unread-subject)
7299                    (gnus-summary-best-unread-subject))))
7300     (if article
7301         (gnus-summary-goto-article article)
7302       (error "No unread articles"))))
7303
7304 (defun gnus-summary-best-unread-subject ()
7305   "Select the unread subject with the highest score."
7306   (interactive)
7307   (let ((best -1000000)
7308         (data gnus-newsgroup-data)
7309         article score)
7310     (while data
7311       (and (gnus-data-unread-p (car data))
7312            (> (setq score
7313                     (gnus-summary-article-score (gnus-data-number (car data))))
7314               best)
7315            (setq best score
7316                  article (gnus-data-number (car data))))
7317       (setq data (cdr data)))
7318     (when article
7319       (gnus-summary-goto-subject article))
7320     (gnus-summary-position-point)
7321     article))
7322
7323 (defun gnus-summary-better-unread-subject ()
7324   "Select the first unread subject that has a score over the default score."
7325   (interactive)
7326   (let ((data gnus-newsgroup-data)
7327         article score)
7328     (while (and (setq article (gnus-data-number (car data)))
7329                 (or (gnus-data-read-p (car data))
7330                     (not (> (gnus-summary-article-score article)
7331                             gnus-summary-default-score))))
7332       (setq data (cdr data)))
7333     (when article
7334       (gnus-summary-goto-subject article))
7335     (gnus-summary-position-point)
7336     article))
7337
7338 (defun gnus-summary-last-subject ()
7339   "Go to the last displayed subject line in the group."
7340   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7341     (when article
7342       (gnus-summary-goto-subject article))))
7343
7344 (defun gnus-summary-goto-article (article &optional all-headers force)
7345   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7346 If ALL-HEADERS is non-nil, no header lines are hidden.
7347 If FORCE, go to the article even if it isn't displayed.  If FORCE
7348 is a number, it is the line the article is to be displayed on."
7349   (interactive
7350    (list
7351     (completing-read
7352      "Article number or Message-ID: "
7353      (mapcar (lambda (number) (list (int-to-string number)))
7354              gnus-newsgroup-limit))
7355     current-prefix-arg
7356     t))
7357   (prog1
7358       (if (and (stringp article)
7359                (string-match "@" article))
7360           (gnus-summary-refer-article article)
7361         (when (stringp article)
7362           (setq article (string-to-number article)))
7363         (if (gnus-summary-goto-subject article force)
7364             (gnus-summary-display-article article all-headers)
7365           (gnus-message 4 "Couldn't go to article %s" article) nil))
7366     (gnus-summary-position-point)))
7367
7368 (defun gnus-summary-goto-last-article ()
7369   "Go to the previously read article."
7370   (interactive)
7371   (prog1
7372       (when gnus-last-article
7373         (gnus-summary-goto-article gnus-last-article nil t))
7374     (gnus-summary-position-point)))
7375
7376 (defun gnus-summary-pop-article (number)
7377   "Pop one article off the history and go to the previous.
7378 NUMBER articles will be popped off."
7379   (interactive "p")
7380   (let (to)
7381     (setq gnus-newsgroup-history
7382           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7383     (if to
7384         (gnus-summary-goto-article (car to) nil t)
7385       (error "Article history empty")))
7386   (gnus-summary-position-point))
7387
7388 ;; Summary commands and functions for limiting the summary buffer.
7389
7390 (defun gnus-summary-limit-to-articles (n)
7391   "Limit the summary buffer to the next N articles.
7392 If not given a prefix, use the process marked articles instead."
7393   (interactive "P")
7394   (prog1
7395       (let ((articles (gnus-summary-work-articles n)))
7396         (setq gnus-newsgroup-processable nil)
7397         (gnus-summary-limit articles))
7398     (gnus-summary-position-point)))
7399
7400 (defun gnus-summary-pop-limit (&optional total)
7401   "Restore the previous limit.
7402 If given a prefix, remove all limits."
7403   (interactive "P")
7404   (when total
7405     (setq gnus-newsgroup-limits
7406           (list (mapcar (lambda (h) (mail-header-number h))
7407                         gnus-newsgroup-headers))))
7408   (unless gnus-newsgroup-limits
7409     (error "No limit to pop"))
7410   (prog1
7411       (gnus-summary-limit nil 'pop)
7412     (gnus-summary-position-point)))
7413
7414 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7415   "Limit the summary buffer to articles that have subjects that match a regexp.
7416 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7417   (interactive
7418    (list (read-string (if current-prefix-arg
7419                           "Exclude subject (regexp): "
7420                         "Limit to subject (regexp): "))
7421          nil current-prefix-arg))
7422   (unless header
7423     (setq header "subject"))
7424   (when (not (equal "" subject))
7425     (prog1
7426         (let ((articles (gnus-summary-find-matching
7427                          (or header "subject") subject 'all nil nil
7428                          not-matching)))
7429           (unless articles
7430             (error "Found no matches for \"%s\"" subject))
7431           (gnus-summary-limit articles))
7432       (gnus-summary-position-point))))
7433
7434 (defun gnus-summary-limit-to-author (from &optional not-matching)
7435   "Limit the summary buffer to articles that have authors that match a regexp.
7436 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7437   (interactive
7438    (list (read-string (if current-prefix-arg
7439                           "Exclude author (regexp): "
7440                         "Limit to author (regexp): "))
7441          current-prefix-arg))
7442   (gnus-summary-limit-to-subject from "from" not-matching))
7443
7444 (defun gnus-summary-limit-to-age (age &optional younger-p)
7445   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7446 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7447 articles that are younger than AGE days."
7448   (interactive
7449    (let ((younger current-prefix-arg)
7450          (days-got nil)
7451          days)
7452      (while (not days-got)
7453        (setq days (if younger
7454                       (read-string "Limit to articles younger than (in days, older when negative): ")
7455                     (read-string
7456                      "Limit to articles older than (in days, younger when negative): ")))
7457        (when (> (length days) 0)
7458          (setq days (read days)))
7459        (if (numberp days)
7460            (progn
7461              (setq days-got t)
7462              (if (< days 0)
7463                  (progn
7464                    (setq younger (not younger))
7465                    (setq days (* days -1)))))
7466          (message "Please enter a number.")
7467          (sleep-for 1)))
7468      (list days younger)))
7469   (prog1
7470       (let ((data gnus-newsgroup-data)
7471             (cutoff (days-to-time age))
7472             articles d date is-younger)
7473         (while (setq d (pop data))
7474           (when (and (vectorp (gnus-data-header d))
7475                      (setq date (mail-header-date (gnus-data-header d))))
7476             (setq is-younger (time-less-p
7477                               (time-since (condition-case ()
7478                                               (date-to-time date)
7479                                             (error '(0 0))))
7480                               cutoff))
7481             (when (if younger-p
7482                       is-younger
7483                     (not is-younger))
7484               (push (gnus-data-number d) articles))))
7485         (gnus-summary-limit (nreverse articles)))
7486     (gnus-summary-position-point)))
7487
7488 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7489   "Limit the summary buffer to articles that match an 'extra' header."
7490   (interactive
7491    (let ((header
7492           (intern
7493            (gnus-completing-read-with-default
7494             (symbol-name (car gnus-extra-headers))
7495             (if current-prefix-arg
7496                 "Exclude extra header:"
7497               "Limit extra header:")
7498             (mapcar (lambda (x)
7499                       (cons (symbol-name x) x))
7500                     gnus-extra-headers)
7501             nil
7502             t))))
7503      (list header
7504            (read-string (format "%s header %s (regexp): "
7505                                 (if current-prefix-arg "Exclude" "Limit to")
7506                                 header))
7507            current-prefix-arg)))
7508   (when (not (equal "" regexp))
7509     (prog1
7510         (let ((articles (gnus-summary-find-matching
7511                          (cons 'extra header) regexp 'all nil nil
7512                          not-matching)))
7513           (unless articles
7514             (error "Found no matches for \"%s\"" regexp))
7515           (gnus-summary-limit articles))
7516       (gnus-summary-position-point))))
7517
7518 (defun gnus-summary-limit-to-display-predicate ()
7519   "Limit the summary buffer to the predicated in the `display' group parameter."
7520   (interactive)
7521   (unless gnus-newsgroup-display
7522     (error "There is no `display' group parameter"))
7523   (let (articles)
7524     (dolist (number gnus-newsgroup-articles)
7525       (when (funcall gnus-newsgroup-display)
7526         (push number articles)))
7527     (gnus-summary-limit articles))
7528   (gnus-summary-position-point))
7529
7530 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7531 (make-obsolete
7532  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7533
7534 (defun gnus-summary-limit-to-unread (&optional all)
7535   "Limit the summary buffer to articles that are not marked as read.
7536 If ALL is non-nil, limit strictly to unread articles."
7537   (interactive "P")
7538   (if all
7539       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7540     (gnus-summary-limit-to-marks
7541      ;; Concat all the marks that say that an article is read and have
7542      ;; those removed.
7543      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7544            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7545            gnus-low-score-mark gnus-expirable-mark
7546            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7547            gnus-duplicate-mark gnus-souped-mark)
7548      'reverse)))
7549
7550 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7551 (make-obsolete 'gnus-summary-delete-marked-with
7552                'gnus-summary-limit-exclude-marks)
7553
7554 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7555   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7556 If REVERSE, limit the summary buffer to articles that are marked
7557 with MARKS.  MARKS can either be a string of marks or a list of marks.
7558 Returns how many articles were removed."
7559   (interactive "sMarks: ")
7560   (gnus-summary-limit-to-marks marks t))
7561
7562 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7563   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7564 If REVERSE (the prefix), limit the summary buffer to articles that are
7565 not marked with MARKS.  MARKS can either be a string of marks or a
7566 list of marks.
7567 Returns how many articles were removed."
7568   (interactive "sMarks: \nP")
7569   (prog1
7570       (let ((data gnus-newsgroup-data)
7571             (marks (if (listp marks) marks
7572                      (append marks nil))) ; Transform to list.
7573             articles)
7574         (while data
7575           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7576                   (memq (gnus-data-mark (car data)) marks))
7577             (push (gnus-data-number (car data)) articles))
7578           (setq data (cdr data)))
7579         (gnus-summary-limit articles))
7580     (gnus-summary-position-point)))
7581
7582 (defun gnus-summary-limit-to-score (score)
7583   "Limit to articles with score at or above SCORE."
7584   (interactive "NLimit to articles with score of at least: ")
7585   (let ((data gnus-newsgroup-data)
7586         articles)
7587     (while data
7588       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7589                 score)
7590         (push (gnus-data-number (car data)) articles))
7591       (setq data (cdr data)))
7592     (prog1
7593         (gnus-summary-limit articles)
7594       (gnus-summary-position-point))))
7595
7596 (defun gnus-summary-limit-to-unseen ()
7597   "Limit to unseen articles."
7598   (interactive)
7599   (prog1
7600       (gnus-summary-limit gnus-newsgroup-unseen)
7601     (gnus-summary-position-point)))
7602
7603 (defun gnus-summary-limit-include-thread (id)
7604   "Display all the hidden articles that is in the thread with ID in it.
7605 When called interactively, ID is the Message-ID of the current
7606 article."
7607   (interactive (list (mail-header-id (gnus-summary-article-header))))
7608   (let ((articles (gnus-articles-in-thread
7609                    (gnus-id-to-thread (gnus-root-id id)))))
7610     (prog1
7611         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7612       (gnus-summary-limit-include-matching-articles
7613        "subject"
7614        (regexp-quote (gnus-simplify-subject-re
7615                       (mail-header-subject (gnus-id-to-header id)))))
7616       (gnus-summary-position-point))))
7617
7618 (defun gnus-summary-limit-include-matching-articles (header regexp)
7619   "Display all the hidden articles that have HEADERs that match REGEXP."
7620   (interactive (list (read-string "Match on header: ")
7621                      (read-string "Regexp: ")))
7622   (let ((articles (gnus-find-matching-articles header regexp)))
7623     (prog1
7624         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7625       (gnus-summary-position-point))))
7626
7627 (defun gnus-summary-insert-dormant-articles ()
7628   "Insert all the dormat articles for this group into the current buffer."
7629   (interactive)
7630   (let ((gnus-verbose (max 6 gnus-verbose)))
7631     (if (not gnus-newsgroup-dormant)
7632         (gnus-message 3 "No cached articles for this group")
7633       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7634
7635 (defun gnus-summary-limit-include-dormant ()
7636   "Display all the hidden articles that are marked as dormant.
7637 Note that this command only works on a subset of the articles currently
7638 fetched for this group."
7639   (interactive)
7640   (unless gnus-newsgroup-dormant
7641     (error "There are no dormant articles in this group"))
7642   (prog1
7643       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7644     (gnus-summary-position-point)))
7645
7646 (defun gnus-summary-limit-exclude-dormant ()
7647   "Hide all dormant articles."
7648   (interactive)
7649   (prog1
7650       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7651     (gnus-summary-position-point)))
7652
7653 (defun gnus-summary-limit-exclude-childless-dormant ()
7654   "Hide all dormant articles that have no children."
7655   (interactive)
7656   (let ((data (gnus-data-list t))
7657         articles d children)
7658     ;; Find all articles that are either not dormant or have
7659     ;; children.
7660     (while (setq d (pop data))
7661       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7662                 (and (setq children
7663                            (gnus-article-children (gnus-data-number d)))
7664                      (let (found)
7665                        (while children
7666                          (when (memq (car children) articles)
7667                            (setq children nil
7668                                  found t))
7669                          (pop children))
7670                        found)))
7671         (push (gnus-data-number d) articles)))
7672     ;; Do the limiting.
7673     (prog1
7674         (gnus-summary-limit articles)
7675       (gnus-summary-position-point))))
7676
7677 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7678   "Mark all unread excluded articles as read.
7679 If ALL, mark even excluded ticked and dormants as read."
7680   (interactive "P")
7681   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7682   (let ((articles (gnus-sorted-ndifference
7683                    (sort
7684                     (mapcar (lambda (h) (mail-header-number h))
7685                             gnus-newsgroup-headers)
7686                     '<)
7687                    gnus-newsgroup-limit))
7688         article)
7689     (setq gnus-newsgroup-unreads
7690           (gnus-sorted-intersection gnus-newsgroup-unreads
7691                                     gnus-newsgroup-limit))
7692     (if all
7693         (setq gnus-newsgroup-dormant nil
7694               gnus-newsgroup-marked nil
7695               gnus-newsgroup-reads
7696               (nconc
7697                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7698                gnus-newsgroup-reads))
7699       (while (setq article (pop articles))
7700         (unless (or (memq article gnus-newsgroup-dormant)
7701                     (memq article gnus-newsgroup-marked))
7702           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7703
7704 (defun gnus-summary-limit (articles &optional pop)
7705   (if pop
7706       ;; We pop the previous limit off the stack and use that.
7707       (setq articles (car gnus-newsgroup-limits)
7708             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7709     ;; We use the new limit, so we push the old limit on the stack.
7710     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7711   ;; Set the limit.
7712   (setq gnus-newsgroup-limit articles)
7713   (let ((total (length gnus-newsgroup-data))
7714         (data (gnus-data-find-list (gnus-summary-article-number)))
7715         (gnus-summary-mark-below nil)   ; Inhibit this.
7716         found)
7717     ;; This will do all the work of generating the new summary buffer
7718     ;; according to the new limit.
7719     (gnus-summary-prepare)
7720     ;; Hide any threads, possibly.
7721     (gnus-summary-maybe-hide-threads)
7722     ;; Try to return to the article you were at, or one in the
7723     ;; neighborhood.
7724     (when data
7725       ;; We try to find some article after the current one.
7726       (while data
7727         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7728           (setq data nil
7729                 found t))
7730         (setq data (cdr data))))
7731     (unless found
7732       ;; If there is no data, that means that we were after the last
7733       ;; article.  The same goes when we can't find any articles
7734       ;; after the current one.
7735       (goto-char (point-max))
7736       (gnus-summary-find-prev))
7737     (gnus-set-mode-line 'summary)
7738     ;; We return how many articles were removed from the summary
7739     ;; buffer as a result of the new limit.
7740     (- total (length gnus-newsgroup-data))))
7741
7742 (defsubst gnus-invisible-cut-children (threads)
7743   (let ((num 0))
7744     (while threads
7745       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7746         (incf num))
7747       (pop threads))
7748     (< num 2)))
7749
7750 (defsubst gnus-cut-thread (thread)
7751   "Go forwards in the thread until we find an article that we want to display."
7752   (when (or (eq gnus-fetch-old-headers 'some)
7753             (eq gnus-fetch-old-headers 'invisible)
7754             (numberp gnus-fetch-old-headers)
7755             (eq gnus-build-sparse-threads 'some)
7756             (eq gnus-build-sparse-threads 'more))
7757     ;; Deal with old-fetched headers and sparse threads.
7758     (while (and
7759             thread
7760             (or
7761              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7762              (gnus-summary-article-ancient-p
7763               (mail-header-number (car thread))))
7764             (if (or (<= (length (cdr thread)) 1)
7765                     (eq gnus-fetch-old-headers 'invisible))
7766                 (setq gnus-newsgroup-limit
7767                       (delq (mail-header-number (car thread))
7768                             gnus-newsgroup-limit)
7769                       thread (cadr thread))
7770               (when (gnus-invisible-cut-children (cdr thread))
7771                 (let ((th (cdr thread)))
7772                   (while th
7773                     (if (memq (mail-header-number (caar th))
7774                               gnus-newsgroup-limit)
7775                         (setq thread (car th)
7776                               th nil)
7777                       (setq th (cdr th))))))))))
7778   thread)
7779
7780 (defun gnus-cut-threads (threads)
7781   "Cut off all uninteresting articles from the beginning of threads."
7782   (when (or (eq gnus-fetch-old-headers 'some)
7783             (eq gnus-fetch-old-headers 'invisible)
7784             (numberp gnus-fetch-old-headers)
7785             (eq gnus-build-sparse-threads 'some)
7786             (eq gnus-build-sparse-threads 'more))
7787     (let ((th threads))
7788       (while th
7789         (setcar th (gnus-cut-thread (car th)))
7790         (setq th (cdr th)))))
7791   ;; Remove nixed out threads.
7792   (delq nil threads))
7793
7794 (defun gnus-summary-initial-limit (&optional show-if-empty)
7795   "Figure out what the initial limit is supposed to be on group entry.
7796 This entails weeding out unwanted dormants, low-scored articles,
7797 fetch-old-headers verbiage, and so on."
7798   ;; Most groups have nothing to remove.
7799   (if (or gnus-inhibit-limiting
7800           (and (null gnus-newsgroup-dormant)
7801                (eq gnus-newsgroup-display 'gnus-not-ignore)
7802                (not (eq gnus-fetch-old-headers 'some))
7803                (not (numberp gnus-fetch-old-headers))
7804                (not (eq gnus-fetch-old-headers 'invisible))
7805                (null gnus-summary-expunge-below)
7806                (not (eq gnus-build-sparse-threads 'some))
7807                (not (eq gnus-build-sparse-threads 'more))
7808                (null gnus-thread-expunge-below)
7809                (not gnus-use-nocem)))
7810       ()                                ; Do nothing.
7811     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7812     (setq gnus-newsgroup-limit nil)
7813     (mapatoms
7814      (lambda (node)
7815        (unless (car (symbol-value node))
7816          ;; These threads have no parents -- they are roots.
7817          (let ((nodes (cdr (symbol-value node)))
7818                thread)
7819            (while nodes
7820              (if (and gnus-thread-expunge-below
7821                       (< (gnus-thread-total-score (car nodes))
7822                          gnus-thread-expunge-below))
7823                  (gnus-expunge-thread (pop nodes))
7824                (setq thread (pop nodes))
7825                (gnus-summary-limit-children thread))))))
7826      gnus-newsgroup-dependencies)
7827     ;; If this limitation resulted in an empty group, we might
7828     ;; pop the previous limit and use it instead.
7829     (when (and (not gnus-newsgroup-limit)
7830                show-if-empty)
7831       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7832     gnus-newsgroup-limit))
7833
7834 (defun gnus-summary-limit-children (thread)
7835   "Return 1 if this subthread is visible and 0 if it is not."
7836   ;; First we get the number of visible children to this thread.  This
7837   ;; is done by recursing down the thread using this function, so this
7838   ;; will really go down to a leaf article first, before slowly
7839   ;; working its way up towards the root.
7840   (when thread
7841     (let* ((max-lisp-eval-depth 5000)
7842            (children
7843            (if (cdr thread)
7844                (apply '+ (mapcar 'gnus-summary-limit-children
7845                                  (cdr thread)))
7846              0))
7847           (number (mail-header-number (car thread)))
7848           score)
7849       (if (and
7850            (not (memq number gnus-newsgroup-marked))
7851            (or
7852             ;; If this article is dormant and has absolutely no visible
7853             ;; children, then this article isn't visible.
7854             (and (memq number gnus-newsgroup-dormant)
7855                  (zerop children))
7856             ;; If this is "fetch-old-headered" and there is no
7857             ;; visible children, then we don't want this article.
7858             (and (or (eq gnus-fetch-old-headers 'some)
7859                      (numberp gnus-fetch-old-headers))
7860                  (gnus-summary-article-ancient-p number)
7861                  (zerop children))
7862             ;; If this is "fetch-old-headered" and `invisible', then
7863             ;; we don't want this article.
7864             (and (eq gnus-fetch-old-headers 'invisible)
7865                  (gnus-summary-article-ancient-p number))
7866             ;; If this is a sparsely inserted article with no children,
7867             ;; we don't want it.
7868             (and (eq gnus-build-sparse-threads 'some)
7869                  (gnus-summary-article-sparse-p number)
7870                  (zerop children))
7871             ;; If we use expunging, and this article is really
7872             ;; low-scored, then we don't want this article.
7873             (when (and gnus-summary-expunge-below
7874                        (< (setq score
7875                                 (or (cdr (assq number gnus-newsgroup-scored))
7876                                     gnus-summary-default-score))
7877                           gnus-summary-expunge-below))
7878               ;; We increase the expunge-tally here, but that has
7879               ;; nothing to do with the limits, really.
7880               (incf gnus-newsgroup-expunged-tally)
7881               ;; We also mark as read here, if that's wanted.
7882               (when (and gnus-summary-mark-below
7883                          (< score gnus-summary-mark-below))
7884                 (setq gnus-newsgroup-unreads
7885                       (delq number gnus-newsgroup-unreads))
7886                 (if gnus-newsgroup-auto-expire
7887                     (push number gnus-newsgroup-expirable)
7888                   (push (cons number gnus-low-score-mark)
7889                         gnus-newsgroup-reads)))
7890               t)
7891             ;; Do the `display' group parameter.
7892             (and gnus-newsgroup-display
7893                  (not (funcall gnus-newsgroup-display)))
7894             ;; Check NoCeM things.
7895             (if (and gnus-use-nocem
7896                      (gnus-nocem-unwanted-article-p
7897                       (mail-header-id (car thread))))
7898                 (progn
7899                   (setq gnus-newsgroup-unreads
7900                         (delq number gnus-newsgroup-unreads))
7901                   t))))
7902           ;; Nope, invisible article.
7903           0
7904         ;; Ok, this article is to be visible, so we add it to the limit
7905         ;; and return 1.
7906         (push number gnus-newsgroup-limit)
7907         1))))
7908
7909 (defun gnus-expunge-thread (thread)
7910   "Mark all articles in THREAD as read."
7911   (let* ((number (mail-header-number (car thread))))
7912     (incf gnus-newsgroup-expunged-tally)
7913     ;; We also mark as read here, if that's wanted.
7914     (setq gnus-newsgroup-unreads
7915           (delq number gnus-newsgroup-unreads))
7916     (if gnus-newsgroup-auto-expire
7917         (push number gnus-newsgroup-expirable)
7918       (push (cons number gnus-low-score-mark)
7919             gnus-newsgroup-reads)))
7920   ;; Go recursively through all subthreads.
7921   (mapcar 'gnus-expunge-thread (cdr thread)))
7922
7923 ;; Summary article oriented commands
7924
7925 (defun gnus-summary-refer-parent-article (n)
7926   "Refer parent article N times.
7927 If N is negative, go to ancestor -N instead.
7928 The difference between N and the number of articles fetched is returned."
7929   (interactive "p")
7930   (let ((skip 1)
7931         error header ref)
7932     (when (not (natnump n))
7933       (setq skip (abs n)
7934             n 1))
7935     (while (and (> n 0)
7936                 (not error))
7937       (setq header (gnus-summary-article-header))
7938       (if (and (eq (mail-header-number header)
7939                    (cdr gnus-article-current))
7940                (equal gnus-newsgroup-name
7941                       (car gnus-article-current)))
7942           ;; If we try to find the parent of the currently
7943           ;; displayed article, then we take a look at the actual
7944           ;; References header, since this is slightly more
7945           ;; reliable than the References field we got from the
7946           ;; server.
7947           (save-excursion
7948             (set-buffer gnus-original-article-buffer)
7949             (nnheader-narrow-to-headers)
7950             (unless (setq ref (message-fetch-field "references"))
7951               (setq ref (message-fetch-field "in-reply-to")))
7952             (widen))
7953         (setq ref
7954               ;; It's not the current article, so we take a bet on
7955               ;; the value we got from the server.
7956               (mail-header-references header)))
7957       (if (and ref
7958                (not (equal ref "")))
7959           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7960             (gnus-message 1 "Couldn't find parent"))
7961         (gnus-message 1 "No references in article %d"
7962                       (gnus-summary-article-number))
7963         (setq error t))
7964       (decf n))
7965     (gnus-summary-position-point)
7966     n))
7967
7968 (defun gnus-summary-refer-references ()
7969   "Fetch all articles mentioned in the References header.
7970 Return the number of articles fetched."
7971   (interactive)
7972   (let ((ref (mail-header-references (gnus-summary-article-header)))
7973         (current (gnus-summary-article-number))
7974         (n 0))
7975     (if (or (not ref)
7976             (equal ref ""))
7977         (error "No References in the current article")
7978       ;; For each Message-ID in the References header...
7979       (while (string-match "<[^>]*>" ref)
7980         (incf n)
7981         ;; ... fetch that article.
7982         (gnus-summary-refer-article
7983          (prog1 (match-string 0 ref)
7984            (setq ref (substring ref (match-end 0))))))
7985       (gnus-summary-goto-subject current)
7986       (gnus-summary-position-point)
7987       n)))
7988
7989 (defun gnus-summary-refer-thread (&optional limit)
7990   "Fetch all articles in the current thread.
7991 If LIMIT (the numerical prefix), fetch that many old headers instead
7992 of what's specified by the `gnus-refer-thread-limit' variable."
7993   (interactive "P")
7994   (let ((id (mail-header-id (gnus-summary-article-header)))
7995         (limit (if limit (prefix-numeric-value limit)
7996                  gnus-refer-thread-limit)))
7997     (unless (eq gnus-fetch-old-headers 'invisible)
7998       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7999       ;; Retrieve the headers and read them in.
8000       (if (eq (if (numberp limit)
8001                   (gnus-retrieve-headers
8002                    (list (min
8003                           (+ (mail-header-number
8004                               (gnus-summary-article-header))
8005                              limit)
8006                           gnus-newsgroup-end))
8007                    gnus-newsgroup-name (* limit 2))
8008                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8009                 ;; headers.
8010                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8011                                        gnus-newsgroup-name limit))
8012               'nov)
8013           (gnus-build-all-threads)
8014         (error "Can't fetch thread from backends that don't support NOV"))
8015       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8016     (gnus-summary-limit-include-thread id)))
8017
8018 (defun gnus-summary-refer-article (message-id)
8019   "Fetch an article specified by MESSAGE-ID."
8020   (interactive "sMessage-ID: ")
8021   (when (and (stringp message-id)
8022              (not (zerop (length message-id))))
8023     ;; Construct the correct Message-ID if necessary.
8024     ;; Suggested by tale@pawl.rpi.edu.
8025     (unless (string-match "^<" message-id)
8026       (setq message-id (concat "<" message-id)))
8027     (unless (string-match ">$" message-id)
8028       (setq message-id (concat message-id ">")))
8029     (let* ((header (gnus-id-to-header message-id))
8030            (sparse (and header
8031                         (gnus-summary-article-sparse-p
8032                          (mail-header-number header))
8033                         (memq (mail-header-number header)
8034                               gnus-newsgroup-limit)))
8035            number)
8036       (cond
8037        ;; If the article is present in the buffer we just go to it.
8038        ((and header
8039              (or (not (gnus-summary-article-sparse-p
8040                        (mail-header-number header)))
8041                  sparse))
8042         (prog1
8043             (gnus-summary-goto-article
8044              (mail-header-number header) nil t)
8045           (when sparse
8046             (gnus-summary-update-article (mail-header-number header)))))
8047        (t
8048         ;; We fetch the article.
8049         (catch 'found
8050           (dolist (gnus-override-method (gnus-refer-article-methods))
8051             (when (and (gnus-check-server gnus-override-method)
8052                        ;; Fetch the header,
8053                        (setq number (gnus-summary-insert-subject message-id)))
8054               ;; and display the article.
8055               (gnus-summary-select-article nil nil nil number)
8056               (throw 'found t)))
8057           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8058
8059 (defun gnus-refer-article-methods ()
8060   "Return a list of referable methods."
8061   (cond
8062    ;; No method, so we default to current and native.
8063    ((null gnus-refer-article-method)
8064     (list gnus-current-select-method gnus-select-method))
8065    ;; Current.
8066    ((eq 'current gnus-refer-article-method)
8067     (list gnus-current-select-method))
8068    ;; List of select methods.
8069    ((not (and (symbolp (car gnus-refer-article-method))
8070               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8071     (let (out)
8072       (dolist (method gnus-refer-article-method)
8073         (push (if (eq 'current method)
8074                   gnus-current-select-method
8075                 method)
8076               out))
8077       (nreverse out)))
8078    ;; One single select method.
8079    (t
8080     (list gnus-refer-article-method))))
8081
8082 (defun gnus-summary-edit-parameters ()
8083   "Edit the group parameters of the current group."
8084   (interactive)
8085   (gnus-group-edit-group gnus-newsgroup-name 'params))
8086
8087 (defun gnus-summary-customize-parameters ()
8088   "Customize the group parameters of the current group."
8089   (interactive)
8090   (gnus-group-customize gnus-newsgroup-name))
8091
8092 (defun gnus-summary-enter-digest-group (&optional force)
8093   "Enter an nndoc group based on the current article.
8094 If FORCE, force a digest interpretation.  If not, try
8095 to guess what the document format is."
8096   (interactive "P")
8097   (let ((conf gnus-current-window-configuration))
8098     (save-excursion
8099       (gnus-summary-select-article))
8100     (setq gnus-current-window-configuration conf)
8101     (let* ((name (format "%s-%d"
8102                          (gnus-group-prefixed-name
8103                           gnus-newsgroup-name (list 'nndoc ""))
8104                          (save-excursion
8105                            (set-buffer gnus-summary-buffer)
8106                            gnus-current-article)))
8107            (ogroup gnus-newsgroup-name)
8108            (params (append (gnus-info-params (gnus-get-info ogroup))
8109                            (list (cons 'to-group ogroup))
8110                            (list (cons 'save-article-group ogroup))))
8111            (case-fold-search t)
8112            (buf (current-buffer))
8113            dig to-address)
8114       (save-excursion
8115         (set-buffer gnus-original-article-buffer)
8116         ;; Have the digest group inherit the main mail address of
8117         ;; the parent article.
8118         (when (setq to-address (or (gnus-fetch-field "reply-to")
8119                                    (gnus-fetch-field "from")))
8120           (setq params (append
8121                         (list (cons 'to-address
8122                                     (funcall gnus-decode-encoded-word-function
8123                                              to-address))))))
8124         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8125         (insert-buffer-substring gnus-original-article-buffer)
8126         ;; Remove lines that may lead nndoc to misinterpret the
8127         ;; document type.
8128         (narrow-to-region
8129          (goto-char (point-min))
8130          (or (search-forward "\n\n" nil t) (point)))
8131         (goto-char (point-min))
8132         (delete-matching-lines "^Path:\\|^From ")
8133         (widen))
8134       (unwind-protect
8135           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8136                     (gnus-newsgroup-ephemeral-ignored-charsets
8137                      gnus-newsgroup-ignored-charsets))
8138                 (gnus-group-read-ephemeral-group
8139                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8140                               (nndoc-article-type
8141                                ,(if force 'mbox 'guess)))
8142                  t nil nil nil
8143                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8144                                                         "ADAPT")))))
8145               ;; Make all postings to this group go to the parent group.
8146               (nconc (gnus-info-params (gnus-get-info name))
8147                      params)
8148             ;; Couldn't select this doc group.
8149             (switch-to-buffer buf)
8150             (gnus-set-global-variables)
8151             (gnus-configure-windows 'summary)
8152             (gnus-message 3 "Article couldn't be entered?"))
8153         (kill-buffer dig)))))
8154
8155 (defun gnus-summary-read-document (n)
8156   "Open a new group based on the current article(s).
8157 This will allow you to read digests and other similar
8158 documents as newsgroups.
8159 Obeys the standard process/prefix convention."
8160   (interactive "P")
8161   (let* ((articles (gnus-summary-work-articles n))
8162          (ogroup gnus-newsgroup-name)
8163          (params (append (gnus-info-params (gnus-get-info ogroup))
8164                          (list (cons 'to-group ogroup))))
8165          article group egroup groups vgroup)
8166     (while (setq article (pop articles))
8167       (setq group (format "%s-%d" gnus-newsgroup-name article))
8168       (gnus-summary-remove-process-mark article)
8169       (when (gnus-summary-display-article article)
8170         (save-excursion
8171           (with-temp-buffer
8172             (insert-buffer-substring gnus-original-article-buffer)
8173             ;; Remove some headers that may lead nndoc to make
8174             ;; the wrong guess.
8175             (message-narrow-to-head)
8176             (goto-char (point-min))
8177             (delete-matching-lines "^\\(Path\\):\\|^From ")
8178             (widen)
8179             (if (setq egroup
8180                       (gnus-group-read-ephemeral-group
8181                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8182                                      (nndoc-article-type guess))
8183                        t nil t))
8184                 (progn
8185             ;; Make all postings to this group go to the parent group.
8186                   (nconc (gnus-info-params (gnus-get-info egroup))
8187                          params)
8188                   (push egroup groups))
8189               ;; Couldn't select this doc group.
8190               (gnus-error 3 "Article couldn't be entered"))))))
8191     ;; Now we have selected all the documents.
8192     (cond
8193      ((not groups)
8194       (error "None of the articles could be interpreted as documents"))
8195      ((gnus-group-read-ephemeral-group
8196        (setq vgroup (format
8197                      "nnvirtual:%s-%s" gnus-newsgroup-name
8198                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8199        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8200        t
8201        (cons (current-buffer) 'summary)))
8202      (t
8203       (error "Couldn't select virtual nndoc group")))))
8204
8205 (defun gnus-summary-isearch-article (&optional regexp-p)
8206   "Do incremental search forward on the current article.
8207 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8208   (interactive "P")
8209   (gnus-summary-select-article)
8210   (gnus-configure-windows 'article)
8211   (gnus-eval-in-buffer-window gnus-article-buffer
8212     (save-restriction
8213       (widen)
8214       (isearch-forward regexp-p))))
8215
8216 (defun gnus-summary-search-article-forward (regexp &optional backward)
8217   "Search for an article containing REGEXP forward.
8218 If BACKWARD, search backward instead."
8219   (interactive
8220    (list (read-string
8221           (format "Search article %s (regexp%s): "
8222                   (if current-prefix-arg "backward" "forward")
8223                   (if gnus-last-search-regexp
8224                       (concat ", default " gnus-last-search-regexp)
8225                     "")))
8226          current-prefix-arg))
8227   (if (string-equal regexp "")
8228       (setq regexp (or gnus-last-search-regexp ""))
8229     (setq gnus-last-search-regexp regexp)
8230     (setq gnus-article-before-search gnus-current-article))
8231   ;; Intentionally set gnus-last-article.
8232   (setq gnus-last-article gnus-article-before-search)
8233   (let ((gnus-last-article gnus-last-article))
8234     (if (gnus-summary-search-article regexp backward)
8235         (gnus-summary-show-thread)
8236       (error "Search failed: \"%s\"" regexp))))
8237
8238 (defun gnus-summary-search-article-backward (regexp)
8239   "Search for an article containing REGEXP backward."
8240   (interactive
8241    (list (read-string
8242           (format "Search article backward (regexp%s): "
8243                   (if gnus-last-search-regexp
8244                       (concat ", default " gnus-last-search-regexp)
8245                     "")))))
8246   (gnus-summary-search-article-forward regexp 'backward))
8247
8248 (defun gnus-summary-search-article (regexp &optional backward)
8249   "Search for an article containing REGEXP.
8250 Optional argument BACKWARD means do search for backward.
8251 `gnus-select-article-hook' is not called during the search."
8252   ;; We have to require this here to make sure that the following
8253   ;; dynamic binding isn't shadowed by autoloading.
8254   (require 'gnus-async)
8255   (require 'gnus-art)
8256   (let ((gnus-select-article-hook nil)  ;Disable hook.
8257         (gnus-article-prepare-hook nil)
8258         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8259         (gnus-use-article-prefetch nil)
8260         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8261         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8262         (gnus-visual nil)
8263         (gnus-keep-backlog nil)
8264         (gnus-break-pages nil)
8265         (gnus-summary-display-arrow nil)
8266         (gnus-updated-mode-lines nil)
8267         (gnus-auto-center-summary nil)
8268         (sum (current-buffer))
8269         (gnus-display-mime-function nil)
8270         (found nil)
8271         point)
8272     (gnus-save-hidden-threads
8273       (gnus-summary-select-article)
8274       (set-buffer gnus-article-buffer)
8275       (goto-char (window-point (get-buffer-window (current-buffer))))
8276       (when backward
8277         (forward-line -1))
8278       (while (not found)
8279         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8280         (if (if backward
8281                 (re-search-backward regexp nil t)
8282               (re-search-forward regexp nil t))
8283             ;; We found the regexp.
8284             (progn
8285               (setq found 'found)
8286               (beginning-of-line)
8287               (set-window-start
8288                (get-buffer-window (current-buffer))
8289                (point))
8290               (forward-line 1)
8291               (set-window-point
8292                (get-buffer-window (current-buffer))
8293                (point))
8294               (set-buffer sum)
8295               (setq point (point)))
8296           ;; We didn't find it, so we go to the next article.
8297           (set-buffer sum)
8298           (setq found 'not)
8299           (while (eq found 'not)
8300             (if (not (if backward (gnus-summary-find-prev)
8301                        (gnus-summary-find-next)))
8302                 ;; No more articles.
8303                 (setq found t)
8304               ;; Select the next article and adjust point.
8305               (unless (gnus-summary-article-sparse-p
8306                        (gnus-summary-article-number))
8307                 (setq found nil)
8308                 (gnus-summary-select-article)
8309                 (set-buffer gnus-article-buffer)
8310                 (widen)
8311                 (goto-char (if backward (point-max) (point-min))))))))
8312       (gnus-message 7 ""))
8313     ;; Return whether we found the regexp.
8314     (when (eq found 'found)
8315       (goto-char point)
8316       (gnus-summary-show-thread)
8317       (gnus-summary-goto-subject gnus-current-article)
8318       (gnus-summary-position-point)
8319       t)))
8320
8321 (defun gnus-find-matching-articles (header regexp)
8322   "Return a list of all articles that match REGEXP on HEADER.
8323 This search includes all articles in the current group that Gnus has
8324 fetched headers for, whether they are displayed or not."
8325   (let ((articles nil)
8326         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8327         (case-fold-search t))
8328     (dolist (header gnus-newsgroup-headers)
8329       (when (string-match regexp (funcall func header))
8330         (push (mail-header-number header) articles)))
8331     (nreverse articles)))
8332
8333 (defun gnus-summary-find-matching (header regexp &optional backward unread
8334                                           not-case-fold not-matching)
8335   "Return a list of all articles that match REGEXP on HEADER.
8336 The search stars on the current article and goes forwards unless
8337 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8338 If UNREAD is non-nil, only unread articles will
8339 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8340 in the comparisons. If NOT-MATCHING, return a list of all articles that
8341 not match REGEXP on HEADER."
8342   (let ((case-fold-search (not not-case-fold))
8343         articles d func)
8344     (if (consp header)
8345         (if (eq (car header) 'extra)
8346             (setq func
8347                   `(lambda (h)
8348                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8349                          "")))
8350           (error "%s is an invalid header" header))
8351       (unless (fboundp (intern (concat "mail-header-" header)))
8352         (error "%s is not a valid header" header))
8353       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8354     (dolist (d (if (eq backward 'all)
8355                    gnus-newsgroup-data
8356                  (gnus-data-find-list
8357                   (gnus-summary-article-number)
8358                   (gnus-data-list backward))))
8359       (when (and (or (not unread)       ; We want all articles...
8360                      (gnus-data-unread-p d)) ; Or just unreads.
8361                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8362                  (if not-matching
8363                      (not (string-match
8364                            regexp
8365                            (funcall func (gnus-data-header d))))
8366                    (string-match regexp
8367                                  (funcall func (gnus-data-header d)))))
8368         (push (gnus-data-number d) articles))) ; Success!
8369     (nreverse articles)))
8370
8371 (defun gnus-summary-execute-command (header regexp command &optional backward)
8372   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8373 If HEADER is an empty string (or nil), the match is done on the entire
8374 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8375   (interactive
8376    (list (let ((completion-ignore-case t))
8377            (completing-read
8378             "Header name: "
8379             (mapcar (lambda (header) (list (format "%s" header)))
8380                     (append
8381                      '("Number" "Subject" "From" "Lines" "Date"
8382                        "Message-ID" "Xref" "References" "Body")
8383                      gnus-extra-headers))
8384             nil 'require-match))
8385          (read-string "Regexp: ")
8386          (read-key-sequence "Command: ")
8387          current-prefix-arg))
8388   (when (equal header "Body")
8389     (setq header ""))
8390   ;; Hidden thread subtrees must be searched as well.
8391   (gnus-summary-show-all-threads)
8392   ;; We don't want to change current point nor window configuration.
8393   (save-excursion
8394     (save-window-excursion
8395       (let (gnus-visual
8396             gnus-treat-strip-trailing-blank-lines
8397             gnus-treat-strip-leading-blank-lines
8398             gnus-treat-strip-multiple-blank-lines
8399             gnus-treat-hide-boring-headers
8400             gnus-treat-fold-newsgroups
8401             gnus-article-prepare-hook)
8402         (gnus-message 6 "Executing %s..." (key-description command))
8403         ;; We'd like to execute COMMAND interactively so as to give arguments.
8404         (gnus-execute header regexp
8405                       `(call-interactively ',(key-binding command))
8406                       backward)
8407         (gnus-message 6 "Executing %s...done" (key-description command))))))
8408
8409 (defun gnus-summary-beginning-of-article ()
8410   "Scroll the article back to the beginning."
8411   (interactive)
8412   (gnus-summary-select-article)
8413   (gnus-configure-windows 'article)
8414   (gnus-eval-in-buffer-window gnus-article-buffer
8415     (widen)
8416     (goto-char (point-min))
8417     (when gnus-page-broken
8418       (gnus-narrow-to-page))))
8419
8420 (defun gnus-summary-end-of-article ()
8421   "Scroll to the end of the article."
8422   (interactive)
8423   (gnus-summary-select-article)
8424   (gnus-configure-windows 'article)
8425   (gnus-eval-in-buffer-window gnus-article-buffer
8426     (widen)
8427     (goto-char (point-max))
8428     (recenter -3)
8429     (when gnus-page-broken
8430       (gnus-narrow-to-page))))
8431
8432 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8433   "Truncate to LEN and quote all \"(\"'s in STRING."
8434   (gnus-replace-in-string (if (and len (> (length string) len))
8435                               (substring string 0 len)
8436                             string)
8437                           "[()]" "\\\\\\&"))
8438
8439 (defun gnus-summary-print-article (&optional filename n)
8440   "Generate and print a PostScript image of the N next (mail) articles.
8441
8442 If N is negative, print the N previous articles.  If N is nil and articles
8443 have been marked with the process mark, print these instead.
8444
8445 If the optional first argument FILENAME is nil, send the image to the
8446 printer.  If FILENAME is a string, save the PostScript image in a file with
8447 that name.  If FILENAME is a number, prompt the user for the name of the file
8448 to save in."
8449   (interactive (list (ps-print-preprint current-prefix-arg)))
8450   (dolist (article (gnus-summary-work-articles n))
8451     (gnus-summary-select-article nil nil 'pseudo article)
8452     (gnus-eval-in-buffer-window gnus-article-buffer
8453       (gnus-print-buffer))
8454     (gnus-summary-remove-process-mark article))
8455   (ps-despool filename))
8456
8457 (defun gnus-print-buffer ()
8458   (let ((buffer (generate-new-buffer " *print*")))
8459     (unwind-protect
8460         (progn
8461           (copy-to-buffer buffer (point-min) (point-max))
8462           (set-buffer buffer)
8463           (gnus-article-delete-invisible-text)
8464           (gnus-remove-text-with-property 'gnus-decoration)
8465           (when (gnus-visual-p 'article-highlight 'highlight)
8466             ;; Copy-to-buffer doesn't copy overlay.  So redo
8467             ;; highlight.
8468             (let ((gnus-article-buffer buffer))
8469               (gnus-article-highlight-citation t)
8470               (gnus-article-highlight-signature)))
8471           (let ((ps-left-header
8472                  (list
8473                   (concat "("
8474                           (gnus-summary-print-truncate-and-quote
8475                            (mail-header-subject gnus-current-headers)
8476                            66) ")")
8477                   (concat "("
8478                           (gnus-summary-print-truncate-and-quote
8479                            (mail-header-from gnus-current-headers)
8480                            45) ")")))
8481                 (ps-right-header
8482                  (list
8483                   "/pagenumberstring load"
8484                   (concat "("
8485                           (mail-header-date gnus-current-headers) ")"))))
8486             (gnus-run-hooks 'gnus-ps-print-hook)
8487             (save-excursion
8488               (if window-system
8489                   (ps-spool-buffer-with-faces)
8490                 (ps-spool-buffer)))))
8491       (kill-buffer buffer))))
8492
8493 (defun gnus-summary-show-article (&optional arg)
8494   "Force redisplaying of the current article.
8495 If ARG (the prefix) is a number, show the article with the charset
8496 defined in `gnus-summary-show-article-charset-alist', or the charset
8497 input.
8498 If ARG (the prefix) is non-nil and not a number, show the raw article
8499 without any article massaging functions being run.  Normally, the key strokes
8500 are `C-u g'."
8501   (interactive "P")
8502   (cond
8503    ((numberp arg)
8504     (gnus-summary-show-article t)
8505     (let ((gnus-newsgroup-charset
8506            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8507                (mm-read-coding-system
8508                 "View as charset: " ;; actually it is coding system.
8509                 (save-excursion
8510                   (set-buffer gnus-article-buffer)
8511                   (mm-detect-coding-region (point) (point-max))))))
8512           (gnus-newsgroup-ignored-charsets 'gnus-all))
8513       (gnus-summary-select-article nil 'force)
8514       (let ((deps gnus-newsgroup-dependencies)
8515             head header lines)
8516         (save-excursion
8517           (set-buffer gnus-original-article-buffer)
8518           (save-restriction
8519             (message-narrow-to-head)
8520             (setq head (buffer-string))
8521             (goto-char (point-min))
8522             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8523               (goto-char (point-max))
8524               (widen)
8525               (setq lines (1- (count-lines (point) (point-max))))))
8526           (with-temp-buffer
8527             (insert (format "211 %d Article retrieved.\n"
8528                             (cdr gnus-article-current)))
8529             (insert head)
8530             (if lines (insert (format "Lines: %d\n" lines)))
8531             (insert ".\n")
8532             (let ((nntp-server-buffer (current-buffer)))
8533               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8534         (gnus-data-set-header
8535          (gnus-data-find (cdr gnus-article-current))
8536          header)
8537         (gnus-summary-update-article-line
8538          (cdr gnus-article-current) header)
8539         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8540           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8541    ((not arg)
8542     ;; Select the article the normal way.
8543     (gnus-summary-select-article nil 'force))
8544    (t
8545     ;; We have to require this here to make sure that the following
8546     ;; dynamic binding isn't shadowed by autoloading.
8547     (require 'gnus-async)
8548     (require 'gnus-art)
8549     ;; Bind the article treatment functions to nil.
8550     (let ((gnus-have-all-headers t)
8551           gnus-article-prepare-hook
8552           gnus-article-decode-hook
8553           gnus-display-mime-function
8554           gnus-break-pages)
8555       ;; Destroy any MIME parts.
8556       (when (gnus-buffer-live-p gnus-article-buffer)
8557         (save-excursion
8558           (set-buffer gnus-article-buffer)
8559           (mm-destroy-parts gnus-article-mime-handles)
8560           ;; Set it to nil for safety reason.
8561           (setq gnus-article-mime-handle-alist nil)
8562           (setq gnus-article-mime-handles nil)))
8563       (gnus-summary-select-article nil 'force))))
8564   (gnus-summary-goto-subject gnus-current-article)
8565   (gnus-summary-position-point))
8566
8567 (defun gnus-summary-show-raw-article ()
8568   "Show the raw article without any article massaging functions being run."
8569   (interactive)
8570   (gnus-summary-show-article t))
8571
8572 (defun gnus-summary-verbose-headers (&optional arg)
8573   "Toggle permanent full header display.
8574 If ARG is a positive number, turn header display on.
8575 If ARG is a negative number, turn header display off."
8576   (interactive "P")
8577   (setq gnus-show-all-headers
8578         (cond ((or (not (numberp arg))
8579                    (zerop arg))
8580                (not gnus-show-all-headers))
8581               ((natnump arg)
8582                t)))
8583   (gnus-summary-show-article))
8584
8585 (defun gnus-summary-toggle-header (&optional arg)
8586   "Show the headers if they are hidden, or hide them if they are shown.
8587 If ARG is a positive number, show the entire header.
8588 If ARG is a negative number, hide the unwanted header lines."
8589   (interactive "P")
8590   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8591                      (get-buffer-window gnus-article-buffer t))))
8592     (with-current-buffer gnus-article-buffer
8593       (widen)
8594       (article-narrow-to-head)
8595       (let* ((buffer-read-only nil)
8596              (inhibit-point-motion-hooks t)
8597              (hidden (if (numberp arg)
8598                          (>= arg 0)
8599                        (gnus-article-hidden-text-p 'headers)))
8600              s e)
8601         (delete-region (point-min) (point-max))
8602         (with-current-buffer gnus-original-article-buffer
8603           (goto-char (setq s (point-min)))
8604           (setq e (if (search-forward "\n\n" nil t)
8605                       (1- (point))
8606                     (point-max))))
8607         (insert-buffer-substring gnus-original-article-buffer s e)
8608         (run-hooks 'gnus-article-decode-hook)
8609         (if hidden
8610             (let ((gnus-treat-hide-headers nil)
8611                   (gnus-treat-hide-boring-headers nil))
8612               (gnus-delete-wash-type 'headers)
8613               (gnus-treat-article 'head))
8614           (gnus-treat-article 'head))
8615         (widen)
8616         (if window
8617             (set-window-start window (goto-char (point-min))))
8618         (setq gnus-page-broken
8619               (when gnus-break-pages
8620                 (gnus-narrow-to-page)
8621                 t))
8622         (gnus-set-mode-line 'article)))))
8623
8624 (defun gnus-summary-show-all-headers ()
8625   "Make all header lines visible."
8626   (interactive)
8627   (gnus-summary-toggle-header 1))
8628
8629 (defun gnus-summary-caesar-message (&optional arg)
8630   "Caesar rotate the current article by 13.
8631 The numerical prefix specifies how many places to rotate each letter
8632 forward."
8633   (interactive "P")
8634   (gnus-summary-select-article)
8635   (let ((mail-header-separator ""))
8636     (gnus-eval-in-buffer-window gnus-article-buffer
8637       (save-restriction
8638         (widen)
8639         (let ((start (window-start))
8640               buffer-read-only)
8641           (message-caesar-buffer-body arg)
8642           (set-window-start (get-buffer-window (current-buffer)) start))))))
8643
8644 (autoload 'unmorse-region "morse"
8645   "Convert morse coded text in region to ordinary ASCII text."
8646   t)
8647
8648 (defun gnus-summary-morse-message (&optional arg)
8649   "Morse decode the current article."
8650   (interactive "P")
8651   (gnus-summary-select-article)
8652   (let ((mail-header-separator ""))
8653     (gnus-eval-in-buffer-window gnus-article-buffer
8654       (save-excursion
8655         (save-restriction
8656           (widen)
8657           (let ((pos (window-start))
8658                 buffer-read-only)
8659             (goto-char (point-min))
8660             (when (message-goto-body)
8661               (gnus-narrow-to-body))
8662             (goto-char (point-min))
8663             (while (re-search-forward "·" (point-max) t)
8664               (replace-match "."))
8665             (unmorse-region (point-min) (point-max))
8666             (widen)
8667             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8668
8669 (defun gnus-summary-stop-page-breaking ()
8670   "Stop page breaking in the current article."
8671   (interactive)
8672   (gnus-summary-select-article)
8673   (gnus-eval-in-buffer-window gnus-article-buffer
8674     (widen)
8675     (when (gnus-visual-p 'page-marker)
8676       (let ((buffer-read-only nil))
8677         (gnus-remove-text-with-property 'gnus-prev)
8678         (gnus-remove-text-with-property 'gnus-next))
8679       (setq gnus-page-broken nil))))
8680
8681 (defun gnus-summary-move-article (&optional n to-newsgroup
8682                                             select-method action)
8683   "Move the current article to a different newsgroup.
8684 If N is a positive number, move the N next articles.
8685 If N is a negative number, move the N previous articles.
8686 If N is nil and any articles have been marked with the process mark,
8687 move those articles instead.
8688 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8689 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8690 re-spool using this method.
8691
8692 When called interactively with TO-NEWSGROUP being nil, the value of
8693 the variable `gnus-move-split-methods' is used for finding a default
8694 for the target newsgroup.
8695
8696 For this function to work, both the current newsgroup and the
8697 newsgroup that you want to move to have to support the `request-move'
8698 and `request-accept' functions.
8699
8700 ACTION can be either `move' (the default), `crosspost' or `copy'."
8701   (interactive "P")
8702   (unless action
8703     (setq action 'move))
8704   ;; Check whether the source group supports the required functions.
8705   (cond ((and (eq action 'move)
8706               (not (gnus-check-backend-function
8707                     'request-move-article gnus-newsgroup-name)))
8708          (error "The current group does not support article moving"))
8709         ((and (eq action 'crosspost)
8710               (not (gnus-check-backend-function
8711                     'request-replace-article gnus-newsgroup-name)))
8712          (error "The current group does not support article editing")))
8713   (let ((articles (gnus-summary-work-articles n))
8714         (prefix (if (gnus-check-backend-function
8715                      'request-move-article gnus-newsgroup-name)
8716                     (gnus-group-real-prefix gnus-newsgroup-name)
8717                   ""))
8718         (names '((move "Move" "Moving")
8719                  (copy "Copy" "Copying")
8720                  (crosspost "Crosspost" "Crossposting")))
8721         (copy-buf (save-excursion
8722                     (nnheader-set-temp-buffer " *copy article*")))
8723         art-group to-method new-xref article to-groups)
8724     (unless (assq action names)
8725       (error "Unknown action %s" action))
8726     ;; Read the newsgroup name.
8727     (when (and (not to-newsgroup)
8728                (not select-method))
8729       (if (and gnus-move-split-methods
8730                (not
8731                 (and (memq gnus-current-article articles)
8732                      (gnus-buffer-live-p gnus-original-article-buffer))))
8733           ;; When `gnus-move-split-methods' is non-nil, we have to
8734           ;; select an article to give `gnus-read-move-group-name' an
8735           ;; opportunity to suggest an appropriate default.  However,
8736           ;; we needn't render or mark the article.
8737           (let ((gnus-display-mime-function nil)
8738                 (gnus-article-prepare-hook nil)
8739                 (gnus-mark-article-hook nil))
8740             (gnus-summary-select-article nil nil nil (car articles))))
8741       (setq to-newsgroup
8742             (gnus-read-move-group-name
8743              (cadr (assq action names))
8744              (symbol-value (intern (format "gnus-current-%s-group" action)))
8745              articles prefix))
8746       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8747     (setq to-method (or select-method
8748                         (gnus-server-to-method
8749                          (gnus-group-method to-newsgroup))))
8750     ;; Check the method we are to move this article to...
8751     (unless (gnus-check-backend-function
8752              'request-accept-article (car to-method))
8753       (error "%s does not support article copying" (car to-method)))
8754     (unless (gnus-check-server to-method)
8755       (error "Can't open server %s" (car to-method)))
8756     (gnus-message 6 "%s to %s: %s..."
8757                   (caddr (assq action names))
8758                   (or (car select-method) to-newsgroup) articles)
8759     (while articles
8760       (setq article (pop articles))
8761       (setq
8762        art-group
8763        (cond
8764         ;; Move the article.
8765         ((eq action 'move)
8766          ;; Remove this article from future suppression.
8767          (gnus-dup-unsuppress-article article)
8768          (gnus-request-move-article
8769           article                       ; Article to move
8770           gnus-newsgroup-name           ; From newsgroup
8771           (nth 1 (gnus-find-method-for-group
8772                   gnus-newsgroup-name)) ; Server
8773           (list 'gnus-request-accept-article
8774                 to-newsgroup (list 'quote select-method)
8775                 (not articles) t)       ; Accept form
8776           (not articles)))              ; Only save nov last time
8777         ;; Copy the article.
8778         ((eq action 'copy)
8779          (save-excursion
8780            (set-buffer copy-buf)
8781            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8782              (gnus-request-accept-article
8783               to-newsgroup select-method (not articles) t))))
8784         ;; Crosspost the article.
8785         ((eq action 'crosspost)
8786          (let ((xref (message-tokenize-header
8787                       (mail-header-xref (gnus-summary-article-header article))
8788                       " ")))
8789            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8790                                   ":" (number-to-string article)))
8791            (unless xref
8792              (setq xref (list (system-name))))
8793            (setq new-xref
8794                  (concat
8795                   (mapconcat 'identity
8796                              (delete "Xref:" (delete new-xref xref))
8797                              " ")
8798                   " " new-xref))
8799            (save-excursion
8800              (set-buffer copy-buf)
8801              ;; First put the article in the destination group.
8802              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8803              (when (consp (setq art-group
8804                                 (gnus-request-accept-article
8805                                  to-newsgroup select-method (not articles))))
8806                (setq new-xref (concat new-xref " " (car art-group)
8807                                       ":"
8808                                       (number-to-string (cdr art-group))))
8809                ;; Now we have the new Xrefs header, so we insert
8810                ;; it and replace the new article.
8811                (nnheader-replace-header "Xref" new-xref)
8812                (gnus-request-replace-article
8813                 (cdr art-group) to-newsgroup (current-buffer))
8814                art-group))))))
8815       (cond
8816        ((not art-group)
8817         (gnus-message 1 "Couldn't %s article %s: %s"
8818                       (cadr (assq action names)) article
8819                       (nnheader-get-report (car to-method))))
8820        ((eq art-group 'junk)
8821         (when (eq action 'move)
8822           (gnus-summary-mark-article article gnus-canceled-mark)
8823           (gnus-message 4 "Deleted article %s" article)
8824           ;; run the delete hook
8825           (run-hook-with-args 'gnus-summary-article-delete-hook
8826                               action
8827                               (gnus-data-header
8828                                (assoc article (gnus-data-list nil)))
8829                               gnus-newsgroup-name nil
8830                               select-method)))
8831        (t
8832         (let* ((pto-group (gnus-group-prefixed-name
8833                            (car art-group) to-method))
8834                (entry
8835                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8836                (info (nth 2 entry))
8837                (to-group (gnus-info-group info))
8838                to-marks)
8839           ;; Update the group that has been moved to.
8840           (when (and info
8841                      (memq action '(move copy)))
8842             (unless (member to-group to-groups)
8843               (push to-group to-groups))
8844
8845             (unless (memq article gnus-newsgroup-unreads)
8846               (push 'read to-marks)
8847               (gnus-info-set-read
8848                info (gnus-add-to-range (gnus-info-read info)
8849                                        (list (cdr art-group)))))
8850
8851             ;; See whether the article is to be put in the cache.
8852             (let ((marks gnus-article-mark-lists)
8853                   (to-article (cdr art-group)))
8854
8855               ;; Enter the article into the cache in the new group,
8856               ;; if that is required.
8857               (when gnus-use-cache
8858                 (gnus-cache-possibly-enter-article
8859                  to-group to-article
8860                  (memq article gnus-newsgroup-marked)
8861                  (memq article gnus-newsgroup-dormant)
8862                  (memq article gnus-newsgroup-unreads)))
8863
8864               (when gnus-preserve-marks
8865                 ;; Copy any marks over to the new group.
8866                 (when (and (equal to-group gnus-newsgroup-name)
8867                            (not (memq article gnus-newsgroup-unreads)))
8868                   ;; Mark this article as read in this group.
8869                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8870                   (setcdr (gnus-active to-group) to-article)
8871                   (setcdr gnus-newsgroup-active to-article))
8872
8873                 (while marks
8874                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8875                     (when (memq article (symbol-value
8876                                          (intern (format "gnus-newsgroup-%s"
8877                                                          (caar marks)))))
8878                       (push (cdar marks) to-marks)
8879                       ;; If the other group is the same as this group,
8880                       ;; then we have to add the mark to the list.
8881                       (when (equal to-group gnus-newsgroup-name)
8882                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8883                              (cons to-article
8884                                    (symbol-value
8885                                     (intern (format "gnus-newsgroup-%s"
8886                                                     (caar marks)))))))
8887                       ;; Copy the marks to other group.
8888                       (gnus-add-marked-articles
8889                        to-group (cdar marks) (list to-article) info)))
8890                   (setq marks (cdr marks)))
8891
8892                 (gnus-request-set-mark
8893                  to-group (list (list (list to-article) 'add to-marks))))
8894
8895               (gnus-dribble-enter
8896                (concat "(gnus-group-set-info '"
8897                        (gnus-prin1-to-string (gnus-get-info to-group))
8898                        ")"))))
8899
8900           ;; Update the Xref header in this article to point to
8901           ;; the new crossposted article we have just created.
8902           (when (eq action 'crosspost)
8903             (save-excursion
8904               (set-buffer copy-buf)
8905               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8906               (nnheader-replace-header "Xref" new-xref)
8907               (gnus-request-replace-article
8908                article gnus-newsgroup-name (current-buffer))))
8909
8910           ;; run the move/copy/crosspost/respool hook
8911           (run-hook-with-args 'gnus-summary-article-move-hook 
8912                               action
8913                               (gnus-data-header 
8914                                (assoc article (gnus-data-list nil)))
8915                               gnus-newsgroup-name
8916                               to-newsgroup
8917                               select-method))
8918
8919         ;;;!!!Why is this necessary?
8920         (set-buffer gnus-summary-buffer)
8921         
8922         (gnus-summary-goto-subject article)
8923         (when (eq action 'move)
8924           (gnus-summary-mark-article article gnus-canceled-mark))))
8925       (gnus-summary-remove-process-mark article))
8926     ;; Re-activate all groups that have been moved to.
8927     (save-excursion
8928       (set-buffer gnus-group-buffer)
8929       (let ((gnus-group-marked to-groups))
8930         (gnus-group-get-new-news-this-group nil t)))
8931
8932     (gnus-kill-buffer copy-buf)
8933     (gnus-summary-position-point)
8934     (gnus-set-mode-line 'summary)))
8935
8936 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8937   "Move the current article to a different newsgroup.
8938 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8939 When called interactively, if TO-NEWSGROUP is nil, use the value of
8940 the variable `gnus-move-split-methods' for finding a default target
8941 newsgroup.
8942 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8943 re-spool using this method."
8944   (interactive "P")
8945   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8946
8947 (defun gnus-summary-crosspost-article (&optional n)
8948   "Crosspost the current article to some other group."
8949   (interactive "P")
8950   (gnus-summary-move-article n nil nil 'crosspost))
8951
8952 (defcustom gnus-summary-respool-default-method nil
8953   "Default method type for respooling an article.
8954 If nil, use to the current newsgroup method."
8955   :type 'symbol
8956   :group 'gnus-summary-mail)
8957
8958 (defcustom gnus-summary-display-while-building nil
8959   "If not-nil, show and update the summary buffer as it's being built.
8960 If the value is t, update the buffer after every line is inserted.  If
8961 the value is an integer (N), update the display every N lines."
8962   :group 'gnus-thread
8963   :type '(choice (const :tag "off" nil)
8964                  number
8965                  (const :tag "frequently" t)))
8966
8967 (defun gnus-summary-respool-article (&optional n method)
8968   "Respool the current article.
8969 The article will be squeezed through the mail spooling process again,
8970 which means that it will be put in some mail newsgroup or other
8971 depending on `nnmail-split-methods'.
8972 If N is a positive number, respool the N next articles.
8973 If N is a negative number, respool the N previous articles.
8974 If N is nil and any articles have been marked with the process mark,
8975 respool those articles instead.
8976
8977 Respooling can be done both from mail groups and \"real\" newsgroups.
8978 In the former case, the articles in question will be moved from the
8979 current group into whatever groups they are destined to.  In the
8980 latter case, they will be copied into the relevant groups."
8981   (interactive
8982    (list current-prefix-arg
8983          (let* ((methods (gnus-methods-using 'respool))
8984                 (methname
8985                  (symbol-name (or gnus-summary-respool-default-method
8986                                   (car (gnus-find-method-for-group
8987                                         gnus-newsgroup-name)))))
8988                 (method
8989                  (gnus-completing-read-with-default
8990                   methname "What backend do you want to use when respooling?"
8991                   methods nil t nil 'gnus-mail-method-history))
8992                 ms)
8993            (cond
8994             ((zerop (length (setq ms (gnus-servers-using-backend
8995                                       (intern method)))))
8996              (list (intern method) ""))
8997             ((= 1 (length ms))
8998              (car ms))
8999             (t
9000              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9001                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9002                            ms-alist))))))))
9003   (unless method
9004     (error "No method given for respooling"))
9005   (if (assoc (symbol-name
9006               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9007              (gnus-methods-using 'respool))
9008       (gnus-summary-move-article n nil method)
9009     (gnus-summary-copy-article n nil method)))
9010
9011 (defun gnus-summary-import-article (file &optional edit)
9012   "Import an arbitrary file into a mail newsgroup."
9013   (interactive "fImport file: \nP")
9014   (let ((group gnus-newsgroup-name)
9015         (now (current-time))
9016         atts lines group-art)
9017     (unless (gnus-check-backend-function 'request-accept-article group)
9018       (error "%s does not support article importing" group))
9019     (or (file-readable-p file)
9020         (not (file-regular-p file))
9021         (error "Can't read %s" file))
9022     (save-excursion
9023       (set-buffer (gnus-get-buffer-create " *import file*"))
9024       (erase-buffer)
9025       (nnheader-insert-file-contents file)
9026       (goto-char (point-min))
9027       (if (nnheader-article-p)
9028           (save-restriction
9029             (goto-char (point-min))
9030             (search-forward "\n\n" nil t)
9031             (narrow-to-region (point-min) (1- (point)))
9032             (goto-char (point-min))
9033             (unless (re-search-forward "^date:" nil t)
9034               (goto-char (point-max))
9035               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9036        ;; This doesn't look like an article, so we fudge some headers.
9037         (setq atts (file-attributes file)
9038               lines (count-lines (point-min) (point-max)))
9039         (insert "From: " (read-string "From: ") "\n"
9040                 "Subject: " (read-string "Subject: ") "\n"
9041                 "Date: " (message-make-date (nth 5 atts)) "\n"
9042                 "Message-ID: " (message-make-message-id) "\n"
9043                 "Lines: " (int-to-string lines) "\n"
9044                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9045       (setq group-art (gnus-request-accept-article group nil t))
9046       (kill-buffer (current-buffer)))
9047     (setq gnus-newsgroup-active (gnus-activate-group group))
9048     (forward-line 1)
9049     (gnus-summary-goto-article (cdr group-art) nil t)
9050     (when edit
9051       (gnus-summary-edit-article))))
9052
9053 (defun gnus-summary-create-article ()
9054   "Create an article in a mail newsgroup."
9055   (interactive)
9056   (let ((group gnus-newsgroup-name)
9057         (now (current-time))
9058         group-art)
9059     (unless (gnus-check-backend-function 'request-accept-article group)
9060       (error "%s does not support article importing" group))
9061     (save-excursion
9062       (set-buffer (gnus-get-buffer-create " *import file*"))
9063       (erase-buffer)
9064       (goto-char (point-min))
9065       ;; This doesn't look like an article, so we fudge some headers.
9066       (insert "From: " (read-string "From: ") "\n"
9067               "Subject: " (read-string "Subject: ") "\n"
9068               "Date: " (message-make-date now) "\n"
9069               "Message-ID: " (message-make-message-id) "\n")
9070       (setq group-art (gnus-request-accept-article group nil t))
9071       (kill-buffer (current-buffer)))
9072     (setq gnus-newsgroup-active (gnus-activate-group group))
9073     (forward-line 1)
9074     (gnus-summary-goto-article (cdr group-art) nil t)
9075     (gnus-summary-edit-article)))
9076
9077 (defun gnus-summary-article-posted-p ()
9078   "Say whether the current (mail) article is available from news as well.
9079 This will be the case if the article has both been mailed and posted."
9080   (interactive)
9081   (let ((id (mail-header-references (gnus-summary-article-header)))
9082         (gnus-override-method (car (gnus-refer-article-methods))))
9083     (if (gnus-request-head id "")
9084         (gnus-message 2 "The current message was found on %s"
9085                       gnus-override-method)
9086       (gnus-message 2 "The current message couldn't be found on %s"
9087                     gnus-override-method)
9088       nil)))
9089
9090 (defun gnus-summary-expire-articles (&optional now)
9091   "Expire all articles that are marked as expirable in the current group."
9092   (interactive)
9093   (when (and (not gnus-group-is-exiting-without-update-p)
9094              (gnus-check-backend-function
9095               'request-expire-articles gnus-newsgroup-name))
9096     ;; This backend supports expiry.
9097     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9098            (expirable (if total
9099                           (progn
9100                             ;; We need to update the info for
9101                             ;; this group for `gnus-list-of-read-articles'
9102                             ;; to give us the right answer.
9103                             (gnus-run-hooks 'gnus-exit-group-hook)
9104                             (gnus-summary-update-info)
9105                             (gnus-list-of-read-articles gnus-newsgroup-name))
9106                         (setq gnus-newsgroup-expirable
9107                               (sort gnus-newsgroup-expirable '<))))
9108            (expiry-wait (if now 'immediate
9109                           (gnus-group-find-parameter
9110                            gnus-newsgroup-name 'expiry-wait)))
9111            (nnmail-expiry-target
9112             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9113                 nnmail-expiry-target))
9114            es)
9115       (when expirable
9116         ;; There are expirable articles in this group, so we run them
9117         ;; through the expiry process.
9118         (gnus-message 6 "Expiring articles...")
9119         (unless (gnus-check-group gnus-newsgroup-name)
9120           (error "Can't open server for %s" gnus-newsgroup-name))
9121         ;; The list of articles that weren't expired is returned.
9122         (save-excursion
9123           (if expiry-wait
9124               (let ((nnmail-expiry-wait-function nil)
9125                     (nnmail-expiry-wait expiry-wait))
9126                 (setq es (gnus-request-expire-articles
9127                           expirable gnus-newsgroup-name)))
9128             (setq es (gnus-request-expire-articles
9129                       expirable gnus-newsgroup-name)))
9130           (unless total
9131             (setq gnus-newsgroup-expirable es))
9132           ;; We go through the old list of expirable, and mark all
9133           ;; really expired articles as nonexistent.
9134           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9135             (let ((gnus-use-cache nil))
9136               (dolist (article expirable)
9137                 (when (and (not (memq article es))
9138                            (gnus-data-find article))
9139                   (gnus-summary-mark-article article gnus-canceled-mark)
9140                   (run-hook-with-args 'gnus-summary-article-expire-hook
9141                                       'delete
9142                                       (gnus-data-header
9143                                        (assoc article (gnus-data-list nil)))
9144                                       gnus-newsgroup-name
9145                                       nil
9146                                       nil))))))
9147         (gnus-message 6 "Expiring articles...done")))))
9148
9149 (defun gnus-summary-expire-articles-now ()
9150   "Expunge all expirable articles in the current group.
9151 This means that *all* articles that are marked as expirable will be
9152 deleted forever, right now."
9153   (interactive)
9154   (or gnus-expert-user
9155       (gnus-yes-or-no-p
9156        "Are you really, really, really sure you want to delete all these messages? ")
9157       (error "Phew!"))
9158   (gnus-summary-expire-articles t))
9159
9160 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9161 (defun gnus-summary-delete-article (&optional n)
9162   "Delete the N next (mail) articles.
9163 This command actually deletes articles.  This is not a marking
9164 command.  The article will disappear forever from your life, never to
9165 return.
9166 If N is negative, delete backwards.
9167 If N is nil and articles have been marked with the process mark,
9168 delete these instead."
9169   (interactive "P")
9170   (unless (gnus-check-backend-function 'request-expire-articles
9171                                        gnus-newsgroup-name)
9172     (error "The current newsgroup does not support article deletion"))
9173   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9174     (error "Couldn't open server"))
9175   ;; Compute the list of articles to delete.
9176   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9177         (nnmail-expiry-target 'delete)
9178         not-deleted)
9179     (if (and gnus-novice-user
9180              (not (gnus-yes-or-no-p
9181                    (format "Do you really want to delete %s forever? "
9182                            (if (> (length articles) 1)
9183                                (format "these %s articles" (length articles))
9184                              "this article")))))
9185         ()
9186       ;; Delete the articles.
9187       (setq not-deleted (gnus-request-expire-articles
9188                          articles gnus-newsgroup-name 'force))
9189       (while articles
9190         (gnus-summary-remove-process-mark (car articles))
9191         ;; The backend might not have been able to delete the article
9192         ;; after all.
9193         (unless (memq (car articles) not-deleted)
9194           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9195         (let* ((article (car articles))
9196                (id (mail-header-id (gnus-data-header 
9197                                     (assoc article (gnus-data-list nil))))))
9198           (run-hook-with-args 'gnus-summary-article-delete-hook
9199                               'delete id gnus-newsgroup-name nil
9200                               nil))
9201         (setq articles (cdr articles)))
9202       (when not-deleted
9203         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9204     (gnus-summary-position-point)
9205     (gnus-set-mode-line 'summary)
9206     not-deleted))
9207
9208 (defun gnus-summary-edit-article (&optional arg)
9209   "Edit the current article.
9210 This will have permanent effect only in mail groups.
9211 If ARG is nil, edit the decoded articles.
9212 If ARG is 1, edit the raw articles.
9213 If ARG is 2, edit the raw articles even in read-only groups.
9214 If ARG is 3, edit the articles with the current handles.
9215 Otherwise, allow editing of articles even in read-only
9216 groups."
9217   (interactive "P")
9218   (let (force raw current-handles)
9219     (cond
9220      ((null arg))
9221      ((eq arg 1)
9222       (setq raw t))
9223      ((eq arg 2)
9224       (setq raw t
9225             force t))
9226      ((eq arg 3)
9227       (setq current-handles
9228             (and (gnus-buffer-live-p gnus-article-buffer)
9229                  (with-current-buffer gnus-article-buffer
9230                    (prog1
9231                        gnus-article-mime-handles
9232                      (setq gnus-article-mime-handles nil))))))
9233      (t
9234       (setq force t)))
9235     (when (and raw (not force)
9236                (member gnus-newsgroup-name '("nndraft:delayed"
9237                                              "nndraft:drafts"
9238                                              "nndraft:queue")))
9239       (error "Can't edit the raw article in group %s"
9240              gnus-newsgroup-name))
9241     (save-excursion
9242       (set-buffer gnus-summary-buffer)
9243       (let ((mail-parse-charset gnus-newsgroup-charset)
9244             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9245         (gnus-set-global-variables)
9246         (when (and (not force)
9247                    (gnus-group-read-only-p))
9248           (error "The current newsgroup does not support article editing"))
9249         (gnus-summary-show-article t)
9250         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9251           (with-current-buffer gnus-article-buffer
9252             (mm-enable-multibyte)))
9253         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9254             (setq raw t))
9255         (gnus-article-edit-article
9256          (if raw 'ignore
9257            `(lambda ()
9258               (let ((mbl mml-buffer-list))
9259                 (setq mml-buffer-list nil)
9260                 (mime-to-mml ,'current-handles)
9261                 (let ((mbl1 mml-buffer-list))
9262                   (setq mml-buffer-list mbl)
9263                   (set (make-local-variable 'mml-buffer-list) mbl1))
9264                 (make-local-hook 'kill-buffer-hook)
9265                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9266          `(lambda (no-highlight)
9267             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9268                   (message-options message-options)
9269                   (message-options-set-recipient)
9270                   (mail-parse-ignored-charsets
9271                    ',gnus-newsgroup-ignored-charsets))
9272               ,(if (not raw) '(progn
9273                                 (mml-to-mime)
9274                                 (mml-destroy-buffers)
9275                                 (remove-hook 'kill-buffer-hook
9276                                              'mml-destroy-buffers t)
9277                                 (kill-local-variable 'mml-buffer-list)))
9278               (gnus-summary-edit-article-done
9279                ,(or (mail-header-references gnus-current-headers) "")
9280                ,(gnus-group-read-only-p)
9281                ,gnus-summary-buffer no-highlight))))))))
9282
9283 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9284
9285 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9286                                                  no-highlight)
9287   "Make edits to the current article permanent."
9288   (interactive)
9289   (save-excursion
9290    ;; The buffer restriction contains the entire article if it exists.
9291     (when (article-goto-body)
9292       (let ((lines (count-lines (point) (point-max)))
9293             (length (- (point-max) (point)))
9294             (case-fold-search t)
9295             (body (copy-marker (point))))
9296         (goto-char (point-min))
9297         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9298           (delete-region (match-beginning 1) (match-end 1))
9299           (insert (number-to-string length)))
9300         (goto-char (point-min))
9301         (when (re-search-forward
9302                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9303           (delete-region (match-beginning 1) (match-end 1))
9304           (insert (number-to-string length)))
9305         (goto-char (point-min))
9306         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9307           (delete-region (match-beginning 1) (match-end 1))
9308           (insert (number-to-string lines))))))
9309   ;; Replace the article.
9310   (let ((buf (current-buffer)))
9311     (with-temp-buffer
9312       (insert-buffer-substring buf)
9313
9314       (if (and (not read-only)
9315                (not (gnus-request-replace-article
9316                      (cdr gnus-article-current) (car gnus-article-current)
9317                      (current-buffer) t)))
9318           (error "Couldn't replace article")
9319         ;; Update the summary buffer.
9320         (if (and references
9321                  (equal (message-tokenize-header references " ")
9322                         (message-tokenize-header
9323                          (or (message-fetch-field "references") "") " ")))
9324             ;; We only have to update this line.
9325             (save-excursion
9326               (save-restriction
9327                 (message-narrow-to-head)
9328                 (let ((head (buffer-string))
9329                       header)
9330                   (with-temp-buffer
9331                     (insert (format "211 %d Article retrieved.\n"
9332                                     (cdr gnus-article-current)))
9333                     (insert head)
9334                     (insert ".\n")
9335                     (let ((nntp-server-buffer (current-buffer)))
9336                       (setq header (car (gnus-get-newsgroup-headers
9337                                          nil t))))
9338                     (save-excursion
9339                       (set-buffer gnus-summary-buffer)
9340                       (gnus-data-set-header
9341                        (gnus-data-find (cdr gnus-article-current))
9342                        header)
9343                       (gnus-summary-update-article-line
9344                        (cdr gnus-article-current) header)
9345                       (if (gnus-summary-goto-subject
9346                            (cdr gnus-article-current) nil t)
9347                           (gnus-summary-update-secondary-mark
9348                            (cdr gnus-article-current))))))))
9349           ;; Update threads.
9350           (set-buffer (or buffer gnus-summary-buffer))
9351           (gnus-summary-update-article (cdr gnus-article-current))
9352           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9353               (gnus-summary-update-secondary-mark
9354                (cdr gnus-article-current))))
9355         ;; Prettify the article buffer again.
9356         (unless no-highlight
9357           (save-excursion
9358             (set-buffer gnus-article-buffer)
9359             ;;;!!! Fix this -- article should be rehighlighted.
9360             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9361             (set-buffer gnus-original-article-buffer)
9362             (gnus-request-article
9363              (cdr gnus-article-current)
9364              (car gnus-article-current) (current-buffer))))
9365         ;; Prettify the summary buffer line.
9366         (when (gnus-visual-p 'summary-highlight 'highlight)
9367           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9368
9369 (defun gnus-summary-edit-wash (key)
9370   "Perform editing command KEY in the article buffer."
9371   (interactive
9372    (list
9373     (progn
9374       (message "%s" (concat (this-command-keys) "- "))
9375       (read-char))))
9376   (message "")
9377   (gnus-summary-edit-article)
9378   (execute-kbd-macro (concat (this-command-keys) key))
9379   (gnus-article-edit-done))
9380
9381 ;;; Respooling
9382
9383 (defun gnus-summary-respool-query (&optional silent trace)
9384   "Query where the respool algorithm would put this article."
9385   (interactive)
9386   (let (gnus-mark-article-hook)
9387     (gnus-summary-select-article)
9388     (save-excursion
9389       (set-buffer gnus-original-article-buffer)
9390       (save-restriction
9391         (message-narrow-to-head)
9392         (let ((groups (nnmail-article-group 'identity trace)))
9393           (unless silent
9394             (if groups
9395                 (message "This message would go to %s"
9396                          (mapconcat 'car groups ", "))
9397               (message "This message would go to no groups"))
9398             groups))))))
9399
9400 (defun gnus-summary-respool-trace ()
9401   "Trace where the respool algorithm would put this article.
9402 Display a buffer showing all fancy splitting patterns which matched."
9403   (interactive)
9404   (gnus-summary-respool-query nil t))
9405
9406 ;; Summary marking commands.
9407
9408 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9409   "Mark articles which has the same subject as read, and then select the next.
9410 If UNMARK is positive, remove any kind of mark.
9411 If UNMARK is negative, tick articles."
9412   (interactive "P")
9413   (when unmark
9414     (setq unmark (prefix-numeric-value unmark)))
9415   (let ((count
9416          (gnus-summary-mark-same-subject
9417           (gnus-summary-article-subject) unmark)))
9418     ;; Select next unread article.  If auto-select-same mode, should
9419     ;; select the first unread article.
9420     (gnus-summary-next-article t (and gnus-auto-select-same
9421                                       (gnus-summary-article-subject)))
9422     (gnus-message 7 "%d article%s marked as %s"
9423                   count (if (= count 1) " is" "s are")
9424                   (if unmark "unread" "read"))))
9425
9426 (defun gnus-summary-kill-same-subject (&optional unmark)
9427   "Mark articles which has the same subject as read.
9428 If UNMARK is positive, remove any kind of mark.
9429 If UNMARK is negative, tick articles."
9430   (interactive "P")
9431   (when unmark
9432     (setq unmark (prefix-numeric-value unmark)))
9433   (let ((count
9434          (gnus-summary-mark-same-subject
9435           (gnus-summary-article-subject) unmark)))
9436     ;; If marked as read, go to next unread subject.
9437     (when (null unmark)
9438       ;; Go to next unread subject.
9439       (gnus-summary-next-subject 1 t))
9440     (gnus-message 7 "%d articles are marked as %s"
9441                   count (if unmark "unread" "read"))))
9442
9443 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9444   "Mark articles with same SUBJECT as read, and return marked number.
9445 If optional argument UNMARK is positive, remove any kinds of marks.
9446 If optional argument UNMARK is negative, mark articles as unread instead."
9447   (let ((count 1))
9448     (save-excursion
9449       (cond
9450        ((null unmark)                   ; Mark as read.
9451         (while (and
9452                 (progn
9453                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9454                   (gnus-summary-show-thread) t)
9455                 (gnus-summary-find-subject subject))
9456           (setq count (1+ count))))
9457        ((> unmark 0)                    ; Tick.
9458         (while (and
9459                 (progn
9460                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9461                   (gnus-summary-show-thread) t)
9462                 (gnus-summary-find-subject subject))
9463           (setq count (1+ count))))
9464        (t                               ; Mark as unread.
9465         (while (and
9466                 (progn
9467                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9468                   (gnus-summary-show-thread) t)
9469                 (gnus-summary-find-subject subject))
9470           (setq count (1+ count)))))
9471       (gnus-set-mode-line 'summary)
9472       ;; Return the number of marked articles.
9473       count)))
9474
9475 (defun gnus-summary-mark-as-processable (n &optional unmark)
9476   "Set the process mark on the next N articles.
9477 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9478 the process mark instead.  The difference between N and the actual
9479 number of articles marked is returned."
9480   (interactive "P")
9481   (if (and (null n) (gnus-region-active-p))
9482       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9483     (setq n (prefix-numeric-value n))
9484     (let ((backward (< n 0))
9485           (n (abs n)))
9486       (while (and
9487               (> n 0)
9488               (if unmark
9489                   (gnus-summary-remove-process-mark
9490                    (gnus-summary-article-number))
9491                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9492               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9493         (setq n (1- n)))
9494       (when (/= 0 n)
9495         (gnus-message 7 "No more articles"))
9496       (gnus-summary-recenter)
9497       (gnus-summary-position-point)
9498       n)))
9499
9500 (defun gnus-summary-unmark-as-processable (n)
9501   "Remove the process mark from the next N articles.
9502 If N is negative, unmark backward instead.  The difference between N and
9503 the actual number of articles unmarked is returned."
9504   (interactive "P")
9505   (gnus-summary-mark-as-processable n t))
9506
9507 (defun gnus-summary-unmark-all-processable ()
9508   "Remove the process mark from all articles."
9509   (interactive)
9510   (save-excursion
9511     (while gnus-newsgroup-processable
9512       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9513   (gnus-summary-position-point))
9514
9515 (defun gnus-summary-add-mark (article type)
9516   "Mark ARTICLE with a mark of TYPE."
9517   (let ((vtype (car (assq type gnus-article-mark-lists)))
9518         var)
9519     (if (not vtype)
9520         (error "No such mark type: %s" type)
9521       (setq var (intern (format "gnus-newsgroup-%s" type)))
9522       (set var (cons article (symbol-value var)))
9523       (if (memq type '(processable cached replied forwarded recent saved))
9524           (gnus-summary-update-secondary-mark article)
9525         ;;; !!! This is bogus.  We should find out what primary
9526         ;;; !!! mark we want to set.
9527         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9528
9529 (defun gnus-summary-mark-as-expirable (n)
9530   "Mark N articles forward as expirable.
9531 If N is negative, mark backward instead.  The difference between N and
9532 the actual number of articles marked is returned."
9533   (interactive "p")
9534   (gnus-summary-mark-forward n gnus-expirable-mark))
9535
9536 (defun gnus-summary-mark-as-spam (n)
9537   "Mark N articles forward as spam.
9538 If N is negative, mark backward instead.  The difference between N and
9539 the actual number of articles marked is returned."
9540   (interactive "p")
9541   (gnus-summary-mark-forward n gnus-spam-mark))
9542
9543 (defun gnus-summary-mark-article-as-replied (article)
9544   "Mark ARTICLE as replied to and update the summary line.
9545 ARTICLE can also be a list of articles."
9546   (interactive (list (gnus-summary-article-number)))
9547   (let ((articles (if (listp article) article (list article))))
9548     (dolist (article articles)
9549       (unless (numberp article)
9550         (error "%s is not a number" article))
9551       (push article gnus-newsgroup-replied)
9552       (let ((buffer-read-only nil))
9553         (when (gnus-summary-goto-subject article nil t)
9554           (gnus-summary-update-secondary-mark article))))))
9555
9556 (defun gnus-summary-mark-article-as-forwarded (article)
9557   "Mark ARTICLE as forwarded and update the summary line.
9558 ARTICLE can also be a list of articles."
9559   (let ((articles (if (listp article) article (list article))))
9560     (dolist (article articles)
9561       (push article gnus-newsgroup-forwarded)
9562       (let ((buffer-read-only nil))
9563         (when (gnus-summary-goto-subject article nil t)
9564           (gnus-summary-update-secondary-mark article))))))
9565
9566 (defun gnus-summary-set-bookmark (article)
9567   "Set a bookmark in current article."
9568   (interactive (list (gnus-summary-article-number)))
9569   (when (or (not (get-buffer gnus-article-buffer))
9570             (not gnus-current-article)
9571             (not gnus-article-current)
9572             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9573     (error "No current article selected"))
9574   ;; Remove old bookmark, if one exists.
9575   (gnus-pull article gnus-newsgroup-bookmarks)
9576   ;; Set the new bookmark, which is on the form
9577   ;; (article-number . line-number-in-body).
9578   (push
9579    (cons article
9580          (save-excursion
9581            (set-buffer gnus-article-buffer)
9582            (count-lines
9583             (min (point)
9584                  (save-excursion
9585                    (article-goto-body)
9586                    (point)))
9587             (point))))
9588    gnus-newsgroup-bookmarks)
9589   (gnus-message 6 "A bookmark has been added to the current article."))
9590
9591 (defun gnus-summary-remove-bookmark (article)
9592   "Remove the bookmark from the current article."
9593   (interactive (list (gnus-summary-article-number)))
9594   ;; Remove old bookmark, if one exists.
9595   (if (not (assq article gnus-newsgroup-bookmarks))
9596       (gnus-message 6 "No bookmark in current article.")
9597     (gnus-pull article gnus-newsgroup-bookmarks)
9598     (gnus-message 6 "Removed bookmark.")))
9599
9600 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9601 (defun gnus-summary-mark-as-dormant (n)
9602   "Mark N articles forward as dormant.
9603 If N is negative, mark backward instead.  The difference between N and
9604 the actual number of articles marked is returned."
9605   (interactive "p")
9606   (gnus-summary-mark-forward n gnus-dormant-mark))
9607
9608 (defun gnus-summary-set-process-mark (article)
9609   "Set the process mark on ARTICLE and update the summary line."
9610   (setq gnus-newsgroup-processable
9611         (cons article
9612               (delq article gnus-newsgroup-processable)))
9613   (when (gnus-summary-goto-subject article)
9614     (gnus-summary-show-thread)
9615     (gnus-summary-goto-subject article)
9616     (gnus-summary-update-secondary-mark article)))
9617
9618 (defun gnus-summary-remove-process-mark (article)
9619   "Remove the process mark from ARTICLE and update the summary line."
9620   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9621   (when (gnus-summary-goto-subject article)
9622     (gnus-summary-show-thread)
9623     (gnus-summary-goto-subject article)
9624     (gnus-summary-update-secondary-mark article)))
9625
9626 (defun gnus-summary-set-saved-mark (article)
9627   "Set the process mark on ARTICLE and update the summary line."
9628   (push article gnus-newsgroup-saved)
9629   (when (gnus-summary-goto-subject article)
9630     (gnus-summary-update-secondary-mark article)))
9631
9632 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9633   "Mark N articles as read forwards.
9634 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9635 The difference between N and the actual number of articles marked is
9636 returned.
9637 If NO-EXPIRE, auto-expiry will be inhibited."
9638   (interactive "p")
9639   (gnus-summary-show-thread)
9640   (let ((backward (< n 0))
9641         (gnus-summary-goto-unread
9642          (and gnus-summary-goto-unread
9643               (not (eq gnus-summary-goto-unread 'never))
9644               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9645                                     gnus-ticked-mark gnus-dormant-mark)))))
9646         (n (abs n))
9647         (mark (or mark gnus-del-mark)))
9648     (while (and (> n 0)
9649                 (gnus-summary-mark-article nil mark no-expire)
9650                 (zerop (gnus-summary-next-subject
9651                         (if backward -1 1)
9652                         (and gnus-summary-goto-unread
9653                              (not (eq gnus-summary-goto-unread 'never)))
9654                         t)))
9655       (setq n (1- n)))
9656     (when (/= 0 n)
9657       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9658     (gnus-summary-recenter)
9659     (gnus-summary-position-point)
9660     (gnus-set-mode-line 'summary)
9661     n))
9662
9663 (defun gnus-summary-mark-article-as-read (mark)
9664   "Mark the current article quickly as read with MARK."
9665   (let ((article (gnus-summary-article-number)))
9666     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9667     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9668     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9669     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9670     (push (cons article mark) gnus-newsgroup-reads)
9671     ;; Possibly remove from cache, if that is used.
9672     (when gnus-use-cache
9673       (gnus-cache-enter-remove-article article))
9674     ;; Allow the backend to change the mark.
9675     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9676     ;; Check for auto-expiry.
9677     (when (and gnus-newsgroup-auto-expire
9678                (memq mark gnus-auto-expirable-marks))
9679       (setq mark gnus-expirable-mark)
9680       ;; Let the backend know about the mark change.
9681       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9682       (push article gnus-newsgroup-expirable))
9683     ;; Set the mark in the buffer.
9684     (gnus-summary-update-mark mark 'unread)
9685     t))
9686
9687 (defun gnus-summary-mark-article-as-unread (mark)
9688   "Mark the current article quickly as unread with MARK."
9689   (let* ((article (gnus-summary-article-number))
9690          (old-mark (gnus-summary-article-mark article)))
9691     ;; Allow the backend to change the mark.
9692     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9693     (if (eq mark old-mark)
9694         t
9695       (if (<= article 0)
9696           (progn
9697             (gnus-error 1 "Can't mark negative article numbers")
9698             nil)
9699         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9700         (setq gnus-newsgroup-spam-marked
9701               (delq article gnus-newsgroup-spam-marked))
9702         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9703         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9704         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9705         (cond ((= mark gnus-ticked-mark)
9706                (setq gnus-newsgroup-marked
9707                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9708                                               article)))
9709               ((= mark gnus-spam-mark)
9710                (setq gnus-newsgroup-spam-marked
9711                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9712                                               article)))
9713               ((= mark gnus-dormant-mark)
9714                (setq gnus-newsgroup-dormant
9715                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9716                                               article)))
9717               (t
9718                (setq gnus-newsgroup-unreads
9719                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9720                                               article))))
9721         (gnus-pull article gnus-newsgroup-reads)
9722
9723         ;; See whether the article is to be put in the cache.
9724         (and gnus-use-cache
9725              (vectorp (gnus-summary-article-header article))
9726              (save-excursion
9727                (gnus-cache-possibly-enter-article
9728                 gnus-newsgroup-name article
9729                 (= mark gnus-ticked-mark)
9730                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9731
9732         ;; Fix the mark.
9733         (gnus-summary-update-mark mark 'unread)
9734         t))))
9735
9736 (defun gnus-summary-mark-article (&optional article mark no-expire)
9737   "Mark ARTICLE with MARK.  MARK can be any character.
9738 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9739 `??' (dormant) and `?E' (expirable).
9740 If MARK is nil, then the default character `?r' is used.
9741 If ARTICLE is nil, then the article on the current line will be
9742 marked.
9743 If NO-EXPIRE, auto-expiry will be inhibited."
9744   ;; The mark might be a string.
9745   (when (stringp mark)
9746     (setq mark (aref mark 0)))
9747   ;; If no mark is given, then we check auto-expiring.
9748   (when (null mark)
9749     (setq mark gnus-del-mark))
9750   (when (and (not no-expire)
9751              gnus-newsgroup-auto-expire
9752              (memq mark gnus-auto-expirable-marks))
9753     (setq mark gnus-expirable-mark))
9754   (let ((article (or article (gnus-summary-article-number)))
9755         (old-mark (gnus-summary-article-mark article)))
9756     ;; Allow the backend to change the mark.
9757     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9758     (if (eq mark old-mark)
9759         t
9760       (unless article
9761         (error "No article on current line"))
9762       (if (not (if (or (= mark gnus-unread-mark)
9763                        (= mark gnus-ticked-mark)
9764                        (= mark gnus-spam-mark)
9765                        (= mark gnus-dormant-mark))
9766                    (gnus-mark-article-as-unread article mark)
9767                  (gnus-mark-article-as-read article mark)))
9768           t
9769         ;; See whether the article is to be put in the cache.
9770         (and gnus-use-cache
9771              (not (= mark gnus-canceled-mark))
9772              (vectorp (gnus-summary-article-header article))
9773              (save-excursion
9774                (gnus-cache-possibly-enter-article
9775                 gnus-newsgroup-name article
9776                 (= mark gnus-ticked-mark)
9777                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9778
9779         (when (gnus-summary-goto-subject article nil t)
9780           (let ((buffer-read-only nil))
9781             (gnus-summary-show-thread)
9782             ;; Fix the mark.
9783             (gnus-summary-update-mark mark 'unread)
9784             t))))))
9785
9786 (defun gnus-summary-update-secondary-mark (article)
9787   "Update the secondary (read, process, cache) mark."
9788   (gnus-summary-update-mark
9789    (cond ((memq article gnus-newsgroup-processable)
9790           gnus-process-mark)
9791          ((memq article gnus-newsgroup-cached)
9792           gnus-cached-mark)
9793          ((memq article gnus-newsgroup-replied)
9794           gnus-replied-mark)
9795          ((memq article gnus-newsgroup-forwarded)
9796           gnus-forwarded-mark)
9797          ((memq article gnus-newsgroup-saved)
9798           gnus-saved-mark)
9799          ((memq article gnus-newsgroup-recent)
9800           gnus-recent-mark)
9801          ((memq article gnus-newsgroup-unseen)
9802           gnus-unseen-mark)
9803          (t gnus-no-mark))
9804    'replied)
9805   (when (gnus-visual-p 'summary-highlight 'highlight)
9806     (gnus-run-hooks 'gnus-summary-update-hook))
9807   t)
9808
9809 (defun gnus-summary-update-download-mark (article)
9810   "Update the download mark."
9811   (gnus-summary-update-mark
9812    (cond ((memq article gnus-newsgroup-undownloaded) 
9813           gnus-undownloaded-mark)
9814          (gnus-newsgroup-agentized
9815           gnus-downloaded-mark)
9816          (t
9817           gnus-no-mark))
9818    'download)
9819   (gnus-summary-update-line t)
9820   t)
9821
9822 (defun gnus-summary-update-mark (mark type)
9823   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9824         (buffer-read-only nil))
9825     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9826     (when forward
9827       (when (looking-at "\r")
9828         (incf forward))
9829       (when (<= (+ forward (point)) (point-max))
9830         ;; Go to the right position on the line.
9831         (goto-char (+ forward (point)))
9832         ;; Replace the old mark with the new mark.
9833         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9834         ;; Optionally update the marks by some user rule.
9835         (when (eq type 'unread)
9836           (gnus-data-set-mark
9837            (gnus-data-find (gnus-summary-article-number)) mark)
9838           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9839
9840 (defun gnus-mark-article-as-read (article &optional mark)
9841   "Enter ARTICLE in the pertinent lists and remove it from others."
9842   ;; Make the article expirable.
9843   (let ((mark (or mark gnus-del-mark)))
9844     (setq gnus-newsgroup-expirable
9845           (if (= mark gnus-expirable-mark)
9846               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9847             (delq article gnus-newsgroup-expirable)))
9848     ;; Remove from unread and marked lists.
9849     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9850     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9851     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9852     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9853     (push (cons article mark) gnus-newsgroup-reads)
9854     ;; Possibly remove from cache, if that is used.
9855     (when gnus-use-cache
9856       (gnus-cache-enter-remove-article article))
9857     t))
9858
9859 (defun gnus-mark-article-as-unread (article &optional mark)
9860   "Enter ARTICLE in the pertinent lists and remove it from others."
9861   (let ((mark (or mark gnus-ticked-mark)))
9862     (if (<= article 0)
9863         (progn
9864           (gnus-error 1 "Can't mark negative article numbers")
9865           nil)
9866       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9867             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9868             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9869             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9870             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9871
9872       ;; Unsuppress duplicates?
9873       (when gnus-suppress-duplicates
9874         (gnus-dup-unsuppress-article article))
9875
9876       (cond ((= mark gnus-ticked-mark)
9877              (setq gnus-newsgroup-marked
9878                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9879             ((= mark gnus-spam-mark)
9880              (setq gnus-newsgroup-spam-marked
9881                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9882                                             article)))
9883             ((= mark gnus-dormant-mark)
9884              (setq gnus-newsgroup-dormant
9885                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9886             (t
9887              (setq gnus-newsgroup-unreads
9888                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9889       (gnus-pull article gnus-newsgroup-reads)
9890       t)))
9891
9892 (defalias 'gnus-summary-mark-as-unread-forward
9893   'gnus-summary-tick-article-forward)
9894 (make-obsolete 'gnus-summary-mark-as-unread-forward
9895                'gnus-summary-tick-article-forward)
9896 (defun gnus-summary-tick-article-forward (n)
9897   "Tick N articles forwards.
9898 If N is negative, tick backwards instead.
9899 The difference between N and the number of articles ticked is returned."
9900   (interactive "p")
9901   (gnus-summary-mark-forward n gnus-ticked-mark))
9902
9903 (defalias 'gnus-summary-mark-as-unread-backward
9904   'gnus-summary-tick-article-backward)
9905 (make-obsolete 'gnus-summary-mark-as-unread-backward
9906                'gnus-summary-tick-article-backward)
9907 (defun gnus-summary-tick-article-backward (n)
9908   "Tick N articles backwards.
9909 The difference between N and the number of articles ticked is returned."
9910   (interactive "p")
9911   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9912
9913 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9914 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9915 (defun gnus-summary-tick-article (&optional article clear-mark)
9916   "Mark current article as unread.
9917 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9918 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9919   (interactive)
9920   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9921                                        gnus-ticked-mark)))
9922
9923 (defun gnus-summary-mark-as-read-forward (n)
9924   "Mark N articles as read forwards.
9925 If N is negative, mark backwards instead.
9926 The difference between N and the actual number of articles marked is
9927 returned."
9928   (interactive "p")
9929   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9930
9931 (defun gnus-summary-mark-as-read-backward (n)
9932   "Mark the N articles as read backwards.
9933 The difference between N and the actual number of articles marked is
9934 returned."
9935   (interactive "p")
9936   (gnus-summary-mark-forward
9937    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9938
9939 (defun gnus-summary-mark-as-read (&optional article mark)
9940   "Mark current article as read.
9941 ARTICLE specifies the article to be marked as read.
9942 MARK specifies a string to be inserted at the beginning of the line."
9943   (gnus-summary-mark-article article mark))
9944
9945 (defun gnus-summary-clear-mark-forward (n)
9946   "Clear marks from N articles forward.
9947 If N is negative, clear backward instead.
9948 The difference between N and the number of marks cleared is returned."
9949   (interactive "p")
9950   (gnus-summary-mark-forward n gnus-unread-mark))
9951
9952 (defun gnus-summary-clear-mark-backward (n)
9953   "Clear marks from N articles backward.
9954 The difference between N and the number of marks cleared is returned."
9955   (interactive "p")
9956   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9957
9958 (defun gnus-summary-mark-unread-as-read ()
9959   "Intended to be used by `gnus-summary-mark-article-hook'."
9960   (when (memq gnus-current-article gnus-newsgroup-unreads)
9961     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9962
9963 (defun gnus-summary-mark-read-and-unread-as-read ()
9964   "Intended to be used by `gnus-summary-mark-article-hook'."
9965   (let ((mark (gnus-summary-article-mark)))
9966     (when (or (gnus-unread-mark-p mark)
9967               (gnus-read-mark-p mark))
9968       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9969
9970 (defun gnus-summary-mark-unread-as-ticked ()
9971   "Intended to be used by `gnus-summary-mark-article-hook'."
9972   (when (memq gnus-current-article gnus-newsgroup-unreads)
9973     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9974
9975 (defun gnus-summary-mark-region-as-read (point mark all)
9976   "Mark all unread articles between point and mark as read.
9977 If given a prefix, mark all articles between point and mark as read,
9978 even ticked and dormant ones."
9979   (interactive "r\nP")
9980   (save-excursion
9981     (let (article)
9982       (goto-char point)
9983       (beginning-of-line)
9984       (while (and
9985               (< (point) mark)
9986               (progn
9987                 (when (or all
9988                           (memq (setq article (gnus-summary-article-number))
9989                                 gnus-newsgroup-unreads))
9990                   (gnus-summary-mark-article article gnus-del-mark))
9991                 t)
9992               (gnus-summary-find-next))))))
9993
9994 (defun gnus-summary-mark-below (score mark)
9995   "Mark articles with score less than SCORE with MARK."
9996   (interactive "P\ncMark: ")
9997   (setq score (if score
9998                   (prefix-numeric-value score)
9999                 (or gnus-summary-default-score 0)))
10000   (save-excursion
10001     (set-buffer gnus-summary-buffer)
10002     (goto-char (point-min))
10003     (while
10004         (progn
10005           (and (< (gnus-summary-article-score) score)
10006                (gnus-summary-mark-article nil mark))
10007           (gnus-summary-find-next)))))
10008
10009 (defun gnus-summary-kill-below (&optional score)
10010   "Mark articles with score below SCORE as read."
10011   (interactive "P")
10012   (gnus-summary-mark-below score gnus-killed-mark))
10013
10014 (defun gnus-summary-clear-above (&optional score)
10015   "Clear all marks from articles with score above SCORE."
10016   (interactive "P")
10017   (gnus-summary-mark-above score gnus-unread-mark))
10018
10019 (defun gnus-summary-tick-above (&optional score)
10020   "Tick all articles with score above SCORE."
10021   (interactive "P")
10022   (gnus-summary-mark-above score gnus-ticked-mark))
10023
10024 (defun gnus-summary-mark-above (score mark)
10025   "Mark articles with score over SCORE with MARK."
10026   (interactive "P\ncMark: ")
10027   (setq score (if score
10028                   (prefix-numeric-value score)
10029                 (or gnus-summary-default-score 0)))
10030   (save-excursion
10031     (set-buffer gnus-summary-buffer)
10032     (goto-char (point-min))
10033     (while (and (progn
10034                   (when (> (gnus-summary-article-score) score)
10035                     (gnus-summary-mark-article nil mark))
10036                   t)
10037                 (gnus-summary-find-next)))))
10038
10039 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10040 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10041 (defun gnus-summary-limit-include-expunged (&optional no-error)
10042   "Display all the hidden articles that were expunged for low scores."
10043   (interactive)
10044   (let ((buffer-read-only nil))
10045     (let ((scored gnus-newsgroup-scored)
10046           headers h)
10047       (while scored
10048         (unless (gnus-summary-article-header (caar scored))
10049           (and (setq h (gnus-number-to-header (caar scored)))
10050                (< (cdar scored) gnus-summary-expunge-below)
10051                (push h headers)))
10052         (setq scored (cdr scored)))
10053       (if (not headers)
10054           (when (not no-error)
10055             (error "No expunged articles hidden"))
10056         (goto-char (point-min))
10057         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10058         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10059         (mapcar (lambda (x) (push (mail-header-number x)
10060                                   gnus-newsgroup-limit))
10061                 headers)
10062         (gnus-summary-prepare-unthreaded (nreverse headers))
10063         (goto-char (point-min))
10064         (gnus-summary-position-point)
10065         t))))
10066
10067 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10068   "Mark all unread articles in this newsgroup as read.
10069 If prefix argument ALL is non-nil, ticked and dormant articles will
10070 also be marked as read.
10071 If QUIETLY is non-nil, no questions will be asked.
10072 If TO-HERE is non-nil, it should be a point in the buffer.  All
10073 articles before (after, if REVERSE is set) this point will be marked as read.
10074 Note that this function will only catch up the unread article
10075 in the current summary buffer limitation.
10076 The number of articles marked as read is returned."
10077   (interactive "P")
10078   (prog1
10079       (save-excursion
10080         (when (or quietly
10081                   (not gnus-interactive-catchup) ;Without confirmation?
10082                   gnus-expert-user
10083                   (gnus-y-or-n-p
10084                    (if all
10085                        "Mark absolutely all articles as read? "
10086                      "Mark all unread articles as read? ")))
10087           (if (and not-mark
10088                    (not gnus-newsgroup-adaptive)
10089                    (not gnus-newsgroup-auto-expire)
10090                    (not gnus-suppress-duplicates)
10091                    (or (not gnus-use-cache)
10092                        (eq gnus-use-cache 'passive)))
10093               (progn
10094                 (when all
10095                   (setq gnus-newsgroup-marked nil
10096                         gnus-newsgroup-spam-marked nil
10097                         gnus-newsgroup-dormant nil))
10098                 (setq gnus-newsgroup-unreads
10099                       (gnus-sorted-nunion
10100                        (gnus-intersection gnus-newsgroup-unreads
10101                                           gnus-newsgroup-downloadable)
10102                        gnus-newsgroup-unfetched)))
10103             ;; We actually mark all articles as canceled, which we
10104             ;; have to do when using auto-expiry or adaptive scoring.
10105             (gnus-summary-show-all-threads)
10106             (if (and to-here reverse)
10107                 (progn
10108                   (goto-char to-here)
10109                   (while (and
10110                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
10111                           (gnus-summary-find-next (not all)))))
10112               (when (gnus-summary-first-subject (not all))
10113                 (while (and
10114                         (if to-here (< (point) to-here) t)
10115                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10116                         (gnus-summary-find-next (not all))))))
10117             (gnus-set-mode-line 'summary))
10118           t))
10119     (gnus-summary-position-point)))
10120
10121 (defun gnus-summary-catchup-to-here (&optional all)
10122   "Mark all unticked articles before the current one as read.
10123 If ALL is non-nil, also mark ticked and dormant articles as read."
10124   (interactive "P")
10125   (save-excursion
10126     (gnus-save-hidden-threads
10127       (let ((beg (point)))
10128         ;; We check that there are unread articles.
10129         (when (or all (gnus-summary-find-prev))
10130           (gnus-summary-catchup all t beg)))))
10131   (gnus-summary-position-point))
10132
10133 (defun gnus-summary-catchup-from-here (&optional all)
10134   "Mark all unticked articles after the current one as read.
10135 If ALL is non-nil, also mark ticked and dormant articles as read."
10136   (interactive "P")
10137   (save-excursion
10138     (gnus-save-hidden-threads
10139       (let ((beg (point)))
10140         ;; We check that there are unread articles.
10141         (when (or all (gnus-summary-find-next))
10142           (gnus-summary-catchup all t beg nil t)))))
10143
10144   (gnus-summary-position-point))
10145 (defun gnus-summary-catchup-all (&optional quietly)
10146   "Mark all articles in this newsgroup as read.
10147 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10148 instead, which marks only unread articles as read."
10149   (interactive "P")
10150   (gnus-summary-catchup t quietly))
10151
10152 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10153   "Mark all unread articles in this group as read, then exit.
10154 If prefix argument ALL is non-nil, all articles are marked as read.
10155 If QUIETLY is non-nil, no questions will be asked."
10156   (interactive "P")
10157   (when (gnus-summary-catchup all quietly nil 'fast)
10158     ;; Select next newsgroup or exit.
10159     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10160              (eq gnus-auto-select-next 'quietly))
10161         (gnus-summary-next-group nil)
10162       (gnus-summary-exit))))
10163
10164 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10165   "Mark all articles in this newsgroup as read, and then exit.
10166 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10167 instead, which marks only unread articles as read."
10168   (interactive "P")
10169   (gnus-summary-catchup-and-exit t quietly))
10170
10171 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10172   "Mark all articles in this group as read and select the next group.
10173 If given a prefix, mark all articles, unread as well as ticked, as
10174 read."
10175   (interactive "P")
10176   (save-excursion
10177     (gnus-summary-catchup all))
10178   (gnus-summary-next-group))
10179
10180 ;;;
10181 ;;; with article
10182 ;;;
10183
10184 (defmacro gnus-with-article (article &rest forms)
10185   "Select ARTICLE and perform FORMS in the original article buffer.
10186 Then replace the article with the result."
10187   `(progn
10188      ;; We don't want the article to be marked as read.
10189      (let (gnus-mark-article-hook)
10190        (gnus-summary-select-article t t nil ,article))
10191      (set-buffer gnus-original-article-buffer)
10192      ,@forms
10193      (if (not (gnus-check-backend-function
10194                'request-replace-article (car gnus-article-current)))
10195          (gnus-message 5 "Read-only group; not replacing")
10196        (unless (gnus-request-replace-article
10197                 ,article (car gnus-article-current)
10198                 (current-buffer) t)
10199          (error "Couldn't replace article")))
10200      ;; The cache and backlog have to be flushed somewhat.
10201      (when gnus-keep-backlog
10202        (gnus-backlog-remove-article
10203         (car gnus-article-current) (cdr gnus-article-current)))
10204      (when gnus-use-cache
10205        (gnus-cache-update-article
10206         (car gnus-article-current) (cdr gnus-article-current)))))
10207
10208 (put 'gnus-with-article 'lisp-indent-function 1)
10209 (put 'gnus-with-article 'edebug-form-spec '(form body))
10210
10211 ;; Thread-based commands.
10212
10213 (defun gnus-summary-articles-in-thread (&optional article)
10214   "Return a list of all articles in the current thread.
10215 If ARTICLE is non-nil, return all articles in the thread that starts
10216 with that article."
10217   (let* ((article (or article (gnus-summary-article-number)))
10218          (data (gnus-data-find-list article))
10219          (top-level (gnus-data-level (car data)))
10220          (top-subject
10221           (cond ((null gnus-thread-operation-ignore-subject)
10222                  (gnus-simplify-subject-re
10223                   (mail-header-subject (gnus-data-header (car data)))))
10224                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10225                  (gnus-simplify-subject-fuzzy
10226                   (mail-header-subject (gnus-data-header (car data)))))
10227                 (t nil)))
10228          (end-point (save-excursion
10229                       (if (gnus-summary-go-to-next-thread)
10230                           (point) (point-max))))
10231          articles)
10232     (while (and data
10233                 (< (gnus-data-pos (car data)) end-point))
10234       (when (or (not top-subject)
10235                 (string= top-subject
10236                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10237                              (gnus-simplify-subject-fuzzy
10238                               (mail-header-subject
10239                                (gnus-data-header (car data))))
10240                            (gnus-simplify-subject-re
10241                             (mail-header-subject
10242                              (gnus-data-header (car data)))))))
10243         (push (gnus-data-number (car data)) articles))
10244       (unless (and (setq data (cdr data))
10245                    (> (gnus-data-level (car data)) top-level))
10246         (setq data nil)))
10247     ;; Return the list of articles.
10248     (nreverse articles)))
10249
10250 (defun gnus-summary-rethread-current ()
10251   "Rethread the thread the current article is part of."
10252   (interactive)
10253   (let* ((gnus-show-threads t)
10254          (article (gnus-summary-article-number))
10255          (id (mail-header-id (gnus-summary-article-header)))
10256          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10257     (unless id
10258       (error "No article on the current line"))
10259     (gnus-rebuild-thread id)
10260     (gnus-summary-goto-subject article)))
10261
10262 (defun gnus-summary-reparent-thread ()
10263   "Make the current article child of the marked (or previous) article.
10264
10265 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10266 is non-nil or the Subject: of both articles are the same."
10267   (interactive)
10268   (unless (not (gnus-group-read-only-p))
10269     (error "The current newsgroup does not support article editing"))
10270   (unless (<= (length gnus-newsgroup-processable) 1)
10271     (error "No more than one article may be marked"))
10272   (save-window-excursion
10273     (let ((gnus-article-buffer " *reparent*")
10274           (current-article (gnus-summary-article-number))
10275           ;; First grab the marked article, otherwise one line up.
10276           (parent-article (if (not (null gnus-newsgroup-processable))
10277                               (car gnus-newsgroup-processable)
10278                             (save-excursion
10279                               (if (eq (forward-line -1) 0)
10280                                   (gnus-summary-article-number)
10281                                 (error "Beginning of summary buffer"))))))
10282       (unless (not (eq current-article parent-article))
10283         (error "An article may not be self-referential"))
10284       (let ((message-id (mail-header-id
10285                          (gnus-summary-article-header parent-article))))
10286         (unless (and message-id (not (equal message-id "")))
10287           (error "No message-id in desired parent"))
10288         (gnus-with-article current-article
10289           (save-restriction
10290             (goto-char (point-min))
10291             (message-narrow-to-head)
10292             (if (re-search-forward "^References: " nil t)
10293                 (progn
10294                   (re-search-forward "^[^ \t]" nil t)
10295                   (forward-line -1)
10296                   (end-of-line)
10297                   (insert " " message-id))
10298               (insert "References: " message-id "\n"))))
10299         (set-buffer gnus-summary-buffer)
10300         (gnus-summary-unmark-all-processable)
10301         (gnus-summary-update-article current-article)
10302         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10303             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10304         (gnus-summary-rethread-current)
10305         (gnus-message 3 "Article %d is now the child of article %d"
10306                       current-article parent-article)))))
10307
10308 (defun gnus-summary-toggle-threads (&optional arg)
10309   "Toggle showing conversation threads.
10310 If ARG is positive number, turn showing conversation threads on."
10311   (interactive "P")
10312   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10313     (setq gnus-show-threads
10314           (if (null arg) (not gnus-show-threads)
10315             (> (prefix-numeric-value arg) 0)))
10316     (gnus-summary-prepare)
10317     (gnus-summary-goto-subject current)
10318     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10319     (gnus-summary-position-point)))
10320
10321 (defun gnus-summary-show-all-threads ()
10322   "Show all threads."
10323   (interactive)
10324   (save-excursion
10325     (let ((buffer-read-only nil))
10326       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10327   (gnus-summary-position-point))
10328
10329 (defun gnus-summary-show-thread ()
10330   "Show thread subtrees.
10331 Returns nil if no thread was there to be shown."
10332   (interactive)
10333   (let ((buffer-read-only nil)
10334         (orig (point))
10335         (end (gnus-point-at-eol))
10336         ;; Leave point at bol
10337         (beg (progn (beginning-of-line) (point))))
10338     (prog1
10339         ;; Any hidden lines here?
10340         (search-forward "\r" end t)
10341       (subst-char-in-region beg end ?\^M ?\n t)
10342       (goto-char orig)
10343       (gnus-summary-position-point))))
10344
10345 (defun gnus-summary-maybe-hide-threads ()
10346   "If requested, hide the threads that should be hidden."
10347   (when (and gnus-show-threads
10348              gnus-thread-hide-subtree)
10349     (gnus-summary-hide-all-threads
10350      (if (or (consp gnus-thread-hide-subtree)
10351              (gnus-functionp gnus-thread-hide-subtree))
10352          (gnus-make-predicate gnus-thread-hide-subtree)
10353        nil))))
10354
10355 ;;; Hiding predicates.
10356
10357 (defun gnus-article-unread-p (header)
10358   (memq (mail-header-number header) gnus-newsgroup-unreads))
10359
10360 (defun gnus-article-unseen-p (header)
10361   (memq (mail-header-number header) gnus-newsgroup-unseen))
10362
10363 (defun gnus-map-articles (predicate articles)
10364   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10365   (apply 'gnus-or (mapcar predicate
10366                           (mapcar 'gnus-summary-article-header articles))))
10367
10368 (defun gnus-summary-hide-all-threads (&optional predicate)
10369   "Hide all thread subtrees.
10370 If PREDICATE is supplied, threads that satisfy this predicate
10371 will not be hidden."
10372   (interactive)
10373   (save-excursion
10374     (goto-char (point-min))
10375     (let ((end nil))
10376       (while (not end)
10377         (when (or (not predicate)
10378                   (gnus-map-articles
10379                    predicate (gnus-summary-article-children)))
10380             (gnus-summary-hide-thread))
10381         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10382   (gnus-summary-position-point))
10383
10384 (defun gnus-summary-hide-thread ()
10385   "Hide thread subtrees.
10386 If PREDICATE is supplied, threads that satisfy this predicate
10387 will not be hidden.
10388 Returns nil if no threads were there to be hidden."
10389   (interactive)
10390   (let ((buffer-read-only nil)
10391         (start (point))
10392         (article (gnus-summary-article-number)))
10393     (goto-char start)
10394     ;; Go forward until either the buffer ends or the subthread
10395     ;; ends.
10396     (when (and (not (eobp))
10397                (or (zerop (gnus-summary-next-thread 1 t))
10398                    (goto-char (point-max))))
10399       (prog1
10400           (if (and (> (point) start)
10401                    (search-backward "\n" start t))
10402               (progn
10403                 (subst-char-in-region start (point) ?\n ?\^M)
10404                 (gnus-summary-goto-subject article))
10405             (goto-char start)
10406             nil)))))
10407
10408 (defun gnus-summary-go-to-next-thread (&optional previous)
10409   "Go to the same level (or less) next thread.
10410 If PREVIOUS is non-nil, go to previous thread instead.
10411 Return the article number moved to, or nil if moving was impossible."
10412   (let ((level (gnus-summary-thread-level))
10413         (way (if previous -1 1))
10414         (beg (point)))
10415     (forward-line way)
10416     (while (and (not (eobp))
10417                 (< level (gnus-summary-thread-level)))
10418       (forward-line way))
10419     (if (eobp)
10420         (progn
10421           (goto-char beg)
10422           nil)
10423       (setq beg (point))
10424       (prog1
10425           (gnus-summary-article-number)
10426         (goto-char beg)))))
10427
10428 (defun gnus-summary-next-thread (n &optional silent)
10429   "Go to the same level next N'th thread.
10430 If N is negative, search backward instead.
10431 Returns the difference between N and the number of skips actually
10432 done.
10433
10434 If SILENT, don't output messages."
10435   (interactive "p")
10436   (let ((backward (< n 0))
10437         (n (abs n)))
10438     (while (and (> n 0)
10439                 (gnus-summary-go-to-next-thread backward))
10440       (decf n))
10441     (unless silent
10442       (gnus-summary-position-point))
10443     (when (and (not silent) (/= 0 n))
10444       (gnus-message 7 "No more threads"))
10445     n))
10446
10447 (defun gnus-summary-prev-thread (n)
10448   "Go to the same level previous N'th thread.
10449 Returns the difference between N and the number of skips actually
10450 done."
10451   (interactive "p")
10452   (gnus-summary-next-thread (- n)))
10453
10454 (defun gnus-summary-go-down-thread ()
10455   "Go down one level in the current thread."
10456   (let ((children (gnus-summary-article-children)))
10457     (when children
10458       (gnus-summary-goto-subject (car children)))))
10459
10460 (defun gnus-summary-go-up-thread ()
10461   "Go up one level in the current thread."
10462   (let ((parent (gnus-summary-article-parent)))
10463     (when parent
10464       (gnus-summary-goto-subject parent))))
10465
10466 (defun gnus-summary-down-thread (n)
10467   "Go down thread N steps.
10468 If N is negative, go up instead.
10469 Returns the difference between N and how many steps down that were
10470 taken."
10471   (interactive "p")
10472   (let ((up (< n 0))
10473         (n (abs n)))
10474     (while (and (> n 0)
10475                 (if up (gnus-summary-go-up-thread)
10476                   (gnus-summary-go-down-thread)))
10477       (setq n (1- n)))
10478     (gnus-summary-position-point)
10479     (when (/= 0 n)
10480       (gnus-message 7 "Can't go further"))
10481     n))
10482
10483 (defun gnus-summary-up-thread (n)
10484   "Go up thread N steps.
10485 If N is negative, go down instead.
10486 Returns the difference between N and how many steps down that were
10487 taken."
10488   (interactive "p")
10489   (gnus-summary-down-thread (- n)))
10490
10491 (defun gnus-summary-top-thread ()
10492   "Go to the top of the thread."
10493   (interactive)
10494   (while (gnus-summary-go-up-thread))
10495   (gnus-summary-article-number))
10496
10497 (defun gnus-summary-kill-thread (&optional unmark)
10498   "Mark articles under current thread as read.
10499 If the prefix argument is positive, remove any kinds of marks.
10500 If the prefix argument is negative, tick articles instead."
10501   (interactive "P")
10502   (when unmark
10503     (setq unmark (prefix-numeric-value unmark)))
10504   (let ((articles (gnus-summary-articles-in-thread)))
10505     (save-excursion
10506       ;; Expand the thread.
10507       (gnus-summary-show-thread)
10508       ;; Mark all the articles.
10509       (while articles
10510         (gnus-summary-goto-subject (car articles))
10511         (cond ((null unmark)
10512                (gnus-summary-mark-article-as-read gnus-killed-mark))
10513               ((> unmark 0)
10514                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10515               (t
10516                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10517         (setq articles (cdr articles))))
10518     ;; Hide killed subtrees.
10519     (and (null unmark)
10520          gnus-thread-hide-killed
10521          (gnus-summary-hide-thread))
10522     ;; If marked as read, go to next unread subject.
10523     (when (null unmark)
10524       ;; Go to next unread subject.
10525       (gnus-summary-next-subject 1 t)))
10526   (gnus-set-mode-line 'summary))
10527
10528 ;; Summary sorting commands
10529
10530 (defun gnus-summary-sort-by-number (&optional reverse)
10531   "Sort the summary buffer by article number.
10532 Argument REVERSE means reverse order."
10533   (interactive "P")
10534   (gnus-summary-sort 'number reverse))
10535
10536 (defun gnus-summary-sort-by-random (&optional reverse)
10537   "Randomize the order in the summary buffer.
10538 Argument REVERSE means to randomize in reverse order."
10539   (interactive "P")
10540   (gnus-summary-sort 'random reverse))
10541
10542 (defun gnus-summary-sort-by-author (&optional reverse)
10543   "Sort the summary buffer by author name alphabetically.
10544 If `case-fold-search' is non-nil, case of letters is ignored.
10545 Argument REVERSE means reverse order."
10546   (interactive "P")
10547   (gnus-summary-sort 'author reverse))
10548
10549 (defun gnus-summary-sort-by-subject (&optional reverse)
10550   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10551 If `case-fold-search' is non-nil, case of letters is ignored.
10552 Argument REVERSE means reverse order."
10553   (interactive "P")
10554   (gnus-summary-sort 'subject reverse))
10555
10556 (defun gnus-summary-sort-by-date (&optional reverse)
10557   "Sort the summary buffer by date.
10558 Argument REVERSE means reverse order."
10559   (interactive "P")
10560   (gnus-summary-sort 'date reverse))
10561
10562 (defun gnus-summary-sort-by-score (&optional reverse)
10563   "Sort the summary buffer by score.
10564 Argument REVERSE means reverse order."
10565   (interactive "P")
10566   (gnus-summary-sort 'score reverse))
10567
10568 (defun gnus-summary-sort-by-lines (&optional reverse)
10569   "Sort the summary buffer by the number of lines.
10570 Argument REVERSE means reverse order."
10571   (interactive "P")
10572   (gnus-summary-sort 'lines reverse))
10573
10574 (defun gnus-summary-sort-by-chars (&optional reverse)
10575   "Sort the summary buffer by article length.
10576 Argument REVERSE means reverse order."
10577   (interactive "P")
10578   (gnus-summary-sort 'chars reverse))
10579
10580 (defun gnus-summary-sort-by-original (&optional reverse)
10581   "Sort the summary buffer using the default sorting method.
10582 Argument REVERSE means reverse order."
10583   (interactive "P")
10584   (let* ((buffer-read-only)
10585          (gnus-summary-prepare-hook nil))
10586     ;; We do the sorting by regenerating the threads.
10587     (gnus-summary-prepare)
10588     ;; Hide subthreads if needed.
10589     (gnus-summary-maybe-hide-threads)))
10590
10591 (defun gnus-summary-sort (predicate reverse)
10592   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10593   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10594          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10595          (gnus-thread-sort-functions
10596           (if (not reverse)
10597               thread
10598             `(lambda (t1 t2)
10599                (,thread t2 t1))))
10600          (gnus-sort-gathered-threads-function
10601           gnus-thread-sort-functions)
10602          (gnus-article-sort-functions
10603           (if (not reverse)
10604               article
10605             `(lambda (t1 t2)
10606                (,article t2 t1))))
10607          (buffer-read-only)
10608          (gnus-summary-prepare-hook nil))
10609     ;; We do the sorting by regenerating the threads.
10610     (gnus-summary-prepare)
10611     ;; Hide subthreads if needed.
10612     (gnus-summary-maybe-hide-threads)))
10613
10614 ;; Summary saving commands.
10615
10616 (defun gnus-summary-save-article (&optional n not-saved)
10617   "Save the current article using the default saver function.
10618 If N is a positive number, save the N next articles.
10619 If N is a negative number, save the N previous articles.
10620 If N is nil and any articles have been marked with the process mark,
10621 save those articles instead.
10622 The variable `gnus-default-article-saver' specifies the saver function."
10623   (interactive "P")
10624   (let* ((articles (gnus-summary-work-articles n))
10625          (save-buffer (save-excursion
10626                         (nnheader-set-temp-buffer " *Gnus Save*")))
10627          (num (length articles))
10628          header file)
10629     (dolist (article articles)
10630       (setq header (gnus-summary-article-header article))
10631       (if (not (vectorp header))
10632           ;; This is a pseudo-article.
10633           (if (assq 'name header)
10634               (gnus-copy-file (cdr (assq 'name header)))
10635             (gnus-message 1 "Article %d is unsaveable" article))
10636         ;; This is a real article.
10637         (save-window-excursion
10638           (let ((gnus-display-mime-function nil)
10639                 (gnus-article-prepare-hook nil))
10640             (gnus-summary-select-article t nil nil article)))
10641         (save-excursion
10642           (set-buffer save-buffer)
10643           (erase-buffer)
10644           (insert-buffer-substring gnus-original-article-buffer))
10645         (setq file (gnus-article-save save-buffer file num))
10646         (gnus-summary-remove-process-mark article)
10647         (unless not-saved
10648           (gnus-summary-set-saved-mark article))))
10649     (gnus-kill-buffer save-buffer)
10650     (gnus-summary-position-point)
10651     (gnus-set-mode-line 'summary)
10652     n))
10653
10654 (defun gnus-summary-pipe-output (&optional arg headers)
10655   "Pipe the current article to a subprocess.
10656 If N is a positive number, pipe the N next articles.
10657 If N is a negative number, pipe the N previous articles.
10658 If N is nil and any articles have been marked with the process mark,
10659 pipe those articles instead.
10660 If HEADERS (the symbolic prefix), include the headers, too."
10661   (interactive (gnus-interactive "P\ny"))
10662   (require 'gnus-art)
10663   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10664         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10665     (gnus-summary-save-article arg t))
10666   (let ((buffer (get-buffer "*Shell Command Output*")))
10667     (when (and buffer
10668                (not (zerop (buffer-size buffer))))
10669       (gnus-configure-windows 'pipe))))
10670
10671 (defun gnus-summary-save-article-mail (&optional arg)
10672   "Append the current article to an mail file.
10673 If N is a positive number, save the N next articles.
10674 If N is a negative number, save the N previous articles.
10675 If N is nil and any articles have been marked with the process mark,
10676 save those articles instead."
10677   (interactive "P")
10678   (require 'gnus-art)
10679   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10680     (gnus-summary-save-article arg)))
10681
10682 (defun gnus-summary-save-article-rmail (&optional arg)
10683   "Append the current article to an rmail file.
10684 If N is a positive number, save the N next articles.
10685 If N is a negative number, save the N previous articles.
10686 If N is nil and any articles have been marked with the process mark,
10687 save those articles instead."
10688   (interactive "P")
10689   (require 'gnus-art)
10690   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10691     (gnus-summary-save-article arg)))
10692
10693 (defun gnus-summary-save-article-file (&optional arg)
10694   "Append the current article to a file.
10695 If N is a positive number, save the N next articles.
10696 If N is a negative number, save the N previous articles.
10697 If N is nil and any articles have been marked with the process mark,
10698 save those articles instead."
10699   (interactive "P")
10700   (require 'gnus-art)
10701   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10702     (gnus-summary-save-article arg)))
10703
10704 (defun gnus-summary-write-article-file (&optional arg)
10705   "Write the current article to a file, deleting the previous file.
10706 If N is a positive number, save the N next articles.
10707 If N is a negative number, save the N previous articles.
10708 If N is nil and any articles have been marked with the process mark,
10709 save those articles instead."
10710   (interactive "P")
10711   (require 'gnus-art)
10712   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10713     (gnus-summary-save-article arg)))
10714
10715 (defun gnus-summary-save-article-body-file (&optional arg)
10716   "Append the current article body to a file.
10717 If N is a positive number, save the N next articles.
10718 If N is a negative number, save the N previous articles.
10719 If N is nil and any articles have been marked with the process mark,
10720 save those articles instead."
10721   (interactive "P")
10722   (require 'gnus-art)
10723   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10724     (gnus-summary-save-article arg)))
10725
10726 (defun gnus-summary-muttprint (&optional arg)
10727   "Print the current article using Muttprint.
10728 If N is a positive number, save the N next articles.
10729 If N is a negative number, save the N previous articles.
10730 If N is nil and any articles have been marked with the process mark,
10731 save those articles instead."
10732   (interactive "P")
10733   (require 'gnus-art)
10734   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10735     (gnus-summary-save-article arg t)))
10736
10737 (defun gnus-summary-pipe-message (program)
10738   "Pipe the current article through PROGRAM."
10739   (interactive "sProgram: ")
10740   (gnus-summary-select-article)
10741   (let ((mail-header-separator ""))
10742     (gnus-eval-in-buffer-window gnus-article-buffer
10743       (save-restriction
10744         (widen)
10745         (let ((start (window-start))
10746               buffer-read-only)
10747           (message-pipe-buffer-body program)
10748           (set-window-start (get-buffer-window (current-buffer)) start))))))
10749
10750 (defun gnus-get-split-value (methods)
10751   "Return a value based on the split METHODS."
10752   (let (split-name method result match)
10753     (when methods
10754       (save-excursion
10755         (set-buffer gnus-original-article-buffer)
10756         (save-restriction
10757           (nnheader-narrow-to-headers)
10758           (while (and methods (not split-name))
10759             (goto-char (point-min))
10760             (setq method (pop methods))
10761             (setq match (car method))
10762             (when (cond
10763                    ((stringp match)
10764                     ;; Regular expression.
10765                     (ignore-errors
10766                       (re-search-forward match nil t)))
10767                    ((gnus-functionp match)
10768                     ;; Function.
10769                     (save-restriction
10770                       (widen)
10771                       (setq result (funcall match gnus-newsgroup-name))))
10772                    ((consp match)
10773                     ;; Form.
10774                     (save-restriction
10775                       (widen)
10776                       (setq result (eval match)))))
10777               (setq split-name (cdr method))
10778               (cond ((stringp result)
10779                      (push (expand-file-name
10780                             result gnus-article-save-directory)
10781                            split-name))
10782                     ((consp result)
10783                      (setq split-name (append result split-name)))))))))
10784     (nreverse split-name)))
10785
10786 (defun gnus-valid-move-group-p (group)
10787   (and (boundp group)
10788        (symbol-name group)
10789        (symbol-value group)
10790        (gnus-get-function (gnus-find-method-for-group
10791                            (symbol-name group)) 'request-accept-article t)))
10792
10793 (defun gnus-read-move-group-name (prompt default articles prefix)
10794   "Read a group name."
10795   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10796          (minibuffer-confirm-incomplete nil) ; XEmacs
10797          (prom
10798           (format "%s %s to:"
10799                   prompt
10800                   (if (> (length articles) 1)
10801                       (format "these %d articles" (length articles))
10802                     "this article")))
10803          (to-newsgroup
10804           (cond
10805            ((null split-name)
10806             (gnus-completing-read-with-default
10807              default prom
10808              gnus-active-hashtb
10809              'gnus-valid-move-group-p
10810              nil prefix
10811              'gnus-group-history))
10812            ((= 1 (length split-name))
10813             (gnus-completing-read-with-default
10814              (car split-name) prom
10815              gnus-active-hashtb
10816              'gnus-valid-move-group-p
10817              nil nil
10818              'gnus-group-history))
10819            (t
10820             (gnus-completing-read-with-default
10821              nil prom
10822              (mapcar (lambda (el) (list el))
10823                      (nreverse split-name))
10824              nil nil nil
10825              'gnus-group-history))))
10826          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10827     (when to-newsgroup
10828       (if (or (string= to-newsgroup "")
10829               (string= to-newsgroup prefix))
10830           (setq to-newsgroup default))
10831       (unless to-newsgroup
10832         (error "No group name entered"))
10833       (or (gnus-active to-newsgroup)
10834           (gnus-activate-group to-newsgroup nil nil to-method)
10835           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10836                                      to-newsgroup))
10837               (or (and (gnus-request-create-group to-newsgroup to-method)
10838                        (gnus-activate-group
10839                         to-newsgroup nil nil to-method)
10840                        (gnus-subscribe-group to-newsgroup))
10841                   (error "Couldn't create group %s" to-newsgroup)))
10842           (error "No such group: %s" to-newsgroup)))
10843     to-newsgroup))
10844
10845 (defun gnus-summary-save-parts (type dir n &optional reverse)
10846   "Save parts matching TYPE to DIR.
10847 If REVERSE, save parts that do not match TYPE."
10848   (interactive
10849    (list (read-string "Save parts of type: "
10850                       (or (car gnus-summary-save-parts-type-history)
10851                           gnus-summary-save-parts-default-mime)
10852                       'gnus-summary-save-parts-type-history)
10853          (setq gnus-summary-save-parts-last-directory
10854                (read-file-name "Save to directory: "
10855                                gnus-summary-save-parts-last-directory
10856                                nil t))
10857          current-prefix-arg))
10858   (gnus-summary-iterate n
10859     (let ((gnus-display-mime-function nil)
10860           (gnus-inhibit-treatment t))
10861       (gnus-summary-select-article))
10862     (save-excursion
10863       (set-buffer gnus-article-buffer)
10864       (let ((handles (or gnus-article-mime-handles
10865                          (mm-dissect-buffer nil gnus-article-loose-mime)
10866                          (and gnus-article-emulate-mime
10867                               (mm-uu-dissect)))))
10868         (when handles
10869           (gnus-summary-save-parts-1 type dir handles reverse)
10870           (unless gnus-article-mime-handles ;; Don't destroy this case.
10871             (mm-destroy-parts handles)))))))
10872
10873 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10874   (if (stringp (car handle))
10875       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10876               (cdr handle))
10877     (when (if reverse
10878               (not (string-match type (mm-handle-media-type handle)))
10879             (string-match type (mm-handle-media-type handle)))
10880       (let ((file (expand-file-name
10881                    (file-name-nondirectory
10882                     (or
10883                      (mail-content-type-get
10884                       (mm-handle-disposition handle) 'filename)
10885                      (concat gnus-newsgroup-name
10886                              "." (number-to-string
10887                                   (cdr gnus-article-current)))))
10888                    dir)))
10889         (unless (file-exists-p file)
10890           (mm-save-part-to-file handle file))))))
10891
10892 ;; Summary extract commands
10893
10894 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10895   (let ((buffer-read-only nil)
10896         (article (gnus-summary-article-number))
10897         after-article b e)
10898     (unless (gnus-summary-goto-subject article)
10899       (error "No such article: %d" article))
10900     (gnus-summary-position-point)
10901     ;; If all commands are to be bunched up on one line, we collect
10902     ;; them here.
10903     (unless gnus-view-pseudos-separately
10904       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10905             files action)
10906         (while ps
10907           (setq action (cdr (assq 'action (car ps))))
10908           (setq files (list (cdr (assq 'name (car ps)))))
10909           (while (and ps (cdr ps)
10910                       (string= (or action "1")
10911                                (or (cdr (assq 'action (cadr ps))) "2")))
10912             (push (cdr (assq 'name (cadr ps))) files)
10913             (setcdr ps (cddr ps)))
10914           (when files
10915             (when (not (string-match "%s" action))
10916               (push " " files))
10917             (push " " files)
10918             (when (assq 'execute (car ps))
10919               (setcdr (assq 'execute (car ps))
10920                       (funcall (if (string-match "%s" action)
10921                                    'format 'concat)
10922                                action
10923                                (mapconcat
10924                                 (lambda (f)
10925                                   (if (equal f " ")
10926                                       f
10927                                     (mm-quote-arg f)))
10928                                 files " ")))))
10929           (setq ps (cdr ps)))))
10930     (if (and gnus-view-pseudos (not not-view))
10931         (while pslist
10932           (when (assq 'execute (car pslist))
10933             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10934                                   (eq gnus-view-pseudos 'not-confirm)))
10935           (setq pslist (cdr pslist)))
10936       (save-excursion
10937         (while pslist
10938           (setq after-article (or (cdr (assq 'article (car pslist)))
10939                                   (gnus-summary-article-number)))
10940           (gnus-summary-goto-subject after-article)
10941           (forward-line 1)
10942           (setq b (point))
10943           (insert "    " (file-name-nondirectory
10944                           (cdr (assq 'name (car pslist))))
10945                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10946           (setq e (point))
10947           (forward-line -1)             ; back to `b'
10948           (gnus-add-text-properties
10949            b (1- e) (list 'gnus-number gnus-reffed-article-number
10950                           gnus-mouse-face-prop gnus-mouse-face))
10951           (gnus-data-enter
10952            after-article gnus-reffed-article-number
10953            gnus-unread-mark b (car pslist) 0 (- e b))
10954           (setq gnus-newsgroup-unreads
10955                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10956                                          gnus-reffed-article-number))
10957           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10958           (setq pslist (cdr pslist)))))))
10959
10960 (defun gnus-pseudos< (p1 p2)
10961   (let ((c1 (cdr (assq 'action p1)))
10962         (c2 (cdr (assq 'action p2))))
10963     (and c1 c2 (string< c1 c2))))
10964
10965 (defun gnus-request-pseudo-article (props)
10966   (cond ((assq 'execute props)
10967          (gnus-execute-command (cdr (assq 'execute props)))))
10968   (let ((gnus-current-article (gnus-summary-article-number)))
10969     (gnus-run-hooks 'gnus-mark-article-hook)))
10970
10971 (defun gnus-execute-command (command &optional automatic)
10972   (save-excursion
10973     (gnus-article-setup-buffer)
10974     (set-buffer gnus-article-buffer)
10975     (setq buffer-read-only nil)
10976     (let ((command (if automatic command
10977                      (read-string "Command: " (cons command 0)))))
10978       (erase-buffer)
10979       (insert "$ " command "\n\n")
10980       (if gnus-view-pseudo-asynchronously
10981           (start-process "gnus-execute" (current-buffer) shell-file-name
10982                          shell-command-switch command)
10983         (call-process shell-file-name nil t nil
10984                       shell-command-switch command)))))
10985
10986 ;; Summary kill commands.
10987
10988 (defun gnus-summary-edit-global-kill (article)
10989   "Edit the \"global\" kill file."
10990   (interactive (list (gnus-summary-article-number)))
10991   (gnus-group-edit-global-kill article))
10992
10993 (defun gnus-summary-edit-local-kill ()
10994   "Edit a local kill file applied to the current newsgroup."
10995   (interactive)
10996   (setq gnus-current-headers (gnus-summary-article-header))
10997   (gnus-group-edit-local-kill
10998    (gnus-summary-article-number) gnus-newsgroup-name))
10999
11000 ;;; Header reading.
11001
11002 (defun gnus-read-header (id &optional header)
11003   "Read the headers of article ID and enter them into the Gnus system."
11004   (let ((group gnus-newsgroup-name)
11005         (gnus-override-method
11006          (or
11007           gnus-override-method
11008           (and (gnus-news-group-p gnus-newsgroup-name)
11009                (car (gnus-refer-article-methods)))))
11010         where)
11011     ;; First we check to see whether the header in question is already
11012     ;; fetched.
11013     (if (stringp id)
11014         ;; This is a Message-ID.
11015         (setq header (or header (gnus-id-to-header id)))
11016       ;; This is an article number.
11017       (setq header (or header (gnus-summary-article-header id))))
11018     (if (and header
11019              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11020         ;; We have found the header.
11021         header
11022       ;; If this is a sparse article, we have to nix out its
11023       ;; previous entry in the thread hashtb.
11024       (when (and header
11025                  (gnus-summary-article-sparse-p (mail-header-number header)))
11026         (let* ((parent (gnus-parent-id (mail-header-references header)))
11027                (thread (and parent (gnus-id-to-thread parent))))
11028           (when thread
11029             (delq (assq header thread) thread))))
11030       ;; We have to really fetch the header to this article.
11031       (save-excursion
11032         (set-buffer nntp-server-buffer)
11033         (when (setq where (gnus-request-head id group))
11034           (nnheader-fold-continuation-lines)
11035           (goto-char (point-max))
11036           (insert ".\n")
11037           (goto-char (point-min))
11038           (insert "211 ")
11039           (princ (cond
11040                   ((numberp id) id)
11041                   ((cdr where) (cdr where))
11042                   (header (mail-header-number header))
11043                   (t gnus-reffed-article-number))
11044                  (current-buffer))
11045           (insert " Article retrieved.\n"))
11046         (if (or (not where)
11047                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11048             ()                          ; Malformed head.
11049           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11050             (when (and (stringp id)
11051                        (not (string= (gnus-group-real-name group)
11052                                      (car where))))
11053               ;; If we fetched by Message-ID and the article came
11054               ;; from a different group, we fudge some bogus article
11055               ;; numbers for this article.
11056               (mail-header-set-number header gnus-reffed-article-number))
11057             (save-excursion
11058               (set-buffer gnus-summary-buffer)
11059               (decf gnus-reffed-article-number)
11060               (gnus-remove-header (mail-header-number header))
11061               (push header gnus-newsgroup-headers)
11062               (setq gnus-current-headers header)
11063               (push (mail-header-number header) gnus-newsgroup-limit)))
11064           header)))))
11065
11066 (defun gnus-remove-header (number)
11067   "Remove header NUMBER from `gnus-newsgroup-headers'."
11068   (if (and gnus-newsgroup-headers
11069            (= number (mail-header-number (car gnus-newsgroup-headers))))
11070       (pop gnus-newsgroup-headers)
11071     (let ((headers gnus-newsgroup-headers))
11072       (while (and (cdr headers)
11073                   (not (= number (mail-header-number (cadr headers)))))
11074         (pop headers))
11075       (when (cdr headers)
11076         (setcdr headers (cddr headers))))))
11077
11078 ;;;
11079 ;;; summary highlights
11080 ;;;
11081
11082 (defun gnus-highlight-selected-summary ()
11083   "Highlight selected article in summary buffer."
11084   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11085   (when gnus-summary-selected-face
11086     (save-excursion
11087       (let* ((beg (gnus-point-at-bol))
11088              (end (gnus-point-at-eol))
11089              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11090              (from (if (get-text-property beg gnus-mouse-face-prop)
11091                        beg
11092                      (or (next-single-property-change
11093                           beg gnus-mouse-face-prop nil end)
11094                          beg)))
11095              (to
11096               (if (= from end)
11097                   (- from 2)
11098                 (or (next-single-property-change
11099                      from gnus-mouse-face-prop nil end)
11100                     end))))
11101         ;; If no mouse-face prop on line we will have to = from = end,
11102         ;; so we highlight the entire line instead.
11103         (when (= (+ to 2) from)
11104           (setq from beg)
11105           (setq to end))
11106         (if gnus-newsgroup-selected-overlay
11107             ;; Move old overlay.
11108             (gnus-move-overlay
11109              gnus-newsgroup-selected-overlay from to (current-buffer))
11110           ;; Create new overlay.
11111           (gnus-overlay-put
11112            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11113            'face gnus-summary-selected-face))))))
11114
11115 (defvar gnus-summary-highlight-line-cached nil)
11116 (defvar gnus-summary-highlight-line-trigger nil)
11117
11118 (defun gnus-summary-highlight-line-0 ()
11119   (if (and (eq gnus-summary-highlight-line-trigger 
11120                gnus-summary-highlight)
11121            gnus-summary-highlight-line-cached)
11122       gnus-summary-highlight-line-cached
11123     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11124           gnus-summary-highlight-line-cached
11125           (let* ((cond (list 'cond))
11126                  (c cond)
11127                  (list gnus-summary-highlight))
11128             (while list
11129               (setcdr c (cons (list (caar list) (list 'quote (cdar list))) nil))
11130               (setq c (cdr c)
11131                     list (cdr list)))
11132             (gnus-byte-compile (list 'lambda nil cond))))))
11133
11134 (defun gnus-summary-highlight-line ()
11135   "Highlight current line according to `gnus-summary-highlight'."
11136   (let* ((beg (gnus-point-at-bol))
11137          (article (or (gnus-summary-article-number) gnus-current-article))
11138          (score (or (cdr (assq article
11139                                gnus-newsgroup-scored))
11140                     gnus-summary-default-score 0))
11141          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11142          (inhibit-read-only t)
11143          (default gnus-summary-default-score)
11144          (default-high gnus-summary-default-high-score)
11145          (default-low gnus-summary-default-low-score)
11146          (uncached (memq article gnus-newsgroup-undownloaded))
11147          (downloaded (not uncached)))
11148     (let ((face (funcall (gnus-summary-highlight-line-0))))
11149       (unless (eq face (get-text-property beg 'face))
11150         (gnus-put-text-property-excluding-characters-with-faces
11151          beg (gnus-point-at-eol) 'face
11152          (setq face (if (boundp face) (symbol-value face) face)))
11153         (when gnus-summary-highlight-line-function
11154           (funcall gnus-summary-highlight-line-function article face))))))
11155
11156 (defun gnus-update-read-articles (group unread &optional compute)
11157   "Update the list of read articles in GROUP.
11158 UNREAD is a sorted list."
11159   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
11160          (entry (gnus-gethash group gnus-newsrc-hashtb))
11161          (info (nth 2 entry))
11162          (prev 1)
11163          read)
11164     (if (or (not info) (not active))
11165         ;; There is no info on this group if it was, in fact,
11166         ;; killed.  Gnus stores no information on killed groups, so
11167         ;; there's nothing to be done.
11168         ;; One could store the information somewhere temporarily,
11169         ;; perhaps...  Hmmm...
11170         ()
11171       ;; Remove any negative articles numbers.
11172       (while (and unread (< (car unread) 0))
11173         (setq unread (cdr unread)))
11174       ;; Remove any expired article numbers
11175       (while (and unread (< (car unread) (car active)))
11176         (setq unread (cdr unread)))
11177       ;; Compute the ranges of read articles by looking at the list of
11178       ;; unread articles.
11179       (while unread
11180         (when (/= (car unread) prev)
11181           (push (if (= prev (1- (car unread))) prev
11182                   (cons prev (1- (car unread))))
11183                 read))
11184         (setq prev (1+ (car unread)))
11185         (setq unread (cdr unread)))
11186       (when (<= prev (cdr active))
11187         (push (cons prev (cdr active)) read))
11188       (setq read (if (> (length read) 1) (nreverse read) read))
11189       (if compute
11190           read
11191         (save-excursion
11192           (let (setmarkundo)
11193             ;; Propagate the read marks to the backend.
11194             (when (gnus-check-backend-function 'request-set-mark group)
11195               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11196                     (add (gnus-remove-from-range read (gnus-info-read info))))
11197                 (when (or add del)
11198                   (unless (gnus-check-group group)
11199                     (error "Can't open server for %s" group))
11200                   (gnus-request-set-mark
11201                    group (delq nil (list (if add (list add 'add '(read)))
11202                                          (if del (list del 'del '(read))))))
11203                   (setq setmarkundo
11204                         `(gnus-request-set-mark
11205                           ,group
11206                           ',(delq nil (list
11207                                        (if del (list del 'add '(read)))
11208                                        (if add (list add 'del '(read))))))))))
11209             (set-buffer gnus-group-buffer)
11210             (gnus-undo-register
11211               `(progn
11212                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11213                  (gnus-info-set-read ',info ',(gnus-info-read info))
11214                  (gnus-get-unread-articles-in-group ',info
11215                                                     (gnus-active ,group))
11216                  (gnus-group-update-group ,group t)
11217                  ,setmarkundo))))
11218         ;; Enter this list into the group info.
11219         (gnus-info-set-read info read)
11220         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11221         (gnus-get-unread-articles-in-group info (gnus-active group))
11222         t))))
11223
11224 (defun gnus-offer-save-summaries ()
11225   "Offer to save all active summary buffers."
11226   (let (buffers)
11227     ;; Go through all buffers and find all summaries.
11228     (dolist (buffer (buffer-list))
11229       (when (and (setq buffer (buffer-name buffer))
11230                  (string-match "Summary" buffer)
11231                  (save-excursion
11232                    (set-buffer buffer)
11233                    ;; We check that this is, indeed, a summary buffer.
11234                    (and (eq major-mode 'gnus-summary-mode)
11235                         ;; Also make sure this isn't bogus.
11236                         gnus-newsgroup-prepared
11237                         ;; Also make sure that this isn't a
11238                         ;; dead summary buffer.
11239                         (not gnus-dead-summary-mode))))
11240         (push buffer buffers)))
11241     ;; Go through all these summary buffers and offer to save them.
11242     (when buffers
11243       (save-excursion
11244         (map-y-or-n-p
11245          "Update summary buffer %s? "
11246          (lambda (buf)
11247            (switch-to-buffer buf)
11248            (gnus-summary-exit))
11249          buffers)))))
11250
11251 (defun gnus-summary-setup-default-charset ()
11252   "Setup newsgroup default charset."
11253   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11254       (setq gnus-newsgroup-charset nil)
11255     (let* ((ignored-charsets
11256             (or gnus-newsgroup-ephemeral-ignored-charsets
11257                 (append
11258                  (and gnus-newsgroup-name
11259                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11260                  gnus-newsgroup-ignored-charsets))))
11261       (setq gnus-newsgroup-charset
11262             (or gnus-newsgroup-ephemeral-charset
11263                 (and gnus-newsgroup-name
11264                      (gnus-parameter-charset gnus-newsgroup-name))
11265                 gnus-default-charset))
11266       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11267            ignored-charsets))))
11268
11269 ;;;
11270 ;;; Mime Commands
11271 ;;;
11272
11273 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11274   "Display the current article buffer fully MIME-buttonized.
11275 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11276 treated as multipart/mixed."
11277   (interactive "P")
11278   (require 'gnus-art)
11279   (let ((gnus-unbuttonized-mime-types nil)
11280         (gnus-mime-display-multipart-as-mixed show-all-parts))
11281     (gnus-summary-show-article)))
11282
11283 (defun gnus-summary-repair-multipart (article)
11284   "Add a Content-Type header to a multipart article without one."
11285   (interactive (list (gnus-summary-article-number)))
11286   (gnus-with-article article
11287     (message-narrow-to-head)
11288     (message-remove-header "Mime-Version")
11289     (goto-char (point-max))
11290     (insert "Mime-Version: 1.0\n")
11291     (widen)
11292     (when (search-forward "\n--" nil t)
11293       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11294         (message-narrow-to-head)
11295         (message-remove-header "Content-Type")
11296         (goto-char (point-max))
11297         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11298                         separator))
11299         (widen))))
11300   (let (gnus-mark-article-hook)
11301     (gnus-summary-select-article t t nil article)))
11302
11303 (defun gnus-summary-toggle-display-buttonized ()
11304   "Toggle the buttonizing of the article buffer."
11305   (interactive)
11306   (require 'gnus-art)
11307   (if (setq gnus-inhibit-mime-unbuttonizing
11308             (not gnus-inhibit-mime-unbuttonizing))
11309       (let ((gnus-unbuttonized-mime-types nil))
11310         (gnus-summary-show-article))
11311     (gnus-summary-show-article)))
11312
11313 ;;;
11314 ;;; Generic summary marking commands
11315 ;;;
11316
11317 (defvar gnus-summary-marking-alist
11318   '((read gnus-del-mark "d")
11319     (unread gnus-unread-mark "u")
11320     (ticked gnus-ticked-mark "!")
11321     (dormant gnus-dormant-mark "?")
11322     (expirable gnus-expirable-mark "e"))
11323   "An alist of names/marks/keystrokes.")
11324
11325 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11326 (defvar gnus-summary-mark-map)
11327
11328 (defun gnus-summary-make-all-marking-commands ()
11329   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11330   (dolist (elem gnus-summary-marking-alist)
11331     (apply 'gnus-summary-make-marking-command elem)))
11332
11333 (defun gnus-summary-make-marking-command (name mark keystroke)
11334   (let ((map (make-sparse-keymap)))
11335     (define-key gnus-summary-generic-mark-map keystroke map)
11336     (dolist (lway `((next "next" next nil "n")
11337                     (next-unread "next unread" next t "N")
11338                     (prev "previous" prev nil "p")
11339                     (prev-unread "previous unread" prev t "P")
11340                     (nomove "" nil nil ,keystroke)))
11341       (let ((func (gnus-summary-make-marking-command-1
11342                    mark (car lway) lway name)))
11343         (setq func (eval func))
11344         (define-key map (nth 4 lway) func)))))
11345
11346 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11347   `(defun ,(intern
11348             (format "gnus-summary-put-mark-as-%s%s"
11349                     name (if (eq way 'nomove)
11350                              ""
11351                            (concat "-" (symbol-name way)))))
11352      (n)
11353      ,(format
11354        "Mark the current article as %s%s.
11355 If N, the prefix, then repeat N times.
11356 If N is negative, move in reverse order.
11357 The difference between N and the actual number of articles marked is
11358 returned."
11359        name (cadr lway))
11360      (interactive "p")
11361      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11362
11363 (defun gnus-summary-generic-mark (n mark move unread)
11364   "Mark N articles with MARK."
11365   (unless (eq major-mode 'gnus-summary-mode)
11366     (error "This command can only be used in the summary buffer"))
11367   (gnus-summary-show-thread)
11368   (let ((nummove
11369          (cond
11370           ((eq move 'next) 1)
11371           ((eq move 'prev) -1)
11372           (t 0))))
11373     (if (zerop nummove)
11374         (setq n 1)
11375       (when (< n 0)
11376         (setq n (abs n)
11377               nummove (* -1 nummove))))
11378     (while (and (> n 0)
11379                 (gnus-summary-mark-article nil mark)
11380                 (zerop (gnus-summary-next-subject nummove unread t)))
11381       (setq n (1- n)))
11382     (when (/= 0 n)
11383       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11384     (gnus-summary-recenter)
11385     (gnus-summary-position-point)
11386     (gnus-set-mode-line 'summary)
11387     n))
11388
11389 (defun gnus-summary-insert-articles (articles)
11390   (when (setq articles
11391               (gnus-sorted-difference articles
11392                                       (mapcar (lambda (h)
11393                                                 (mail-header-number h))
11394                                               gnus-newsgroup-headers)))
11395     (setq gnus-newsgroup-headers
11396           (merge 'list
11397                  gnus-newsgroup-headers
11398                  (gnus-fetch-headers articles)
11399                  'gnus-article-sort-by-number))
11400     ;; Suppress duplicates?
11401     (when gnus-suppress-duplicates
11402       (gnus-dup-suppress-articles))
11403
11404     ;; We might want to build some more threads first.
11405     (when (and gnus-fetch-old-headers
11406                (eq gnus-headers-retrieved-by 'nov))
11407       (if (eq gnus-fetch-old-headers 'invisible)
11408           (gnus-build-all-threads)
11409         (gnus-build-old-threads)))
11410     ;; Let the Gnus agent mark articles as read.
11411     (when gnus-agent
11412       (gnus-agent-get-undownloaded-list))
11413     ;; Remove list identifiers from subject
11414     (when gnus-list-identifiers
11415       (gnus-summary-remove-list-identifiers))
11416     ;; First and last article in this newsgroup.
11417     (when gnus-newsgroup-headers
11418       (setq gnus-newsgroup-begin
11419             (mail-header-number (car gnus-newsgroup-headers))
11420             gnus-newsgroup-end
11421             (mail-header-number
11422              (gnus-last-element gnus-newsgroup-headers))))
11423     (when gnus-use-scoring
11424       (gnus-possibly-score-headers))))
11425
11426 (defun gnus-summary-insert-old-articles (&optional all)
11427   "Insert all old articles in this group.
11428 If ALL is non-nil, already read articles become readable.
11429 If ALL is a number, fetch this number of articles."
11430   (interactive "P")
11431   (prog1
11432       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11433             older len)
11434         (setq older
11435               ;; Some nntp servers lie about their active range.  When
11436               ;; this happens, the active range can be in the millions.
11437               ;; Use a compressed range to avoid creating a huge list.
11438               (gnus-range-difference (list gnus-newsgroup-active) old))
11439         (setq len (gnus-range-length older))
11440         (cond
11441          ((null older) nil)
11442          ((numberp all)
11443           (if (< all len)
11444               (let ((older-range (nreverse older)))
11445                 (setq older nil)
11446
11447                 (while (> all 0)
11448                   (let* ((r (pop older-range))
11449                          (min (if (numberp r) r (car r)))
11450                          (max (if (numberp r) r (cdr r))))
11451                     (while (and (<= min max)
11452                                 (> all 0))
11453                       (push max older)
11454                       (setq all (1- all)
11455                             max (1- max))))))
11456             (setq older (gnus-uncompress-range older))))
11457          (all
11458           (setq older (gnus-uncompress-range older)))
11459          (t
11460           (when (and (numberp gnus-large-newsgroup)
11461                    (> len gnus-large-newsgroup))
11462               (let* ((cursor-in-echo-area nil)
11463                      (initial (gnus-parameter-large-newsgroup-initial
11464                                gnus-newsgroup-name))
11465                      (input
11466                       (read-string
11467                        (format
11468                         "How many articles from %s (%s %d): "
11469                         (gnus-limit-string
11470                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11471                         (if initial "max" "default")
11472                         len)
11473                        (if initial
11474                            (cons (number-to-string initial)
11475                                  0)))))
11476                 (unless (string-match "^[ \t]*$" input)
11477                   (setq all (string-to-number input))
11478                   (if (< all len)
11479                       (let ((older-range (nreverse older)))
11480                         (setq older nil)
11481
11482                         (while (> all 0)
11483                           (let* ((r (pop older-range))
11484                                  (min (if (numberp r) r (car r)))
11485                                  (max (if (numberp r) r (cdr r))))
11486                             (while (and (<= min max)
11487                                         (> all 0))
11488                               (push max older)
11489                               (setq all (1- all)
11490                                     max (1- max))))))))))
11491           (setq older (gnus-uncompress-range older))))
11492         (if (not older)
11493             (message "No old news.")
11494           (gnus-summary-insert-articles older)
11495           (gnus-summary-limit (gnus-sorted-nunion old older))))
11496     (gnus-summary-position-point)))
11497
11498 (defun gnus-summary-insert-new-articles ()
11499   "Insert all new articles in this group."
11500   (interactive)
11501   (prog1
11502       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11503             (old-active gnus-newsgroup-active)
11504             (nnmail-fetched-sources (list t))
11505             i new)
11506         (setq gnus-newsgroup-active
11507               (gnus-activate-group gnus-newsgroup-name 'scan))
11508         (setq i (cdr gnus-newsgroup-active))
11509         (while (> i (cdr old-active))
11510           (push i new)
11511           (decf i))
11512         (if (not new)
11513             (message "No gnus is bad news.")
11514           (gnus-summary-insert-articles new)
11515           (setq gnus-newsgroup-unreads
11516                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11517           (gnus-summary-limit (gnus-sorted-nunion old new))))
11518     (gnus-summary-position-point)))
11519
11520 (gnus-summary-make-all-marking-commands)
11521
11522 (gnus-ems-redefine)
11523
11524 (provide 'gnus-sum)
11525
11526 (run-hooks 'gnus-sum-load-hook)
11527
11528 ;;; gnus-sum.el ends here