(mm-codepage-setup): New helper function.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-map))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'mm-decode)
42 (require 'nnoo)
43
44 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
45 (autoload 'gnus-cache-write-active "gnus-cache")
46 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
47 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
48 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
49 (autoload 'mm-uu-dissect "mm-uu")
50 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
51   "Deuglify broken Outlook (Express) articles and redisplay."
52   t)
53 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
54 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
55 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
56
57 (defcustom gnus-kill-summary-on-exit t
58   "*If non-nil, kill the summary buffer when you exit from it.
59 If nil, the summary will become a \"*Dead Summary*\" buffer, and
60 it will be killed sometime later."
61   :group 'gnus-summary-exit
62   :type 'boolean)
63
64 (defcustom gnus-fetch-old-headers nil
65   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
66 If an unread article in the group refers to an older, already read (or
67 just marked as read) article, the old article will not normally be
68 displayed in the Summary buffer.  If this variable is t, Gnus
69 will attempt to grab the headers to the old articles, and thereby
70 build complete threads.  If it has the value `some', only enough
71 headers to connect otherwise loose threads will be displayed.  This
72 variable can also be a number.  In that case, no more than that number
73 of old headers will be fetched.  If it has the value `invisible', all
74 old headers will be fetched, but none will be displayed.
75
76 The server has to support NOV for any of this to work."
77   :group 'gnus-thread
78   :type '(choice (const :tag "off" nil)
79                  (const :tag "on" t)
80                  (const some)
81                  (const invisible)
82                  number
83                  (sexp :menu-tag "other" t)))
84
85 (defcustom gnus-refer-thread-limit 200
86   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
87 If t, fetch all the available old headers."
88   :group 'gnus-thread
89   :type '(choice number
90                  (sexp :menu-tag "other" t)))
91
92 (defcustom gnus-summary-make-false-root 'adopt
93   "*nil means that Gnus won't gather loose threads.
94 If the root of a thread has expired or been read in a previous
95 session, the information necessary to build a complete thread has been
96 lost.  Instead of having many small sub-threads from this original thread
97 scattered all over the summary buffer, Gnus can gather them.
98
99 If non-nil, Gnus will try to gather all loose sub-threads from an
100 original thread into one large thread.
101
102 If this variable is non-nil, it should be one of `none', `adopt',
103 `dummy' or `empty'.
104
105 If this variable is `none', Gnus will not make a false root, but just
106 present the sub-threads after another.
107 If this variable is `dummy', Gnus will create a dummy root that will
108 have all the sub-threads as children.
109 If this variable is `adopt', Gnus will make one of the \"children\"
110 the parent and mark all the step-children as such.
111 If this variable is `empty', the \"children\" are printed with empty
112 subject fields.  (Or rather, they will be printed with a string
113 given by the `gnus-summary-same-subject' variable.)"
114   :group 'gnus-thread
115   :type '(choice (const :tag "off" nil)
116                  (const none)
117                  (const dummy)
118                  (const adopt)
119                  (const empty)))
120
121 (defcustom gnus-summary-make-false-root-always nil
122   "Always make a false dummy root."
123   :version "22.1"
124   :group 'gnus-thread
125   :type 'boolean)
126
127 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
128   "*A regexp to match subjects to be excluded from loose thread gathering.
129 As loose thread gathering is done on subjects only, that means that
130 there can be many false gatherings performed.  By rooting out certain
131 common subjects, gathering might become saner."
132   :group 'gnus-thread
133   :type 'regexp)
134
135 (defcustom gnus-summary-gather-subject-limit nil
136   "*Maximum length of subject comparisons when gathering loose threads.
137 Use nil to compare full subjects.  Setting this variable to a low
138 number will help gather threads that have been corrupted by
139 newsreaders chopping off subject lines, but it might also mean that
140 unrelated articles that have subject that happen to begin with the
141 same few characters will be incorrectly gathered.
142
143 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
144 comparing subjects."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const fuzzy)
148                  (sexp :menu-tag "on" t)))
149
150 (defcustom gnus-simplify-subject-functions nil
151   "List of functions taking a string argument that simplify subjects.
152 The functions are applied recursively.
153
154 Useful functions to put in this list include:
155 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
156 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
157   :group 'gnus-thread
158   :type '(repeat function))
159
160 (defcustom gnus-simplify-ignored-prefixes nil
161   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
162   :group 'gnus-thread
163   :type '(choice (const :tag "off" nil)
164                  regexp))
165
166 (defcustom gnus-build-sparse-threads nil
167   "*If non-nil, fill in the gaps in threads.
168 If `some', only fill in the gaps that are needed to tie loose threads
169 together.  If `more', fill in all leaf nodes that Gnus can find.  If
170 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
171   :group 'gnus-thread
172   :type '(choice (const :tag "off" nil)
173                  (const some)
174                  (const more)
175                  (sexp :menu-tag "all" t)))
176
177 (defcustom gnus-summary-thread-gathering-function
178   'gnus-gather-threads-by-subject
179   "*Function used for gathering loose threads.
180 There are two pre-defined functions: `gnus-gather-threads-by-subject',
181 which only takes Subjects into consideration; and
182 `gnus-gather-threads-by-references', which compared the References
183 headers of the articles to find matches."
184   :group 'gnus-thread
185   :type '(radio (function-item gnus-gather-threads-by-subject)
186                 (function-item gnus-gather-threads-by-references)
187                 (function :tag "other")))
188
189 (defcustom gnus-summary-same-subject ""
190   "*String indicating that the current article has the same subject as the previous.
191 This variable will only be used if the value of
192 `gnus-summary-make-false-root' is `empty'."
193   :group 'gnus-summary-format
194   :type 'string)
195
196 (defcustom gnus-summary-goto-unread t
197   "*If t, many commands will go to the next unread article.
198 This applies to marking commands as well as other commands that
199 \"naturally\" select the next article, like, for instance, `SPC' at
200 the end of an article.
201
202 If nil, the marking commands do NOT go to the next unread article
203 \(they go to the next article instead).  If `never', commands that
204 usually go to the next unread article, will go to the next article,
205 whether it is read or not."
206   :group 'gnus-summary-marks
207   :link '(custom-manual "(gnus)Setting Marks")
208   :type '(choice (const :tag "off" nil)
209                  (const never)
210                  (sexp :menu-tag "on" t)))
211
212 (defcustom gnus-summary-default-score 0
213   "*Default article score level.
214 All scores generated by the score files will be added to this score.
215 If this variable is nil, scoring will be disabled."
216   :group 'gnus-score-default
217   :type '(choice (const :tag "disable")
218                  integer))
219
220 (defcustom gnus-summary-default-high-score 0
221   "*Default threshold for a high scored article.
222 An article will be highlighted as high scored if its score is greater
223 than this score."
224   :version "22.1"
225   :group 'gnus-score-default
226   :type 'integer)
227
228 (defcustom gnus-summary-default-low-score 0
229   "*Default threshold for a low scored article.
230 An article will be highlighted as low scored if its score is smaller
231 than this score."
232   :version "22.1"
233   :group 'gnus-score-default
234   :type 'integer)
235
236 (defcustom gnus-summary-zcore-fuzz 0
237   "*Fuzziness factor for the zcore in the summary buffer.
238 Articles with scores closer than this to `gnus-summary-default-score'
239 will not be marked."
240   :group 'gnus-summary-format
241   :type 'integer)
242
243 (defcustom gnus-simplify-subject-fuzzy-regexp nil
244   "*Strings to be removed when doing fuzzy matches.
245 This can either be a regular expression or list of regular expressions
246 that will be removed from subject strings if fuzzy subject
247 simplification is selected."
248   :group 'gnus-thread
249   :type '(repeat regexp))
250
251 (defcustom gnus-show-threads t
252   "*If non-nil, display threads in summary mode."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-subtree nil
257   "*If non-nil, hide all threads initially.
258 This can be a predicate specifier which says which threads to hide.
259 If threads are hidden, you have to run the command
260 `gnus-summary-show-thread' by hand or select an article."
261   :group 'gnus-thread
262   :type '(radio (sexp :format "Non-nil\n"
263                       :match (lambda (widget value)
264                                (not (or (consp value) (functionp value))))
265                       :value t)
266                 (const nil)
267                 (sexp :tag "Predicate specifier")))
268
269 (defcustom gnus-thread-hide-killed t
270   "*If non-nil, hide killed threads automatically."
271   :group 'gnus-thread
272   :type 'boolean)
273
274 (defcustom gnus-thread-ignore-subject t
275   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
276 If nil, articles that have different subjects from their parents will
277 start separate threads."
278   :group 'gnus-thread
279   :type 'boolean)
280
281 (defcustom gnus-thread-operation-ignore-subject t
282   "*If non-nil, subjects will be ignored when doing thread commands.
283 This affects commands like `gnus-summary-kill-thread' and
284 `gnus-summary-lower-thread'.
285
286 If this variable is nil, articles in the same thread with different
287 subjects will not be included in the operation in question.  If this
288 variable is `fuzzy', only articles that have subjects that are fuzzily
289 equal will be included."
290   :group 'gnus-thread
291   :type '(choice (const :tag "off" nil)
292                  (const fuzzy)
293                  (sexp :tag "on" t)))
294
295 (defcustom gnus-thread-indent-level 4
296   "*Number that says how much each sub-thread should be indented."
297   :group 'gnus-thread
298   :type 'integer)
299
300 (defcustom gnus-auto-extend-newsgroup t
301   "*If non-nil, extend newsgroup forward and backward when requested."
302   :group 'gnus-summary-choose
303   :type 'boolean)
304
305 (defcustom gnus-auto-select-first t
306   "*If non-nil, select the article under point.
307 Which article this is is controlled by the `gnus-auto-select-subject'
308 variable.
309
310 If you want to prevent automatic selection of articles in some
311 newsgroups, set the variable to nil in `gnus-select-group-hook'."
312   :group 'gnus-group-select
313   :type '(choice (const :tag "none" nil)
314                  (sexp :menu-tag "first" t)))
315
316 (defcustom gnus-auto-select-subject 'unread
317   "*Says what subject to place under point when entering a group.
318
319 This variable can either be the symbols `first' (place point on the
320 first subject), `unread' (place point on the subject line of the first
321 unread article), `best' (place point on the subject line of the
322 higest-scored article), `unseen' (place point on the subject line of
323 the first unseen article), `unseen-or-unread' (place point on the subject
324 line of the first unseen article or, if all article have been seen, on the
325 subject line of the first unread article), or a function to be called to
326 place point on some subject line."
327   :version "22.1"
328   :group 'gnus-group-select
329   :type '(choice (const best)
330                  (const unread)
331                  (const first)
332                  (const unseen)
333                  (const unseen-or-unread)))
334
335 (defcustom gnus-auto-select-next t
336   "*If non-nil, offer to go to the next group from the end of the previous.
337 If the value is t and the next newsgroup is empty, Gnus will exit
338 summary mode and go back to group mode.  If the value is neither nil
339 nor t, Gnus will select the following unread newsgroup.  In
340 particular, if the value is the symbol `quietly', the next unread
341 newsgroup will be selected without any confirmation, and if it is
342 `almost-quietly', the next group will be selected without any
343 confirmation if you are located on the last article in the group.
344 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
345 will go to the next group without confirmation."
346   :group 'gnus-summary-maneuvering
347   :type '(choice (const :tag "off" nil)
348                  (const quietly)
349                  (const almost-quietly)
350                  (const slightly-quietly)
351                  (sexp :menu-tag "on" t)))
352
353 (defcustom gnus-auto-select-same nil
354   "*If non-nil, select the next article with the same subject.
355 If there are no more articles with the same subject, go to
356 the first unread article."
357   :group 'gnus-summary-maneuvering
358   :type 'boolean)
359
360 (defcustom gnus-auto-goto-ignores 'unfetched
361   "*Says how to handle unfetched articles when maneuvering.
362
363 This variable can either be the symbols nil (maneuver to any
364 article), `undownloaded' (maneuvering while unplugged ignores articles
365 that have not been fetched), `always-undownloaded' (maneuvering always
366 ignores articles that have not been fetched), `unfetched' (maneuvering
367 ignores articles whose headers have not been fetched).
368
369 NOTE: The list of unfetched articles will always be nil when plugged
370 and, when unplugged, a subset of the undownloaded article list."
371   :version "22.1"
372   :group 'gnus-summary-maneuvering
373   :type '(choice (const :tag "None" nil)
374                  (const :tag "Undownloaded when unplugged" undownloaded)
375                  (const :tag "Undownloaded" always-undownloaded)
376                  (const :tag "Unfetched" unfetched)))
377
378 (defcustom gnus-summary-check-current nil
379   "*If non-nil, consider the current article when moving.
380 The \"unread\" movement commands will stay on the same line if the
381 current article is unread."
382   :group 'gnus-summary-maneuvering
383   :type 'boolean)
384
385 (defcustom gnus-auto-center-summary 2
386   "*If non-nil, always center the current summary buffer.
387 In particular, if `vertical' do only vertical recentering.  If non-nil
388 and non-`vertical', do both horizontal and vertical recentering."
389   :group 'gnus-summary-maneuvering
390   :type '(choice (const :tag "none" nil)
391                  (const vertical)
392                  (integer :tag "height")
393                  (sexp :menu-tag "both" t)))
394
395 (defvar gnus-auto-center-group t
396   "*If non-nil, always center the group buffer.")
397
398 (defcustom gnus-show-all-headers nil
399   "*If non-nil, don't hide any headers."
400   :group 'gnus-article-hiding
401   :group 'gnus-article-headers
402   :type 'boolean)
403
404 (defcustom gnus-summary-ignore-duplicates nil
405   "*If non-nil, ignore articles with identical Message-ID headers."
406   :group 'gnus-summary
407   :type 'boolean)
408
409 (defcustom gnus-single-article-buffer t
410   "*If non-nil, display all articles in the same buffer.
411 If nil, each group will get its own article buffer."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-break-pages t
416   "*If non-nil, do page breaking on articles.
417 The page delimiter is specified by the `gnus-page-delimiter'
418 variable."
419   :group 'gnus-article-various
420   :type 'boolean)
421
422 (defcustom gnus-move-split-methods nil
423   "*Variable used to suggest where articles are to be moved to.
424 It uses the same syntax as the `gnus-split-methods' variable.
425 However, whereas `gnus-split-methods' specifies file names as targets,
426 this variable specifies group names."
427   :group 'gnus-summary-mail
428   :type '(repeat (choice (list :value (fun) function)
429                          (cons :value ("" "") regexp (repeat string))
430                          (sexp :value nil))))
431
432 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
433   "Function used to compute default prefix for article move/copy/etc prompts.
434 The function should take one argument, a group name, and return a
435 string with the suggested prefix."
436   :group 'gnus-summary-mail
437   :type 'function)
438
439 ;; FIXME: Although the custom type is `character' for the following variables,
440 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
441
442 (defcustom gnus-unread-mark ?           ;Whitespace
443   "*Mark used for unread articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-ticked-mark ?!
448   "*Mark used for ticked articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-dormant-mark ??
453   "*Mark used for dormant articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-del-mark ?r
458   "*Mark used for del'd articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-read-mark ?R
463   "*Mark used for read articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-expirable-mark ?E
468   "*Mark used for expirable articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-killed-mark ?K
473   "*Mark used for killed articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-spam-mark ?$
478   "*Mark used for spam articles."
479   :version "22.1"
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-souped-mark ?F
484   "*Mark used for souped articles."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-kill-file-mark ?X
489   "*Mark used for articles killed by kill files."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-low-score-mark ?Y
494   "*Mark used for articles with a low score."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-catchup-mark ?C
499   "*Mark used for articles that are caught up."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-replied-mark ?A
504   "*Mark used for articles that have been replied to."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-forwarded-mark ?F
509   "*Mark used for articles that have been forwarded."
510   :version "22.1"
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-recent-mark ?N
515   "*Mark used for articles that are recent."
516   :version "22.1"
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-cached-mark ?*
521   "*Mark used for articles that are in the cache."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-saved-mark ?S
526   "*Mark used for articles that have been saved."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-unseen-mark ?.
531   "*Mark used for articles that haven't been seen."
532   :version "22.1"
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-no-mark ?               ;Whitespace
537   "*Mark used for articles that have no other secondary mark."
538   :version "22.1"
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-ancient-mark ?O
543   "*Mark used for ancient articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-sparse-mark ?Q
548   "*Mark used for sparsely reffed articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-canceled-mark ?G
553   "*Mark used for canceled articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-duplicate-mark ?M
558   "*Mark used for duplicate articles."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-undownloaded-mark ?-
563   "*Mark used for articles that weren't downloaded."
564   :version "22.1"
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-downloaded-mark ?+
569   "*Mark used for articles that were downloaded."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloadable-mark ?%
574   "*Mark used for articles that are to be downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-unsendable-mark ?=
579   "*Mark used for articles that won't be sent."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-score-over-mark ?+
584   "*Score mark used for articles with high scores."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-below-mark ?-
589   "*Score mark used for articles with low scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-empty-thread-mark ?     ;Whitespace
594   "*There is no thread under the article."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-not-empty-thread-mark ?=
599   "*There is a thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-view-pseudo-asynchronously nil
604   "*If non-nil, Gnus will view pseudo-articles asynchronously."
605   :group 'gnus-extract-view
606   :type 'boolean)
607
608 (defcustom gnus-auto-expirable-marks
609   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
610         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
611         gnus-souped-mark gnus-duplicate-mark)
612   "*The list of marks converted into expiration if a group is auto-expirable."
613   :version "21.1"
614   :group 'gnus-summary
615   :type '(repeat character))
616
617 (defcustom gnus-inhibit-user-auto-expire t
618   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
619   :version "21.1"
620   :group 'gnus-summary
621   :type 'boolean)
622
623 (defcustom gnus-view-pseudos nil
624   "*If `automatic', pseudo-articles will be viewed automatically.
625 If `not-confirm', pseudos will be viewed automatically, and the user
626 will not be asked to confirm the command."
627   :group 'gnus-extract-view
628   :type '(choice (const :tag "off" nil)
629                  (const automatic)
630                  (const not-confirm)))
631
632 (defcustom gnus-view-pseudos-separately t
633   "*If non-nil, one pseudo-article will be created for each file to be viewed.
634 If nil, all files that use the same viewing command will be given as a
635 list of parameters to that command."
636   :group 'gnus-extract-view
637   :type 'boolean)
638
639 (defcustom gnus-insert-pseudo-articles t
640   "*If non-nil, insert pseudo-articles when decoding articles."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-summary-dummy-line-format
645   "   %(:                             :%) %S\n"
646   "*The format specification for the dummy roots in the summary buffer.
647 It works along the same lines as a normal formatting string,
648 with some simple extensions.
649
650 %S  The subject
651
652 General format specifiers can also be used.
653 See `(gnus)Formatting Variables'."
654   :link '(custom-manual "(gnus)Formatting Variables")
655   :group 'gnus-threading
656   :type 'string)
657
658 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
659   "*The format specification for the summary mode line.
660 It works along the same lines as a normal formatting string,
661 with some simple extensions:
662
663 %G  Group name
664 %p  Unprefixed group name
665 %A  Current article number
666 %z  Current article score
667 %V  Gnus version
668 %U  Number of unread articles in the group
669 %e  Number of unselected articles in the group
670 %Z  A string with unread/unselected article counts
671 %g  Shortish group name
672 %S  Subject of the current article
673 %u  User-defined spec
674 %s  Current score file name
675 %d  Number of dormant articles
676 %r  Number of articles that have been marked as read in this session
677 %E  Number of articles expunged by the score files"
678   :group 'gnus-summary-format
679   :type 'string)
680
681 (defcustom gnus-list-identifiers nil
682   "Regexp that matches list identifiers to be removed from subject.
683 This can also be a list of regexps."
684   :version "21.1"
685   :group 'gnus-summary-format
686   :group 'gnus-article-hiding
687   :type '(choice (const :tag "none" nil)
688                  (regexp :value ".*")
689                  (repeat :value (".*") regexp)))
690
691 (defcustom gnus-summary-mark-below 0
692   "*Mark all articles with a score below this variable as read.
693 This variable is local to each summary buffer and usually set by the
694 score file."
695   :group 'gnus-score-default
696   :type 'integer)
697
698 (defun gnus-widget-reversible-match (widget value)
699   "Ignoring WIDGET, convert VALUE to internal form.
700 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
701   ;; (debug value)
702   (or (symbolp value)
703       (and (listp value)
704            (eq (length value) 2)
705            (eq (nth 0 value) 'not)
706            (symbolp (nth 1 value)))))
707
708 (defun gnus-widget-reversible-to-internal (widget value)
709   "Ignoring WIDGET, convert VALUE to internal form.
710 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
711 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
712   ;; (debug value)
713   (if (atom value)
714       (list value nil)
715     (list (nth 1 value) t)))
716
717 (defun gnus-widget-reversible-to-external (widget value)
718   "Ignoring WIDGET, convert VALUE to external form.
719 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
720 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
721   ;; (debug value)
722   (if (nth 1 value)
723       (list 'not (nth 0 value))
724     (nth 0 value)))
725
726 (define-widget 'gnus-widget-reversible 'group
727   "A `group' that convert values."
728   :match 'gnus-widget-reversible-match
729   :value-to-internal 'gnus-widget-reversible-to-internal
730   :value-to-external 'gnus-widget-reversible-to-external)
731
732 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
733   "*List of functions used for sorting articles in the summary buffer.
734
735 Each function takes two articles and returns non-nil if the first
736 article should be sorted before the other.  If you use more than one
737 function, the primary sort function should be the last.  You should
738 probably always include `gnus-article-sort-by-number' in the list of
739 sorting functions -- preferably first.  Also note that sorting by date
740 is often much slower than sorting by number, and the sorting order is
741 very similar.  (Sorting by date means sorting by the time the message
742 was sent, sorting by number means sorting by arrival time.)
743
744 Each item can also be a list `(not F)' where F is a function;
745 this reverses the sort order.
746
747 Ready-made functions include `gnus-article-sort-by-number',
748 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
749 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
750 and `gnus-article-sort-by-score'.
751
752 When threading is turned on, the variable `gnus-thread-sort-functions'
753 controls how articles are sorted."
754   :group 'gnus-summary-sort
755   :type '(repeat (gnus-widget-reversible
756                   (choice (function-item gnus-article-sort-by-number)
757                           (function-item gnus-article-sort-by-author)
758                           (function-item gnus-article-sort-by-subject)
759                           (function-item gnus-article-sort-by-date)
760                           (function-item gnus-article-sort-by-score)
761                           (function-item gnus-article-sort-by-random)
762                           (function :tag "other"))
763                   (boolean :tag "Reverse order"))))
764
765
766 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
767   "*List of functions used for sorting threads in the summary buffer.
768 By default, threads are sorted by article number.
769
770 Each function takes two threads and returns non-nil if the first
771 thread should be sorted before the other.  If you use more than one
772 function, the primary sort function should be the last.  You should
773 probably always include `gnus-thread-sort-by-number' in the list of
774 sorting functions -- preferably first.  Also note that sorting by date
775 is often much slower than sorting by number, and the sorting order is
776 very similar.  (Sorting by date means sorting by the time the message
777 was sent, sorting by number means sorting by arrival time.)
778
779 Each list item can also be a list `(not F)' where F is a
780 function; this specifies reversed sort order.
781
782 Ready-made functions include `gnus-thread-sort-by-number',
783 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
784 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
785 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
786 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
787 and `gnus-thread-sort-by-total-score' (see
788 `gnus-thread-score-function').
789
790 When threading is turned off, the variable
791 `gnus-article-sort-functions' controls how articles are sorted."
792   :group 'gnus-summary-sort
793   :type '(repeat
794           (gnus-widget-reversible
795            (choice (function-item gnus-thread-sort-by-number)
796                    (function-item gnus-thread-sort-by-author)
797                    (function-item gnus-thread-sort-by-recipient)
798                    (function-item gnus-thread-sort-by-subject)
799                    (function-item gnus-thread-sort-by-date)
800                    (function-item gnus-thread-sort-by-score)
801                    (function-item gnus-thread-sort-by-most-recent-number)
802                    (function-item gnus-thread-sort-by-most-recent-date)
803                    (function-item gnus-thread-sort-by-random)
804                    (function-item gnus-thread-sort-by-total-score)
805                    (function :tag "other"))
806            (boolean :tag "Reverse order"))))
807
808 (defcustom gnus-thread-score-function '+
809   "*Function used for calculating the total score of a thread.
810
811 The function is called with the scores of the article and each
812 subthread and should then return the score of the thread.
813
814 Some functions you can use are `+', `max', or `min'."
815   :group 'gnus-summary-sort
816   :type 'function)
817
818 (defcustom gnus-summary-expunge-below nil
819   "All articles that have a score less than this variable will be expunged.
820 This variable is local to the summary buffers."
821   :group 'gnus-score-default
822   :type '(choice (const :tag "off" nil)
823                  integer))
824
825 (defcustom gnus-thread-expunge-below nil
826   "All threads that have a total score less than this variable will be expunged.
827 See `gnus-thread-score-function' for en explanation of what a
828 \"thread score\" is.
829
830 This variable is local to the summary buffers."
831   :group 'gnus-threading
832   :group 'gnus-score-default
833   :type '(choice (const :tag "off" nil)
834                  integer))
835
836 (defcustom gnus-summary-mode-hook nil
837   "*A hook for Gnus summary mode.
838 This hook is run before any variables are set in the summary buffer."
839   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
840   :group 'gnus-summary-various
841   :type 'hook)
842
843 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
844 (when (featurep 'xemacs)
845   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
846   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
847   (add-hook 'gnus-summary-mode-hook
848             'gnus-xmas-switch-horizontal-scrollbar-off))
849
850 (defcustom gnus-summary-menu-hook nil
851   "*Hook run after the creation of the summary mode menu."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-summary-exit-hook nil
856   "*A hook called on exit from the summary buffer.
857 It will be called with point in the group buffer."
858   :group 'gnus-summary-exit
859   :type 'hook)
860
861 (defcustom gnus-summary-prepare-hook nil
862   "*A hook called after the summary buffer has been generated.
863 If you want to modify the summary buffer, you can use this hook."
864   :group 'gnus-summary-various
865   :type 'hook)
866
867 (defcustom gnus-summary-prepared-hook nil
868   "*A hook called as the last thing after the summary buffer has been generated."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-summary-generate-hook nil
873   "*A hook run just before generating the summary buffer.
874 This hook is commonly used to customize threading variables and the
875 like."
876   :group 'gnus-summary-various
877   :type 'hook)
878
879 (defcustom gnus-select-group-hook nil
880   "*A hook called when a newsgroup is selected.
881
882 If you'd like to simplify subjects like the
883 `gnus-summary-next-same-subject' command does, you can use the
884 following hook:
885
886  (add-hook gnus-select-group-hook
887            (lambda ()
888              (mapcar (lambda (header)
889                        (mail-header-set-subject
890                         header
891                         (gnus-simplify-subject
892                          (mail-header-subject header) 're-only)))
893                      gnus-newsgroup-headers)))"
894   :group 'gnus-group-select
895   :type 'hook)
896
897 (defcustom gnus-select-article-hook nil
898   "*A hook called when an article is selected."
899   :group 'gnus-summary-choose
900   :options '(gnus-agent-fetch-selected-article)
901   :type 'hook)
902
903 (defcustom gnus-visual-mark-article-hook
904   (list 'gnus-highlight-selected-summary)
905   "*Hook run after selecting an article in the summary buffer.
906 It is meant to be used for highlighting the article in some way.  It
907 is not run if `gnus-visual' is nil."
908   :group 'gnus-summary-visual
909   :type 'hook)
910
911 (defcustom gnus-parse-headers-hook nil
912   "*A hook called before parsing the headers."
913   :group 'gnus-various
914   :type 'hook)
915
916 (defcustom gnus-exit-group-hook nil
917   "*A hook called when exiting summary mode.
918 This hook is not called from the non-updating exit commands like `Q'."
919   :group 'gnus-various
920   :type 'hook)
921
922 (defcustom gnus-summary-update-hook
923   (list 'gnus-summary-highlight-line)
924   "*A hook called when a summary line is changed.
925 The hook will not be called if `gnus-visual' is nil.
926
927 The default function `gnus-summary-highlight-line' will
928 highlight the line according to the `gnus-summary-highlight'
929 variable."
930   :group 'gnus-summary-visual
931   :type 'hook)
932
933 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
934   "*A hook called when an article is selected for the first time.
935 The hook is intended to mark an article as read (or unread)
936 automatically when it is selected."
937   :group 'gnus-summary-choose
938   :type 'hook)
939
940 (defcustom gnus-group-no-more-groups-hook nil
941   "*A hook run when returning to group mode having no more (unread) groups."
942   :group 'gnus-group-select
943   :type 'hook)
944
945 (defcustom gnus-ps-print-hook nil
946   "*A hook run before ps-printing something from Gnus."
947   :group 'gnus-summary
948   :type 'hook)
949
950 (defcustom gnus-summary-article-move-hook nil
951   "*A hook called after an article is moved, copied, respooled, or crossposted."
952   :version "22.1"
953   :group 'gnus-summary
954   :type 'hook)
955
956 (defcustom gnus-summary-article-delete-hook nil
957   "*A hook called after an article is deleted."
958   :version "22.1"
959   :group 'gnus-summary
960   :type 'hook)
961
962 (defcustom gnus-summary-article-expire-hook nil
963   "*A hook called after an article is expired."
964   :version "22.1"
965   :group 'gnus-summary
966   :type 'hook)
967
968 (defcustom gnus-summary-display-arrow
969   (and (fboundp 'display-graphic-p)
970        (display-graphic-p))
971   "*If non-nil, display an arrow highlighting the current article."
972   :version "22.1"
973   :group 'gnus-summary
974   :type 'boolean)
975
976 (defcustom gnus-summary-selected-face 'gnus-summary-selected
977   "Face used for highlighting the current article in the summary buffer."
978   :group 'gnus-summary-visual
979   :type 'face)
980
981 (defvar gnus-tmp-downloaded nil)
982
983 (defcustom gnus-summary-highlight
984   '(((eq mark gnus-canceled-mark)
985      . gnus-summary-cancelled)
986     ((and uncached (> score default-high))
987      . gnus-summary-high-undownloaded)
988     ((and uncached (< score default-low))
989      . gnus-summary-low-undownloaded)
990     (uncached
991      . gnus-summary-normal-undownloaded)
992     ((and (> score default-high)
993           (or (eq mark gnus-dormant-mark)
994               (eq mark gnus-ticked-mark)))
995      . gnus-summary-high-ticked)
996     ((and (< score default-low)
997           (or (eq mark gnus-dormant-mark)
998               (eq mark gnus-ticked-mark)))
999      . gnus-summary-low-ticked)
1000     ((or (eq mark gnus-dormant-mark)
1001          (eq mark gnus-ticked-mark))
1002      . gnus-summary-normal-ticked)
1003     ((and (> score default-high) (eq mark gnus-ancient-mark))
1004      . gnus-summary-high-ancient)
1005     ((and (< score default-low) (eq mark gnus-ancient-mark))
1006      . gnus-summary-low-ancient)
1007     ((eq mark gnus-ancient-mark)
1008      . gnus-summary-normal-ancient)
1009     ((and (> score default-high) (eq mark gnus-unread-mark))
1010      . gnus-summary-high-unread)
1011     ((and (< score default-low) (eq mark gnus-unread-mark))
1012      . gnus-summary-low-unread)
1013     ((eq mark gnus-unread-mark)
1014      . gnus-summary-normal-unread)
1015     ((> score default-high)
1016      . gnus-summary-high-read)
1017     ((< score default-low)
1018      . gnus-summary-low-read)
1019     (t
1020      . gnus-summary-normal-read))
1021   "*Controls the highlighting of summary buffer lines.
1022
1023 A list of (FORM . FACE) pairs.  When deciding how a a particular
1024 summary line should be displayed, each form is evaluated.  The content
1025 of the face field after the first true form is used.  You can change
1026 how those summary lines are displayed, by editing the face field.
1027
1028 You can use the following variables in the FORM field.
1029
1030 score:        The article's score
1031 default:      The default article score.
1032 default-high: The default score for high scored articles.
1033 default-low:  The default score for low scored articles.
1034 below:        The score below which articles are automatically marked as read.
1035 mark:         The article's mark.
1036 uncached:     Non-nil if the article is uncached."
1037   :group 'gnus-summary-visual
1038   :type '(repeat (cons (sexp :tag "Form" nil)
1039                        face)))
1040
1041 (defcustom gnus-alter-header-function nil
1042   "Function called to allow alteration of article header structures.
1043 The function is called with one parameter, the article header vector,
1044 which it may alter in any way."
1045   :type '(choice (const :tag "None" nil)
1046                  function)
1047   :group 'gnus-summary)
1048
1049 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1050   "Variable that says which function should be used to decode a string with encoded words.")
1051
1052 (defcustom gnus-extra-headers '(To Newsgroups)
1053   "*Extra headers to parse."
1054   :version "21.1"
1055   :group 'gnus-summary
1056   :type '(repeat symbol))
1057
1058 (defcustom gnus-ignored-from-addresses
1059   (and user-mail-address (regexp-quote user-mail-address))
1060   "*Regexp of From headers that may be suppressed in favor of To headers."
1061   :version "21.1"
1062   :group 'gnus-summary
1063   :type 'regexp)
1064
1065 (defcustom gnus-summary-to-prefix "-> "
1066   "*String prefixed to the To field in the summary line when
1067 using `gnus-ignored-from-addresses'."
1068   :version "22.1"
1069   :group 'gnus-summary
1070   :type 'string)
1071
1072 (defcustom gnus-summary-newsgroup-prefix "=> "
1073   "*String prefixed to the Newsgroup field in the summary
1074 line when using `gnus-ignored-from-addresses'."
1075   :version "22.1"
1076   :group 'gnus-summary
1077   :type 'string)
1078
1079 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1080   "List of charsets that should be ignored.
1081 When these charsets are used in the \"charset\" parameter, the
1082 default charset will be used instead."
1083   :version "21.1"
1084   :type '(repeat symbol)
1085   :group 'gnus-charset)
1086
1087 (gnus-define-group-parameter
1088  ignored-charsets
1089  :type list
1090  :function-document
1091  "Return the ignored charsets of GROUP."
1092  :variable gnus-group-ignored-charsets-alist
1093  :variable-default
1094  '(("alt\\.chinese\\.text" iso-8859-1))
1095  :variable-document
1096  "Alist of regexps (to match group names) and charsets that should be ignored.
1097 When these charsets are used in the \"charset\" parameter, the
1098 default charset will be used instead."
1099  :variable-group gnus-charset
1100  :variable-type '(repeat (cons (regexp :tag "Group")
1101                                (repeat symbol)))
1102  :parameter-type '(choice :tag "Ignored charsets"
1103                           :value nil
1104                           (repeat (symbol)))
1105  :parameter-document       "\
1106 List of charsets that should be ignored.
1107
1108 When these charsets are used in the \"charset\" parameter, the
1109 default charset will be used instead.")
1110
1111 (defcustom gnus-group-highlight-words-alist nil
1112   "Alist of group regexps and highlight regexps.
1113 This variable uses the same syntax as `gnus-emphasis-alist'."
1114   :version "21.1"
1115   :type '(repeat (cons (regexp :tag "Group")
1116                        (repeat (list (regexp :tag "Highlight regexp")
1117                                      (number :tag "Group for entire word" 0)
1118                                      (number :tag "Group for displayed part" 0)
1119                                      (symbol :tag "Face"
1120                                              gnus-emphasis-highlight-words)))))
1121   :group 'gnus-summary-visual)
1122
1123 (defcustom gnus-summary-show-article-charset-alist
1124   nil
1125   "Alist of number and charset.
1126 The article will be shown with the charset corresponding to the
1127 numbered argument.
1128 For example: ((1 . cn-gb-2312) (2 . big5))."
1129   :version "21.1"
1130   :type '(repeat (cons (number :tag "Argument" 1)
1131                        (symbol :tag "Charset")))
1132   :group 'gnus-charset)
1133
1134 (defcustom gnus-preserve-marks t
1135   "Whether marks are preserved when moving, copying and respooling messages."
1136   :version "21.1"
1137   :type 'boolean
1138   :group 'gnus-summary-marks)
1139
1140 (defcustom gnus-alter-articles-to-read-function nil
1141   "Function to be called to alter the list of articles to be selected."
1142   :type '(choice (const nil) function)
1143   :group 'gnus-summary)
1144
1145 (defcustom gnus-orphan-score nil
1146   "*All orphans get this score added.  Set in the score file."
1147   :group 'gnus-score-default
1148   :type '(choice (const nil)
1149                  integer))
1150
1151 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1152   "*A regexp to match MIME parts when saving multiple parts of a
1153 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1154 This regexp will be used by default when prompting the user for which
1155 type of files to save."
1156   :group 'gnus-summary
1157   :type 'regexp)
1158
1159 (defcustom gnus-read-all-available-headers nil
1160   "Whether Gnus should parse all headers made available to it.
1161 This is mostly relevant for slow back ends where the user may
1162 wish to widen the summary buffer to include all headers
1163 that were fetched.  Say, for nnultimate groups."
1164   :version "22.1"
1165   :group 'gnus-summary
1166   :type '(choice boolean regexp))
1167
1168 (defcustom gnus-summary-muttprint-program "muttprint"
1169   "Command (and optional arguments) used to run Muttprint."
1170   :version "22.1"
1171   :group 'gnus-summary
1172   :type 'string)
1173
1174 (defcustom gnus-article-loose-mime t
1175   "If non-nil, don't require MIME-Version header.
1176 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1177 supply the MIME-Version header or deliberately strip it from the mail.
1178 If non-nil (the default), Gnus will treat some articles as MIME
1179 even if the MIME-Version header is missing."
1180   :version "22.1"
1181   :type 'boolean
1182   :group 'gnus-article-mime)
1183
1184 (defcustom gnus-article-emulate-mime t
1185   "If non-nil, use MIME emulation for uuencode and the like.
1186 This means that Gnus will search message bodies for text that look
1187 like uuencoded bits, yEncoded bits, and so on, and present that using
1188 the normal Gnus MIME machinery."
1189   :version "22.1"
1190   :type 'boolean
1191   :group 'gnus-article-mime)
1192
1193 ;;; Internal variables
1194
1195 (defvar gnus-summary-display-cache nil)
1196 (defvar gnus-article-mime-handles nil)
1197 (defvar gnus-article-decoded-p nil)
1198 (defvar gnus-article-charset nil)
1199 (defvar gnus-article-ignored-charsets nil)
1200 (defvar gnus-scores-exclude-files nil)
1201 (defvar gnus-page-broken nil)
1202
1203 (defvar gnus-original-article nil)
1204 (defvar gnus-article-internal-prepare-hook nil)
1205 (defvar gnus-newsgroup-process-stack nil)
1206
1207 (defvar gnus-thread-indent-array nil)
1208 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1209 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1210   "Function called to sort the articles within a thread after it has been gathered together.")
1211
1212 (defvar gnus-summary-save-parts-type-history nil)
1213 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1214
1215 ;; Avoid highlighting in kill files.
1216 (defvar gnus-summary-inhibit-highlight nil)
1217 (defvar gnus-newsgroup-selected-overlay nil)
1218 (defvar gnus-inhibit-limiting nil)
1219 (defvar gnus-newsgroup-adaptive-score-file nil)
1220 (defvar gnus-current-score-file nil)
1221 (defvar gnus-current-move-group nil)
1222 (defvar gnus-current-copy-group nil)
1223 (defvar gnus-current-crosspost-group nil)
1224 (defvar gnus-newsgroup-display nil)
1225
1226 (defvar gnus-newsgroup-dependencies nil)
1227 (defvar gnus-newsgroup-adaptive nil)
1228 (defvar gnus-summary-display-article-function nil)
1229 (defvar gnus-summary-highlight-line-function nil
1230   "Function called after highlighting a summary line.")
1231
1232 (defvar gnus-summary-line-format-alist
1233   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1234     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1235     (?s gnus-tmp-subject-or-nil ?s)
1236     (?n gnus-tmp-name ?s)
1237     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1238         ?s)
1239     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1240             gnus-tmp-from) ?s)
1241     (?F gnus-tmp-from ?s)
1242     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1243     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1244     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1245     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1246     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1247     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1248     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1249     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1250     (?L gnus-tmp-lines ?s)
1251     (?O gnus-tmp-downloaded ?c)
1252     (?I gnus-tmp-indentation ?s)
1253     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1254     (?R gnus-tmp-replied ?c)
1255     (?\[ gnus-tmp-opening-bracket ?c)
1256     (?\] gnus-tmp-closing-bracket ?c)
1257     (?\> (make-string gnus-tmp-level ? ) ?s)
1258     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1259     (?i gnus-tmp-score ?d)
1260     (?z gnus-tmp-score-char ?c)
1261     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1262     (?U gnus-tmp-unread ?c)
1263     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1264         ?s)
1265     (?t (gnus-summary-number-of-articles-in-thread
1266          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1267         ?d)
1268     (?e (gnus-summary-number-of-articles-in-thread
1269          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1270         ?c)
1271     (?u gnus-tmp-user-defined ?s)
1272     (?P (gnus-pick-line-number) ?d)
1273     (?B gnus-tmp-thread-tree-header-string ?s)
1274     (user-date (gnus-user-date
1275                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1276   "An alist of format specifications that can appear in summary lines.
1277 These are paired with what variables they correspond with, along with
1278 the type of the variable (string, integer, character, etc).")
1279
1280 (defvar gnus-summary-dummy-line-format-alist
1281   `((?S gnus-tmp-subject ?s)
1282     (?N gnus-tmp-number ?d)
1283     (?u gnus-tmp-user-defined ?s)))
1284
1285 (defvar gnus-summary-mode-line-format-alist
1286   `((?G gnus-tmp-group-name ?s)
1287     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1288     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1289     (?A gnus-tmp-article-number ?d)
1290     (?Z gnus-tmp-unread-and-unselected ?s)
1291     (?V gnus-version ?s)
1292     (?U gnus-tmp-unread-and-unticked ?d)
1293     (?S gnus-tmp-subject ?s)
1294     (?e gnus-tmp-unselected ?d)
1295     (?u gnus-tmp-user-defined ?s)
1296     (?d (length gnus-newsgroup-dormant) ?d)
1297     (?t (length gnus-newsgroup-marked) ?d)
1298     (?h (length gnus-newsgroup-spam-marked) ?d)
1299     (?r (length gnus-newsgroup-reads) ?d)
1300     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1301     (?E gnus-newsgroup-expunged-tally ?d)
1302     (?s (gnus-current-score-file-nondirectory) ?s)))
1303
1304 (defvar gnus-last-search-regexp nil
1305   "Default regexp for article search command.")
1306
1307 (defvar gnus-last-shell-command nil
1308   "Default shell command on article.")
1309
1310 (defvar gnus-newsgroup-agentized nil
1311   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1312 (defvar gnus-newsgroup-begin nil)
1313 (defvar gnus-newsgroup-end nil)
1314 (defvar gnus-newsgroup-last-rmail nil)
1315 (defvar gnus-newsgroup-last-mail nil)
1316 (defvar gnus-newsgroup-last-folder nil)
1317 (defvar gnus-newsgroup-last-file nil)
1318 (defvar gnus-newsgroup-auto-expire nil)
1319 (defvar gnus-newsgroup-active nil)
1320
1321 (defvar gnus-newsgroup-data nil)
1322 (defvar gnus-newsgroup-data-reverse nil)
1323 (defvar gnus-newsgroup-limit nil)
1324 (defvar gnus-newsgroup-limits nil)
1325 (defvar gnus-summary-use-undownloaded-faces nil)
1326
1327 (defvar gnus-newsgroup-unreads nil
1328   "Sorted list of unread articles in the current newsgroup.")
1329
1330 (defvar gnus-newsgroup-unselected nil
1331   "Sorted list of unselected unread articles in the current newsgroup.")
1332
1333 (defvar gnus-newsgroup-reads nil
1334   "Alist of read articles and article marks in the current newsgroup.")
1335
1336 (defvar gnus-newsgroup-expunged-tally nil)
1337
1338 (defvar gnus-newsgroup-marked nil
1339   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1340
1341 (defvar gnus-newsgroup-spam-marked nil
1342   "List of ranges of articles that have been marked as spam.")
1343
1344 (defvar gnus-newsgroup-killed nil
1345   "List of ranges of articles that have been through the scoring process.")
1346
1347 (defvar gnus-newsgroup-cached nil
1348   "Sorted list of articles that come from the article cache.")
1349
1350 (defvar gnus-newsgroup-saved nil
1351   "List of articles that have been saved.")
1352
1353 (defvar gnus-newsgroup-kill-headers nil)
1354
1355 (defvar gnus-newsgroup-replied nil
1356   "List of articles that have been replied to in the current newsgroup.")
1357
1358 (defvar gnus-newsgroup-forwarded nil
1359   "List of articles that have been forwarded in the current newsgroup.")
1360
1361 (defvar gnus-newsgroup-recent nil
1362   "List of articles that have are recent in the current newsgroup.")
1363
1364 (defvar gnus-newsgroup-expirable nil
1365   "Sorted list of articles in the current newsgroup that can be expired.")
1366
1367 (defvar gnus-newsgroup-processable nil
1368   "List of articles in the current newsgroup that can be processed.")
1369
1370 (defvar gnus-newsgroup-downloadable nil
1371   "Sorted list of articles in the current newsgroup that can be processed.")
1372
1373 (defvar gnus-newsgroup-unfetched nil
1374   "Sorted list of articles in the current newsgroup whose headers have
1375 not been fetched into the agent.
1376
1377 This list will always be a subset of gnus-newsgroup-undownloaded.")
1378
1379 (defvar gnus-newsgroup-undownloaded nil
1380   "List of articles in the current newsgroup that haven't been downloaded.")
1381
1382 (defvar gnus-newsgroup-unsendable nil
1383   "List of articles in the current newsgroup that won't be sent.")
1384
1385 (defvar gnus-newsgroup-bookmarks nil
1386   "List of articles in the current newsgroup that have bookmarks.")
1387
1388 (defvar gnus-newsgroup-dormant nil
1389   "Sorted list of dormant articles in the current newsgroup.")
1390
1391 (defvar gnus-newsgroup-unseen nil
1392   "List of unseen articles in the current newsgroup.")
1393
1394 (defvar gnus-newsgroup-seen nil
1395   "Range of seen articles in the current newsgroup.")
1396
1397 (defvar gnus-newsgroup-articles nil
1398   "List of articles in the current newsgroup.")
1399
1400 (defvar gnus-newsgroup-scored nil
1401   "List of scored articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-headers nil
1404   "List of article headers in the current newsgroup.")
1405
1406 (defvar gnus-newsgroup-threads nil)
1407
1408 (defvar gnus-newsgroup-prepared nil
1409   "Whether the current group has been prepared properly.")
1410
1411 (defvar gnus-newsgroup-ancient nil
1412   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-sparse nil)
1415
1416 (defvar gnus-current-article nil)
1417 (defvar gnus-article-current nil)
1418 (defvar gnus-current-headers nil)
1419 (defvar gnus-have-all-headers nil)
1420 (defvar gnus-last-article nil)
1421 (defvar gnus-newsgroup-history nil)
1422 (defvar gnus-newsgroup-charset nil)
1423 (defvar gnus-newsgroup-ephemeral-charset nil)
1424 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1425
1426 (defvar gnus-article-before-search nil)
1427
1428 (defvar gnus-summary-local-variables
1429   '(gnus-newsgroup-name
1430     gnus-newsgroup-begin gnus-newsgroup-end
1431     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1432     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1433     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1434     gnus-newsgroup-unselected gnus-newsgroup-marked
1435     gnus-newsgroup-spam-marked
1436     gnus-newsgroup-reads gnus-newsgroup-saved
1437     gnus-newsgroup-replied gnus-newsgroup-forwarded
1438     gnus-newsgroup-recent
1439     gnus-newsgroup-expirable
1440     gnus-newsgroup-processable gnus-newsgroup-killed
1441     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1442     gnus-newsgroup-unfetched
1443     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1444     gnus-newsgroup-seen gnus-newsgroup-articles
1445     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1446     gnus-newsgroup-headers gnus-newsgroup-threads
1447     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1448     gnus-current-article gnus-current-headers gnus-have-all-headers
1449     gnus-last-article gnus-article-internal-prepare-hook
1450     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1451     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1452     gnus-thread-expunge-below
1453     gnus-score-alist gnus-current-score-file
1454     (gnus-summary-expunge-below . global)
1455     (gnus-summary-mark-below . global)
1456     (gnus-orphan-score . global)
1457     gnus-newsgroup-active gnus-scores-exclude-files
1458     gnus-newsgroup-history gnus-newsgroup-ancient
1459     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1460     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1461     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1462     (gnus-newsgroup-expunged-tally . 0)
1463     gnus-cache-removable-articles gnus-newsgroup-cached
1464     gnus-newsgroup-data gnus-newsgroup-data-reverse
1465     gnus-newsgroup-limit gnus-newsgroup-limits
1466     gnus-newsgroup-charset gnus-newsgroup-display
1467     gnus-summary-use-undownloaded-faces)
1468   "Variables that are buffer-local to the summary buffers.")
1469
1470 (defvar gnus-newsgroup-variables nil
1471   "A list of variables that have separate values in different newsgroups.
1472 A list of newsgroup (summary buffer) local variables, or cons of
1473 variables and their default expressions to be evalled (when the default
1474 values are not nil), that should be made global while the summary buffer
1475 is active.
1476
1477 Note: The default expressions will be evaluated (using function `eval')
1478 before assignment to the local variable rather than just assigned to it.
1479 If the default expression is the symbol `global', that symbol will not
1480 be evaluated but the global value of the local variable will be used
1481 instead.
1482
1483 These variables can be used to set variables in the group parameters
1484 while still allowing them to affect operations done in other buffers.
1485 For example:
1486
1487 \(setq gnus-newsgroup-variables
1488      '(message-use-followup-to
1489        (gnus-visible-headers .
1490          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1491 ")
1492
1493 ;; Byte-compiler warning.
1494 (eval-when-compile
1495   ;; Bind features so that require will believe that gnus-sum has
1496   ;; already been loaded (avoids infinite recursion)
1497   (let ((features (cons 'gnus-sum features)))
1498     ;; Several of the declarations in gnus-sum are needed to load the
1499     ;; following files. Right now, these definitions have been
1500     ;; compiled but not defined (evaluated).  We could either do a
1501     ;; eval-and-compile about all of the declarations or evaluate the
1502     ;; source file.
1503     (if (boundp 'gnus-newsgroup-variables)
1504         nil
1505       (load "gnus-sum.el" t t t))
1506     (require 'gnus)
1507     (require 'gnus-art)))
1508
1509 ;; MIME stuff.
1510
1511 (defvar gnus-decode-encoded-word-methods
1512   '(mail-decode-encoded-word-string)
1513   "List of methods used to decode encoded words.
1514
1515 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1516 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1517 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1518 whose names match REGEXP.
1519
1520 For example:
1521 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1522  mail-decode-encoded-word-string
1523  (\"chinese\" . rfc1843-decode-string))")
1524
1525 (defvar gnus-decode-encoded-word-methods-cache nil)
1526
1527 (defun gnus-multi-decode-encoded-word-string (string)
1528   "Apply the functions from `gnus-encoded-word-methods' that match."
1529   (unless (and gnus-decode-encoded-word-methods-cache
1530                (eq gnus-newsgroup-name
1531                    (car gnus-decode-encoded-word-methods-cache)))
1532     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1533     (mapcar (lambda (x)
1534               (if (symbolp x)
1535                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1536                 (if (and gnus-newsgroup-name
1537                          (string-match (car x) gnus-newsgroup-name))
1538                     (nconc gnus-decode-encoded-word-methods-cache
1539                            (list (cdr x))))))
1540             gnus-decode-encoded-word-methods))
1541   (let ((xlist gnus-decode-encoded-word-methods-cache))
1542     (pop xlist)
1543     (while xlist
1544       (setq string (funcall (pop xlist) string))))
1545   string)
1546
1547 ;; Subject simplification.
1548
1549 (defun gnus-simplify-whitespace (str)
1550   "Remove excessive whitespace from STR."
1551   ;; Multiple spaces.
1552   (while (string-match "[ \t][ \t]+" str)
1553     (setq str (concat (substring str 0 (match-beginning 0))
1554                         " "
1555                         (substring str (match-end 0)))))
1556   ;; Leading spaces.
1557   (when (string-match "^[ \t]+" str)
1558     (setq str (substring str (match-end 0))))
1559   ;; Trailing spaces.
1560   (when (string-match "[ \t]+$" str)
1561     (setq str (substring str 0 (match-beginning 0))))
1562   str)
1563
1564 (defun gnus-simplify-all-whitespace (str)
1565   "Remove all whitespace from STR."
1566   (while (string-match "[ \t\n]+" str)
1567     (setq str (replace-match "" nil nil str)))
1568   str)
1569
1570 (defsubst gnus-simplify-subject-re (subject)
1571   "Remove \"Re:\" from subject lines."
1572   (if (string-match message-subject-re-regexp subject)
1573       (substring subject (match-end 0))
1574     subject))
1575
1576 (defun gnus-simplify-subject (subject &optional re-only)
1577   "Remove `Re:' and words in parentheses.
1578 If RE-ONLY is non-nil, strip leading `Re:'s only."
1579   (let ((case-fold-search t))           ;Ignore case.
1580     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1581     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1582       (setq subject (substring subject (match-end 0))))
1583     ;; Remove uninteresting prefixes.
1584     (when (and (not re-only)
1585                gnus-simplify-ignored-prefixes
1586                (string-match gnus-simplify-ignored-prefixes subject))
1587       (setq subject (substring subject (match-end 0))))
1588     ;; Remove words in parentheses from end.
1589     (unless re-only
1590       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1591         (setq subject (substring subject 0 (match-beginning 0)))))
1592     ;; Return subject string.
1593     subject))
1594
1595 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1596 ;; all whitespace.
1597 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1598   (goto-char (point-min))
1599   (while (re-search-forward regexp nil t)
1600     (replace-match (or newtext ""))))
1601
1602 (defun gnus-simplify-buffer-fuzzy ()
1603   "Simplify string in the buffer fuzzily.
1604 The string in the accessible portion of the current buffer is simplified.
1605 It is assumed to be a single-line subject.
1606 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1607 matter is removed.  Additional things can be deleted by setting
1608 `gnus-simplify-subject-fuzzy-regexp'."
1609   (let ((case-fold-search t)
1610         (modified-tick))
1611     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1612
1613     (while (not (eq modified-tick (buffer-modified-tick)))
1614       (setq modified-tick (buffer-modified-tick))
1615       (cond
1616        ((listp gnus-simplify-subject-fuzzy-regexp)
1617         (mapcar 'gnus-simplify-buffer-fuzzy-step
1618                 gnus-simplify-subject-fuzzy-regexp))
1619        (gnus-simplify-subject-fuzzy-regexp
1620         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1621       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1622       (gnus-simplify-buffer-fuzzy-step
1623        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1624       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1625
1626     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1627     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1628     (gnus-simplify-buffer-fuzzy-step " $")
1629     (gnus-simplify-buffer-fuzzy-step "^ +")))
1630
1631 (defun gnus-simplify-subject-fuzzy (subject)
1632   "Simplify a subject string fuzzily.
1633 See `gnus-simplify-buffer-fuzzy' for details."
1634   (save-excursion
1635     (gnus-set-work-buffer)
1636     (let ((case-fold-search t))
1637       ;; Remove uninteresting prefixes.
1638       (when (and gnus-simplify-ignored-prefixes
1639                  (string-match gnus-simplify-ignored-prefixes subject))
1640         (setq subject (substring subject (match-end 0))))
1641       (insert subject)
1642       (inline (gnus-simplify-buffer-fuzzy))
1643       (buffer-string))))
1644
1645 (defsubst gnus-simplify-subject-fully (subject)
1646   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1647   (cond
1648    (gnus-simplify-subject-functions
1649     (gnus-map-function gnus-simplify-subject-functions subject))
1650    ((null gnus-summary-gather-subject-limit)
1651     (gnus-simplify-subject-re subject))
1652    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1653     (gnus-simplify-subject-fuzzy subject))
1654    ((numberp gnus-summary-gather-subject-limit)
1655     (gnus-limit-string (gnus-simplify-subject-re subject)
1656                        gnus-summary-gather-subject-limit))
1657    (t
1658     subject)))
1659
1660 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1661   "Check whether two subjects are equal.
1662 If optional argument SIMPLE-FIRST is t, first argument is already
1663 simplified."
1664   (cond
1665    ((null simple-first)
1666     (equal (gnus-simplify-subject-fully s1)
1667            (gnus-simplify-subject-fully s2)))
1668    (t
1669     (equal s1
1670            (gnus-simplify-subject-fully s2)))))
1671
1672 (defun gnus-summary-bubble-group ()
1673   "Increase the score of the current group.
1674 This is a handy function to add to `gnus-summary-exit-hook' to
1675 increase the score of each group you read."
1676   (gnus-group-add-score gnus-newsgroup-name))
1677
1678 \f
1679 ;;;
1680 ;;; Gnus summary mode
1681 ;;;
1682
1683 (put 'gnus-summary-mode 'mode-class 'special)
1684
1685 (defvar gnus-article-commands-menu)
1686
1687 ;; Non-orthogonal keys
1688
1689 (gnus-define-keys gnus-summary-mode-map
1690   " " gnus-summary-next-page
1691   "\177" gnus-summary-prev-page
1692   [delete] gnus-summary-prev-page
1693   [backspace] gnus-summary-prev-page
1694   "\r" gnus-summary-scroll-up
1695   "\M-\r" gnus-summary-scroll-down
1696   "n" gnus-summary-next-unread-article
1697   "p" gnus-summary-prev-unread-article
1698   "N" gnus-summary-next-article
1699   "P" gnus-summary-prev-article
1700   "\M-\C-n" gnus-summary-next-same-subject
1701   "\M-\C-p" gnus-summary-prev-same-subject
1702   "\M-n" gnus-summary-next-unread-subject
1703   "\M-p" gnus-summary-prev-unread-subject
1704   "." gnus-summary-first-unread-article
1705   "," gnus-summary-best-unread-article
1706   "\M-s" gnus-summary-search-article-forward
1707   "\M-r" gnus-summary-search-article-backward
1708   "<" gnus-summary-beginning-of-article
1709   ">" gnus-summary-end-of-article
1710   "j" gnus-summary-goto-article
1711   "^" gnus-summary-refer-parent-article
1712   "\M-^" gnus-summary-refer-article
1713   "u" gnus-summary-tick-article-forward
1714   "!" gnus-summary-tick-article-forward
1715   "U" gnus-summary-tick-article-backward
1716   "d" gnus-summary-mark-as-read-forward
1717   "D" gnus-summary-mark-as-read-backward
1718   "E" gnus-summary-mark-as-expirable
1719   "\M-u" gnus-summary-clear-mark-forward
1720   "\M-U" gnus-summary-clear-mark-backward
1721   "k" gnus-summary-kill-same-subject-and-select
1722   "\C-k" gnus-summary-kill-same-subject
1723   "\M-\C-k" gnus-summary-kill-thread
1724   "\M-\C-l" gnus-summary-lower-thread
1725   "e" gnus-summary-edit-article
1726   "#" gnus-summary-mark-as-processable
1727   "\M-#" gnus-summary-unmark-as-processable
1728   "\M-\C-t" gnus-summary-toggle-threads
1729   "\M-\C-s" gnus-summary-show-thread
1730   "\M-\C-h" gnus-summary-hide-thread
1731   "\M-\C-f" gnus-summary-next-thread
1732   "\M-\C-b" gnus-summary-prev-thread
1733   [(meta down)] gnus-summary-next-thread
1734   [(meta up)] gnus-summary-prev-thread
1735   "\M-\C-u" gnus-summary-up-thread
1736   "\M-\C-d" gnus-summary-down-thread
1737   "&" gnus-summary-execute-command
1738   "c" gnus-summary-catchup-and-exit
1739   "\C-w" gnus-summary-mark-region-as-read
1740   "\C-t" gnus-summary-toggle-truncation
1741   "?" gnus-summary-mark-as-dormant
1742   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1743   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1744   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1745   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1746   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1747   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1748   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1749   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1750   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1751   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1752   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1753   "=" gnus-summary-expand-window
1754   "\C-x\C-s" gnus-summary-reselect-current-group
1755   "\M-g" gnus-summary-rescan-group
1756   "w" gnus-summary-stop-page-breaking
1757   "\C-c\C-r" gnus-summary-caesar-message
1758   "f" gnus-summary-followup
1759   "F" gnus-summary-followup-with-original
1760   "C" gnus-summary-cancel-article
1761   "r" gnus-summary-reply
1762   "R" gnus-summary-reply-with-original
1763   "\C-c\C-f" gnus-summary-mail-forward
1764   "o" gnus-summary-save-article
1765   "\C-o" gnus-summary-save-article-mail
1766   "|" gnus-summary-pipe-output
1767   "\M-k" gnus-summary-edit-local-kill
1768   "\M-K" gnus-summary-edit-global-kill
1769   ;; "V" gnus-version
1770   "\C-c\C-d" gnus-summary-describe-group
1771   "q" gnus-summary-exit
1772   "Q" gnus-summary-exit-no-update
1773   "\C-c\C-i" gnus-info-find-node
1774   gnus-mouse-2 gnus-mouse-pick-article
1775   [follow-link] mouse-face
1776   "m" gnus-summary-mail-other-window
1777   "a" gnus-summary-post-news
1778   "i" gnus-summary-news-other-window
1779   "x" gnus-summary-limit-to-unread
1780   "s" gnus-summary-isearch-article
1781   "t" gnus-summary-toggle-header
1782   "g" gnus-summary-show-article
1783   "l" gnus-summary-goto-last-article
1784   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1785   "\C-d" gnus-summary-enter-digest-group
1786   "\M-\C-d" gnus-summary-read-document
1787   "\M-\C-e" gnus-summary-edit-parameters
1788   "\M-\C-a" gnus-summary-customize-parameters
1789   "\C-c\C-b" gnus-bug
1790   "*" gnus-cache-enter-article
1791   "\M-*" gnus-cache-remove-article
1792   "\M-&" gnus-summary-universal-argument
1793   "\C-l" gnus-recenter
1794   "I" gnus-summary-increase-score
1795   "L" gnus-summary-lower-score
1796   "\M-i" gnus-symbolic-argument
1797   "h" gnus-summary-select-article-buffer
1798
1799   "b" gnus-article-view-part
1800   "\M-t" gnus-summary-toggle-display-buttonized
1801
1802   "V" gnus-summary-score-map
1803   "X" gnus-uu-extract-map
1804   "S" gnus-summary-send-map)
1805
1806 ;; Sort of orthogonal keymap
1807 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1808   "t" gnus-summary-tick-article-forward
1809   "!" gnus-summary-tick-article-forward
1810   "d" gnus-summary-mark-as-read-forward
1811   "r" gnus-summary-mark-as-read-forward
1812   "c" gnus-summary-clear-mark-forward
1813   " " gnus-summary-clear-mark-forward
1814   "e" gnus-summary-mark-as-expirable
1815   "x" gnus-summary-mark-as-expirable
1816   "?" gnus-summary-mark-as-dormant
1817   "b" gnus-summary-set-bookmark
1818   "B" gnus-summary-remove-bookmark
1819   "#" gnus-summary-mark-as-processable
1820   "\M-#" gnus-summary-unmark-as-processable
1821   "S" gnus-summary-limit-include-expunged
1822   "C" gnus-summary-catchup
1823   "H" gnus-summary-catchup-to-here
1824   "h" gnus-summary-catchup-from-here
1825   "\C-c" gnus-summary-catchup-all
1826   "k" gnus-summary-kill-same-subject-and-select
1827   "K" gnus-summary-kill-same-subject
1828   "P" gnus-uu-mark-map)
1829
1830 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1831   "c" gnus-summary-clear-above
1832   "u" gnus-summary-tick-above
1833   "m" gnus-summary-mark-above
1834   "k" gnus-summary-kill-below)
1835
1836 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1837   "/" gnus-summary-limit-to-subject
1838   "n" gnus-summary-limit-to-articles
1839   "w" gnus-summary-pop-limit
1840   "s" gnus-summary-limit-to-subject
1841   "a" gnus-summary-limit-to-author
1842   "u" gnus-summary-limit-to-unread
1843   "m" gnus-summary-limit-to-marks
1844   "M" gnus-summary-limit-exclude-marks
1845   "v" gnus-summary-limit-to-score
1846   "*" gnus-summary-limit-include-cached
1847   "D" gnus-summary-limit-include-dormant
1848   "T" gnus-summary-limit-include-thread
1849   "d" gnus-summary-limit-exclude-dormant
1850   "t" gnus-summary-limit-to-age
1851   "." gnus-summary-limit-to-unseen
1852   "x" gnus-summary-limit-to-extra
1853   "p" gnus-summary-limit-to-display-predicate
1854   "E" gnus-summary-limit-include-expunged
1855   "c" gnus-summary-limit-exclude-childless-dormant
1856   "C" gnus-summary-limit-mark-excluded-as-read
1857   "o" gnus-summary-insert-old-articles
1858   "N" gnus-summary-insert-new-articles
1859   "r" gnus-summary-limit-to-replied
1860   "R" gnus-summary-limit-to-recipient)
1861
1862 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1863   "n" gnus-summary-next-unread-article
1864   "p" gnus-summary-prev-unread-article
1865   "N" gnus-summary-next-article
1866   "P" gnus-summary-prev-article
1867   "\C-n" gnus-summary-next-same-subject
1868   "\C-p" gnus-summary-prev-same-subject
1869   "\M-n" gnus-summary-next-unread-subject
1870   "\M-p" gnus-summary-prev-unread-subject
1871   "f" gnus-summary-first-unread-article
1872   "b" gnus-summary-best-unread-article
1873   "j" gnus-summary-goto-article
1874   "g" gnus-summary-goto-subject
1875   "l" gnus-summary-goto-last-article
1876   "o" gnus-summary-pop-article)
1877
1878 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1879   "k" gnus-summary-kill-thread
1880   "l" gnus-summary-lower-thread
1881   "i" gnus-summary-raise-thread
1882   "T" gnus-summary-toggle-threads
1883   "t" gnus-summary-rethread-current
1884   "^" gnus-summary-reparent-thread
1885   "s" gnus-summary-show-thread
1886   "S" gnus-summary-show-all-threads
1887   "h" gnus-summary-hide-thread
1888   "H" gnus-summary-hide-all-threads
1889   "n" gnus-summary-next-thread
1890   "p" gnus-summary-prev-thread
1891   "u" gnus-summary-up-thread
1892   "o" gnus-summary-top-thread
1893   "d" gnus-summary-down-thread
1894   "#" gnus-uu-mark-thread
1895   "\M-#" gnus-uu-unmark-thread)
1896
1897 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1898   "g" gnus-summary-prepare
1899   "c" gnus-summary-insert-cached-articles
1900   "d" gnus-summary-insert-dormant-articles)
1901
1902 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1903   "c" gnus-summary-catchup-and-exit
1904   "C" gnus-summary-catchup-all-and-exit
1905   "E" gnus-summary-exit-no-update
1906   "Q" gnus-summary-exit
1907   "Z" gnus-summary-exit
1908   "n" gnus-summary-catchup-and-goto-next-group
1909   "R" gnus-summary-reselect-current-group
1910   "G" gnus-summary-rescan-group
1911   "N" gnus-summary-next-group
1912   "s" gnus-summary-save-newsrc
1913   "P" gnus-summary-prev-group)
1914
1915 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1916   " " gnus-summary-next-page
1917   "n" gnus-summary-next-page
1918   "\177" gnus-summary-prev-page
1919   [delete] gnus-summary-prev-page
1920   "p" gnus-summary-prev-page
1921   "\r" gnus-summary-scroll-up
1922   "\M-\r" gnus-summary-scroll-down
1923   "<" gnus-summary-beginning-of-article
1924   ">" gnus-summary-end-of-article
1925   "b" gnus-summary-beginning-of-article
1926   "e" gnus-summary-end-of-article
1927   "^" gnus-summary-refer-parent-article
1928   "r" gnus-summary-refer-parent-article
1929   "D" gnus-summary-enter-digest-group
1930   "R" gnus-summary-refer-references
1931   "T" gnus-summary-refer-thread
1932   "g" gnus-summary-show-article
1933   "s" gnus-summary-isearch-article
1934   "P" gnus-summary-print-article
1935   "M" gnus-mailing-list-insinuate
1936   "t" gnus-article-babel)
1937
1938 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1939   "b" gnus-article-add-buttons
1940   "B" gnus-article-add-buttons-to-head
1941   "o" gnus-article-treat-overstrike
1942   "e" gnus-article-emphasize
1943   "w" gnus-article-fill-cited-article
1944   "Q" gnus-article-fill-long-lines
1945   "C" gnus-article-capitalize-sentences
1946   "c" gnus-article-remove-cr
1947   "q" gnus-article-de-quoted-unreadable
1948   "6" gnus-article-de-base64-unreadable
1949   "Z" gnus-article-decode-HZ
1950   "A" gnus-article-treat-ansi-sequences
1951   "h" gnus-article-wash-html
1952   "u" gnus-article-unsplit-urls
1953   "s" gnus-summary-force-verify-and-decrypt
1954   "f" gnus-article-display-x-face
1955   "l" gnus-summary-stop-page-breaking
1956   "r" gnus-summary-caesar-message
1957   "m" gnus-summary-morse-message
1958   "t" gnus-summary-toggle-header
1959   "g" gnus-treat-smiley
1960   "v" gnus-summary-verbose-headers
1961   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1962   "p" gnus-article-verify-x-pgp-sig
1963   "d" gnus-article-treat-dumbquotes
1964   "i" gnus-summary-idna-message)
1965
1966 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1967   ;; mnemonic: deuglif*Y*
1968   "u" gnus-article-outlook-unwrap-lines
1969   "a" gnus-article-outlook-repair-attribution
1970   "c" gnus-article-outlook-rearrange-citation
1971   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1972
1973 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1974   "a" gnus-article-hide
1975   "h" gnus-article-hide-headers
1976   "b" gnus-article-hide-boring-headers
1977   "s" gnus-article-hide-signature
1978   "c" gnus-article-hide-citation
1979   "C" gnus-article-hide-citation-in-followups
1980   "l" gnus-article-hide-list-identifiers
1981   "B" gnus-article-strip-banner
1982   "P" gnus-article-hide-pem
1983   "\C-c" gnus-article-hide-citation-maybe)
1984
1985 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1986   "a" gnus-article-highlight
1987   "h" gnus-article-highlight-headers
1988   "c" gnus-article-highlight-citation
1989   "s" gnus-article-highlight-signature)
1990
1991 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1992   "f" gnus-article-treat-fold-headers
1993   "u" gnus-article-treat-unfold-headers
1994   "n" gnus-article-treat-fold-newsgroups)
1995
1996 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1997   "x" gnus-article-display-x-face
1998   "d" gnus-article-display-face
1999   "s" gnus-treat-smiley
2000   "D" gnus-article-remove-images
2001   "f" gnus-treat-from-picon
2002   "m" gnus-treat-mail-picon
2003   "n" gnus-treat-newsgroups-picon)
2004
2005 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2006   "w" gnus-article-decode-mime-words
2007   "c" gnus-article-decode-charset
2008   "v" gnus-mime-view-all-parts
2009   "b" gnus-article-view-part)
2010
2011 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2012   "z" gnus-article-date-ut
2013   "u" gnus-article-date-ut
2014   "l" gnus-article-date-local
2015   "p" gnus-article-date-english
2016   "e" gnus-article-date-lapsed
2017   "o" gnus-article-date-original
2018   "i" gnus-article-date-iso8601
2019   "s" gnus-article-date-user)
2020
2021 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2022   "t" gnus-article-remove-trailing-blank-lines
2023   "l" gnus-article-strip-leading-blank-lines
2024   "m" gnus-article-strip-multiple-blank-lines
2025   "a" gnus-article-strip-blank-lines
2026   "A" gnus-article-strip-all-blank-lines
2027   "s" gnus-article-strip-leading-space
2028   "e" gnus-article-strip-trailing-space
2029   "w" gnus-article-remove-leading-whitespace)
2030
2031 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2032   "v" gnus-version
2033   "f" gnus-summary-fetch-faq
2034   "d" gnus-summary-describe-group
2035   "h" gnus-summary-describe-briefly
2036   "i" gnus-info-find-node
2037   "c" gnus-group-fetch-charter
2038   "C" gnus-group-fetch-control)
2039
2040 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2041   "e" gnus-summary-expire-articles
2042   "\M-\C-e" gnus-summary-expire-articles-now
2043   "\177" gnus-summary-delete-article
2044   [delete] gnus-summary-delete-article
2045   [backspace] gnus-summary-delete-article
2046   "m" gnus-summary-move-article
2047   "r" gnus-summary-respool-article
2048   "w" gnus-summary-edit-article
2049   "c" gnus-summary-copy-article
2050   "B" gnus-summary-crosspost-article
2051   "q" gnus-summary-respool-query
2052   "t" gnus-summary-respool-trace
2053   "i" gnus-summary-import-article
2054   "I" gnus-summary-create-article
2055   "p" gnus-summary-article-posted-p)
2056
2057 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2058   "o" gnus-summary-save-article
2059   "m" gnus-summary-save-article-mail
2060   "F" gnus-summary-write-article-file
2061   "r" gnus-summary-save-article-rmail
2062   "f" gnus-summary-save-article-file
2063   "b" gnus-summary-save-article-body-file
2064   "h" gnus-summary-save-article-folder
2065   "v" gnus-summary-save-article-vm
2066   "p" gnus-summary-pipe-output
2067   "P" gnus-summary-muttprint
2068   "s" gnus-soup-add-article)
2069
2070 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2071   "b" gnus-summary-display-buttonized
2072   "m" gnus-summary-repair-multipart
2073   "v" gnus-article-view-part
2074   "o" gnus-article-save-part
2075   "O" gnus-article-save-part-and-strip
2076   "r" gnus-article-replace-part
2077   "d" gnus-article-delete-part
2078   "j" gnus-article-jump-to-part
2079   "c" gnus-article-copy-part
2080   "C" gnus-article-view-part-as-charset
2081   "e" gnus-article-view-part-externally
2082   "E" gnus-article-encrypt-body
2083   "i" gnus-article-inline-part
2084   "|" gnus-article-pipe-part)
2085
2086 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2087   "p" gnus-summary-mark-as-processable
2088   "u" gnus-summary-unmark-as-processable
2089   "U" gnus-summary-unmark-all-processable
2090   "v" gnus-uu-mark-over
2091   "s" gnus-uu-mark-series
2092   "r" gnus-uu-mark-region
2093   "g" gnus-uu-unmark-region
2094   "R" gnus-uu-mark-by-regexp
2095   "G" gnus-uu-unmark-by-regexp
2096   "t" gnus-uu-mark-thread
2097   "T" gnus-uu-unmark-thread
2098   "a" gnus-uu-mark-all
2099   "b" gnus-uu-mark-buffer
2100   "S" gnus-uu-mark-sparse
2101   "k" gnus-summary-kill-process-mark
2102   "y" gnus-summary-yank-process-mark
2103   "w" gnus-summary-save-process-mark
2104   "i" gnus-uu-invert-processable)
2105
2106 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2107   ;;"x" gnus-uu-extract-any
2108   "m" gnus-summary-save-parts
2109   "u" gnus-uu-decode-uu
2110   "U" gnus-uu-decode-uu-and-save
2111   "s" gnus-uu-decode-unshar
2112   "S" gnus-uu-decode-unshar-and-save
2113   "o" gnus-uu-decode-save
2114   "O" gnus-uu-decode-save
2115   "b" gnus-uu-decode-binhex
2116   "B" gnus-uu-decode-binhex
2117   "p" gnus-uu-decode-postscript
2118   "P" gnus-uu-decode-postscript-and-save)
2119
2120 (gnus-define-keys
2121     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2122   "u" gnus-uu-decode-uu-view
2123   "U" gnus-uu-decode-uu-and-save-view
2124   "s" gnus-uu-decode-unshar-view
2125   "S" gnus-uu-decode-unshar-and-save-view
2126   "o" gnus-uu-decode-save-view
2127   "O" gnus-uu-decode-save-view
2128   "b" gnus-uu-decode-binhex-view
2129   "B" gnus-uu-decode-binhex-view
2130   "p" gnus-uu-decode-postscript-view
2131   "P" gnus-uu-decode-postscript-and-save-view)
2132
2133 (defvar gnus-article-post-menu nil)
2134
2135 (defconst gnus-summary-menu-maxlen 20)
2136
2137 (defun gnus-summary-menu-split (menu)
2138   ;; If we have lots of elements, divide them into groups of 20
2139   ;; and make a pane (or submenu) for each one.
2140   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2141       (let ((menu menu) sublists next
2142             (i 1))
2143         (while menu
2144           ;; Pull off the next gnus-summary-menu-maxlen elements
2145           ;; and make them the next element of sublist.
2146           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2147           (if next
2148               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2149                       nil))
2150           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2151                                              (aref (car (last menu)) 0)) menu)
2152                                sublists))
2153           (setq i (1+ i))
2154           (setq menu next))
2155         (nreverse sublists))
2156     ;; Few elements--put them all in one pane.
2157     menu))
2158
2159 (defun gnus-summary-make-menu-bar ()
2160   (gnus-turn-off-edit-menu 'summary)
2161
2162   (unless (boundp 'gnus-summary-misc-menu)
2163
2164     (easy-menu-define
2165       gnus-summary-kill-menu gnus-summary-mode-map ""
2166       (cons
2167        "Score"
2168        (nconc
2169         (list
2170          ["Customize" gnus-score-customize t])
2171         (gnus-make-score-map 'increase)
2172         (gnus-make-score-map 'lower)
2173         '(("Mark"
2174            ["Kill below" gnus-summary-kill-below t]
2175            ["Mark above" gnus-summary-mark-above t]
2176            ["Tick above" gnus-summary-tick-above t]
2177            ["Clear above" gnus-summary-clear-above t])
2178           ["Current score" gnus-summary-current-score t]
2179           ["Set score" gnus-summary-set-score t]
2180           ["Switch current score file..." gnus-score-change-score-file t]
2181           ["Set mark below..." gnus-score-set-mark-below t]
2182           ["Set expunge below..." gnus-score-set-expunge-below t]
2183           ["Edit current score file" gnus-score-edit-current-scores t]
2184           ["Edit score file" gnus-score-edit-file t]
2185           ["Trace score" gnus-score-find-trace t]
2186           ["Find words" gnus-score-find-favourite-words t]
2187           ["Rescore buffer" gnus-summary-rescore t]
2188           ["Increase score..." gnus-summary-increase-score t]
2189           ["Lower score..." gnus-summary-lower-score t]))))
2190
2191     ;; Define both the Article menu in the summary buffer and the
2192     ;; equivalent Commands menu in the article buffer here for
2193     ;; consistency.
2194     (let ((innards
2195            `(("Hide"
2196               ["All" gnus-article-hide t]
2197               ["Headers" gnus-article-hide-headers t]
2198               ["Signature" gnus-article-hide-signature t]
2199               ["Citation" gnus-article-hide-citation t]
2200               ["List identifiers" gnus-article-hide-list-identifiers t]
2201               ["Banner" gnus-article-strip-banner t]
2202               ["Boring headers" gnus-article-hide-boring-headers t])
2203              ("Highlight"
2204               ["All" gnus-article-highlight t]
2205               ["Headers" gnus-article-highlight-headers t]
2206               ["Signature" gnus-article-highlight-signature t]
2207               ["Citation" gnus-article-highlight-citation t])
2208              ("MIME"
2209               ["Words" gnus-article-decode-mime-words t]
2210               ["Charset" gnus-article-decode-charset t]
2211               ["QP" gnus-article-de-quoted-unreadable t]
2212               ["Base64" gnus-article-de-base64-unreadable t]
2213               ["View MIME buttons" gnus-summary-display-buttonized t]
2214               ["View all" gnus-mime-view-all-parts t]
2215               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2216               ["Encrypt body" gnus-article-encrypt-body
2217                :active (not (gnus-group-read-only-p))
2218                ,@(if (featurep 'xemacs) nil
2219                    '(:help "Encrypt the message body on disk"))]
2220               ["Extract all parts..." gnus-summary-save-parts t]
2221               ("Multipart"
2222                ["Repair multipart" gnus-summary-repair-multipart t]
2223                ["Pipe part..." gnus-article-pipe-part t]
2224                ["Inline part" gnus-article-inline-part t]
2225                ["Encrypt body" gnus-article-encrypt-body
2226                 :active (not (gnus-group-read-only-p))
2227                ,@(if (featurep 'xemacs) nil
2228                    '(:help "Encrypt the message body on disk"))]
2229                ["View part externally" gnus-article-view-part-externally t]
2230                ["View part with charset..." gnus-article-view-part-as-charset t]
2231                ["Copy part" gnus-article-copy-part t]
2232                ["Save part..." gnus-article-save-part t]
2233                ["View part" gnus-article-view-part t]))
2234              ("Date"
2235               ["Local" gnus-article-date-local t]
2236               ["ISO8601" gnus-article-date-iso8601 t]
2237               ["UT" gnus-article-date-ut t]
2238               ["Original" gnus-article-date-original t]
2239               ["Lapsed" gnus-article-date-lapsed t]
2240               ["User-defined" gnus-article-date-user t])
2241              ("Display"
2242               ["Remove images" gnus-article-remove-images t]
2243               ["Toggle smiley" gnus-treat-smiley t]
2244               ["Show X-Face" gnus-article-display-x-face t]
2245               ["Show picons in From" gnus-treat-from-picon t]
2246               ["Show picons in mail headers" gnus-treat-mail-picon t]
2247               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2248               ("View as different encoding"
2249                ,@(gnus-summary-menu-split
2250                   (mapcar
2251                    (lambda (cs)
2252                      ;; Since easymenu under Emacs doesn't allow
2253                      ;; lambda forms for menu commands, we should
2254                      ;; provide intern'ed function symbols.
2255                      (let ((command (intern (format "\
2256 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2257                        (fset command
2258                              `(lambda ()
2259                                 (interactive)
2260                                 (let ((gnus-summary-show-article-charset-alist
2261                                        '((1 . ,cs))))
2262                                   (gnus-summary-show-article 1))))
2263                        `[,(symbol-name cs) ,command t]))
2264                    (sort (if (fboundp 'coding-system-list)
2265                              (coding-system-list)
2266                            (mapcar 'car mm-mime-mule-charset-alist))
2267                          'string<)))))
2268              ("Washing"
2269               ("Remove Blanks"
2270                ["Leading" gnus-article-strip-leading-blank-lines t]
2271                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2272                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2273                ["All of the above" gnus-article-strip-blank-lines t]
2274                ["All" gnus-article-strip-all-blank-lines t]
2275                ["Leading space" gnus-article-strip-leading-space t]
2276                ["Trailing space" gnus-article-strip-trailing-space t]
2277                ["Leading space in headers"
2278                 gnus-article-remove-leading-whitespace t])
2279               ["Overstrike" gnus-article-treat-overstrike t]
2280               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2281               ["Emphasis" gnus-article-emphasize t]
2282               ["Word wrap" gnus-article-fill-cited-article t]
2283               ["Fill long lines" gnus-article-fill-long-lines t]
2284               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2285               ["Remove CR" gnus-article-remove-cr t]
2286               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2287               ["Base64" gnus-article-de-base64-unreadable t]
2288               ["Rot 13" gnus-summary-caesar-message
2289                ,@(if (featurep 'xemacs) '(t)
2290                    '(:help "\"Caesar rotate\" article by 13"))]
2291               ["De-IDNA" gnus-summary-idna-message t]
2292               ["Morse decode" gnus-summary-morse-message t]
2293               ["Unix pipe..." gnus-summary-pipe-message t]
2294               ["Add buttons" gnus-article-add-buttons t]
2295               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2296               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2297               ["Verbose header" gnus-summary-verbose-headers t]
2298               ["Toggle header" gnus-summary-toggle-header t]
2299               ["Unfold headers" gnus-article-treat-unfold-headers t]
2300               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2301               ["Html" gnus-article-wash-html t]
2302               ["Unsplit URLs" gnus-article-unsplit-urls t]
2303               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2304               ["Decode HZ" gnus-article-decode-HZ t]
2305               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2306               ("(Outlook) Deuglify"
2307                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2308                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2309                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2310                ["Full (Outlook) deuglify"
2311                 gnus-article-outlook-deuglify-article t])
2312               )
2313              ("Output"
2314               ["Save in default format..." gnus-summary-save-article
2315                ,@(if (featurep 'xemacs) '(t)
2316                    '(:help "Save article using default method"))]
2317               ["Save in file..." gnus-summary-save-article-file
2318                ,@(if (featurep 'xemacs) '(t)
2319                    '(:help "Save article in file"))]
2320               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2321               ["Save in MH folder..." gnus-summary-save-article-folder t]
2322               ["Save in VM folder..." gnus-summary-save-article-vm t]
2323               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2324               ["Save body in file..." gnus-summary-save-article-body-file t]
2325               ["Pipe through a filter..." gnus-summary-pipe-output t]
2326               ["Add to SOUP packet" gnus-soup-add-article t]
2327               ["Print with Muttprint..." gnus-summary-muttprint t]
2328               ["Print" gnus-summary-print-article
2329                ,@(if (featurep 'xemacs) '(t)
2330                    '(:help "Generate and print a PostScript image"))])
2331              ("Copy, move,... (Backend)"
2332               ,@(if (featurep 'xemacs) nil
2333                   '(:help "Copying, moving, expiring articles..."))
2334               ["Respool article..." gnus-summary-respool-article t]
2335               ["Move article..." gnus-summary-move-article
2336                (gnus-check-backend-function
2337                 'request-move-article gnus-newsgroup-name)]
2338               ["Copy article..." gnus-summary-copy-article t]
2339               ["Crosspost article..." gnus-summary-crosspost-article
2340                (gnus-check-backend-function
2341                 'request-replace-article gnus-newsgroup-name)]
2342               ["Import file..." gnus-summary-import-article
2343                (gnus-check-backend-function
2344                 'request-accept-article gnus-newsgroup-name)]
2345               ["Create article..." gnus-summary-create-article
2346                (gnus-check-backend-function
2347                 'request-accept-article gnus-newsgroup-name)]
2348               ["Check if posted" gnus-summary-article-posted-p t]
2349               ["Edit article" gnus-summary-edit-article
2350                (not (gnus-group-read-only-p))]
2351               ["Delete article" gnus-summary-delete-article
2352                (gnus-check-backend-function
2353                 'request-expire-articles gnus-newsgroup-name)]
2354               ["Query respool" gnus-summary-respool-query t]
2355               ["Trace respool" gnus-summary-respool-trace t]
2356               ["Delete expirable articles" gnus-summary-expire-articles-now
2357                (gnus-check-backend-function
2358                 'request-expire-articles gnus-newsgroup-name)])
2359              ("Extract"
2360               ["Uudecode" gnus-uu-decode-uu
2361                ,@(if (featurep 'xemacs) '(t)
2362                    '(:help "Decode uuencoded article(s)"))]
2363               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2364               ["Unshar" gnus-uu-decode-unshar t]
2365               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2366               ["Save" gnus-uu-decode-save t]
2367               ["Binhex" gnus-uu-decode-binhex t]
2368               ["Postscript" gnus-uu-decode-postscript t]
2369               ["All MIME parts" gnus-summary-save-parts t])
2370              ("Cache"
2371               ["Enter article" gnus-cache-enter-article t]
2372               ["Remove article" gnus-cache-remove-article t])
2373              ["Translate" gnus-article-babel t]
2374              ["Select article buffer" gnus-summary-select-article-buffer t]
2375              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2376              ["Isearch article..." gnus-summary-isearch-article t]
2377              ["Beginning of the article" gnus-summary-beginning-of-article t]
2378              ["End of the article" gnus-summary-end-of-article t]
2379              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2380              ["Fetch referenced articles" gnus-summary-refer-references t]
2381              ["Fetch current thread" gnus-summary-refer-thread t]
2382              ["Fetch article with id..." gnus-summary-refer-article t]
2383              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2384              ["Redisplay" gnus-summary-show-article t]
2385              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2386       (easy-menu-define
2387         gnus-summary-article-menu gnus-summary-mode-map ""
2388         (cons "Article" innards))
2389
2390       (if (not (keymapp gnus-summary-article-menu))
2391           (easy-menu-define
2392             gnus-article-commands-menu gnus-article-mode-map ""
2393             (cons "Commands" innards))
2394         ;; in Emacs, don't share menu.
2395         (setq gnus-article-commands-menu
2396               (copy-keymap gnus-summary-article-menu))
2397         (define-key gnus-article-mode-map [menu-bar commands]
2398           (cons "Commands" gnus-article-commands-menu))))
2399
2400     (easy-menu-define
2401       gnus-summary-thread-menu gnus-summary-mode-map ""
2402       '("Threads"
2403         ["Find all messages in thread" gnus-summary-refer-thread t]
2404         ["Toggle threading" gnus-summary-toggle-threads t]
2405         ["Hide threads" gnus-summary-hide-all-threads t]
2406         ["Show threads" gnus-summary-show-all-threads t]
2407         ["Hide thread" gnus-summary-hide-thread t]
2408         ["Show thread" gnus-summary-show-thread t]
2409         ["Go to next thread" gnus-summary-next-thread t]
2410         ["Go to previous thread" gnus-summary-prev-thread t]
2411         ["Go down thread" gnus-summary-down-thread t]
2412         ["Go up thread" gnus-summary-up-thread t]
2413         ["Top of thread" gnus-summary-top-thread t]
2414         ["Mark thread as read" gnus-summary-kill-thread t]
2415         ["Lower thread score" gnus-summary-lower-thread t]
2416         ["Raise thread score" gnus-summary-raise-thread t]
2417         ["Rethread current" gnus-summary-rethread-current t]))
2418
2419     (easy-menu-define
2420       gnus-summary-post-menu gnus-summary-mode-map ""
2421       `("Post"
2422         ["Send a message (mail or news)" gnus-summary-post-news
2423          ,@(if (featurep 'xemacs) '(t)
2424              '(:help "Compose a new message (mail or news)"))]
2425         ["Followup" gnus-summary-followup
2426          ,@(if (featurep 'xemacs) '(t)
2427              '(:help "Post followup to this article"))]
2428         ["Followup and yank" gnus-summary-followup-with-original
2429          ,@(if (featurep 'xemacs) '(t)
2430              '(:help "Post followup to this article, quoting its contents"))]
2431         ["Supersede article" gnus-summary-supersede-article t]
2432         ["Cancel article" gnus-summary-cancel-article
2433          ,@(if (featurep 'xemacs) '(t)
2434              '(:help "Cancel an article you posted"))]
2435         ["Reply" gnus-summary-reply t]
2436         ["Reply and yank" gnus-summary-reply-with-original t]
2437         ["Wide reply" gnus-summary-wide-reply t]
2438         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2439          ,@(if (featurep 'xemacs) '(t)
2440              '(:help "Mail a reply, quoting this article"))]
2441         ["Very wide reply" gnus-summary-very-wide-reply t]
2442         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2443          ,@(if (featurep 'xemacs) '(t)
2444              '(:help "Mail a very wide reply, quoting this article"))]
2445         ["Mail forward" gnus-summary-mail-forward t]
2446         ["Post forward" gnus-summary-post-forward t]
2447         ["Digest and mail" gnus-uu-digest-mail-forward t]
2448         ["Digest and post" gnus-uu-digest-post-forward t]
2449         ["Resend message" gnus-summary-resend-message t]
2450         ["Resend message edit" gnus-summary-resend-message-edit t]
2451         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2452         ["Send a mail" gnus-summary-mail-other-window t]
2453         ["Create a local message" gnus-summary-news-other-window t]
2454         ["Uuencode and post" gnus-uu-post-news
2455          ,@(if (featurep 'xemacs) '(t)
2456              '(:help "Post a uuencoded article"))]
2457         ["Followup via news" gnus-summary-followup-to-mail t]
2458         ["Followup via news and yank"
2459          gnus-summary-followup-to-mail-with-original t]
2460         ;;("Draft"
2461         ;;["Send" gnus-summary-send-draft t]
2462         ;;["Send bounced" gnus-resend-bounced-mail t])
2463         ))
2464
2465     (cond
2466      ((not (keymapp gnus-summary-post-menu))
2467       (setq gnus-article-post-menu gnus-summary-post-menu))
2468      ((not gnus-article-post-menu)
2469       ;; Don't share post menu.
2470       (setq gnus-article-post-menu
2471             (copy-keymap gnus-summary-post-menu))))
2472     (define-key gnus-article-mode-map [menu-bar post]
2473       (cons "Post" gnus-article-post-menu))
2474
2475     (easy-menu-define
2476       gnus-summary-misc-menu gnus-summary-mode-map ""
2477       `("Gnus"
2478         ("Mark Read"
2479          ["Mark as read" gnus-summary-mark-as-read-forward t]
2480          ["Mark same subject and select"
2481           gnus-summary-kill-same-subject-and-select t]
2482          ["Mark same subject" gnus-summary-kill-same-subject t]
2483          ["Catchup" gnus-summary-catchup
2484           ,@(if (featurep 'xemacs) '(t)
2485               '(:help "Mark unread articles in this group as read"))]
2486          ["Catchup all" gnus-summary-catchup-all t]
2487          ["Catchup to here" gnus-summary-catchup-to-here t]
2488          ["Catchup from here" gnus-summary-catchup-from-here t]
2489          ["Catchup region" gnus-summary-mark-region-as-read
2490           (gnus-mark-active-p)]
2491          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2492         ("Mark Various"
2493          ["Tick" gnus-summary-tick-article-forward t]
2494          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2495          ["Remove marks" gnus-summary-clear-mark-forward t]
2496          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2497          ["Set bookmark" gnus-summary-set-bookmark t]
2498          ["Remove bookmark" gnus-summary-remove-bookmark t])
2499         ("Limit to"
2500          ["Marks..." gnus-summary-limit-to-marks t]
2501          ["Subject..." gnus-summary-limit-to-subject t]
2502          ["Author..." gnus-summary-limit-to-author t]
2503          ["Recipient..." gnus-summary-limit-to-recipient t]
2504          ["Age..." gnus-summary-limit-to-age t]
2505          ["Extra..." gnus-summary-limit-to-extra t]
2506          ["Score..." gnus-summary-limit-to-score t]
2507          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2508          ["Unread" gnus-summary-limit-to-unread t]
2509          ["Unseen" gnus-summary-limit-to-unseen t]
2510          ["Replied" gnus-summary-limit-to-replied t]
2511          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2512          ["Next articles" gnus-summary-limit-to-articles t]
2513          ["Pop limit" gnus-summary-pop-limit t]
2514          ["Show dormant" gnus-summary-limit-include-dormant t]
2515          ["Hide childless dormant"
2516           gnus-summary-limit-exclude-childless-dormant t]
2517          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2518          ["Hide marked" gnus-summary-limit-exclude-marks t]
2519          ["Show expunged" gnus-summary-limit-include-expunged t])
2520         ("Process Mark"
2521          ["Set mark" gnus-summary-mark-as-processable t]
2522          ["Remove mark" gnus-summary-unmark-as-processable t]
2523          ["Remove all marks" gnus-summary-unmark-all-processable t]
2524          ["Invert marks" gnus-uu-invert-processable t]
2525          ["Mark above" gnus-uu-mark-over t]
2526          ["Mark series" gnus-uu-mark-series t]
2527          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2528          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2529          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2530          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2531          ["Mark all" gnus-uu-mark-all t]
2532          ["Mark buffer" gnus-uu-mark-buffer t]
2533          ["Mark sparse" gnus-uu-mark-sparse t]
2534          ["Mark thread" gnus-uu-mark-thread t]
2535          ["Unmark thread" gnus-uu-unmark-thread t]
2536          ("Process Mark Sets"
2537           ["Kill" gnus-summary-kill-process-mark t]
2538           ["Yank" gnus-summary-yank-process-mark
2539            gnus-newsgroup-process-stack]
2540           ["Save" gnus-summary-save-process-mark t]
2541           ["Run command on marked..." gnus-summary-universal-argument t]))
2542         ("Scroll article"
2543          ["Page forward" gnus-summary-next-page
2544           ,@(if (featurep 'xemacs) '(t)
2545               '(:help "Show next page of article"))]
2546          ["Page backward" gnus-summary-prev-page
2547           ,@(if (featurep 'xemacs) '(t)
2548               '(:help "Show previous page of article"))]
2549          ["Line forward" gnus-summary-scroll-up t])
2550         ("Move"
2551          ["Next unread article" gnus-summary-next-unread-article t]
2552          ["Previous unread article" gnus-summary-prev-unread-article t]
2553          ["Next article" gnus-summary-next-article t]
2554          ["Previous article" gnus-summary-prev-article t]
2555          ["Next unread subject" gnus-summary-next-unread-subject t]
2556          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2557          ["Next article same subject" gnus-summary-next-same-subject t]
2558          ["Previous article same subject" gnus-summary-prev-same-subject t]
2559          ["First unread article" gnus-summary-first-unread-article t]
2560          ["Best unread article" gnus-summary-best-unread-article t]
2561          ["Go to subject number..." gnus-summary-goto-subject t]
2562          ["Go to article number..." gnus-summary-goto-article t]
2563          ["Go to the last article" gnus-summary-goto-last-article t]
2564          ["Pop article off history" gnus-summary-pop-article t])
2565         ("Sort"
2566          ["Sort by number" gnus-summary-sort-by-number t]
2567          ["Sort by author" gnus-summary-sort-by-author t]
2568          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2569          ["Sort by subject" gnus-summary-sort-by-subject t]
2570          ["Sort by date" gnus-summary-sort-by-date t]
2571          ["Sort by score" gnus-summary-sort-by-score t]
2572          ["Sort by lines" gnus-summary-sort-by-lines t]
2573          ["Sort by characters" gnus-summary-sort-by-chars t]
2574          ["Randomize" gnus-summary-sort-by-random t]
2575          ["Original sort" gnus-summary-sort-by-original t])
2576         ("Help"
2577          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2578          ["Describe group" gnus-summary-describe-group t]
2579          ["Fetch charter" gnus-group-fetch-charter
2580           ,@(if (featurep 'xemacs) nil
2581               '(:help "Display the charter of the current group"))]
2582          ["Fetch control message" gnus-group-fetch-control
2583           ,@(if (featurep 'xemacs) nil
2584               '(:help "Display the archived control message for the current group"))]
2585          ["Read manual" gnus-info-find-node t])
2586         ("Modes"
2587          ["Pick and read" gnus-pick-mode t]
2588          ["Binary" gnus-binary-mode t])
2589         ("Regeneration"
2590          ["Regenerate" gnus-summary-prepare t]
2591          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2592          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2593          ["Toggle threading" gnus-summary-toggle-threads t])
2594         ["See old articles" gnus-summary-insert-old-articles t]
2595         ["See new articles" gnus-summary-insert-new-articles t]
2596         ["Filter articles..." gnus-summary-execute-command t]
2597         ["Run command on articles..." gnus-summary-universal-argument t]
2598         ["Search articles forward..." gnus-summary-search-article-forward t]
2599         ["Search articles backward..." gnus-summary-search-article-backward t]
2600         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2601         ["Expand window" gnus-summary-expand-window t]
2602         ["Expire expirable articles" gnus-summary-expire-articles
2603          (gnus-check-backend-function
2604           'request-expire-articles gnus-newsgroup-name)]
2605         ["Edit local kill file" gnus-summary-edit-local-kill t]
2606         ["Edit main kill file" gnus-summary-edit-global-kill t]
2607         ["Edit group parameters" gnus-summary-edit-parameters t]
2608         ["Customize group parameters" gnus-summary-customize-parameters t]
2609         ["Send a bug report" gnus-bug t]
2610         ("Exit"
2611          ["Catchup and exit" gnus-summary-catchup-and-exit
2612           ,@(if (featurep 'xemacs) '(t)
2613               '(:help "Mark unread articles in this group as read, then exit"))]
2614          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2615          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2616          ["Exit group" gnus-summary-exit
2617           ,@(if (featurep 'xemacs) '(t)
2618               '(:help "Exit current group, return to group selection mode"))]
2619          ["Exit group without updating" gnus-summary-exit-no-update t]
2620          ["Exit and goto next group" gnus-summary-next-group t]
2621          ["Exit and goto prev group" gnus-summary-prev-group t]
2622          ["Reselect group" gnus-summary-reselect-current-group t]
2623          ["Rescan group" gnus-summary-rescan-group t]
2624          ["Update dribble" gnus-summary-save-newsrc t])))
2625
2626     (gnus-run-hooks 'gnus-summary-menu-hook)))
2627
2628 (defvar gnus-summary-tool-bar-map nil)
2629
2630 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2631 (defun gnus-summary-make-tool-bar ()
2632   (if (and (fboundp 'tool-bar-add-item-from-menu)
2633            (default-value 'tool-bar-mode)
2634            (not gnus-summary-tool-bar-map))
2635       (setq gnus-summary-tool-bar-map
2636             (let ((tool-bar-map (make-sparse-keymap))
2637                   (load-path (mm-image-load-path)))
2638               (tool-bar-add-item-from-menu
2639                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2640               (tool-bar-add-item-from-menu
2641                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2642               (tool-bar-add-item-from-menu
2643                'gnus-summary-post-news "post" gnus-summary-mode-map)
2644               (tool-bar-add-item-from-menu
2645                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2646               (tool-bar-add-item-from-menu
2647                'gnus-summary-followup "followup" gnus-summary-mode-map)
2648               (tool-bar-add-item-from-menu
2649                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2650               (tool-bar-add-item-from-menu
2651                'gnus-summary-reply "reply" gnus-summary-mode-map)
2652               (tool-bar-add-item-from-menu
2653                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2654               (tool-bar-add-item-from-menu
2655                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2656               (tool-bar-add-item-from-menu
2657                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2658               (tool-bar-add-item-from-menu
2659                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2660               (tool-bar-add-item-from-menu
2661                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2662               (tool-bar-add-item-from-menu
2663                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2664               (tool-bar-add-item-from-menu
2665                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2666               (tool-bar-add-item-from-menu
2667                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2668               (tool-bar-add-item-from-menu
2669                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2670               tool-bar-map)))
2671   (if gnus-summary-tool-bar-map
2672       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2673
2674 (defun gnus-score-set-default (var value)
2675   "A version of set that updates the GNU Emacs menu-bar."
2676   (set var value)
2677   ;; It is the message that forces the active status to be updated.
2678   (message ""))
2679
2680 (defun gnus-make-score-map (type)
2681   "Make a summary score map of type TYPE."
2682   (if t
2683       nil
2684     (let ((headers '(("author" "from" string)
2685                      ("subject" "subject" string)
2686                      ("article body" "body" string)
2687                      ("article head" "head" string)
2688                      ("xref" "xref" string)
2689                      ("extra header" "extra" string)
2690                      ("lines" "lines" number)
2691                      ("followups to author" "followup" string)))
2692           (types '((number ("less than" <)
2693                            ("greater than" >)
2694                            ("equal" =))
2695                    (string ("substring" s)
2696                            ("exact string" e)
2697                            ("fuzzy string" f)
2698                            ("regexp" r))))
2699           (perms '(("temporary" (current-time-string))
2700                    ("permanent" nil)
2701                    ("immediate" now)))
2702           header)
2703       (list
2704        (apply
2705         'nconc
2706         (list
2707          (if (eq type 'lower)
2708              "Lower score"
2709            "Increase score"))
2710         (let (outh)
2711           (while headers
2712             (setq header (car headers))
2713             (setq outh
2714                   (cons
2715                    (apply
2716                     'nconc
2717                     (list (car header))
2718                     (let ((ts (cdr (assoc (nth 2 header) types)))
2719                           outt)
2720                       (while ts
2721                         (setq outt
2722                               (cons
2723                                (apply
2724                                 'nconc
2725                                 (list (caar ts))
2726                                 (let ((ps perms)
2727                                       outp)
2728                                   (while ps
2729                                     (setq outp
2730                                           (cons
2731                                            (vector
2732                                             (caar ps)
2733                                             (list
2734                                              'gnus-summary-score-entry
2735                                              (nth 1 header)
2736                                              (if (or (string= (nth 1 header)
2737                                                               "head")
2738                                                      (string= (nth 1 header)
2739                                                               "body"))
2740                                                  ""
2741                                                (list 'gnus-summary-header
2742                                                      (nth 1 header)))
2743                                              (list 'quote (nth 1 (car ts)))
2744                                              (list 'gnus-score-delta-default
2745                                                    nil)
2746                                              (nth 1 (car ps))
2747                                              t)
2748                                             t)
2749                                            outp))
2750                                     (setq ps (cdr ps)))
2751                                   (list (nreverse outp))))
2752                                outt))
2753                         (setq ts (cdr ts)))
2754                       (list (nreverse outt))))
2755                    outh))
2756             (setq headers (cdr headers)))
2757           (list (nreverse outh))))))))
2758
2759 \f
2760
2761 (defun gnus-summary-mode (&optional group)
2762   "Major mode for reading articles.
2763
2764 All normal editing commands are switched off.
2765 \\<gnus-summary-mode-map>
2766 Each line in this buffer represents one article.  To read an
2767 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2768 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2769 respectively.
2770
2771 You can also post articles and send mail from this buffer.  To
2772 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2773 of an article, type `\\[gnus-summary-reply]'.
2774
2775 There are approx. one gazillion commands you can execute in this
2776 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2777
2778 The following commands are available:
2779
2780 \\{gnus-summary-mode-map}"
2781   (interactive)
2782   (kill-all-local-variables)
2783   (when (gnus-visual-p 'summary-menu 'menu)
2784     (gnus-summary-make-menu-bar)
2785     (gnus-summary-make-tool-bar))
2786   (gnus-summary-make-local-variables)
2787   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2788     (gnus-summary-make-local-variables))
2789   (gnus-make-thread-indent-array)
2790   (gnus-simplify-mode-line)
2791   (setq major-mode 'gnus-summary-mode)
2792   (setq mode-name "Summary")
2793   (make-local-variable 'minor-mode-alist)
2794   (use-local-map gnus-summary-mode-map)
2795   (buffer-disable-undo)
2796   (setq buffer-read-only t              ;Disable modification
2797         show-trailing-whitespace nil)
2798   (setq truncate-lines t)
2799   (setq selective-display t)
2800   (setq selective-display-ellipses t)   ;Display `...'
2801   (gnus-summary-set-display-table)
2802   (gnus-set-default-directory)
2803   (setq gnus-newsgroup-name group)
2804   (make-local-variable 'gnus-summary-line-format)
2805   (make-local-variable 'gnus-summary-line-format-spec)
2806   (make-local-variable 'gnus-summary-dummy-line-format)
2807   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2808   (make-local-variable 'gnus-summary-mark-positions)
2809   (gnus-make-local-hook 'pre-command-hook)
2810   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2811   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2812   (turn-on-gnus-mailing-list-mode)
2813   (mm-enable-multibyte)
2814   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2815   (gnus-update-summary-mark-positions))
2816
2817 (defun gnus-summary-make-local-variables ()
2818   "Make all the local summary buffer variables."
2819   (let (global)
2820     (dolist (local gnus-summary-local-variables)
2821       (if (consp local)
2822           (progn
2823             (if (eq (cdr local) 'global)
2824                 ;; Copy the global value of the variable.
2825                 (setq global (symbol-value (car local)))
2826               ;; Use the value from the list.
2827               (setq global (eval (cdr local))))
2828             (set (make-local-variable (car local)) global))
2829         ;; Simple nil-valued local variable.
2830         (set (make-local-variable local) nil)))))
2831
2832 (defun gnus-summary-clear-local-variables ()
2833   (let ((locals gnus-summary-local-variables))
2834     (while locals
2835       (if (consp (car locals))
2836           (and (symbolp (caar locals))
2837                (set (caar locals) nil))
2838         (and (symbolp (car locals))
2839              (set (car locals) nil)))
2840       (setq locals (cdr locals)))))
2841
2842 ;; Summary data functions.
2843
2844 (defmacro gnus-data-number (data)
2845   `(car ,data))
2846
2847 (defmacro gnus-data-set-number (data number)
2848   `(setcar ,data ,number))
2849
2850 (defmacro gnus-data-mark (data)
2851   `(nth 1 ,data))
2852
2853 (defmacro gnus-data-set-mark (data mark)
2854   `(setcar (nthcdr 1 ,data) ,mark))
2855
2856 (defmacro gnus-data-pos (data)
2857   `(nth 2 ,data))
2858
2859 (defmacro gnus-data-set-pos (data pos)
2860   `(setcar (nthcdr 2 ,data) ,pos))
2861
2862 (defmacro gnus-data-header (data)
2863   `(nth 3 ,data))
2864
2865 (defmacro gnus-data-set-header (data header)
2866   `(setf (nth 3 ,data) ,header))
2867
2868 (defmacro gnus-data-level (data)
2869   `(nth 4 ,data))
2870
2871 (defmacro gnus-data-unread-p (data)
2872   `(= (nth 1 ,data) gnus-unread-mark))
2873
2874 (defmacro gnus-data-read-p (data)
2875   `(/= (nth 1 ,data) gnus-unread-mark))
2876
2877 (defmacro gnus-data-pseudo-p (data)
2878   `(consp (nth 3 ,data)))
2879
2880 (defmacro gnus-data-find (number)
2881   `(assq ,number gnus-newsgroup-data))
2882
2883 (defmacro gnus-data-find-list (number &optional data)
2884   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2885      (memq (assq ,number bdata)
2886            bdata)))
2887
2888 (defmacro gnus-data-make (number mark pos header level)
2889   `(list ,number ,mark ,pos ,header ,level))
2890
2891 (defun gnus-data-enter (after-article number mark pos header level offset)
2892   (let ((data (gnus-data-find-list after-article)))
2893     (unless data
2894       (error "No such article: %d" after-article))
2895     (setcdr data (cons (gnus-data-make number mark pos header level)
2896                        (cdr data)))
2897     (setq gnus-newsgroup-data-reverse nil)
2898     (gnus-data-update-list (cddr data) offset)))
2899
2900 (defun gnus-data-enter-list (after-article list &optional offset)
2901   (when list
2902     (let ((data (and after-article (gnus-data-find-list after-article)))
2903           (ilist list))
2904       (if (not (or data
2905                    after-article))
2906           (let ((odata gnus-newsgroup-data))
2907             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2908             (when offset
2909               (gnus-data-update-list odata offset)))
2910         ;; Find the last element in the list to be spliced into the main
2911         ;; list.
2912         (setq list (last list))
2913         (if (not data)
2914             (progn
2915               (setcdr list gnus-newsgroup-data)
2916               (setq gnus-newsgroup-data ilist)
2917               (when offset
2918                 (gnus-data-update-list (cdr list) offset)))
2919           (setcdr list (cdr data))
2920           (setcdr data ilist)
2921           (when offset
2922             (gnus-data-update-list (cdr list) offset))))
2923       (setq gnus-newsgroup-data-reverse nil))))
2924
2925 (defun gnus-data-remove (article &optional offset)
2926   (let ((data gnus-newsgroup-data))
2927     (if (= (gnus-data-number (car data)) article)
2928         (progn
2929           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2930                 gnus-newsgroup-data-reverse nil)
2931           (when offset
2932             (gnus-data-update-list gnus-newsgroup-data offset)))
2933       (while (cdr data)
2934         (when (= (gnus-data-number (cadr data)) article)
2935           (setcdr data (cddr data))
2936           (when offset
2937             (gnus-data-update-list (cdr data) offset))
2938           (setq data nil
2939                 gnus-newsgroup-data-reverse nil))
2940         (setq data (cdr data))))))
2941
2942 (defmacro gnus-data-list (backward)
2943   `(if ,backward
2944        (or gnus-newsgroup-data-reverse
2945            (setq gnus-newsgroup-data-reverse
2946                  (reverse gnus-newsgroup-data)))
2947      gnus-newsgroup-data))
2948
2949 (defun gnus-data-update-list (data offset)
2950   "Add OFFSET to the POS of all data entries in DATA."
2951   (setq gnus-newsgroup-data-reverse nil)
2952   (while data
2953     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2954     (setq data (cdr data))))
2955
2956 (defun gnus-summary-article-pseudo-p (article)
2957   "Say whether this article is a pseudo article or not."
2958   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2959
2960 (defmacro gnus-summary-article-sparse-p (article)
2961   "Say whether this article is a sparse article or not."
2962   `(memq ,article gnus-newsgroup-sparse))
2963
2964 (defmacro gnus-summary-article-ancient-p (article)
2965   "Say whether this article is a sparse article or not."
2966   `(memq ,article gnus-newsgroup-ancient))
2967
2968 (defun gnus-article-parent-p (number)
2969   "Say whether this article is a parent or not."
2970   (let ((data (gnus-data-find-list number)))
2971     (and (cdr data)              ; There has to be an article after...
2972          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2973             (gnus-data-level (nth 1 data))))))
2974
2975 (defun gnus-article-children (number)
2976   "Return a list of all children to NUMBER."
2977   (let* ((data (gnus-data-find-list number))
2978          (level (gnus-data-level (car data)))
2979          children)
2980     (setq data (cdr data))
2981     (while (and data
2982                 (= (gnus-data-level (car data)) (1+ level)))
2983       (push (gnus-data-number (car data)) children)
2984       (setq data (cdr data)))
2985     children))
2986
2987 (defmacro gnus-summary-skip-intangible ()
2988   "If the current article is intangible, then jump to a different article."
2989   '(let ((to (get-text-property (point) 'gnus-intangible)))
2990      (and to (gnus-summary-goto-subject to))))
2991
2992 (defmacro gnus-summary-article-intangible-p ()
2993   "Say whether this article is intangible or not."
2994   '(get-text-property (point) 'gnus-intangible))
2995
2996 (defun gnus-article-read-p (article)
2997   "Say whether ARTICLE is read or not."
2998   (not (or (memq article gnus-newsgroup-marked)
2999            (memq article gnus-newsgroup-spam-marked)
3000            (memq article gnus-newsgroup-unreads)
3001            (memq article gnus-newsgroup-unselected)
3002            (memq article gnus-newsgroup-dormant))))
3003
3004 ;; Some summary mode macros.
3005
3006 (defmacro gnus-summary-article-number ()
3007   "The article number of the article on the current line.
3008 If there isn't an article number here, then we return the current
3009 article number."
3010   '(progn
3011      (gnus-summary-skip-intangible)
3012      (or (get-text-property (point) 'gnus-number)
3013          (gnus-summary-last-subject))))
3014
3015 (defmacro gnus-summary-article-header (&optional number)
3016   "Return the header of article NUMBER."
3017   `(gnus-data-header (gnus-data-find
3018                       ,(or number '(gnus-summary-article-number)))))
3019
3020 (defmacro gnus-summary-thread-level (&optional number)
3021   "Return the level of thread that starts with article NUMBER."
3022   `(if (and (eq gnus-summary-make-false-root 'dummy)
3023             (get-text-property (point) 'gnus-intangible))
3024        0
3025      (gnus-data-level (gnus-data-find
3026                        ,(or number '(gnus-summary-article-number))))))
3027
3028 (defmacro gnus-summary-article-mark (&optional number)
3029   "Return the mark of article NUMBER."
3030   `(gnus-data-mark (gnus-data-find
3031                     ,(or number '(gnus-summary-article-number)))))
3032
3033 (defmacro gnus-summary-article-pos (&optional number)
3034   "Return the position of the line of article NUMBER."
3035   `(gnus-data-pos (gnus-data-find
3036                    ,(or number '(gnus-summary-article-number)))))
3037
3038 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3039 (defmacro gnus-summary-article-subject (&optional number)
3040   "Return current subject string or nil if nothing."
3041   `(let ((headers
3042           ,(if number
3043                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3044              '(gnus-data-header (assq (gnus-summary-article-number)
3045                                       gnus-newsgroup-data)))))
3046      (and headers
3047           (vectorp headers)
3048           (mail-header-subject headers))))
3049
3050 (defmacro gnus-summary-article-score (&optional number)
3051   "Return current article score."
3052   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3053                   gnus-newsgroup-scored))
3054        gnus-summary-default-score 0))
3055
3056 (defun gnus-summary-article-children (&optional number)
3057   "Return a list of article numbers that are children of article NUMBER."
3058   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3059          (level (gnus-data-level (car data)))
3060          l children)
3061     (while (and (setq data (cdr data))
3062                 (> (setq l (gnus-data-level (car data))) level))
3063       (and (= (1+ level) l)
3064            (push (gnus-data-number (car data))
3065                  children)))
3066     (nreverse children)))
3067
3068 (defun gnus-summary-article-parent (&optional number)
3069   "Return the article number of the parent of article NUMBER."
3070   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3071                                     (gnus-data-list t)))
3072          (level (gnus-data-level (car data))))
3073     (if (zerop level)
3074         ()                              ; This is a root.
3075       ;; We search until we find an article with a level less than
3076       ;; this one.  That function has to be the parent.
3077       (while (and (setq data (cdr data))
3078                   (not (< (gnus-data-level (car data)) level))))
3079       (and data (gnus-data-number (car data))))))
3080
3081 (defun gnus-unread-mark-p (mark)
3082   "Say whether MARK is the unread mark."
3083   (= mark gnus-unread-mark))
3084
3085 (defun gnus-read-mark-p (mark)
3086   "Say whether MARK is one of the marks that mark as read.
3087 This is all marks except unread, ticked, dormant, and expirable."
3088   (not (or (= mark gnus-unread-mark)
3089            (= mark gnus-ticked-mark)
3090            (= mark gnus-spam-mark)
3091            (= mark gnus-dormant-mark)
3092            (= mark gnus-expirable-mark))))
3093
3094 (defmacro gnus-article-mark (number)
3095   "Return the MARK of article NUMBER.
3096 This macro should only be used when computing the mark the \"first\"
3097 time; i.e., when generating the summary lines.  After that,
3098 `gnus-summary-article-mark' should be used to examine the
3099 marks of articles."
3100   `(cond
3101     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3102     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3103     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3104     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3105     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3106     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3107     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3108     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3109            gnus-ancient-mark))))
3110
3111 ;; Saving hidden threads.
3112
3113 (defmacro gnus-save-hidden-threads (&rest forms)
3114   "Save hidden threads, eval FORMS, and restore the hidden threads."
3115   (let ((config (make-symbol "config")))
3116     `(let ((,config (gnus-hidden-threads-configuration)))
3117        (unwind-protect
3118            (save-excursion
3119              ,@forms)
3120          (gnus-restore-hidden-threads-configuration ,config)))))
3121 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3122 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3123
3124 (defun gnus-data-compute-positions ()
3125   "Compute the positions of all articles."
3126   (setq gnus-newsgroup-data-reverse nil)
3127   (let ((data gnus-newsgroup-data))
3128     (save-excursion
3129       (gnus-save-hidden-threads
3130         (gnus-summary-show-all-threads)
3131         (goto-char (point-min))
3132         (while data
3133           (while (get-text-property (point) 'gnus-intangible)
3134             (forward-line 1))
3135           (gnus-data-set-pos (car data) (+ (point) 3))
3136           (setq data (cdr data))
3137           (forward-line 1))))))
3138
3139 (defun gnus-hidden-threads-configuration ()
3140   "Return the current hidden threads configuration."
3141   (save-excursion
3142     (let (config)
3143       (goto-char (point-min))
3144       (while (search-forward "\r" nil t)
3145         (push (1- (point)) config))
3146       config)))
3147
3148 (defun gnus-restore-hidden-threads-configuration (config)
3149   "Restore hidden threads configuration from CONFIG."
3150   (save-excursion
3151     (let (point buffer-read-only)
3152       (while (setq point (pop config))
3153         (when (and (< point (point-max))
3154                    (goto-char point)
3155                    (eq (char-after) ?\n))
3156           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3157
3158 ;; Various summary mode internalish functions.
3159
3160 (defun gnus-mouse-pick-article (e)
3161   (interactive "e")
3162   (mouse-set-point e)
3163   (gnus-summary-next-page nil t))
3164
3165 (defun gnus-summary-set-display-table ()
3166   "Change the display table.
3167 Odd characters have a tendency to mess
3168 up nicely formatted displays - we make all possible glyphs
3169 display only a single character."
3170
3171   ;; We start from the standard display table, if any.
3172   (let ((table (or (copy-sequence standard-display-table)
3173                    (make-display-table)))
3174         (i 32))
3175     ;; Nix out all the control chars...
3176     (while (>= (setq i (1- i)) 0)
3177       (aset table i [??]))
3178    ;; ... but not newline and cr, of course.  (cr is necessary for the
3179     ;; selective display).
3180     (aset table ?\n nil)
3181     (aset table ?\r nil)
3182     ;; We keep TAB as well.
3183     (aset table ?\t nil)
3184     ;; We nix out any glyphs over 126 that are not set already.
3185     (let ((i 256))
3186       (while (>= (setq i (1- i)) 127)
3187         ;; Only modify if the entry is nil.
3188         (unless (aref table i)
3189           (aset table i [??]))))
3190     (setq buffer-display-table table)))
3191
3192 (defun gnus-summary-set-article-display-arrow (pos)
3193   "Update the overlay arrow to point to line at position POS."
3194   (when (and gnus-summary-display-arrow
3195              (boundp 'overlay-arrow-position)
3196              (boundp 'overlay-arrow-string))
3197     (save-excursion
3198       (goto-char pos)
3199       (beginning-of-line)
3200       (unless overlay-arrow-position
3201         (setq overlay-arrow-position (make-marker)))
3202       (setq overlay-arrow-string "=>"
3203             overlay-arrow-position (set-marker overlay-arrow-position
3204                                                (point)
3205                                                (current-buffer))))))
3206
3207 (defun gnus-summary-setup-buffer (group)
3208   "Initialize summary buffer."
3209   (let ((buffer (gnus-summary-buffer-name group))
3210         (dead-name (concat "*Dead Summary "
3211                            (gnus-group-decoded-name group) "*")))
3212     ;; If a dead summary buffer exists, we kill it.
3213     (when (gnus-buffer-live-p dead-name)
3214       (gnus-kill-buffer dead-name))
3215     (if (get-buffer buffer)
3216         (progn
3217           (set-buffer buffer)
3218           (setq gnus-summary-buffer (current-buffer))
3219           (not gnus-newsgroup-prepared))
3220       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3221       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3222       (gnus-summary-mode group)
3223       (when gnus-carpal
3224         (gnus-carpal-setup-buffer 'summary))
3225       (unless gnus-single-article-buffer
3226         (make-local-variable 'gnus-article-buffer)
3227         (make-local-variable 'gnus-article-current)
3228         (make-local-variable 'gnus-original-article-buffer))
3229       (setq gnus-newsgroup-name group)
3230       ;; Set any local variables in the group parameters.
3231       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3232       t)))
3233
3234 (defun gnus-set-global-variables ()
3235   "Set the global equivalents of the buffer-local variables.
3236 They are set to the latest values they had.  These reflect the summary
3237 buffer that was in action when the last article was fetched."
3238   (when (eq major-mode 'gnus-summary-mode)
3239     (setq gnus-summary-buffer (current-buffer))
3240     (let ((name gnus-newsgroup-name)
3241           (marked gnus-newsgroup-marked)
3242           (spam gnus-newsgroup-spam-marked)
3243           (unread gnus-newsgroup-unreads)
3244           (headers gnus-current-headers)
3245           (data gnus-newsgroup-data)
3246           (summary gnus-summary-buffer)
3247           (article-buffer gnus-article-buffer)
3248           (original gnus-original-article-buffer)
3249           (gac gnus-article-current)
3250           (reffed gnus-reffed-article-number)
3251           (score-file gnus-current-score-file)
3252           (default-charset gnus-newsgroup-charset)
3253           vlist)
3254       (let ((locals gnus-newsgroup-variables))
3255         (while locals
3256           (if (consp (car locals))
3257               (push (eval (caar locals)) vlist)
3258             (push (eval (car locals)) vlist))
3259           (setq locals (cdr locals)))
3260         (setq vlist (nreverse vlist)))
3261       (with-current-buffer gnus-group-buffer
3262         (setq gnus-newsgroup-name name
3263               gnus-newsgroup-marked marked
3264               gnus-newsgroup-spam-marked spam
3265               gnus-newsgroup-unreads unread
3266               gnus-current-headers headers
3267               gnus-newsgroup-data data
3268               gnus-article-current gac
3269               gnus-summary-buffer summary
3270               gnus-article-buffer article-buffer
3271               gnus-original-article-buffer original
3272               gnus-reffed-article-number reffed
3273               gnus-current-score-file score-file
3274               gnus-newsgroup-charset default-charset)
3275         (let ((locals gnus-newsgroup-variables))
3276           (while locals
3277             (if (consp (car locals))
3278                 (set (caar locals) (pop vlist))
3279               (set (car locals) (pop vlist)))
3280             (setq locals (cdr locals))))
3281         ;; The article buffer also has local variables.
3282         (when (gnus-buffer-live-p gnus-article-buffer)
3283           (set-buffer gnus-article-buffer)
3284           (setq gnus-summary-buffer summary))))))
3285
3286 (defun gnus-summary-article-unread-p (article)
3287   "Say whether ARTICLE is unread or not."
3288   (memq article gnus-newsgroup-unreads))
3289
3290 (defun gnus-summary-first-article-p (&optional article)
3291   "Return whether ARTICLE is the first article in the buffer."
3292   (if (not (setq article (or article (gnus-summary-article-number))))
3293       nil
3294     (eq article (caar gnus-newsgroup-data))))
3295
3296 (defun gnus-summary-last-article-p (&optional article)
3297   "Return whether ARTICLE is the last article in the buffer."
3298   (if (not (setq article (or article (gnus-summary-article-number))))
3299       ;; All non-existent numbers are the last article.  :-)
3300       t
3301     (not (cdr (gnus-data-find-list article)))))
3302
3303 (defun gnus-make-thread-indent-array ()
3304   (let ((n 200))
3305     (unless (and gnus-thread-indent-array
3306                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3307       (setq gnus-thread-indent-array (make-vector 201 "")
3308             gnus-thread-indent-array-level gnus-thread-indent-level)
3309       (while (>= n 0)
3310         (aset gnus-thread-indent-array n
3311               (make-string (* n gnus-thread-indent-level) ? ))
3312         (setq n (1- n))))))
3313
3314 (defun gnus-update-summary-mark-positions ()
3315   "Compute where the summary marks are to go."
3316   (save-excursion
3317     (when (gnus-buffer-exists-p gnus-summary-buffer)
3318       (set-buffer gnus-summary-buffer))
3319     (let ((spec gnus-summary-line-format-spec)
3320           pos)
3321       (save-excursion
3322         (gnus-set-work-buffer)
3323         (let ((gnus-tmp-unread ?Z)
3324               (gnus-replied-mark ?Z)
3325               (gnus-score-below-mark ?Z)
3326               (gnus-score-over-mark ?Z)
3327               (gnus-undownloaded-mark ?Z)
3328               (gnus-summary-line-format-spec spec)
3329               (gnus-newsgroup-downloadable '(0))
3330               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3331               case-fold-search ignores)
3332           ;; Here, all marks are bound to Z.
3333           (gnus-summary-insert-line header
3334                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3335           (goto-char (point-min))
3336           ;; Memorize the positions of the same characters as dummy marks.
3337           (while (re-search-forward "[A-D]" nil t)
3338             (push (point) ignores))
3339           (erase-buffer)
3340           ;; We use A-D as dummy marks in order to know column positions
3341           ;; where marks should be inserted.
3342           (setq gnus-tmp-unread ?A
3343                 gnus-replied-mark ?B
3344                 gnus-score-below-mark ?C
3345                 gnus-score-over-mark ?C
3346                 gnus-undownloaded-mark ?D)
3347           (gnus-summary-insert-line header
3348                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3349           ;; Ignore characters which aren't dummy marks.
3350           (dolist (p ignores)
3351             (delete-region (goto-char (1- p)) p)
3352             (insert ?Z))
3353           (goto-char (point-min))
3354           (setq pos (list (cons 'unread
3355                                 (and (search-forward "A" nil t)
3356                                      (- (point) (point-min) 1)))))
3357           (goto-char (point-min))
3358           (push (cons 'replied (and (search-forward "B" nil t)
3359                                     (- (point) (point-min) 1)))
3360                 pos)
3361           (goto-char (point-min))
3362           (push (cons 'score (and (search-forward "C" nil t)
3363                                   (- (point) (point-min) 1)))
3364                 pos)
3365           (goto-char (point-min))
3366           (push (cons 'download (and (search-forward "D" nil t)
3367                                      (- (point) (point-min) 1)))
3368                 pos)))
3369       (setq gnus-summary-mark-positions pos))))
3370
3371 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3372   "Insert a dummy root in the summary buffer."
3373   (beginning-of-line)
3374   (gnus-add-text-properties
3375    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3376    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3377
3378 (defun gnus-summary-extract-address-component (from)
3379   (or (car (funcall gnus-extract-address-components from))
3380       from))
3381
3382 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3383   (let ((mail-parse-charset gnus-newsgroup-charset)
3384         ; Is it really necessary to do this next part for each summary line?
3385         ; Luckily, doesn't seem to slow things down much.
3386         (mail-parse-ignored-charsets
3387          (with-current-buffer gnus-summary-buffer
3388            gnus-newsgroup-ignored-charsets)))
3389     (or
3390      (and gnus-ignored-from-addresses
3391           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3392           (let ((extra-headers (mail-header-extra header))
3393                 to
3394                 newsgroups)
3395             (cond
3396              ((setq to (cdr (assq 'To extra-headers)))
3397               (concat gnus-summary-to-prefix
3398                       (inline
3399                         (gnus-summary-extract-address-component
3400                          (funcall gnus-decode-encoded-word-function to)))))
3401              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3402               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3403      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3404
3405 (defun gnus-summary-insert-line (gnus-tmp-header
3406                                  gnus-tmp-level gnus-tmp-current
3407                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3408                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3409                                  &optional gnus-tmp-dummy gnus-tmp-score
3410                                  gnus-tmp-process)
3411   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3412          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3413          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3414          (gnus-tmp-score-char
3415           (if (or (null gnus-summary-default-score)
3416                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3417                       gnus-summary-zcore-fuzz))
3418               ?                         ;Whitespace
3419             (if (< gnus-tmp-score gnus-summary-default-score)
3420                 gnus-score-below-mark gnus-score-over-mark)))
3421          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3422          (gnus-tmp-replied
3423           (cond (gnus-tmp-process gnus-process-mark)
3424                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3425                  gnus-cached-mark)
3426                 (gnus-tmp-replied gnus-replied-mark)
3427                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3428                  gnus-forwarded-mark)
3429                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3430                  gnus-saved-mark)
3431                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3432                  gnus-recent-mark)
3433                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3434                  gnus-unseen-mark)
3435                 (t gnus-no-mark)))
3436          (gnus-tmp-downloaded
3437           (cond (undownloaded
3438                  gnus-undownloaded-mark)
3439                 (gnus-newsgroup-agentized
3440                  gnus-downloaded-mark)
3441                 (t
3442                  gnus-no-mark)))
3443          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3444          (gnus-tmp-name
3445           (cond
3446            ((string-match "<[^>]+> *$" gnus-tmp-from)
3447             (let ((beg (match-beginning 0)))
3448               (or (and (string-match "^\".+\"" gnus-tmp-from)
3449                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3450                   (substring gnus-tmp-from 0 beg))))
3451            ((string-match "(.+)" gnus-tmp-from)
3452             (substring gnus-tmp-from
3453                        (1+ (match-beginning 0)) (1- (match-end 0))))
3454            (t gnus-tmp-from)))
3455          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3456          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3457          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3458          (buffer-read-only nil))
3459     (when (string= gnus-tmp-name "")
3460       (setq gnus-tmp-name gnus-tmp-from))
3461     (unless (numberp gnus-tmp-lines)
3462       (setq gnus-tmp-lines -1))
3463     (if (= gnus-tmp-lines -1)
3464         (setq gnus-tmp-lines "?")
3465       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3466       (gnus-put-text-property
3467      (point)
3468      (progn (eval gnus-summary-line-format-spec) (point))
3469        'gnus-number gnus-tmp-number)
3470     (when (gnus-visual-p 'summary-highlight 'highlight)
3471       (forward-line -1)
3472       (gnus-run-hooks 'gnus-summary-update-hook)
3473       (forward-line 1))))
3474
3475 (defun gnus-summary-update-line (&optional dont-update)
3476   "Update summary line after change."
3477   (when (and gnus-summary-default-score
3478              (not gnus-summary-inhibit-highlight))
3479     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3480            (article (gnus-summary-article-number))
3481            (score (gnus-summary-article-score article)))
3482       (unless dont-update
3483         (if (and gnus-summary-mark-below
3484                  (< (gnus-summary-article-score)
3485                     gnus-summary-mark-below))
3486             ;; This article has a low score, so we mark it as read.
3487             (when (memq article gnus-newsgroup-unreads)
3488               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3489           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3490             ;; This article was previously marked as read on account
3491             ;; of a low score, but now it has risen, so we mark it as
3492             ;; unread.
3493             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3494         (gnus-summary-update-mark
3495          (if (or (null gnus-summary-default-score)
3496                  (<= (abs (- score gnus-summary-default-score))
3497                      gnus-summary-zcore-fuzz))
3498              ?                          ;Whitespace
3499            (if (< score gnus-summary-default-score)
3500                gnus-score-below-mark gnus-score-over-mark))
3501          'score))
3502       ;; Do visual highlighting.
3503       (when (gnus-visual-p 'summary-highlight 'highlight)
3504         (gnus-run-hooks 'gnus-summary-update-hook)))))
3505
3506 (defvar gnus-tmp-new-adopts nil)
3507
3508 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3509   "Return the number of articles in THREAD.
3510 This may be 0 in some cases -- if none of the articles in
3511 the thread are to be displayed."
3512   (let* ((number
3513          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3514           (cond
3515            ((not (listp thread))
3516             1)
3517            ((and (consp thread) (cdr thread))
3518             (apply
3519              '+ 1 (mapcar
3520                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3521            ((null thread)
3522             1)
3523            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3524             1)
3525            (t 0))))
3526     (when (and level (zerop level) gnus-tmp-new-adopts)
3527       (incf number
3528             (apply '+ (mapcar
3529                        'gnus-summary-number-of-articles-in-thread
3530                        gnus-tmp-new-adopts))))
3531     (if char
3532         (if (> number 1) gnus-not-empty-thread-mark
3533           gnus-empty-thread-mark)
3534       number)))
3535
3536 (defsubst gnus-summary-line-message-size (head)
3537   "Return pretty-printed version of message size.
3538 This function is intended to be used in
3539 `gnus-summary-line-format-alist'."
3540   (let ((c (or (mail-header-chars head) -1)))
3541     (cond ((< c 0) "n/a")               ; chars not available
3542           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3543           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3544           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3545           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3546
3547
3548 (defun gnus-summary-set-local-parameters (group)
3549   "Go through the local params of GROUP and set all variable specs in that list."
3550   (let ((vars '(quit-config)))          ; Ignore quit-config.
3551     (dolist (elem (gnus-group-find-parameter group))
3552       (and (consp elem)                 ; Has to be a cons.
3553            (consp (cdr elem))           ; The cdr has to be a list.
3554            (symbolp (car elem))         ; Has to be a symbol in there.
3555            (not (memq (car elem) vars))
3556            (ignore-errors               ; So we set it.
3557              (push (car elem) vars)
3558              (make-local-variable (car elem))
3559              (set (car elem) (eval (nth 1 elem))))))))
3560
3561 (defun gnus-summary-read-group (group &optional show-all no-article
3562                                       kill-buffer no-display backward
3563                                       select-articles)
3564   "Start reading news in newsgroup GROUP.
3565 If SHOW-ALL is non-nil, already read articles are also listed.
3566 If NO-ARTICLE is non-nil, no article is selected initially.
3567 If NO-DISPLAY, don't generate a summary buffer."
3568   (let (result)
3569     (while (and group
3570                 (null (setq result
3571                             (let ((gnus-auto-select-next nil))
3572                               (or (gnus-summary-read-group-1
3573                                    group show-all no-article
3574                                    kill-buffer no-display
3575                                    select-articles)
3576                                   (setq show-all nil
3577                                         select-articles nil)))))
3578                 (eq gnus-auto-select-next 'quietly))
3579       (set-buffer gnus-group-buffer)
3580       ;; The entry function called above goes to the next
3581       ;; group automatically, so we go two groups back
3582       ;; if we are searching for the previous group.
3583       (when backward
3584         (gnus-group-prev-unread-group 2))
3585       (if (not (equal group (gnus-group-group-name)))
3586           (setq group (gnus-group-group-name))
3587         (setq group nil)))
3588     result))
3589
3590 (defun gnus-summary-read-group-1 (group show-all no-article
3591                                         kill-buffer no-display
3592                                         &optional select-articles)
3593   ;; Killed foreign groups can't be entered.
3594   ;;  (when (and (not (gnus-group-native-p group))
3595   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3596   ;;    (error "Dead non-native groups can't be entered"))
3597   (gnus-message 5 "Retrieving newsgroup: %s..."
3598                 (gnus-group-decoded-name group))
3599   (let* ((new-group (gnus-summary-setup-buffer group))
3600          (quit-config (gnus-group-quit-config group))
3601          (did-select (and new-group (gnus-select-newsgroup
3602                                      group show-all select-articles))))
3603     (cond
3604      ;; This summary buffer exists already, so we just select it.
3605      ((not new-group)
3606       (gnus-set-global-variables)
3607       (when kill-buffer
3608         (gnus-kill-or-deaden-summary kill-buffer))
3609       (gnus-configure-windows 'summary 'force)
3610       (gnus-set-mode-line 'summary)
3611       (gnus-summary-position-point)
3612       (message "")
3613       t)
3614      ;; We couldn't select this group.
3615      ((null did-select)
3616       (when (and (eq major-mode 'gnus-summary-mode)
3617                  (not (equal (current-buffer) kill-buffer)))
3618         (kill-buffer (current-buffer))
3619         (if (not quit-config)
3620             (progn
3621               ;; Update the info -- marks might need to be removed,
3622               ;; for instance.
3623               (gnus-summary-update-info)
3624               (set-buffer gnus-group-buffer)
3625               (gnus-group-jump-to-group group)
3626               (gnus-group-next-unread-group 1))
3627           (gnus-handle-ephemeral-exit quit-config)))
3628       (let ((grpinfo (gnus-get-info group)))
3629         (if (null (gnus-info-read grpinfo))
3630             (gnus-message 3 "Group %s contains no messages"
3631                           (gnus-group-decoded-name group))
3632           (gnus-message 3 "Can't select group")))
3633       nil)
3634      ;; The user did a `C-g' while prompting for number of articles,
3635      ;; so we exit this group.
3636      ((eq did-select 'quit)
3637       (and (eq major-mode 'gnus-summary-mode)
3638            (not (equal (current-buffer) kill-buffer))
3639            (kill-buffer (current-buffer)))
3640       (when kill-buffer
3641         (gnus-kill-or-deaden-summary kill-buffer))
3642       (if (not quit-config)
3643           (progn
3644             (set-buffer gnus-group-buffer)
3645             (gnus-group-jump-to-group group)
3646             (gnus-group-next-unread-group 1)
3647             (gnus-configure-windows 'group 'force))
3648         (gnus-handle-ephemeral-exit quit-config))
3649       ;; Finally signal the quit.
3650       (signal 'quit nil))
3651      ;; The group was successfully selected.
3652      (t
3653       (gnus-set-global-variables)
3654       ;; Save the active value in effect when the group was entered.
3655       (setq gnus-newsgroup-active
3656             (gnus-copy-sequence
3657              (gnus-active gnus-newsgroup-name)))
3658       ;; You can change the summary buffer in some way with this hook.
3659       (gnus-run-hooks 'gnus-select-group-hook)
3660       (when (memq 'summary (gnus-update-format-specifications
3661                             nil 'summary 'summary-mode 'summary-dummy))
3662         ;; The format specification for the summary line was updated,
3663         ;; so we need to update the mark positions as well.
3664         (gnus-update-summary-mark-positions))
3665       ;; Do score processing.
3666       (when gnus-use-scoring
3667         (gnus-possibly-score-headers))
3668       ;; Check whether to fill in the gaps in the threads.
3669       (when gnus-build-sparse-threads
3670         (gnus-build-sparse-threads))
3671       ;; Find the initial limit.
3672       (if show-all
3673           (let ((gnus-newsgroup-dormant nil))
3674             (gnus-summary-initial-limit show-all))
3675         (gnus-summary-initial-limit show-all))
3676       ;; Generate the summary buffer.
3677       (unless no-display
3678         (gnus-summary-prepare))
3679       (when gnus-use-trees
3680         (gnus-tree-open group)
3681         (setq gnus-summary-highlight-line-function
3682               'gnus-tree-highlight-article))
3683       ;; If the summary buffer is empty, but there are some low-scored
3684       ;; articles or some excluded dormants, we include these in the
3685       ;; buffer.
3686       (when (and (zerop (buffer-size))
3687                  (not no-display))
3688         (cond (gnus-newsgroup-dormant
3689                (gnus-summary-limit-include-dormant))
3690               ((and gnus-newsgroup-scored show-all)
3691                (gnus-summary-limit-include-expunged t))))
3692       ;; Function `gnus-apply-kill-file' must be called in this hook.
3693       (gnus-run-hooks 'gnus-apply-kill-hook)
3694       (if (and (zerop (buffer-size))
3695                (not no-display))
3696           (progn
3697             ;; This newsgroup is empty.
3698             (gnus-summary-catchup-and-exit nil t)
3699             (gnus-message 6 "No unread news")
3700             (when kill-buffer
3701               (gnus-kill-or-deaden-summary kill-buffer))
3702             ;; Return nil from this function.
3703             nil)
3704         ;; Hide conversation thread subtrees.  We cannot do this in
3705         ;; gnus-summary-prepare-hook since kill processing may not
3706         ;; work with hidden articles.
3707         (gnus-summary-maybe-hide-threads)
3708         (when kill-buffer
3709           (gnus-kill-or-deaden-summary kill-buffer))
3710         (gnus-summary-auto-select-subject)
3711         ;; Show first unread article if requested.
3712         (if (and (not no-article)
3713                  (not no-display)
3714                  gnus-newsgroup-unreads
3715                  gnus-auto-select-first)
3716             (progn
3717               (gnus-configure-windows 'summary)
3718               (let ((art (gnus-summary-article-number)))
3719                 (unless (and (not gnus-plugged)
3720                              (or (memq art gnus-newsgroup-undownloaded)
3721                                  (memq art gnus-newsgroup-downloadable)))
3722                   (gnus-summary-goto-article art))))
3723           ;; Don't select any articles.
3724           (gnus-summary-position-point)
3725           (gnus-configure-windows 'summary 'force)
3726           (gnus-set-mode-line 'summary))
3727         (when (and gnus-auto-center-group
3728                    (get-buffer-window gnus-group-buffer t))
3729           ;; Gotta use windows, because recenter does weird stuff if
3730           ;; the current buffer ain't the displayed window.
3731           (let ((owin (selected-window)))
3732             (select-window (get-buffer-window gnus-group-buffer t))
3733             (when (gnus-group-goto-group group)
3734               (recenter))
3735             (select-window owin)))
3736         ;; Mark this buffer as "prepared".
3737         (setq gnus-newsgroup-prepared t)
3738         (gnus-run-hooks 'gnus-summary-prepared-hook)
3739         (unless (gnus-ephemeral-group-p group)
3740           (gnus-group-update-group group))
3741         t)))))
3742
3743 (defun gnus-summary-auto-select-subject ()
3744   "Select the subject line on initial group entry."
3745   (goto-char (point-min))
3746   (cond
3747    ((eq gnus-auto-select-subject 'best)
3748     (gnus-summary-best-unread-subject))
3749    ((eq gnus-auto-select-subject 'unread)
3750     (gnus-summary-first-unread-subject))
3751    ((eq gnus-auto-select-subject 'unseen)
3752     (gnus-summary-first-unseen-subject))
3753    ((eq gnus-auto-select-subject 'unseen-or-unread)
3754     (gnus-summary-first-unseen-or-unread-subject))
3755    ((eq gnus-auto-select-subject 'first)
3756     ;; Do nothing.
3757     )
3758    ((functionp gnus-auto-select-subject)
3759     (funcall gnus-auto-select-subject))))
3760
3761 (defun gnus-summary-prepare ()
3762   "Generate the summary buffer."
3763   (interactive)
3764   (let ((buffer-read-only nil))
3765     (erase-buffer)
3766     (setq gnus-newsgroup-data nil
3767           gnus-newsgroup-data-reverse nil)
3768     (gnus-run-hooks 'gnus-summary-generate-hook)
3769     ;; Generate the buffer, either with threads or without.
3770     (when gnus-newsgroup-headers
3771       (gnus-summary-prepare-threads
3772        (if gnus-show-threads
3773            (gnus-sort-gathered-threads
3774             (funcall gnus-summary-thread-gathering-function
3775                      (gnus-sort-threads
3776                       (gnus-cut-threads (gnus-make-threads)))))
3777          ;; Unthreaded display.
3778          (gnus-sort-articles gnus-newsgroup-headers))))
3779     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3780     ;; Call hooks for modifying summary buffer.
3781     (goto-char (point-min))
3782     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3783
3784 (defsubst gnus-general-simplify-subject (subject)
3785   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3786   (setq subject
3787         (cond
3788          ;; Truncate the subject.
3789          (gnus-simplify-subject-functions
3790           (gnus-map-function gnus-simplify-subject-functions subject))
3791          ((numberp gnus-summary-gather-subject-limit)
3792           (setq subject (gnus-simplify-subject-re subject))
3793           (if (> (length subject) gnus-summary-gather-subject-limit)
3794               (substring subject 0 gnus-summary-gather-subject-limit)
3795             subject))
3796          ;; Fuzzily simplify it.
3797          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3798           (gnus-simplify-subject-fuzzy subject))
3799          ;; Just remove the leading "Re:".
3800          (t
3801           (gnus-simplify-subject-re subject))))
3802
3803   (if (and gnus-summary-gather-exclude-subject
3804            (string-match gnus-summary-gather-exclude-subject subject))
3805       nil                         ; This article shouldn't be gathered
3806     subject))
3807
3808 (defun gnus-summary-simplify-subject-query ()
3809   "Query where the respool algorithm would put this article."
3810   (interactive)
3811   (gnus-summary-select-article)
3812   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3813
3814 (defun gnus-gather-threads-by-subject (threads)
3815   "Gather threads by looking at Subject headers."
3816   (if (not gnus-summary-make-false-root)
3817       threads
3818     (let ((hashtb (gnus-make-hashtable 1024))
3819           (prev threads)
3820           (result threads)
3821           subject hthread whole-subject)
3822       (while threads
3823         (setq subject (gnus-general-simplify-subject
3824                        (setq whole-subject (mail-header-subject
3825                                             (caar threads)))))
3826         (when subject
3827           (if (setq hthread (gnus-gethash subject hashtb))
3828               (progn
3829                 ;; We enter a dummy root into the thread, if we
3830                 ;; haven't done that already.
3831                 (unless (stringp (caar hthread))
3832                   (setcar hthread (list whole-subject (car hthread))))
3833                 ;; We add this new gathered thread to this gathered
3834                 ;; thread.
3835                 (setcdr (car hthread)
3836                         (nconc (cdar hthread) (list (car threads))))
3837                 ;; Remove it from the list of threads.
3838                 (setcdr prev (cdr threads))
3839                 (setq threads prev))
3840             ;; Enter this thread into the hash table.
3841             (gnus-sethash subject
3842                           (if gnus-summary-make-false-root-always
3843                               (progn
3844                                 ;; If you want a dummy root above all
3845                                 ;; threads...
3846                                 (setcar threads (list whole-subject
3847                                                       (car threads)))
3848                                 threads)
3849                             threads)
3850                           hashtb)))
3851         (setq prev threads)
3852         (setq threads (cdr threads)))
3853       result)))
3854
3855 (defun gnus-gather-threads-by-references (threads)
3856   "Gather threads by looking at References headers."
3857   (let ((idhashtb (gnus-make-hashtable 1024))
3858         (thhashtb (gnus-make-hashtable 1024))
3859         (prev threads)
3860         (result threads)
3861         ids references id gthread gid entered ref)
3862     (while threads
3863       (when (setq references (mail-header-references (caar threads)))
3864         (setq id (mail-header-id (caar threads))
3865               ids (inline (gnus-split-references references))
3866               entered nil)
3867         (while (setq ref (pop ids))
3868           (setq ids (delete ref ids))
3869           (if (not (setq gid (gnus-gethash ref idhashtb)))
3870               (progn
3871                 (gnus-sethash ref id idhashtb)
3872                 (gnus-sethash id threads thhashtb))
3873             (setq gthread (gnus-gethash gid thhashtb))
3874             (unless entered
3875               ;; We enter a dummy root into the thread, if we
3876               ;; haven't done that already.
3877               (unless (stringp (caar gthread))
3878                 (setcar gthread (list (mail-header-subject (caar gthread))
3879                                       (car gthread))))
3880               ;; We add this new gathered thread to this gathered
3881               ;; thread.
3882               (setcdr (car gthread)
3883                       (nconc (cdar gthread) (list (car threads)))))
3884             ;; Add it into the thread hash table.
3885             (gnus-sethash id gthread thhashtb)
3886             (setq entered t)
3887             ;; Remove it from the list of threads.
3888             (setcdr prev (cdr threads))
3889             (setq threads prev))))
3890       (setq prev threads)
3891       (setq threads (cdr threads)))
3892     result))
3893
3894 (defun gnus-sort-gathered-threads (threads)
3895   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3896   (let ((result threads))
3897     (while threads
3898       (when (stringp (caar threads))
3899         (setcdr (car threads)
3900                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3901       (setq threads (cdr threads)))
3902     result))
3903
3904 (defun gnus-thread-loop-p (root thread)
3905   "Say whether ROOT is in THREAD."
3906   (let ((stack (list thread))
3907         (infloop 0)
3908         th)
3909     (while (setq thread (pop stack))
3910       (setq th (cdr thread))
3911       (while (and th
3912                   (not (eq (caar th) root)))
3913         (pop th))
3914       (if th
3915           ;; We have found a loop.
3916           (let (ref-dep)
3917             (setcdr thread (delq (car th) (cdr thread)))
3918             (if (boundp (setq ref-dep (intern "none"
3919                                               gnus-newsgroup-dependencies)))
3920                 (setcdr (symbol-value ref-dep)
3921                         (nconc (cdr (symbol-value ref-dep))
3922                                (list (car th))))
3923               (set ref-dep (list nil (car th))))
3924             (setq infloop 1
3925                   stack nil))
3926         ;; Push all the subthreads onto the stack.
3927         (push (cdr thread) stack)))
3928     infloop))
3929
3930 (defun gnus-make-threads ()
3931   "Go through the dependency hashtb and find the roots.  Return all threads."
3932   (let (threads)
3933     (while (catch 'infloop
3934              (mapatoms
3935               (lambda (refs)
3936                 ;; Deal with self-referencing References loops.
3937                 (when (and (car (symbol-value refs))
3938                            (not (zerop
3939                                  (apply
3940                                   '+
3941                                   (mapcar
3942                                    (lambda (thread)
3943                                      (gnus-thread-loop-p
3944                                       (car (symbol-value refs)) thread))
3945                                    (cdr (symbol-value refs)))))))
3946                   (setq threads nil)
3947                   (throw 'infloop t))
3948                 (unless (car (symbol-value refs))
3949                   ;; These threads do not refer back to any other
3950                   ;; articles, so they're roots.
3951                   (setq threads (append (cdr (symbol-value refs)) threads))))
3952               gnus-newsgroup-dependencies)))
3953     threads))
3954
3955 ;; Build the thread tree.
3956 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3957   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3958
3959 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3960 if it was already present.
3961
3962 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3963 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3964 Message-IDs will be renamed to a unique Message-ID before being
3965 entered.
3966
3967 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3968   (let* ((id (mail-header-id header))
3969          (id-dep (and id (intern id dependencies)))
3970          parent-id ref ref-dep ref-header replaced)
3971     ;; Enter this `header' in the `dependencies' table.
3972     (cond
3973      ((not id-dep)
3974       (setq header nil))
3975      ;; The first two cases do the normal part: enter a new `header'
3976      ;; in the `dependencies' table.
3977      ((not (boundp id-dep))
3978       (set id-dep (list header)))
3979      ((null (car (symbol-value id-dep)))
3980       (setcar (symbol-value id-dep) header))
3981
3982      ;; From here the `header' was already present in the
3983      ;; `dependencies' table.
3984      (force-new
3985       ;; Overrides an existing entry;
3986       ;; just set the header part of the entry.
3987       (setcar (symbol-value id-dep) header)
3988       (setq replaced t))
3989
3990      ;; Renames the existing `header' to a unique Message-ID.
3991      ((not gnus-summary-ignore-duplicates)
3992       ;; An article with this Message-ID has already been seen.
3993       ;; We rename the Message-ID.
3994       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3995            (list header))
3996       (mail-header-set-id header id))
3997
3998      ;; The last case ignores an existing entry, except it adds any
3999      ;; additional Xrefs (in case the two articles came from different
4000      ;; servers.
4001      ;; Also sets `header' to `nil' meaning that the `dependencies'
4002      ;; table was *not* modified.
4003      (t
4004       (mail-header-set-xref
4005        (car (symbol-value id-dep))
4006        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4007                    "")
4008                (or (mail-header-xref header) "")))
4009       (setq header nil)))
4010
4011     (when (and header (not replaced))
4012       ;; First check that we are not creating a References loop.
4013       (setq parent-id (gnus-parent-id (mail-header-references header)))
4014       (setq ref parent-id)
4015       (while (and ref
4016                   (setq ref-dep (intern-soft ref dependencies))
4017                   (boundp ref-dep)
4018                   (setq ref-header (car (symbol-value ref-dep))))
4019         (if (string= id ref)
4020             ;; Yuk!  This is a reference loop.  Make the article be a
4021             ;; root article.
4022             (progn
4023               (mail-header-set-references (car (symbol-value id-dep)) "none")
4024               (setq ref nil)
4025               (setq parent-id nil))
4026           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4027       (setq ref-dep (intern (or parent-id "none") dependencies))
4028       (if (boundp ref-dep)
4029           (setcdr (symbol-value ref-dep)
4030                   (nconc (cdr (symbol-value ref-dep))
4031                          (list (symbol-value id-dep))))
4032         (set ref-dep (list nil (symbol-value id-dep)))))
4033     header))
4034
4035 (defun gnus-extract-message-id-from-in-reply-to (string)
4036   (if (string-match "<[^>]+>" string)
4037       (substring string (match-beginning 0) (match-end 0))
4038     nil))
4039
4040 (defun gnus-build-sparse-threads ()
4041   (let ((headers gnus-newsgroup-headers)
4042         (mail-parse-charset gnus-newsgroup-charset)
4043         (gnus-summary-ignore-duplicates t)
4044         header references generation relations
4045         subject child end new-child date)
4046     ;; First we create an alist of generations/relations, where
4047     ;; generations is how much we trust the relation, and the relation
4048     ;; is parent/child.
4049     (gnus-message 7 "Making sparse threads...")
4050     (save-excursion
4051       (nnheader-set-temp-buffer " *gnus sparse threads*")
4052       (while (setq header (pop headers))
4053         (when (and (setq references (mail-header-references header))
4054                    (not (string= references "")))
4055           (insert references)
4056           (setq child (mail-header-id header)
4057                 subject (mail-header-subject header)
4058                 date (mail-header-date header)
4059                 generation 0)
4060           (while (search-backward ">" nil t)
4061             (setq end (1+ (point)))
4062             (when (search-backward "<" nil t)
4063               (setq new-child (buffer-substring (point) end))
4064               (push (list (incf generation)
4065                           child (setq child new-child)
4066                           subject date)
4067                     relations)))
4068           (when child
4069             (push (list (1+ generation) child nil subject) relations))
4070           (erase-buffer)))
4071       (kill-buffer (current-buffer)))
4072     ;; Sort over trustworthiness.
4073     (mapcar
4074      (lambda (relation)
4075        (when (gnus-dependencies-add-header
4076               (make-full-mail-header
4077                gnus-reffed-article-number
4078                (nth 3 relation) "" (or (nth 4 relation) "")
4079                (nth 1 relation)
4080                (or (nth 2 relation) "") 0 0 "")
4081               gnus-newsgroup-dependencies nil)
4082          (push gnus-reffed-article-number gnus-newsgroup-limit)
4083          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4084          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4085                gnus-newsgroup-reads)
4086          (decf gnus-reffed-article-number)))
4087      (sort relations 'car-less-than-car))
4088     (gnus-message 7 "Making sparse threads...done")))
4089
4090 (defun gnus-build-old-threads ()
4091   ;; Look at all the articles that refer back to old articles, and
4092   ;; fetch the headers for the articles that aren't there.  This will
4093   ;; build complete threads - if the roots haven't been expired by the
4094   ;; server, that is.
4095   (let ((mail-parse-charset gnus-newsgroup-charset)
4096         id heads)
4097     (mapatoms
4098      (lambda (refs)
4099        (when (not (car (symbol-value refs)))
4100          (setq heads (cdr (symbol-value refs)))
4101          (while heads
4102            (if (memq (mail-header-number (caar heads))
4103                      gnus-newsgroup-dormant)
4104                (setq heads (cdr heads))
4105              (setq id (symbol-name refs))
4106              (while (and (setq id (gnus-build-get-header id))
4107                          (not (car (gnus-id-to-thread id)))))
4108              (setq heads nil)))))
4109      gnus-newsgroup-dependencies)))
4110
4111 (defsubst gnus-remove-odd-characters (string)
4112   "Translate STRING into something that doesn't contain weird characters."
4113   (mm-subst-char-in-string
4114    ?\r ?\-
4115    (mm-subst-char-in-string ?\n ?\- string t) t))
4116
4117 ;; This function has to be called with point after the article number
4118 ;; on the beginning of the line.
4119 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4120   (let ((eol (point-at-eol))
4121         (buffer (current-buffer))
4122         header references in-reply-to)
4123
4124     ;; overview: [num subject from date id refs chars lines misc]
4125     (unwind-protect
4126         (let (x)
4127           (narrow-to-region (point) eol)
4128           (unless (eobp)
4129             (forward-char))
4130
4131           (setq header
4132                 (make-full-mail-header
4133                  number                 ; number
4134                  (condition-case ()     ; subject
4135                      (gnus-remove-odd-characters
4136                       (funcall gnus-decode-encoded-word-function
4137                                (setq x (nnheader-nov-field))))
4138                    (error x))
4139                  (condition-case ()     ; from
4140                      (gnus-remove-odd-characters
4141                       (funcall gnus-decode-encoded-word-function
4142                                (setq x (nnheader-nov-field))))
4143                    (error x))
4144                  (nnheader-nov-field)   ; date
4145                  (nnheader-nov-read-message-id number)  ; id
4146                  (setq references (nnheader-nov-field)) ; refs
4147                  (nnheader-nov-read-integer) ; chars
4148                  (nnheader-nov-read-integer) ; lines
4149                  (unless (eobp)
4150                    (if (looking-at "Xref: ")
4151                        (goto-char (match-end 0)))
4152                    (nnheader-nov-field)) ; Xref
4153                  (nnheader-nov-parse-extra)))) ; extra
4154
4155       (widen))
4156
4157     (when (and (string= references "")
4158                (setq in-reply-to (mail-header-extra header))
4159                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4160       (mail-header-set-references
4161        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4162
4163     (when gnus-alter-header-function
4164       (funcall gnus-alter-header-function header))
4165     (gnus-dependencies-add-header header dependencies force-new)))
4166
4167 (defun gnus-build-get-header (id)
4168   "Look through the buffer of NOV lines and find the header to ID.
4169 Enter this line into the dependencies hash table, and return
4170 the id of the parent article (if any)."
4171   (let ((deps gnus-newsgroup-dependencies)
4172         found header)
4173     (prog1
4174         (save-excursion
4175           (set-buffer nntp-server-buffer)
4176           (let ((case-fold-search nil))
4177             (goto-char (point-min))
4178             (while (and (not found)
4179                         (search-forward id nil t))
4180               (beginning-of-line)
4181               (setq found (looking-at
4182                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4183                                    (regexp-quote id))))
4184               (or found (beginning-of-line 2)))
4185             (when found
4186               (beginning-of-line)
4187               (and
4188                (setq header (gnus-nov-parse-line
4189                              (read (current-buffer)) deps))
4190                (gnus-parent-id (mail-header-references header))))))
4191       (when header
4192         (let ((number (mail-header-number header)))
4193           (push number gnus-newsgroup-limit)
4194           (push header gnus-newsgroup-headers)
4195           (if (memq number gnus-newsgroup-unselected)
4196               (progn
4197                 (setq gnus-newsgroup-unreads
4198                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4199                                                number))
4200                 (setq gnus-newsgroup-unselected
4201                       (delq number gnus-newsgroup-unselected)))
4202             (push number gnus-newsgroup-ancient)))))))
4203
4204 (defun gnus-build-all-threads ()
4205   "Read all the headers."
4206   (let ((gnus-summary-ignore-duplicates t)
4207         (mail-parse-charset gnus-newsgroup-charset)
4208         (dependencies gnus-newsgroup-dependencies)
4209         header article)
4210     (save-excursion
4211       (set-buffer nntp-server-buffer)
4212       (let ((case-fold-search nil))
4213         (goto-char (point-min))
4214         (while (not (eobp))
4215           (ignore-errors
4216             (setq article (read (current-buffer))
4217                   header (gnus-nov-parse-line article dependencies)))
4218           (when header
4219             (with-current-buffer gnus-summary-buffer
4220               (push header gnus-newsgroup-headers)
4221               (if (memq (setq article (mail-header-number header))
4222                         gnus-newsgroup-unselected)
4223                   (progn
4224                     (setq gnus-newsgroup-unreads
4225                           (gnus-add-to-sorted-list
4226                            gnus-newsgroup-unreads article))
4227                     (setq gnus-newsgroup-unselected
4228                           (delq article gnus-newsgroup-unselected)))
4229                 (push article gnus-newsgroup-ancient)))
4230             (forward-line 1)))))))
4231
4232 (defun gnus-summary-update-article-line (article header)
4233   "Update the line for ARTICLE using HEADER."
4234   (let* ((id (mail-header-id header))
4235          (thread (gnus-id-to-thread id)))
4236     (unless thread
4237       (error "Article in no thread"))
4238     ;; Update the thread.
4239     (setcar thread header)
4240     (gnus-summary-goto-subject article)
4241     (let* ((datal (gnus-data-find-list article))
4242            (data (car datal))
4243            (buffer-read-only nil)
4244            (level (gnus-summary-thread-level)))
4245       (gnus-delete-line)
4246       (let ((inserted (- (point)
4247                          (progn
4248                            (gnus-summary-insert-line
4249                             header level nil
4250                             (memq article gnus-newsgroup-undownloaded)
4251                             (gnus-article-mark article)
4252                             (memq article gnus-newsgroup-replied)
4253                             (memq article gnus-newsgroup-expirable)
4254                             ;; Only insert the Subject string when it's different
4255                             ;; from the previous Subject string.
4256                             (if (and
4257                                  gnus-show-threads
4258                                  (gnus-subject-equal
4259                                   (condition-case ()
4260                                       (mail-header-subject
4261                                        (gnus-data-header
4262                                         (cadr
4263                                          (gnus-data-find-list
4264                                           article
4265                                           (gnus-data-list t)))))
4266                                     ;; Error on the side of excessive subjects.
4267                                     (error ""))
4268                                   (mail-header-subject header)))
4269                                 ""
4270                               (mail-header-subject header))
4271                             nil (cdr (assq article gnus-newsgroup-scored))
4272                             (memq article gnus-newsgroup-processable))
4273                            (point)))))
4274         (when (cdr datal)
4275           (gnus-data-update-list
4276            (cdr datal)
4277            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4278
4279 (defun gnus-summary-update-article (article &optional iheader)
4280   "Update ARTICLE in the summary buffer."
4281   (set-buffer gnus-summary-buffer)
4282   (let* ((header (gnus-summary-article-header article))
4283          (id (mail-header-id header))
4284          (data (gnus-data-find article))
4285          (thread (gnus-id-to-thread id))
4286          (references (mail-header-references header))
4287          (parent
4288           (gnus-id-to-thread
4289            (or (gnus-parent-id
4290                 (when (and references
4291                            (not (equal "" references)))
4292                   references))
4293                "none")))
4294          (buffer-read-only nil)
4295          (old (car thread)))
4296     (when thread
4297       (unless iheader
4298         (setcar thread nil)
4299         (when parent
4300           (delq thread parent)))
4301       (if (gnus-summary-insert-subject id header)
4302           ;; Set the (possibly) new article number in the data structure.
4303           (gnus-data-set-number data (gnus-id-to-article id))
4304         (setcar thread old)
4305         nil))))
4306
4307 (defun gnus-rebuild-thread (id &optional line)
4308   "Rebuild the thread containing ID.
4309 If LINE, insert the rebuilt thread starting on line LINE."
4310   (let ((buffer-read-only nil)
4311         old-pos current thread data)
4312     (if (not gnus-show-threads)
4313         (setq thread (list (car (gnus-id-to-thread id))))
4314       ;; Get the thread this article is part of.
4315       (setq thread (gnus-remove-thread id)))
4316     (setq old-pos (point-at-bol))
4317     (setq current (save-excursion
4318                     (and (re-search-backward "[\r\n]" nil t)
4319                          (gnus-summary-article-number))))
4320     ;; If this is a gathered thread, we have to go some re-gathering.
4321     (when (stringp (car thread))
4322       (let ((subject (car thread))
4323             roots thr)
4324         (setq thread (cdr thread))
4325         (while thread
4326           (unless (memq (setq thr (gnus-id-to-thread
4327                                    (gnus-root-id
4328                                     (mail-header-id (caar thread)))))
4329                         roots)
4330             (push thr roots))
4331           (setq thread (cdr thread)))
4332         ;; We now have all (unique) roots.
4333         (if (= (length roots) 1)
4334             ;; All the loose roots are now one solid root.
4335             (setq thread (car roots))
4336           (setq thread (cons subject (gnus-sort-threads roots))))))
4337     (let (threads)
4338       ;; We then insert this thread into the summary buffer.
4339       (when line
4340         (goto-char (point-min))
4341         (forward-line (1- line)))
4342       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4343         (if gnus-show-threads
4344             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4345           (gnus-summary-prepare-unthreaded thread))
4346         (setq data (nreverse gnus-newsgroup-data))
4347         (setq threads gnus-newsgroup-threads))
4348       ;; We splice the new data into the data structure.
4349       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4350       ;;!!! then we want to insert at the beginning of the buffer.
4351       ;;!!! That happens to be true with Gnus now, but that may
4352       ;;!!! change in the future.  Perhaps.
4353       (gnus-data-enter-list
4354        (if line nil current) data (- (point) old-pos))
4355       (setq gnus-newsgroup-threads
4356             (nconc threads gnus-newsgroup-threads))
4357       (gnus-data-compute-positions))))
4358
4359 (defun gnus-number-to-header (number)
4360   "Return the header for article NUMBER."
4361   (let ((headers gnus-newsgroup-headers))
4362     (while (and headers
4363                 (not (= number (mail-header-number (car headers)))))
4364       (pop headers))
4365     (when headers
4366       (car headers))))
4367
4368 (defun gnus-parent-headers (in-headers &optional generation)
4369   "Return the headers of the GENERATIONeth parent of HEADERS."
4370   (unless generation
4371     (setq generation 1))
4372   (let ((parent t)
4373         (headers in-headers)
4374         references)
4375     (while (and parent
4376                 (not (zerop generation))
4377                 (setq references (mail-header-references headers)))
4378       (setq headers (if (and references
4379                              (setq parent (gnus-parent-id references)))
4380                         (car (gnus-id-to-thread parent))
4381                       nil))
4382       (decf generation))
4383     (and (not (eq headers in-headers))
4384          headers)))
4385
4386 (defun gnus-id-to-thread (id)
4387   "Return the (sub-)thread where ID appears."
4388   (gnus-gethash id gnus-newsgroup-dependencies))
4389
4390 (defun gnus-id-to-article (id)
4391   "Return the article number of ID."
4392   (let ((thread (gnus-id-to-thread id)))
4393     (when (and thread
4394                (car thread))
4395       (mail-header-number (car thread)))))
4396
4397 (defun gnus-id-to-header (id)
4398   "Return the article headers of ID."
4399   (car (gnus-id-to-thread id)))
4400
4401 (defun gnus-article-displayed-root-p (article)
4402   "Say whether ARTICLE is a root(ish) article."
4403   (let ((level (gnus-summary-thread-level article))
4404         (refs (mail-header-references  (gnus-summary-article-header article)))
4405         particle)
4406     (cond
4407      ((null level) nil)
4408      ((zerop level) t)
4409      ((null refs) t)
4410      ((null (gnus-parent-id refs)) t)
4411      ((and (= 1 level)
4412            (null (setq particle (gnus-id-to-article
4413                                  (gnus-parent-id refs))))
4414            (null (gnus-summary-thread-level particle)))))))
4415
4416 (defun gnus-root-id (id)
4417   "Return the id of the root of the thread where ID appears."
4418   (let (last-id prev)
4419     (while (and id (setq prev (car (gnus-id-to-thread id))))
4420       (setq last-id id
4421             id (gnus-parent-id (mail-header-references prev))))
4422     last-id))
4423
4424 (defun gnus-articles-in-thread (thread)
4425   "Return the list of articles in THREAD."
4426   (cons (mail-header-number (car thread))
4427         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4428
4429 (defun gnus-remove-thread (id &optional dont-remove)
4430   "Remove the thread that has ID in it."
4431   (let (headers thread last-id)
4432     ;; First go up in this thread until we find the root.
4433     (setq last-id (gnus-root-id id)
4434           headers (message-flatten-list (gnus-id-to-thread last-id)))
4435     ;; We have now found the real root of this thread.  It might have
4436     ;; been gathered into some loose thread, so we have to search
4437     ;; through the threads to find the thread we wanted.
4438     (let ((threads gnus-newsgroup-threads)
4439           sub)
4440       (while threads
4441         (setq sub (car threads))
4442         (if (stringp (car sub))
4443             ;; This is a gathered thread, so we look at the roots
4444             ;; below it to find whether this article is in this
4445             ;; gathered root.
4446             (progn
4447               (setq sub (cdr sub))
4448               (while sub
4449                 (when (member (caar sub) headers)
4450                   (setq thread (car threads)
4451                         threads nil
4452                         sub nil))
4453                 (setq sub (cdr sub))))
4454           ;; It's an ordinary thread, so we check it.
4455           (when (eq (car sub) (car headers))
4456             (setq thread sub
4457                   threads nil)))
4458         (setq threads (cdr threads)))
4459       ;; If this article is in no thread, then it's a root.
4460       (if thread
4461           (unless dont-remove
4462             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4463         (setq thread (gnus-id-to-thread last-id)))
4464       (when thread
4465         (prog1
4466             thread                      ; We return this thread.
4467           (unless dont-remove
4468             (if (stringp (car thread))
4469                 (progn
4470                   ;; If we use dummy roots, then we have to remove the
4471                   ;; dummy root as well.
4472                   (when (eq gnus-summary-make-false-root 'dummy)
4473                     ;; We go to the dummy root by going to
4474                     ;; the first sub-"thread", and then one line up.
4475                     (gnus-summary-goto-article
4476                      (mail-header-number (caadr thread)))
4477                     (forward-line -1)
4478                     (gnus-delete-line)
4479                     (gnus-data-compute-positions))
4480                   (setq thread (cdr thread))
4481                   (while thread
4482                     (gnus-remove-thread-1 (car thread))
4483                     (setq thread (cdr thread))))
4484               (gnus-remove-thread-1 thread))))))))
4485
4486 (defun gnus-remove-thread-1 (thread)
4487   "Remove the thread THREAD recursively."
4488   (let ((number (mail-header-number (pop thread)))
4489         d)
4490     (setq thread (reverse thread))
4491     (while thread
4492       (gnus-remove-thread-1 (pop thread)))
4493     (when (setq d (gnus-data-find number))
4494       (goto-char (gnus-data-pos d))
4495       (gnus-summary-show-thread)
4496       (gnus-data-remove
4497        number
4498        (- (point-at-bol)
4499           (prog1
4500               (1+ (point-at-eol))
4501             (gnus-delete-line)))))))
4502
4503 (defun gnus-sort-threads-1 (threads func)
4504   (sort (mapcar (lambda (thread)
4505                   (cons (car thread)
4506                         (and (cdr thread)
4507                              (gnus-sort-threads-1 (cdr thread) func))))
4508                 threads) func))
4509
4510 (defun gnus-sort-threads (threads)
4511   "Sort THREADS."
4512   (if (not gnus-thread-sort-functions)
4513       threads
4514     (gnus-message 8 "Sorting threads...")
4515     (let ((max-lisp-eval-depth 5000))
4516       (prog1 (gnus-sort-threads-1
4517          threads
4518          (gnus-make-sort-function gnus-thread-sort-functions))
4519         (gnus-message 8 "Sorting threads...done")))))
4520
4521 (defun gnus-sort-articles (articles)
4522   "Sort ARTICLES."
4523   (when gnus-article-sort-functions
4524     (gnus-message 7 "Sorting articles...")
4525     (prog1
4526         (setq gnus-newsgroup-headers
4527               (sort articles (gnus-make-sort-function
4528                               gnus-article-sort-functions)))
4529       (gnus-message 7 "Sorting articles...done"))))
4530
4531 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4532 (defmacro gnus-thread-header (thread)
4533   "Return header of first article in THREAD.
4534 Note that THREAD must never, ever be anything else than a variable -
4535 using some other form will lead to serious barfage."
4536   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4537   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4538   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4539         (vector thread) 2))
4540
4541 (defsubst gnus-article-sort-by-number (h1 h2)
4542   "Sort articles by article number."
4543   (< (mail-header-number h1)
4544      (mail-header-number h2)))
4545
4546 (defun gnus-thread-sort-by-number (h1 h2)
4547   "Sort threads by root article number."
4548   (gnus-article-sort-by-number
4549    (gnus-thread-header h1) (gnus-thread-header h2)))
4550
4551 (defsubst gnus-article-sort-by-random (h1 h2)
4552   "Sort articles by article number."
4553   (zerop (random 2)))
4554
4555 (defun gnus-thread-sort-by-random (h1 h2)
4556   "Sort threads by root article number."
4557   (gnus-article-sort-by-random
4558    (gnus-thread-header h1) (gnus-thread-header h2)))
4559
4560 (defsubst gnus-article-sort-by-lines (h1 h2)
4561   "Sort articles by article Lines header."
4562   (< (mail-header-lines h1)
4563      (mail-header-lines h2)))
4564
4565 (defun gnus-thread-sort-by-lines (h1 h2)
4566   "Sort threads by root article Lines header."
4567   (gnus-article-sort-by-lines
4568    (gnus-thread-header h1) (gnus-thread-header h2)))
4569
4570 (defsubst gnus-article-sort-by-chars (h1 h2)
4571   "Sort articles by octet length."
4572   (< (mail-header-chars h1)
4573      (mail-header-chars h2)))
4574
4575 (defun gnus-thread-sort-by-chars (h1 h2)
4576   "Sort threads by root article octet length."
4577   (gnus-article-sort-by-chars
4578    (gnus-thread-header h1) (gnus-thread-header h2)))
4579
4580 (defsubst gnus-article-sort-by-author (h1 h2)
4581   "Sort articles by root author."
4582   (string-lessp
4583    (let ((extract (funcall
4584                    gnus-extract-address-components
4585                    (mail-header-from h1))))
4586      (or (car extract) (cadr extract) ""))
4587    (let ((extract (funcall
4588                    gnus-extract-address-components
4589                    (mail-header-from h2))))
4590      (or (car extract) (cadr extract) ""))))
4591
4592 (defun gnus-thread-sort-by-author (h1 h2)
4593   "Sort threads by root author."
4594   (gnus-article-sort-by-author
4595    (gnus-thread-header h1)  (gnus-thread-header h2)))
4596
4597 (defsubst gnus-article-sort-by-recipient (h1 h2)
4598   "Sort articles by recipient."
4599   (string-lessp
4600    (let ((extract (funcall
4601                    gnus-extract-address-components
4602                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4603      (or (car extract) (cadr extract)))
4604    (let ((extract (funcall
4605                    gnus-extract-address-components
4606                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4607      (or (car extract) (cadr extract)))))
4608
4609 (defun gnus-thread-sort-by-recipient (h1 h2)
4610   "Sort threads by root recipient."
4611   (gnus-article-sort-by-recipient
4612    (gnus-thread-header h1) (gnus-thread-header h2)))
4613
4614 (defsubst gnus-article-sort-by-subject (h1 h2)
4615   "Sort articles by root subject."
4616   (string-lessp
4617    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4618    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4619
4620 (defun gnus-thread-sort-by-subject (h1 h2)
4621   "Sort threads by root subject."
4622   (gnus-article-sort-by-subject
4623    (gnus-thread-header h1) (gnus-thread-header h2)))
4624
4625 (defsubst gnus-article-sort-by-date (h1 h2)
4626   "Sort articles by root article date."
4627   (time-less-p
4628    (gnus-date-get-time (mail-header-date h1))
4629    (gnus-date-get-time (mail-header-date h2))))
4630
4631 (defun gnus-thread-sort-by-date (h1 h2)
4632   "Sort threads by root article date."
4633   (gnus-article-sort-by-date
4634    (gnus-thread-header h1) (gnus-thread-header h2)))
4635
4636 (defsubst gnus-article-sort-by-score (h1 h2)
4637   "Sort articles by root article score.
4638 Unscored articles will be counted as having a score of zero."
4639   (> (or (cdr (assq (mail-header-number h1)
4640                     gnus-newsgroup-scored))
4641          gnus-summary-default-score 0)
4642      (or (cdr (assq (mail-header-number h2)
4643                     gnus-newsgroup-scored))
4644          gnus-summary-default-score 0)))
4645
4646 (defun gnus-thread-sort-by-score (h1 h2)
4647   "Sort threads by root article score."
4648   (gnus-article-sort-by-score
4649    (gnus-thread-header h1) (gnus-thread-header h2)))
4650
4651 (defun gnus-thread-sort-by-total-score (h1 h2)
4652   "Sort threads by the sum of all scores in the thread.
4653 Unscored articles will be counted as having a score of zero."
4654   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4655
4656 (defun gnus-thread-total-score (thread)
4657   ;; This function find the total score of THREAD.
4658   (cond
4659    ((null thread)
4660     0)
4661    ((consp thread)
4662     (if (stringp (car thread))
4663         (apply gnus-thread-score-function 0
4664                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4665       (gnus-thread-total-score-1 thread)))
4666    (t
4667     (gnus-thread-total-score-1 (list thread)))))
4668
4669 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4670   "Sort threads such that the thread with the most recently arrived article comes first."
4671   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4672
4673 (defun gnus-thread-highest-number (thread)
4674   "Return the highest article number in THREAD."
4675   (apply 'max (mapcar (lambda (header)
4676                         (mail-header-number header))
4677                       (message-flatten-list thread))))
4678
4679 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4680   "Sort threads such that the thread with the most recently dated article comes first."
4681   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4682
4683 (defun gnus-thread-latest-date (thread)
4684   "Return the highest article date in THREAD."
4685   (let ((previous-time 0))
4686     (apply 'max
4687            (mapcar
4688             (lambda (header)
4689               (setq previous-time
4690                     (condition-case ()
4691                         (time-to-seconds (mail-header-parse-date
4692                                           (mail-header-date header)))
4693                       (error previous-time))))
4694             (sort
4695              (message-flatten-list thread)
4696              (lambda (h1 h2)
4697                (< (mail-header-number h1)
4698                   (mail-header-number h2))))))))
4699
4700 (defun gnus-thread-total-score-1 (root)
4701   ;; This function find the total score of the thread below ROOT.
4702   (setq root (car root))
4703   (apply gnus-thread-score-function
4704          (or (append
4705               (mapcar 'gnus-thread-total-score
4706                       (cdr (gnus-id-to-thread (mail-header-id root))))
4707               (when (> (mail-header-number root) 0)
4708                 (list (or (cdr (assq (mail-header-number root)
4709                                      gnus-newsgroup-scored))
4710                           gnus-summary-default-score 0))))
4711              (list gnus-summary-default-score)
4712              '(0))))
4713
4714 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4715 (defvar gnus-tmp-prev-subject nil)
4716 (defvar gnus-tmp-false-parent nil)
4717 (defvar gnus-tmp-root-expunged nil)
4718 (defvar gnus-tmp-dummy-line nil)
4719
4720 (eval-when-compile (defvar gnus-tmp-header))
4721 (defun gnus-extra-header (type &optional header)
4722   "Return the extra header of TYPE."
4723   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4724       ""))
4725
4726 (defvar gnus-tmp-thread-tree-header-string "")
4727
4728 (defcustom gnus-sum-thread-tree-root "> "
4729   "With %B spec, used for the root of a thread.
4730 If nil, use subject instead."
4731   :version "22.1"
4732   :type '(radio (const :format "%v  " nil) string)
4733   :group 'gnus-thread)
4734
4735 (defcustom gnus-sum-thread-tree-false-root "> "
4736   "With %B spec, used for a false root of a thread.
4737 If nil, use subject instead."
4738   :version "22.1"
4739   :type '(radio (const :format "%v  " nil) string)
4740   :group 'gnus-thread)
4741
4742 (defcustom gnus-sum-thread-tree-single-indent ""
4743   "With %B spec, used for a thread with just one message.
4744 If nil, use subject instead."
4745   :version "22.1"
4746   :type '(radio (const :format "%v  " nil) string)
4747   :group 'gnus-thread)
4748
4749 (defcustom gnus-sum-thread-tree-vertical "| "
4750   "With %B spec, used for drawing a vertical line."
4751   :version "22.1"
4752   :type 'string
4753   :group 'gnus-thread)
4754
4755 (defcustom gnus-sum-thread-tree-indent "  "
4756   "With %B spec, used for indenting."
4757   :version "22.1"
4758   :type 'string
4759   :group 'gnus-thread)
4760
4761 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4762   "With %B spec, used for a leaf with brothers."
4763   :version "22.1"
4764   :type 'string
4765   :group 'gnus-thread)
4766
4767 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4768   "With %B spec, used for a leaf without brothers."
4769   :version "22.1"
4770   :type 'string
4771   :group 'gnus-thread)
4772
4773 (defun gnus-summary-prepare-threads (threads)
4774   "Prepare summary buffer from THREADS and indentation LEVEL.
4775 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4776 or a straight list of headers."
4777   (gnus-message 7 "Generating summary...")
4778
4779   (setq gnus-newsgroup-threads threads)
4780   (beginning-of-line)
4781
4782   (let ((gnus-tmp-level 0)
4783         (default-score (or gnus-summary-default-score 0))
4784         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4785         (building-line-count gnus-summary-display-while-building)
4786         (building-count (integerp gnus-summary-display-while-building))
4787         thread number subject stack state gnus-tmp-gathered beg-match
4788         new-roots gnus-tmp-new-adopts thread-end simp-subject
4789         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4790         gnus-tmp-replied gnus-tmp-subject-or-nil
4791         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4792         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4793         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4794         tree-stack)
4795
4796     (setq gnus-tmp-prev-subject nil
4797           gnus-tmp-thread-tree-header-string "")
4798
4799     (if (vectorp (car threads))
4800         ;; If this is a straight (sic) list of headers, then a
4801         ;; threaded summary display isn't required, so we just create
4802         ;; an unthreaded one.
4803         (gnus-summary-prepare-unthreaded threads)
4804
4805       ;; Do the threaded display.
4806
4807       (if gnus-summary-display-while-building
4808           (switch-to-buffer (buffer-name)))
4809       (while (or threads stack gnus-tmp-new-adopts new-roots)
4810
4811         (if (and (= gnus-tmp-level 0)
4812                  (or (not stack)
4813                      (= (caar stack) 0))
4814                  (not gnus-tmp-false-parent)
4815                  (or gnus-tmp-new-adopts new-roots))
4816             (if gnus-tmp-new-adopts
4817                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4818                       thread (list (car gnus-tmp-new-adopts))
4819                       gnus-tmp-header (caar thread)
4820                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4821               (when new-roots
4822                 (setq thread (list (car new-roots))
4823                       gnus-tmp-header (caar thread)
4824                       new-roots (cdr new-roots))))
4825
4826           (if threads
4827               ;; If there are some threads, we do them before the
4828               ;; threads on the stack.
4829               (setq thread threads
4830                     gnus-tmp-header (caar thread))
4831             ;; There were no current threads, so we pop something off
4832             ;; the stack.
4833             (setq state (car stack)
4834                   gnus-tmp-level (car state)
4835                   tree-stack (cadr state)
4836                   thread (caddr state)
4837                   stack (cdr stack)
4838                   gnus-tmp-header (caar thread))))
4839
4840         (setq gnus-tmp-false-parent nil)
4841         (setq gnus-tmp-root-expunged nil)
4842         (setq thread-end nil)
4843
4844         (if (stringp gnus-tmp-header)
4845             ;; The header is a dummy root.
4846             (cond
4847              ((eq gnus-summary-make-false-root 'adopt)
4848               ;; We let the first article adopt the rest.
4849               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4850                                                (cddar thread)))
4851               (setq gnus-tmp-gathered
4852                     (nconc (mapcar
4853                             (lambda (h) (mail-header-number (car h)))
4854                             (cddar thread))
4855                            gnus-tmp-gathered))
4856               (setq thread (cons (list (caar thread)
4857                                        (cadar thread))
4858                                  (cdr thread)))
4859               (setq gnus-tmp-level -1
4860                     gnus-tmp-false-parent t))
4861              ((eq gnus-summary-make-false-root 'empty)
4862               ;; We print adopted articles with empty subject fields.
4863               (setq gnus-tmp-gathered
4864                     (nconc (mapcar
4865                             (lambda (h) (mail-header-number (car h)))
4866                             (cddar thread))
4867                            gnus-tmp-gathered))
4868               (setq gnus-tmp-level -1))
4869              ((eq gnus-summary-make-false-root 'dummy)
4870               ;; We remember that we probably want to output a dummy
4871               ;; root.
4872               (setq gnus-tmp-dummy-line gnus-tmp-header)
4873               (setq gnus-tmp-prev-subject gnus-tmp-header))
4874              (t
4875               ;; We do not make a root for the gathered
4876               ;; sub-threads at all.
4877               (setq gnus-tmp-level -1)))
4878
4879           (setq number (mail-header-number gnus-tmp-header)
4880                 subject (mail-header-subject gnus-tmp-header)
4881                 simp-subject (gnus-simplify-subject-fully subject))
4882
4883           (cond
4884            ;; If the thread has changed subject, we might want to make
4885            ;; this subthread into a root.
4886            ((and (null gnus-thread-ignore-subject)
4887                  (not (zerop gnus-tmp-level))
4888                  gnus-tmp-prev-subject
4889                  (not (string= gnus-tmp-prev-subject simp-subject)))
4890             (setq new-roots (nconc new-roots (list (car thread)))
4891                   thread-end t
4892                   gnus-tmp-header nil))
4893            ;; If the article lies outside the current limit,
4894            ;; then we do not display it.
4895            ((not (memq number gnus-newsgroup-limit))
4896             (setq gnus-tmp-gathered
4897                   (nconc (mapcar
4898                           (lambda (h) (mail-header-number (car h)))
4899                           (cdar thread))
4900                          gnus-tmp-gathered))
4901             (setq gnus-tmp-new-adopts (if (cdar thread)
4902                                           (append gnus-tmp-new-adopts
4903                                                   (cdar thread))
4904                                         gnus-tmp-new-adopts)
4905                   thread-end t
4906                   gnus-tmp-header nil)
4907             (when (zerop gnus-tmp-level)
4908               (setq gnus-tmp-root-expunged t)))
4909            ;; Perhaps this article is to be marked as read?
4910            ((and gnus-summary-mark-below
4911                  (< (or (cdr (assq number gnus-newsgroup-scored))
4912                         default-score)
4913                     gnus-summary-mark-below)
4914                  ;; Don't touch sparse articles.
4915                  (not (gnus-summary-article-sparse-p number))
4916                  (not (gnus-summary-article-ancient-p number)))
4917             (setq gnus-newsgroup-unreads
4918                   (delq number gnus-newsgroup-unreads))
4919             (if gnus-newsgroup-auto-expire
4920                 (setq gnus-newsgroup-expirable
4921                       (gnus-add-to-sorted-list
4922                        gnus-newsgroup-expirable number))
4923               (push (cons number gnus-low-score-mark)
4924                     gnus-newsgroup-reads))))
4925
4926           (when gnus-tmp-header
4927             ;; We may have an old dummy line to output before this
4928             ;; article.
4929             (when (and gnus-tmp-dummy-line
4930                        (gnus-subject-equal
4931                         gnus-tmp-dummy-line
4932                         (mail-header-subject gnus-tmp-header)))
4933               (gnus-summary-insert-dummy-line
4934                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4935               (setq gnus-tmp-dummy-line nil))
4936
4937             ;; Compute the mark.
4938             (setq gnus-tmp-unread (gnus-article-mark number))
4939
4940             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4941                                   gnus-tmp-header gnus-tmp-level)
4942                   gnus-newsgroup-data)
4943
4944             ;; Actually insert the line.
4945             (setq
4946              gnus-tmp-subject-or-nil
4947              (cond
4948               ((and gnus-thread-ignore-subject
4949                     gnus-tmp-prev-subject
4950                     (not (string= gnus-tmp-prev-subject simp-subject)))
4951                subject)
4952               ((zerop gnus-tmp-level)
4953                (if (and (eq gnus-summary-make-false-root 'empty)
4954                         (memq number gnus-tmp-gathered)
4955                         gnus-tmp-prev-subject
4956                         (string= gnus-tmp-prev-subject simp-subject))
4957                    gnus-summary-same-subject
4958                  subject))
4959               (t gnus-summary-same-subject)))
4960             (if (and (eq gnus-summary-make-false-root 'adopt)
4961                      (= gnus-tmp-level 1)
4962                      (memq number gnus-tmp-gathered))
4963                 (setq gnus-tmp-opening-bracket ?\<
4964                       gnus-tmp-closing-bracket ?\>)
4965               (setq gnus-tmp-opening-bracket ?\[
4966                     gnus-tmp-closing-bracket ?\]))
4967             (setq
4968              gnus-tmp-indentation
4969              (aref gnus-thread-indent-array gnus-tmp-level)
4970              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4971              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4972                                 gnus-summary-default-score 0)
4973              gnus-tmp-score-char
4974              (if (or (null gnus-summary-default-score)
4975                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4976                          gnus-summary-zcore-fuzz))
4977                  ?                      ;Whitespace
4978                (if (< gnus-tmp-score gnus-summary-default-score)
4979                    gnus-score-below-mark gnus-score-over-mark))
4980              gnus-tmp-replied
4981              (cond ((memq number gnus-newsgroup-processable)
4982                     gnus-process-mark)
4983                    ((memq number gnus-newsgroup-cached)
4984                     gnus-cached-mark)
4985                    ((memq number gnus-newsgroup-replied)
4986                     gnus-replied-mark)
4987                    ((memq number gnus-newsgroup-forwarded)
4988                     gnus-forwarded-mark)
4989                    ((memq number gnus-newsgroup-saved)
4990                     gnus-saved-mark)
4991                    ((memq number gnus-newsgroup-recent)
4992                     gnus-recent-mark)
4993                    ((memq number gnus-newsgroup-unseen)
4994                     gnus-unseen-mark)
4995                    (t gnus-no-mark))
4996              gnus-tmp-downloaded
4997              (cond ((memq number gnus-newsgroup-undownloaded)
4998                     gnus-undownloaded-mark)
4999                    (gnus-newsgroup-agentized
5000                     gnus-downloaded-mark)
5001                    (t
5002                     gnus-no-mark))
5003              gnus-tmp-from (mail-header-from gnus-tmp-header)
5004              gnus-tmp-name
5005              (cond
5006               ((string-match "<[^>]+> *$" gnus-tmp-from)
5007                (setq beg-match (match-beginning 0))
5008                (or (and (string-match "^\".+\"" gnus-tmp-from)
5009                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5010                    (substring gnus-tmp-from 0 beg-match)))
5011               ((string-match "(.+)" gnus-tmp-from)
5012                (substring gnus-tmp-from
5013                           (1+ (match-beginning 0)) (1- (match-end 0))))
5014               (t gnus-tmp-from))
5015
5016              ;; Do the %B string
5017              gnus-tmp-thread-tree-header-string
5018              (cond
5019               ((not gnus-show-threads) "")
5020               ((zerop gnus-tmp-level)
5021                (cond ((cdar thread)
5022                       (or gnus-sum-thread-tree-root subject))
5023                      (gnus-tmp-new-adopts
5024                       (or gnus-sum-thread-tree-false-root subject))
5025                      (t
5026                       (or gnus-sum-thread-tree-single-indent subject))))
5027               (t
5028                (concat (apply 'concat
5029                               (mapcar (lambda (item)
5030                                         (if (= item 1)
5031                                             gnus-sum-thread-tree-vertical
5032                                           gnus-sum-thread-tree-indent))
5033                                       (cdr (reverse tree-stack))))
5034                        (if (nth 1 thread)
5035                            gnus-sum-thread-tree-leaf-with-other
5036                          gnus-sum-thread-tree-single-leaf)))))
5037             (when (string= gnus-tmp-name "")
5038               (setq gnus-tmp-name gnus-tmp-from))
5039             (unless (numberp gnus-tmp-lines)
5040               (setq gnus-tmp-lines -1))
5041             (if (= gnus-tmp-lines -1)
5042                 (setq gnus-tmp-lines "?")
5043               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5044               (gnus-put-text-property
5045              (point)
5046              (progn (eval gnus-summary-line-format-spec) (point))
5047                'gnus-number number)
5048             (when gnus-visual-p
5049               (forward-line -1)
5050               (gnus-run-hooks 'gnus-summary-update-hook)
5051               (forward-line 1))
5052
5053             (setq gnus-tmp-prev-subject simp-subject)))
5054
5055         (when (nth 1 thread)
5056           (push (list (max 0 gnus-tmp-level)
5057                       (copy-sequence tree-stack)
5058                       (nthcdr 1 thread))
5059                 stack))
5060         (push (if (nth 1 thread) 1 0) tree-stack)
5061         (incf gnus-tmp-level)
5062         (setq threads (if thread-end nil (cdar thread)))
5063         (if gnus-summary-display-while-building
5064             (if building-count
5065                 (progn
5066                   ;; use a set frequency
5067                   (setq building-line-count (1- building-line-count))
5068                   (when (= building-line-count 0)
5069                     (sit-for 0)
5070                     (setq building-line-count
5071                           gnus-summary-display-while-building)))
5072               ;; always
5073               (sit-for 0)))
5074         (unless threads
5075           (setq gnus-tmp-level 0)))))
5076   (gnus-message 7 "Generating summary...done"))
5077
5078 (defun gnus-summary-prepare-unthreaded (headers)
5079   "Generate an unthreaded summary buffer based on HEADERS."
5080   (let (header number mark)
5081
5082     (beginning-of-line)
5083
5084     (while headers
5085       ;; We may have to root out some bad articles...
5086       (when (memq (setq number (mail-header-number
5087                                 (setq header (pop headers))))
5088                   gnus-newsgroup-limit)
5089         ;; Mark article as read when it has a low score.
5090         (when (and gnus-summary-mark-below
5091                    (< (or (cdr (assq number gnus-newsgroup-scored))
5092                           gnus-summary-default-score 0)
5093                       gnus-summary-mark-below)
5094                    (not (gnus-summary-article-ancient-p number)))
5095           (setq gnus-newsgroup-unreads
5096                 (delq number gnus-newsgroup-unreads))
5097           (if gnus-newsgroup-auto-expire
5098               (push number gnus-newsgroup-expirable)
5099             (push (cons number gnus-low-score-mark)
5100                   gnus-newsgroup-reads)))
5101
5102         (setq mark (gnus-article-mark number))
5103         (push (gnus-data-make number mark (1+ (point)) header 0)
5104               gnus-newsgroup-data)
5105         (gnus-summary-insert-line
5106          header 0 number
5107          (memq number gnus-newsgroup-undownloaded)
5108          mark (memq number gnus-newsgroup-replied)
5109          (memq number gnus-newsgroup-expirable)
5110          (mail-header-subject header) nil
5111          (cdr (assq number gnus-newsgroup-scored))
5112          (memq number gnus-newsgroup-processable))))))
5113
5114 (defun gnus-summary-remove-list-identifiers ()
5115   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5116   (let ((regexp (if (consp gnus-list-identifiers)
5117                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5118                   gnus-list-identifiers))
5119         changed subject)
5120     (when regexp
5121       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5122       (dolist (header gnus-newsgroup-headers)
5123         (setq subject (mail-header-subject header)
5124               changed nil)
5125         (while (string-match regexp subject)
5126           (setq subject
5127                 (concat (substring subject 0 (match-beginning 1))
5128                         (substring subject (match-end 0)))
5129                 changed t))
5130         (when changed
5131           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5132             (setq subject
5133                   (concat (substring subject 0 (match-beginning 1))
5134                           (substring subject (match-end 1)))))
5135           (mail-header-set-subject header subject))))))
5136
5137 (defun gnus-fetch-headers (articles)
5138   "Fetch headers of ARTICLES."
5139   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5140     (gnus-message 5 "Fetching headers for %s..." name)
5141     (prog1
5142         (if (eq 'nov
5143                 (setq gnus-headers-retrieved-by
5144                       (gnus-retrieve-headers
5145                        articles gnus-newsgroup-name
5146                        ;; We might want to fetch old headers, but
5147                        ;; not if there is only 1 article.
5148                        (and (or (and
5149                                  (not (eq gnus-fetch-old-headers 'some))
5150                                  (not (numberp gnus-fetch-old-headers)))
5151                                 (> (length articles) 1))
5152                             gnus-fetch-old-headers))))
5153             (gnus-get-newsgroup-headers-xover
5154              articles nil nil gnus-newsgroup-name t)
5155           (gnus-get-newsgroup-headers))
5156       (gnus-message 5 "Fetching headers for %s...done" name))))
5157
5158 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5159   "Select newsgroup GROUP.
5160 If READ-ALL is non-nil, all articles in the group are selected.
5161 If SELECT-ARTICLES, only select those articles from GROUP."
5162   (let* ((entry (gnus-group-entry group))
5163          ;;!!! Dirty hack; should be removed.
5164          (gnus-summary-ignore-duplicates
5165           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5166               t
5167             gnus-summary-ignore-duplicates))
5168          (info (nth 2 entry))
5169          articles fetched-articles cached)
5170
5171     (unless (gnus-check-server
5172              (set (make-local-variable 'gnus-current-select-method)
5173                   (gnus-find-method-for-group group)))
5174       (error "Couldn't open server"))
5175
5176     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5177         (gnus-activate-group group)     ; Or we can activate it...
5178         (progn                          ; Or we bug out.
5179           (when (equal major-mode 'gnus-summary-mode)
5180             (gnus-kill-buffer (current-buffer)))
5181           (error "Couldn't activate group %s: %s"
5182                  group (gnus-status-message group))))
5183
5184     (unless (gnus-request-group group t)
5185       (when (equal major-mode 'gnus-summary-mode)
5186         (gnus-kill-buffer (current-buffer)))
5187       (error "Couldn't request group %s: %s"
5188              group (gnus-status-message group)))
5189
5190     (when gnus-agent
5191       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5192
5193       (setq gnus-summary-use-undownloaded-faces
5194             (gnus-agent-find-parameter
5195              group
5196              'agent-enable-undownloaded-faces)))
5197
5198     (setq gnus-newsgroup-name group
5199           gnus-newsgroup-unselected nil
5200           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5201
5202     (let ((display (gnus-group-find-parameter group 'display)))
5203       (setq gnus-newsgroup-display
5204             (cond
5205              ((not (zerop (or (car-safe read-all) 0)))
5206               ;; The user entered the group with C-u SPC/RET, let's show
5207               ;; all articles.
5208               'gnus-not-ignore)
5209              ((eq display 'all)
5210               'gnus-not-ignore)
5211              ((arrayp display)
5212               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5213              ((numberp display)
5214               ;; The following is probably the "correct" solution, but
5215               ;; it makes Gnus fetch all headers and then limit the
5216               ;; articles (which is slow), so instead we hack the
5217               ;; select-articles parameter instead. -- Simon Josefsson
5218               ;; <jas@kth.se>
5219               ;;
5220               ;; (gnus-byte-compile
5221               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5222               ;;                         display)))))
5223               (setq select-articles
5224                     (gnus-uncompress-range
5225                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5226                              (if (> tmp 0)
5227                                  tmp
5228                                1))
5229                            (cdr (gnus-active group)))))
5230               nil)
5231              (t
5232               nil))))
5233
5234     (gnus-summary-setup-default-charset)
5235
5236     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5237     (when (gnus-virtual-group-p group)
5238       (setq cached gnus-newsgroup-cached))
5239
5240     (setq gnus-newsgroup-unreads
5241           (gnus-sorted-ndifference
5242            (gnus-sorted-ndifference gnus-newsgroup-unreads
5243                                     gnus-newsgroup-marked)
5244            gnus-newsgroup-dormant))
5245
5246     (setq gnus-newsgroup-processable nil)
5247
5248     (gnus-update-read-articles group gnus-newsgroup-unreads)
5249
5250     ;; Adjust and set lists of article marks.
5251     (when info
5252       (gnus-adjust-marked-articles info))
5253     (if (setq articles select-articles)
5254         (setq gnus-newsgroup-unselected
5255               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5256       (setq articles (gnus-articles-to-read group read-all)))
5257
5258     (cond
5259      ((null articles)
5260       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5261       'quit)
5262      ((eq articles 0) nil)
5263      (t
5264       ;; Init the dependencies hash table.
5265       (setq gnus-newsgroup-dependencies
5266             (gnus-make-hashtable (length articles)))
5267       (gnus-set-global-variables)
5268       ;; Retrieve the headers and read them in.
5269
5270       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5271
5272       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5273       (when cached
5274         (setq gnus-newsgroup-cached cached))
5275
5276       ;; Suppress duplicates?
5277       (when gnus-suppress-duplicates
5278         (gnus-dup-suppress-articles))
5279
5280       ;; Set the initial limit.
5281       (setq gnus-newsgroup-limit (copy-sequence articles))
5282       ;; Remove canceled articles from the list of unread articles.
5283       (setq fetched-articles
5284             (mapcar (lambda (headers) (mail-header-number headers))
5285                     gnus-newsgroup-headers))
5286       (setq gnus-newsgroup-articles fetched-articles)
5287       (setq gnus-newsgroup-unreads
5288             (gnus-sorted-nintersection
5289              gnus-newsgroup-unreads fetched-articles))
5290       (gnus-compute-unseen-list)
5291
5292       ;; Removed marked articles that do not exist.
5293       (gnus-update-missing-marks
5294        (gnus-sorted-difference articles fetched-articles))
5295       ;; We might want to build some more threads first.
5296       (when (and gnus-fetch-old-headers
5297                  (eq gnus-headers-retrieved-by 'nov))
5298         (if (eq gnus-fetch-old-headers 'invisible)
5299             (gnus-build-all-threads)
5300           (gnus-build-old-threads)))
5301       ;; Let the Gnus agent mark articles as read.
5302       (when gnus-agent
5303         (gnus-agent-get-undownloaded-list))
5304       ;; Remove list identifiers from subject
5305       (when gnus-list-identifiers
5306         (gnus-summary-remove-list-identifiers))
5307       ;; Check whether auto-expire is to be done in this group.
5308       (setq gnus-newsgroup-auto-expire
5309             (gnus-group-auto-expirable-p group))
5310       ;; Set up the article buffer now, if necessary.
5311       (unless gnus-single-article-buffer
5312         (gnus-article-setup-buffer))
5313       ;; First and last article in this newsgroup.
5314       (when gnus-newsgroup-headers
5315         (setq gnus-newsgroup-begin
5316               (mail-header-number (car gnus-newsgroup-headers))
5317               gnus-newsgroup-end
5318               (mail-header-number
5319                (gnus-last-element gnus-newsgroup-headers))))
5320       ;; GROUP is successfully selected.
5321       (or gnus-newsgroup-headers t)))))
5322
5323 (defun gnus-compute-unseen-list ()
5324   ;; The `seen' marks are treated specially.
5325   (if (not gnus-newsgroup-seen)
5326       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5327     (setq gnus-newsgroup-unseen
5328           (gnus-inverse-list-range-intersection
5329            gnus-newsgroup-articles gnus-newsgroup-seen))))
5330
5331 (defun gnus-summary-display-make-predicate (display)
5332   (require 'gnus-agent)
5333   (when (= (length display) 1)
5334     (setq display (car display)))
5335   (unless gnus-summary-display-cache
5336     (dolist (elem (append '((unread . unread)
5337                             (read . read)
5338                             (unseen . unseen))
5339                           gnus-article-mark-lists))
5340       (push (cons (cdr elem)
5341                   (gnus-byte-compile
5342                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5343             gnus-summary-display-cache)))
5344   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5345         (gnus-category-predicate-cache gnus-summary-display-cache))
5346     (gnus-get-predicate display)))
5347
5348 ;; Uses the dynamically bound `number' variable.
5349 (eval-when-compile
5350   (defvar number))
5351 (defun gnus-article-marked-p (type &optional article)
5352   (let ((article (or article number)))
5353     (cond
5354      ((eq type 'tick)
5355       (memq article gnus-newsgroup-marked))
5356      ((eq type 'spam)
5357       (memq article gnus-newsgroup-spam-marked))
5358      ((eq type 'unsend)
5359       (memq article gnus-newsgroup-unsendable))
5360      ((eq type 'undownload)
5361       (memq article gnus-newsgroup-undownloaded))
5362      ((eq type 'download)
5363       (memq article gnus-newsgroup-downloadable))
5364      ((eq type 'unread)
5365       (memq article gnus-newsgroup-unreads))
5366      ((eq type 'read)
5367       (memq article gnus-newsgroup-reads))
5368      ((eq type 'dormant)
5369       (memq article gnus-newsgroup-dormant) )
5370      ((eq type 'expire)
5371       (memq article gnus-newsgroup-expirable))
5372      ((eq type 'reply)
5373       (memq article gnus-newsgroup-replied))
5374      ((eq type 'killed)
5375       (memq article gnus-newsgroup-killed))
5376      ((eq type 'bookmark)
5377       (assq article gnus-newsgroup-bookmarks))
5378      ((eq type 'score)
5379       (assq article gnus-newsgroup-scored))
5380      ((eq type 'save)
5381       (memq article gnus-newsgroup-saved))
5382      ((eq type 'cache)
5383       (memq article gnus-newsgroup-cached))
5384      ((eq type 'forward)
5385       (memq article gnus-newsgroup-forwarded))
5386      ((eq type 'seen)
5387       (not (memq article gnus-newsgroup-unseen)))
5388      ((eq type 'recent)
5389       (memq article gnus-newsgroup-recent))
5390      (t t))))
5391
5392 (defun gnus-articles-to-read (group &optional read-all)
5393   "Find out what articles the user wants to read."
5394   (let* ((articles
5395           ;; Select all articles if `read-all' is non-nil, or if there
5396           ;; are no unread articles.
5397           (if (or read-all
5398                   (and (zerop (length gnus-newsgroup-marked))
5399                        (zerop (length gnus-newsgroup-unreads)))
5400                   ;; Fetch all if the predicate is non-nil.
5401                   gnus-newsgroup-display)
5402               ;; We want to select the headers for all the articles in
5403               ;; the group, so we select either all the active
5404               ;; articles in the group, or (if that's nil), the
5405               ;; articles in the cache.
5406               (or
5407                (gnus-uncompress-range (gnus-active group))
5408                (gnus-cache-articles-in-group group))
5409             ;; Select only the "normal" subset of articles.
5410             (gnus-sorted-nunion
5411              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5412              gnus-newsgroup-unreads)))
5413          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5414          (scored (length scored-list))
5415          (number (length articles))
5416          (marked (+ (length gnus-newsgroup-marked)
5417                     (length gnus-newsgroup-dormant)))
5418          (select
5419           (cond
5420            ((numberp read-all)
5421             read-all)
5422            ((numberp gnus-newsgroup-display)
5423             gnus-newsgroup-display)
5424            (t
5425             (condition-case ()
5426                 (cond
5427                  ((and (or (<= scored marked) (= scored number))
5428                        (numberp gnus-large-newsgroup)
5429                        (> number gnus-large-newsgroup))
5430                   (let* ((cursor-in-echo-area nil)
5431                          (initial (gnus-parameter-large-newsgroup-initial
5432                                    gnus-newsgroup-name))
5433                          (input
5434                           (read-string
5435                            (format
5436                             "How many articles from %s (%s %d): "
5437                             (gnus-limit-string
5438                              (gnus-group-decoded-name gnus-newsgroup-name)
5439                              35)
5440                             (if initial "max" "default")
5441                             number)
5442                            (if initial
5443                                (cons (number-to-string initial)
5444                                      0)))))
5445                     (if (string-match "^[ \t]*$" input) number input)))
5446                  ((and (> scored marked) (< scored number)
5447                        (> (- scored number) 20))
5448                   (let ((input
5449                          (read-string
5450                           (format "%s %s (%d scored, %d total): "
5451                                   "How many articles from"
5452                                   (gnus-group-decoded-name group)
5453                                   scored number))))
5454                     (if (string-match "^[ \t]*$" input)
5455                         number input)))
5456                  (t number))
5457               (quit
5458                (message "Quit getting the articles to read")
5459                nil))))))
5460     (setq select (if (stringp select) (string-to-number select) select))
5461     (if (or (null select) (zerop select))
5462         select
5463       (if (and (not (zerop scored)) (<= (abs select) scored))
5464           (progn
5465             (setq articles (sort scored-list '<))
5466             (setq number (length articles)))
5467         (setq articles (copy-sequence articles)))
5468
5469       (when (< (abs select) number)
5470         (if (< select 0)
5471             ;; Select the N oldest articles.
5472             (setcdr (nthcdr (1- (abs select)) articles) nil)
5473           ;; Select the N most recent articles.
5474           (setq articles (nthcdr (- number select) articles))))
5475       (setq gnus-newsgroup-unselected
5476             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5477       (when gnus-alter-articles-to-read-function
5478         (setq articles
5479               (sort
5480                (funcall gnus-alter-articles-to-read-function
5481                         gnus-newsgroup-name articles)
5482                '<)))
5483       articles)))
5484
5485 (defun gnus-killed-articles (killed articles)
5486   (let (out)
5487     (while articles
5488       (when (inline (gnus-member-of-range (car articles) killed))
5489         (push (car articles) out))
5490       (setq articles (cdr articles)))
5491     out))
5492
5493 (defun gnus-uncompress-marks (marks)
5494   "Uncompress the mark ranges in MARKS."
5495   (let ((uncompressed '(score bookmark))
5496         out)
5497     (while marks
5498       (if (memq (caar marks) uncompressed)
5499           (push (car marks) out)
5500         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5501       (setq marks (cdr marks)))
5502     out))
5503
5504 (defun gnus-article-mark-to-type (mark)
5505   "Return the type of MARK."
5506   (or (cadr (assq mark gnus-article-special-mark-lists))
5507       'list))
5508
5509 (defun gnus-article-unpropagatable-p (mark)
5510   "Return whether MARK should be propagated to back end."
5511   (memq mark gnus-article-unpropagated-mark-lists))
5512
5513 (defun gnus-adjust-marked-articles (info)
5514   "Set all article lists and remove all marks that are no longer valid."
5515   (let* ((marked-lists (gnus-info-marks info))
5516          (active (gnus-active (gnus-info-group info)))
5517          (min (car active))
5518          (max (cdr active))
5519          (types gnus-article-mark-lists)
5520          marks var articles article mark mark-type
5521          bgn end)
5522
5523     (dolist (marks marked-lists)
5524       (setq mark (car marks)
5525             mark-type (gnus-article-mark-to-type mark)
5526             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5527
5528       ;; We set the variable according to the type of the marks list,
5529       ;; and then adjust the marks to a subset of the active articles.
5530       (cond
5531        ;; Adjust "simple" lists - compressed yet unsorted
5532        ((eq mark-type 'list)
5533         ;; Simultaneously uncompress and clip to active range
5534         ;; See gnus-uncompress-range for a description of possible marks
5535         (let (l lh)
5536           (if (not (cadr marks))
5537               (set var nil)
5538             (setq articles (if (numberp (cddr marks))
5539                                (list (cdr marks))
5540                              (cdr marks))
5541                   lh (cons nil nil)
5542                   l lh)
5543
5544             (while (setq article (pop articles))
5545               (cond ((consp article)
5546                      (setq bgn (max (car article) min)
5547                            end (min (cdr article) max))
5548                      (while (<= bgn end)
5549                        (setq l (setcdr l (cons bgn nil))
5550                              bgn (1+ bgn))))
5551                     ((and (<= min article)
5552                           (>= max article))
5553                      (setq l (setcdr l (cons article nil))))))
5554             (set var (cdr lh)))))
5555        ;; Adjust assocs.
5556        ((eq mark-type 'tuple)
5557         (set var (setq articles (cdr marks)))
5558         (when (not (listp (cdr (symbol-value var))))
5559           (set var (list (symbol-value var))))
5560         (when (not (listp (cdr articles)))
5561           (setq articles (list articles)))
5562         (while articles
5563           (when (or (not (consp (setq article (pop articles))))
5564                     (< (car article) min)
5565                     (> (car article) max))
5566             (set var (delq article (symbol-value var))))))
5567        ;; Adjust ranges (sloppily).
5568        ((eq mark-type 'range)
5569         (cond
5570          ((eq mark 'seen)
5571           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5572           ;; It should be (seen (NUM1 . NUM2)).
5573           (when (numberp (cddr marks))
5574             (setcdr marks (list (cdr marks))))
5575           (setq articles (cdr marks))
5576           (while (and articles
5577                       (or (and (consp (car articles))
5578                                (> min (cdar articles)))
5579                           (and (numberp (car articles))
5580                                (> min (car articles)))))
5581             (pop articles))
5582           (set var articles))))))))
5583
5584 (defun gnus-update-missing-marks (missing)
5585   "Go through the list of MISSING articles and remove them from the mark lists."
5586   (when missing
5587     (let (var m)
5588       ;; Go through all types.
5589       (dolist (elem gnus-article-mark-lists)
5590         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5591           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5592           (when (symbol-value var)
5593             ;; This list has articles.  So we delete all missing
5594             ;; articles from it.
5595             (setq m missing)
5596             (while m
5597               (set var (delq (pop m) (symbol-value var))))))))))
5598
5599 (defun gnus-update-marks ()
5600   "Enter the various lists of marked articles into the newsgroup info list."
5601   (let ((types gnus-article-mark-lists)
5602         (info (gnus-get-info gnus-newsgroup-name))
5603         type list newmarked symbol delta-marks)
5604     (when info
5605       ;; Add all marks lists to the list of marks lists.
5606       (while (setq type (pop types))
5607         (setq list (symbol-value
5608                     (setq symbol
5609                           (intern (format "gnus-newsgroup-%s" (car type))))))
5610
5611         (when list
5612           ;; Get rid of the entries of the articles that have the
5613           ;; default score.
5614           (when (and (eq (cdr type) 'score)
5615                      gnus-save-score
5616                      list)
5617             (let* ((arts list)
5618                    (prev (cons nil list))
5619                    (all prev))
5620               (while arts
5621                 (if (or (not (consp (car arts)))
5622                         (= (cdar arts) gnus-summary-default-score))
5623                     (setcdr prev (cdr arts))
5624                   (setq prev arts))
5625                 (setq arts (cdr arts)))
5626               (setq list (cdr all)))))
5627
5628         (when (eq (cdr type) 'seen)
5629           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5630
5631         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5632           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5633
5634         (when (and (gnus-check-backend-function
5635                     'request-set-mark gnus-newsgroup-name)
5636                    (not (gnus-article-unpropagatable-p (cdr type))))
5637           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5638                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5639                  (add (gnus-remove-from-range
5640                        (gnus-copy-sequence list) old)))
5641             (when add
5642               (push (list add 'add (list (cdr type))) delta-marks))
5643             (when del
5644               (push (list del 'del (list (cdr type))) delta-marks))))
5645
5646         (when list
5647           (push (cons (cdr type) list) newmarked)))
5648
5649       (when delta-marks
5650         (unless (gnus-check-group gnus-newsgroup-name)
5651           (error "Can't open server for %s" gnus-newsgroup-name))
5652         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5653
5654       ;; Enter these new marks into the info of the group.
5655       (if (nthcdr 3 info)
5656           (setcar (nthcdr 3 info) newmarked)
5657         ;; Add the marks lists to the end of the info.
5658         (when newmarked
5659           (setcdr (nthcdr 2 info) (list newmarked))))
5660
5661       ;; Cut off the end of the info if there's nothing else there.
5662       (let ((i 5))
5663         (while (and (> i 2)
5664                     (not (nth i info)))
5665           (when (nthcdr (decf i) info)
5666             (setcdr (nthcdr i info) nil)))))))
5667
5668 (defun gnus-set-mode-line (where)
5669   "Set the mode line of the article or summary buffers.
5670 If WHERE is `summary', the summary mode line format will be used."
5671   ;; Is this mode line one we keep updated?
5672   (when (and (memq where gnus-updated-mode-lines)
5673              (symbol-value
5674               (intern (format "gnus-%s-mode-line-format-spec" where))))
5675     (let (mode-string)
5676       (save-excursion
5677         ;; We evaluate this in the summary buffer since these
5678         ;; variables are buffer-local to that buffer.
5679         (set-buffer gnus-summary-buffer)
5680        ;; We bind all these variables that are used in the `eval' form
5681         ;; below.
5682         (let* ((mformat (symbol-value
5683                          (intern
5684                           (format "gnus-%s-mode-line-format-spec" where))))
5685                (gnus-tmp-group-name (gnus-group-decoded-name
5686                                      gnus-newsgroup-name))
5687                (gnus-tmp-article-number (or gnus-current-article 0))
5688                (gnus-tmp-unread gnus-newsgroup-unreads)
5689                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5690                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5691                (gnus-tmp-unread-and-unselected
5692                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5693                             (zerop gnus-tmp-unselected))
5694                        "")
5695                       ((zerop gnus-tmp-unselected)
5696                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5697                       (t (format "{%d(+%d) more}"
5698                                  gnus-tmp-unread-and-unticked
5699                                  gnus-tmp-unselected))))
5700                (gnus-tmp-subject
5701                 (if (and gnus-current-headers
5702                          (vectorp gnus-current-headers))
5703                     (gnus-mode-string-quote
5704                      (mail-header-subject gnus-current-headers))
5705                   ""))
5706                bufname-length max-len
5707                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5708           (setq mode-string (eval mformat))
5709           (setq bufname-length (if (string-match "%b" mode-string)
5710                                    (- (length
5711                                        (buffer-name
5712                                         (if (eq where 'summary)
5713                                             nil
5714                                           (get-buffer gnus-article-buffer))))
5715                                       2)
5716                                  0))
5717           (setq max-len (max 4 (if gnus-mode-non-string-length
5718                                    (- (window-width)
5719                                       gnus-mode-non-string-length
5720                                       bufname-length)
5721                                  (length mode-string))))
5722           ;; We might have to chop a bit of the string off...
5723           (when (> (length mode-string) max-len)
5724             (setq mode-string
5725                   (concat (truncate-string-to-width mode-string (- max-len 3))
5726                           "...")))
5727           ;; Pad the mode string a bit.
5728           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5729       ;; Update the mode line.
5730       (setq mode-line-buffer-identification
5731             (gnus-mode-line-buffer-identification (list mode-string)))
5732       (set-buffer-modified-p t))))
5733
5734 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5735   "Go through the HEADERS list and add all Xrefs to a hash table.
5736 The resulting hash table is returned, or nil if no Xrefs were found."
5737   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5738          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5739          (xref-hashtb (gnus-make-hashtable))
5740          start group entry number xrefs header)
5741     (while headers
5742       (setq header (pop headers))
5743       (when (and (setq xrefs (mail-header-xref header))
5744                  (not (memq (setq number (mail-header-number header))
5745                             unreads)))
5746         (setq start 0)
5747         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5748           (setq start (match-end 0))
5749           (setq group (if prefix
5750                           (concat prefix (substring xrefs (match-beginning 1)
5751                                                     (match-end 1)))
5752                         (substring xrefs (match-beginning 1) (match-end 1))))
5753           (setq number
5754                 (string-to-number (substring xrefs (match-beginning 2)
5755                                           (match-end 2))))
5756           (if (setq entry (gnus-gethash group xref-hashtb))
5757               (setcdr entry (cons number (cdr entry)))
5758             (gnus-sethash group (cons number nil) xref-hashtb)))))
5759     (and start xref-hashtb)))
5760
5761 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5762   "Look through all the headers and mark the Xrefs as read."
5763   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5764         name info xref-hashtb idlist method nth4)
5765     (save-excursion
5766       (set-buffer gnus-group-buffer)
5767       (when (setq xref-hashtb
5768                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5769         (mapatoms
5770          (lambda (group)
5771            (unless (string= from-newsgroup (setq name (symbol-name group)))
5772              (setq idlist (symbol-value group))
5773              ;; Dead groups are not updated.
5774              (and (prog1
5775                       (setq info (gnus-get-info name))
5776                     (when (stringp (setq nth4 (gnus-info-method info)))
5777                       (setq nth4 (gnus-server-to-method nth4))))
5778                   ;; Only do the xrefs if the group has the same
5779                   ;; select method as the group we have just read.
5780                   (or (gnus-methods-equal-p
5781                        nth4 (gnus-find-method-for-group from-newsgroup))
5782                       virtual
5783                       (equal nth4 (setq method (gnus-find-method-for-group
5784                                                 from-newsgroup)))
5785                       (and (equal (car nth4) (car method))
5786                            (equal (nth 1 nth4) (nth 1 method))))
5787                   gnus-use-cross-reference
5788                   (or (not (eq gnus-use-cross-reference t))
5789                       virtual
5790                       ;; Only do cross-references on subscribed
5791                       ;; groups, if that is what is wanted.
5792                       (<= (gnus-info-level info) gnus-level-subscribed))
5793                   (gnus-group-make-articles-read name idlist))))
5794          xref-hashtb)))))
5795
5796 (defun gnus-compute-read-articles (group articles)
5797   (let* ((entry (gnus-group-entry group))
5798          (info (nth 2 entry))
5799          (active (gnus-active group))
5800          ninfo)
5801     (when entry
5802       ;; First peel off all invalid article numbers.
5803       (when active
5804         (let ((ids articles)
5805               id first)
5806           (while (setq id (pop ids))
5807             (when (and first (> id (cdr active)))
5808               ;; We'll end up in this situation in one particular
5809               ;; obscure situation.  If you re-scan a group and get
5810               ;; a new article that is cross-posted to a different
5811               ;; group that has not been re-scanned, you might get
5812               ;; crossposted article that has a higher number than
5813               ;; Gnus believes possible.  So we re-activate this
5814               ;; group as well.  This might mean doing the
5815               ;; crossposting thingy will *increase* the number
5816               ;; of articles in some groups.  Tsk, tsk.
5817               (setq active (or (gnus-activate-group group) active)))
5818             (when (or (> id (cdr active))
5819                       (< id (car active)))
5820               (setq articles (delq id articles))))))
5821       ;; If the read list is nil, we init it.
5822       (if (and active
5823                (null (gnus-info-read info))
5824                (> (car active) 1))
5825           (setq ninfo (cons 1 (1- (car active))))
5826         (setq ninfo (gnus-info-read info)))
5827       ;; Then we add the read articles to the range.
5828       (gnus-add-to-range
5829        ninfo (setq articles (sort articles '<))))))
5830
5831 (defun gnus-group-make-articles-read (group articles)
5832   "Update the info of GROUP to say that ARTICLES are read."
5833   (let* ((num 0)
5834          (entry (gnus-group-entry group))
5835          (info (nth 2 entry))
5836          (active (gnus-active group))
5837          range)
5838     (when entry
5839       (setq range (gnus-compute-read-articles group articles))
5840       (with-current-buffer gnus-group-buffer
5841         (gnus-undo-register
5842           `(progn
5843              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5844              (gnus-info-set-read ',info ',(gnus-info-read info))
5845              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5846              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5847              (gnus-group-update-group ,group t))))
5848       ;; Add the read articles to the range.
5849       (gnus-info-set-read info range)
5850       (gnus-request-set-mark group (list (list range 'add '(read))))
5851       ;; Then we have to re-compute how many unread
5852       ;; articles there are in this group.
5853       (when active
5854         (cond
5855          ((not range)
5856           (setq num (- (1+ (cdr active)) (car active))))
5857          ((not (listp (cdr range)))
5858           (setq num (- (cdr active) (- (1+ (cdr range))
5859                                        (car range)))))
5860          (t
5861           (while range
5862             (if (numberp (car range))
5863                 (setq num (1+ num))
5864               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5865             (setq range (cdr range)))
5866           (setq num (- (cdr active) num))))
5867         ;; Update the number of unread articles.
5868         (setcar entry num)
5869         ;; Update the group buffer.
5870         (unless (gnus-ephemeral-group-p group)
5871           (gnus-group-update-group group t))))))
5872
5873 (defvar gnus-newsgroup-none-id 0)
5874
5875 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5876   (let ((cur nntp-server-buffer)
5877         (dependencies
5878          (or dependencies
5879              (with-current-buffer gnus-summary-buffer
5880                gnus-newsgroup-dependencies)))
5881         headers id end ref number
5882         (mail-parse-charset gnus-newsgroup-charset)
5883         (mail-parse-ignored-charsets
5884          (save-excursion (condition-case nil
5885                              (set-buffer gnus-summary-buffer)
5886                            (error))
5887                          gnus-newsgroup-ignored-charsets)))
5888     (save-excursion
5889       (set-buffer nntp-server-buffer)
5890       ;; Translate all TAB characters into SPACE characters.
5891       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5892       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5893       (ietf-drums-unfold-fws)
5894       (gnus-run-hooks 'gnus-parse-headers-hook)
5895       (let ((case-fold-search t)
5896             in-reply-to header p lines chars)
5897         (goto-char (point-min))
5898         ;; Search to the beginning of the next header.  Error messages
5899         ;; do not begin with 2 or 3.
5900         (while (re-search-forward "^[23][0-9]+ " nil t)
5901           (setq id nil
5902                 ref nil)
5903           ;; This implementation of this function, with nine
5904           ;; search-forwards instead of the one re-search-forward and
5905           ;; a case (which basically was the old function) is actually
5906           ;; about twice as fast, even though it looks messier.  You
5907           ;; can't have everything, I guess.  Speed and elegance
5908           ;; doesn't always go hand in hand.
5909           (setq
5910            header
5911            (vector
5912             ;; Number.
5913             (prog1
5914                 (setq number (read cur))
5915               (end-of-line)
5916               (setq p (point))
5917               (narrow-to-region (point)
5918                                 (or (and (search-forward "\n.\n" nil t)
5919                                          (- (point) 2))
5920                                     (point))))
5921             ;; Subject.
5922             (progn
5923               (goto-char p)
5924               (if (search-forward "\nsubject:" nil t)
5925                   (funcall gnus-decode-encoded-word-function
5926                            (nnheader-header-value))
5927                 "(none)"))
5928             ;; From.
5929             (progn
5930               (goto-char p)
5931               (if (search-forward "\nfrom:" nil t)
5932                   (funcall gnus-decode-encoded-word-function
5933                            (nnheader-header-value))
5934                 "(nobody)"))
5935             ;; Date.
5936             (progn
5937               (goto-char p)
5938               (if (search-forward "\ndate:" nil t)
5939                   (nnheader-header-value) ""))
5940             ;; Message-ID.
5941             (progn
5942               (goto-char p)
5943               (setq id (if (re-search-forward
5944                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5945                            ;; We do it this way to make sure the Message-ID
5946                            ;; is (somewhat) syntactically valid.
5947                            (buffer-substring (match-beginning 1)
5948                                              (match-end 1))
5949                          ;; If there was no message-id, we just fake one
5950                          ;; to make subsequent routines simpler.
5951                          (nnheader-generate-fake-message-id number))))
5952             ;; References.
5953             (progn
5954               (goto-char p)
5955               (if (search-forward "\nreferences:" nil t)
5956                   (progn
5957                     (setq end (point))
5958                     (prog1
5959                         (nnheader-header-value)
5960                       (setq ref
5961                             (buffer-substring
5962                              (progn
5963                                (end-of-line)
5964                                (search-backward ">" end t)
5965                                (1+ (point)))
5966                              (progn
5967                                (search-backward "<" end t)
5968                                (point))))))
5969                 ;; Get the references from the in-reply-to header if there
5970                 ;; were no references and the in-reply-to header looks
5971                 ;; promising.
5972                 (if (and (search-forward "\nin-reply-to:" nil t)
5973                          (setq in-reply-to (nnheader-header-value))
5974                          (string-match "<[^>]+>" in-reply-to))
5975                     (let (ref2)
5976                       (setq ref (substring in-reply-to (match-beginning 0)
5977                                            (match-end 0)))
5978                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5979                         (setq ref2 (substring in-reply-to (match-beginning 0)
5980                                               (match-end 0)))
5981                         (when (> (length ref2) (length ref))
5982                           (setq ref ref2)))
5983                       ref)
5984                   (setq ref nil))))
5985             ;; Chars.
5986             (progn
5987               (goto-char p)
5988               (if (search-forward "\nchars: " nil t)
5989                   (if (numberp (setq chars (ignore-errors (read cur))))
5990                       chars -1)
5991                 -1))
5992             ;; Lines.
5993             (progn
5994               (goto-char p)
5995               (if (search-forward "\nlines: " nil t)
5996                   (if (numberp (setq lines (ignore-errors (read cur))))
5997                       lines -1)
5998                 -1))
5999             ;; Xref.
6000             (progn
6001               (goto-char p)
6002               (and (search-forward "\nxref:" nil t)
6003                    (nnheader-header-value)))
6004             ;; Extra.
6005             (when gnus-extra-headers
6006               (let ((extra gnus-extra-headers)
6007                     out)
6008                 (while extra
6009                   (goto-char p)
6010                   (when (search-forward
6011                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6012                     (push (cons (car extra) (nnheader-header-value))
6013                           out))
6014                   (pop extra))
6015                 out))))
6016           (when (equal id ref)
6017             (setq ref nil))
6018
6019           (when gnus-alter-header-function
6020             (funcall gnus-alter-header-function header)
6021             (setq id (mail-header-id header)
6022                   ref (gnus-parent-id (mail-header-references header))))
6023
6024           (when (setq header
6025                       (gnus-dependencies-add-header
6026                        header dependencies force-new))
6027             (push header headers))
6028           (goto-char (point-max))
6029           (widen))
6030         (nreverse headers)))))
6031
6032 ;; Goes through the xover lines and returns a list of vectors
6033 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6034                                                   force-new dependencies
6035                                                   group also-fetch-heads)
6036   "Parse the news overview data in the server buffer.
6037 Return a list of headers that match SEQUENCE (see
6038 `nntp-retrieve-headers')."
6039   ;; Get the Xref when the users reads the articles since most/some
6040   ;; NNTP servers do not include Xrefs when using XOVER.
6041   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6042   (let ((mail-parse-charset gnus-newsgroup-charset)
6043         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6044         (cur nntp-server-buffer)
6045         (dependencies (or dependencies gnus-newsgroup-dependencies))
6046         (allp (cond
6047                ((eq gnus-read-all-available-headers t)
6048                 t)
6049                ((stringp gnus-read-all-available-headers)
6050                 (string-match gnus-read-all-available-headers group))
6051                (t
6052                 nil)))
6053         number headers header)
6054     (save-excursion
6055       (set-buffer nntp-server-buffer)
6056       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6057       ;; Allow the user to mangle the headers before parsing them.
6058       (gnus-run-hooks 'gnus-parse-headers-hook)
6059       (goto-char (point-min))
6060       (gnus-parse-without-error
6061         (while (and (or sequence allp)
6062                     (not (eobp)))
6063           (setq number (read cur))
6064           (when (not allp)
6065             (while (and sequence
6066                         (< (car sequence) number))
6067               (setq sequence (cdr sequence))))
6068           (when (and (or allp
6069                          (and sequence
6070                               (eq number (car sequence))))
6071                      (progn
6072                        (setq sequence (cdr sequence))
6073                        (setq header (inline
6074                                       (gnus-nov-parse-line
6075                                        number dependencies force-new)))))
6076             (push header headers))
6077           (forward-line 1)))
6078       ;; A common bug in inn is that if you have posted an article and
6079       ;; then retrieves the active file, it will answer correctly --
6080       ;; the new article is included.  However, a NOV entry for the
6081       ;; article may not have been generated yet, so this may fail.
6082       ;; We work around this problem by retrieving the last few
6083       ;; headers using HEAD.
6084       (if (or (not also-fetch-heads)
6085               (not sequence))
6086           ;; We (probably) got all the headers.
6087           (nreverse headers)
6088         (let ((gnus-nov-is-evil t))
6089           (nconc
6090            (nreverse headers)
6091            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6092              (gnus-get-newsgroup-headers))))))))
6093
6094 (defun gnus-article-get-xrefs ()
6095   "Fill in the Xref value in `gnus-current-headers', if necessary.
6096 This is meant to be called in `gnus-article-internal-prepare-hook'."
6097   (let ((headers (with-current-buffer gnus-summary-buffer
6098                    gnus-current-headers)))
6099     (or (not gnus-use-cross-reference)
6100         (not headers)
6101         (and (mail-header-xref headers)
6102              (not (string= (mail-header-xref headers) "")))
6103         (let ((case-fold-search t)
6104               xref)
6105           (save-restriction
6106             (nnheader-narrow-to-headers)
6107             (goto-char (point-min))
6108             (when (or (and (not (eobp))
6109                            (eq (downcase (char-after)) ?x)
6110                            (looking-at "Xref:"))
6111                       (search-forward "\nXref:" nil t))
6112               (goto-char (1+ (match-end 0)))
6113               (setq xref (buffer-substring (point) (point-at-eol)))
6114               (mail-header-set-xref headers xref)))))))
6115
6116 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6117   "Find article ID and insert the summary line for that article.
6118 OLD-HEADER can either be a header or a line number to insert
6119 the subject line on."
6120   (let* ((line (and (numberp old-header) old-header))
6121          (old-header (and (vectorp old-header) old-header))
6122          (header (cond ((and old-header use-old-header)
6123                         old-header)
6124                        ((and (numberp id)
6125                              (gnus-number-to-header id))
6126                         (gnus-number-to-header id))
6127                        (t
6128                         (gnus-read-header id))))
6129          (number (and (numberp id) id))
6130          d)
6131     (when header
6132       ;; Rebuild the thread that this article is part of and go to the
6133       ;; article we have fetched.
6134       (when (and (not gnus-show-threads)
6135                  old-header)
6136         (when (and number
6137                    (setq d (gnus-data-find (mail-header-number old-header))))
6138           (goto-char (gnus-data-pos d))
6139           (gnus-data-remove
6140            number
6141            (- (point-at-bol)
6142               (prog1
6143                   (1+ (point-at-eol))
6144                 (gnus-delete-line))))))
6145       ;; Remove list identifiers from subject.
6146       (when gnus-list-identifiers
6147         (let ((gnus-newsgroup-headers (list header)))
6148           (gnus-summary-remove-list-identifiers)))
6149       (when old-header
6150         (mail-header-set-number header (mail-header-number old-header)))
6151       (setq gnus-newsgroup-sparse
6152             (delq (setq number (mail-header-number header))
6153                   gnus-newsgroup-sparse))
6154       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6155       (push number gnus-newsgroup-limit)
6156       (gnus-rebuild-thread (mail-header-id header) line)
6157       (gnus-summary-goto-subject number nil t))
6158     (when (and (numberp number)
6159                (> number 0))
6160       ;; We have to update the boundaries even if we can't fetch the
6161       ;; article if ID is a number -- so that the next `P' or `N'
6162       ;; command will fetch the previous (or next) article even
6163       ;; if the one we tried to fetch this time has been canceled.
6164       (when (> number gnus-newsgroup-end)
6165         (setq gnus-newsgroup-end number))
6166       (when (< number gnus-newsgroup-begin)
6167         (setq gnus-newsgroup-begin number))
6168       (setq gnus-newsgroup-unselected
6169             (delq number gnus-newsgroup-unselected)))
6170     ;; Report back a success?
6171     (and header (mail-header-number header))))
6172
6173 ;;; Process/prefix in the summary buffer
6174
6175 (defun gnus-summary-work-articles (n)
6176   "Return a list of articles to be worked upon.
6177 The prefix argument, the list of process marked articles, and the
6178 current article will be taken into consideration."
6179   (save-excursion
6180     (set-buffer gnus-summary-buffer)
6181     (cond
6182      (n
6183       ;; A numerical prefix has been given.
6184       (setq n (prefix-numeric-value n))
6185       (let ((backward (< n 0))
6186             (n (abs (prefix-numeric-value n)))
6187             articles article)
6188         (save-excursion
6189           (while
6190               (and (> n 0)
6191                    (push (setq article (gnus-summary-article-number))
6192                          articles)
6193                    (if backward
6194                        (gnus-summary-find-prev nil article)
6195                      (gnus-summary-find-next nil article)))
6196             (decf n)))
6197         (nreverse articles)))
6198      ((and (gnus-region-active-p) (mark))
6199       (message "region active")
6200       ;; Work on the region between point and mark.
6201       (let ((max (max (point) (mark)))
6202             articles article)
6203         (save-excursion
6204           (goto-char (min (min (point) (mark))))
6205           (while
6206               (and
6207                (push (setq article (gnus-summary-article-number)) articles)
6208                (gnus-summary-find-next nil article)
6209                (< (point) max)))
6210           (nreverse articles))))
6211      (gnus-newsgroup-processable
6212       ;; There are process-marked articles present.
6213       ;; Save current state.
6214       (gnus-summary-save-process-mark)
6215       ;; Return the list.
6216       (reverse gnus-newsgroup-processable))
6217      (t
6218       ;; Just return the current article.
6219       (list (gnus-summary-article-number))))))
6220
6221 (defmacro gnus-summary-iterate (arg &rest forms)
6222   "Iterate over the process/prefixed articles and do FORMS.
6223 ARG is the interactive prefix given to the command.  FORMS will be
6224 executed with point over the summary line of the articles."
6225   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6226     `(let ((,articles (gnus-summary-work-articles ,arg)))
6227        (while ,articles
6228          (gnus-summary-goto-subject (car ,articles))
6229          ,@forms
6230          (pop ,articles)))))
6231
6232 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6233 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6234
6235 (defun gnus-summary-save-process-mark ()
6236   "Push the current set of process marked articles on the stack."
6237   (interactive)
6238   (push (copy-sequence gnus-newsgroup-processable)
6239         gnus-newsgroup-process-stack))
6240
6241 (defun gnus-summary-kill-process-mark ()
6242   "Push the current set of process marked articles on the stack and unmark."
6243   (interactive)
6244   (gnus-summary-save-process-mark)
6245   (gnus-summary-unmark-all-processable))
6246
6247 (defun gnus-summary-yank-process-mark ()
6248   "Pop the last process mark state off the stack and restore it."
6249   (interactive)
6250   (unless gnus-newsgroup-process-stack
6251     (error "Empty mark stack"))
6252   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6253
6254 (defun gnus-summary-process-mark-set (set)
6255   "Make SET into the current process marked articles."
6256   (gnus-summary-unmark-all-processable)
6257   (mapc 'gnus-summary-set-process-mark set))
6258
6259 ;;; Searching and stuff
6260
6261 (defun gnus-summary-search-group (&optional backward use-level)
6262   "Search for next unread newsgroup.
6263 If optional argument BACKWARD is non-nil, search backward instead."
6264   (save-excursion
6265     (set-buffer gnus-group-buffer)
6266     (when (gnus-group-search-forward
6267            backward nil (if use-level (gnus-group-group-level) nil))
6268       (gnus-group-group-name))))
6269
6270 (defun gnus-summary-best-group (&optional exclude-group)
6271   "Find the name of the best unread group.
6272 If EXCLUDE-GROUP, do not go to this group."
6273   (with-current-buffer gnus-group-buffer
6274     (save-excursion
6275       (gnus-group-best-unread-group exclude-group))))
6276
6277 (defun gnus-summary-find-next (&optional unread article backward)
6278   (if backward
6279       (gnus-summary-find-prev unread article)
6280     (let* ((dummy (gnus-summary-article-intangible-p))
6281            (article (or article (gnus-summary-article-number)))
6282            (data (gnus-data-find-list article))
6283            result)
6284       (when (and (not dummy)
6285                  (or (not gnus-summary-check-current)
6286                      (not unread)
6287                      (not (gnus-data-unread-p (car data)))))
6288         (setq data (cdr data)))
6289       (when (setq result
6290                   (if unread
6291                       (progn
6292                         (while data
6293                           (unless (memq (gnus-data-number (car data))
6294                                         (cond
6295                                          ((eq gnus-auto-goto-ignores
6296                                               'always-undownloaded)
6297                                           gnus-newsgroup-undownloaded)
6298                                          (gnus-plugged
6299                                           nil)
6300                                          ((eq gnus-auto-goto-ignores
6301                                               'unfetched)
6302                                           gnus-newsgroup-unfetched)
6303                                          ((eq gnus-auto-goto-ignores
6304                                               'undownloaded)
6305                                           gnus-newsgroup-undownloaded)))
6306                             (when (gnus-data-unread-p (car data))
6307                               (setq result (car data)
6308                                     data nil)))
6309                           (setq data (cdr data)))
6310                         result)
6311                     (car data)))
6312         (goto-char (gnus-data-pos result))
6313         (gnus-data-number result)))))
6314
6315 (defun gnus-summary-find-prev (&optional unread article)
6316   (let* ((eobp (eobp))
6317          (article (or article (gnus-summary-article-number)))
6318          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6319          result)
6320     (when (and (not eobp)
6321                (or (not gnus-summary-check-current)
6322                    (not unread)
6323                    (not (gnus-data-unread-p (car data)))))
6324       (setq data (cdr data)))
6325     (when (setq result
6326                 (if unread
6327                     (progn
6328                       (while data
6329                         (unless (memq (gnus-data-number (car data))
6330                                       (cond
6331                                        ((eq gnus-auto-goto-ignores
6332                                             'always-undownloaded)
6333                                         gnus-newsgroup-undownloaded)
6334                                        (gnus-plugged
6335                                         nil)
6336                                        ((eq gnus-auto-goto-ignores
6337                                             'unfetched)
6338                                         gnus-newsgroup-unfetched)
6339                                        ((eq gnus-auto-goto-ignores
6340                                             'undownloaded)
6341                                         gnus-newsgroup-undownloaded)))
6342                           (when (gnus-data-unread-p (car data))
6343                             (setq result (car data)
6344                                   data nil)))
6345                         (setq data (cdr data)))
6346                       result)
6347                   (car data)))
6348       (goto-char (gnus-data-pos result))
6349       (gnus-data-number result))))
6350
6351 (defun gnus-summary-find-subject (subject &optional unread backward article)
6352   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6353          (article (or article (gnus-summary-article-number)))
6354          (articles (gnus-data-list backward))
6355          (arts (gnus-data-find-list article articles))
6356          result)
6357     (when (or (not gnus-summary-check-current)
6358               (not unread)
6359               (not (gnus-data-unread-p (car arts))))
6360       (setq arts (cdr arts)))
6361     (while arts
6362       (and (or (not unread)
6363                (gnus-data-unread-p (car arts)))
6364            (vectorp (gnus-data-header (car arts)))
6365            (gnus-subject-equal
6366             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6367            (setq result (car arts)
6368                  arts nil))
6369       (setq arts (cdr arts)))
6370     (and result
6371          (goto-char (gnus-data-pos result))
6372          (gnus-data-number result))))
6373
6374 (defun gnus-summary-search-forward (&optional unread subject backward)
6375   "Search forward for an article.
6376 If UNREAD, look for unread articles.  If SUBJECT, look for
6377 articles with that subject.  If BACKWARD, search backward instead."
6378   (cond (subject (gnus-summary-find-subject subject unread backward))
6379         (backward (gnus-summary-find-prev unread))
6380         (t (gnus-summary-find-next unread))))
6381
6382 (defun gnus-recenter (&optional n)
6383   "Center point in window and redisplay frame.
6384 Also do horizontal recentering."
6385   (interactive "P")
6386   (when (and gnus-auto-center-summary
6387              (not (eq gnus-auto-center-summary 'vertical)))
6388     (gnus-horizontal-recenter))
6389   (recenter n))
6390
6391 (defun gnus-summary-recenter ()
6392   "Center point in the summary window.
6393 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6394 displayed, no centering will be performed."
6395   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6396   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6397   (interactive)
6398   ;; The user has to want it.
6399   (when gnus-auto-center-summary
6400     (let* ((top (cond ((< (window-height) 4) 0)
6401                       ((< (window-height) 7) 1)
6402                       (t (if (numberp gnus-auto-center-summary)
6403                              gnus-auto-center-summary
6404                            (/ (1- (window-height)) 2)))))
6405            (height (1- (window-height)))
6406            (bottom (save-excursion (goto-char (point-max))
6407                                    (forward-line (- height))
6408                                    (point)))
6409            (window (get-buffer-window (current-buffer))))
6410       (when (get-buffer-window gnus-article-buffer)
6411         ;; Only do recentering when the article buffer is displayed,
6412         ;; Set the window start to either `bottom', which is the biggest
6413         ;; possible valid number, or the second line from the top,
6414         ;; whichever is the least.
6415         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6416           (if (> bottom top-pos)
6417               ;; Keep the second line from the top visible
6418               (set-window-start window top-pos t)
6419             ;; Try to keep the bottom line visible; if it's partially
6420             ;; obscured, either scroll one more line to make it fully
6421             ;; visible, or revert to using TOP-POS.
6422             (save-excursion
6423               (goto-char (point-max))
6424               (forward-line -1)
6425               (let ((last-line-start (point)))
6426                 (goto-char bottom)
6427                 (set-window-start window (point) t)
6428                 (when (not (pos-visible-in-window-p last-line-start window))
6429                   (forward-line 1)
6430                   (set-window-start window (min (point) top-pos) t)))))))
6431       ;; Do horizontal recentering while we're at it.
6432       (when (and (get-buffer-window (current-buffer) t)
6433                  (not (eq gnus-auto-center-summary 'vertical)))
6434         (let ((selected (selected-window)))
6435           (select-window (get-buffer-window (current-buffer) t))
6436           (gnus-summary-position-point)
6437           (gnus-horizontal-recenter)
6438           (select-window selected))))))
6439
6440 (defun gnus-summary-jump-to-group (newsgroup)
6441   "Move point to NEWSGROUP in group mode buffer."
6442   ;; Keep update point of group mode buffer if visible.
6443   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6444       (save-window-excursion
6445         ;; Take care of tree window mode.
6446         (when (get-buffer-window gnus-group-buffer)
6447           (pop-to-buffer gnus-group-buffer))
6448         (gnus-group-jump-to-group newsgroup))
6449     (save-excursion
6450       ;; Take care of tree window mode.
6451       (if (get-buffer-window gnus-group-buffer)
6452           (pop-to-buffer gnus-group-buffer)
6453         (set-buffer gnus-group-buffer))
6454       (gnus-group-jump-to-group newsgroup))))
6455
6456 ;; This function returns a list of article numbers based on the
6457 ;; difference between the ranges of read articles in this group and
6458 ;; the range of active articles.
6459 (defun gnus-list-of-unread-articles (group)
6460   (let* ((read (gnus-info-read (gnus-get-info group)))
6461          (active (or (gnus-active group) (gnus-activate-group group)))
6462          (last (or (cdr active)
6463                    (error "Group %s couldn't be activated " group)))
6464          first nlast unread)
6465     ;; If none are read, then all are unread.
6466     (if (not read)
6467         (setq first (car active))
6468       ;; If the range of read articles is a single range, then the
6469       ;; first unread article is the article after the last read
6470       ;; article.  Sounds logical, doesn't it?
6471       (if (and (not (listp (cdr read)))
6472                (or (< (car read) (car active))
6473                    (progn (setq read (list read))
6474                           nil)))
6475           (setq first (max (car active) (1+ (cdr read))))
6476         ;; `read' is a list of ranges.
6477         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6478                                   (caar read)))
6479                   1)
6480           (setq first (car active)))
6481         (while read
6482           (when first
6483             (while (< first nlast)
6484               (setq unread (cons first unread)
6485                     first (1+ first))))
6486           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6487           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6488           (setq read (cdr read)))))
6489     ;; And add the last unread articles.
6490     (while (<= first last)
6491       (setq unread (cons first unread)
6492             first (1+ first)))
6493     ;; Return the list of unread articles.
6494     (delq 0 (nreverse unread))))
6495
6496 (defun gnus-list-of-read-articles (group)
6497   "Return a list of unread, unticked and non-dormant articles."
6498   (let* ((info (gnus-get-info group))
6499          (marked (gnus-info-marks info))
6500          (active (gnus-active group)))
6501     (and info active
6502          (gnus-list-range-difference
6503           (gnus-list-range-difference
6504            (gnus-sorted-complement
6505             (gnus-uncompress-range active)
6506             (gnus-list-of-unread-articles group))
6507            (cdr (assq 'dormant marked)))
6508           (cdr (assq 'tick marked))))))
6509
6510 ;; This function returns a sequence of article numbers based on the
6511 ;; difference between the ranges of read articles in this group and
6512 ;; the range of active articles.
6513 (defun gnus-sequence-of-unread-articles (group)
6514   (let* ((read (gnus-info-read (gnus-get-info group)))
6515          (active (or (gnus-active group) (gnus-activate-group group)))
6516          (last (cdr active))
6517          first nlast unread)
6518     ;; If none are read, then all are unread.
6519     (if (not read)
6520         (setq first (car active))
6521       ;; If the range of read articles is a single range, then the
6522       ;; first unread article is the article after the last read
6523       ;; article.  Sounds logical, doesn't it?
6524       (if (and (not (listp (cdr read)))
6525                (or (< (car read) (car active))
6526                    (progn (setq read (list read))
6527                           nil)))
6528           (setq first (max (car active) (1+ (cdr read))))
6529         ;; `read' is a list of ranges.
6530         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6531                                   (caar read)))
6532                   1)
6533           (setq first (car active)))
6534         (while read
6535           (when first
6536             (push (cons first nlast) unread))
6537           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6538           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6539           (setq read (cdr read)))))
6540     ;; And add the last unread articles.
6541     (cond ((< first last)
6542            (push (cons first last) unread))
6543           ((= first last)
6544            (push first unread)))
6545     ;; Return the sequence of unread articles.
6546     (delq 0 (nreverse unread))))
6547
6548 ;; Various summary commands
6549
6550 (defun gnus-summary-select-article-buffer ()
6551   "Reconfigure windows to show article buffer."
6552   (interactive)
6553   (if (not (gnus-buffer-live-p gnus-article-buffer))
6554       (error "There is no article buffer for this summary buffer")
6555     (gnus-configure-windows 'article)
6556     (select-window (get-buffer-window gnus-article-buffer))))
6557
6558 (defun gnus-summary-universal-argument (arg)
6559   "Perform any operation on all articles that are process/prefixed."
6560   (interactive "P")
6561   (let ((articles (gnus-summary-work-articles arg))
6562         func article)
6563     (if (eq
6564          (setq
6565           func
6566           (key-binding
6567            (read-key-sequence
6568             (substitute-command-keys
6569              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6570          'undefined)
6571         (gnus-error 1 "Undefined key")
6572       (save-excursion
6573         (while articles
6574           (gnus-summary-goto-subject (setq article (pop articles)))
6575           (let (gnus-newsgroup-processable)
6576             (command-execute func))
6577           (gnus-summary-remove-process-mark article)))))
6578   (gnus-summary-position-point))
6579
6580 (defun gnus-summary-toggle-truncation (&optional arg)
6581   "Toggle truncation of summary lines.
6582 With ARG, turn line truncation on if ARG is positive."
6583   (interactive "P")
6584   (setq truncate-lines
6585         (if (null arg) (not truncate-lines)
6586           (> (prefix-numeric-value arg) 0)))
6587   (redraw-display))
6588
6589 (defun gnus-summary-find-for-reselect ()
6590   "Return the number of an article to stay on across a reselect.
6591 The current article is considered, then following articles, then previous
6592 articles.  An article is sought which is not cancelled and isn't a temporary
6593 insertion from another group.  If there's no such then return a dummy 0."
6594   (let (found)
6595     (dolist (rev '(nil t))
6596       (unless found      ; don't demand the reverse list if we don't need it
6597         (let ((data (gnus-data-find-list
6598                      (gnus-summary-article-number) (gnus-data-list rev))))
6599           (while (and data (not found))
6600             (if (and (< 0 (gnus-data-number (car data)))
6601                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6602                 (setq found (gnus-data-number (car data))))
6603             (setq data (cdr data))))))
6604     (or found 0)))
6605
6606 (defun gnus-summary-reselect-current-group (&optional all rescan)
6607   "Exit and then reselect the current newsgroup.
6608 The prefix argument ALL means to select all articles."
6609   (interactive "P")
6610   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6611     (error "Ephemeral groups can't be reselected"))
6612   (let ((current-subject (gnus-summary-find-for-reselect))
6613         (group gnus-newsgroup-name))
6614     (setq gnus-newsgroup-begin nil)
6615     (gnus-summary-exit nil 'leave-hidden)
6616     ;; We have to adjust the point of group mode buffer because
6617     ;; point was moved to the next unread newsgroup by exiting.
6618     (gnus-summary-jump-to-group group)
6619     (when rescan
6620       (save-excursion
6621         (gnus-group-get-new-news-this-group 1)))
6622     (gnus-group-read-group all t)
6623     (gnus-summary-goto-subject current-subject nil t)))
6624
6625 (defun gnus-summary-rescan-group (&optional all)
6626   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6627   (interactive "P")
6628   (gnus-summary-reselect-current-group all t))
6629
6630 (defun gnus-summary-update-info (&optional non-destructive)
6631   (save-excursion
6632     (let ((group gnus-newsgroup-name))
6633       (when group
6634         (when gnus-newsgroup-kill-headers
6635           (setq gnus-newsgroup-killed
6636                 (gnus-compress-sequence
6637                  (gnus-sorted-union
6638                   (gnus-list-range-intersection
6639                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6640                   gnus-newsgroup-unreads)
6641                  t)))
6642         (unless (listp (cdr gnus-newsgroup-killed))
6643           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6644         (let ((headers gnus-newsgroup-headers))
6645           ;; Set the new ranges of read articles.
6646           (with-current-buffer gnus-group-buffer
6647             (gnus-undo-force-boundary))
6648           (gnus-update-read-articles
6649            group (gnus-sorted-union
6650                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6651           ;; Set the current article marks.
6652           (let ((gnus-newsgroup-scored
6653                  (if (and (not gnus-save-score)
6654                           (not non-destructive))
6655                      nil
6656                    gnus-newsgroup-scored)))
6657             (save-excursion
6658               (gnus-update-marks)))
6659           ;; Do the cross-ref thing.
6660           (when gnus-use-cross-reference
6661             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6662           ;; Do not switch windows but change the buffer to work.
6663           (set-buffer gnus-group-buffer)
6664           (unless (gnus-ephemeral-group-p group)
6665             (gnus-group-update-group group)))))))
6666
6667 (defun gnus-summary-save-newsrc (&optional force)
6668   "Save the current number of read/marked articles in the dribble buffer.
6669 The dribble buffer will then be saved.
6670 If FORCE (the prefix), also save the .newsrc file(s)."
6671   (interactive "P")
6672   (gnus-summary-update-info t)
6673   (if force
6674       (gnus-save-newsrc-file)
6675     (gnus-dribble-save)))
6676
6677 (defun gnus-summary-exit (&optional temporary leave-hidden)
6678   "Exit reading current newsgroup, and then return to group selection mode.
6679 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6680   (interactive)
6681   (gnus-set-global-variables)
6682   (when (gnus-buffer-live-p gnus-article-buffer)
6683     (save-excursion
6684       (set-buffer gnus-article-buffer)
6685       (mm-destroy-parts gnus-article-mime-handles)
6686       ;; Set it to nil for safety reason.
6687       (setq gnus-article-mime-handle-alist nil)
6688       (setq gnus-article-mime-handles nil)))
6689   (gnus-kill-save-kill-buffer)
6690   (gnus-async-halt-prefetch)
6691   (let* ((group gnus-newsgroup-name)
6692          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6693          (gnus-group-is-exiting-p t)
6694          (mode major-mode)
6695          (group-point nil)
6696          (buf (current-buffer)))
6697     (unless quit-config
6698       ;; Do adaptive scoring, and possibly save score files.
6699       (when gnus-newsgroup-adaptive
6700         (gnus-score-adaptive))
6701       (when gnus-use-scoring
6702         (gnus-score-save)))
6703     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6704     ;; If we have several article buffers, we kill them at exit.
6705     (unless gnus-single-article-buffer
6706       (gnus-kill-buffer gnus-original-article-buffer)
6707       (setq gnus-article-current nil))
6708     (when gnus-use-cache
6709       (gnus-cache-possibly-remove-articles)
6710       (gnus-cache-save-buffers))
6711     (gnus-async-prefetch-remove-group group)
6712     (when gnus-suppress-duplicates
6713       (gnus-dup-enter-articles))
6714     (when gnus-use-trees
6715       (gnus-tree-close group))
6716     (when gnus-use-cache
6717       (gnus-cache-write-active))
6718     ;; Remove entries for this group.
6719     (nnmail-purge-split-history (gnus-group-real-name group))
6720     ;; Make all changes in this group permanent.
6721     (unless quit-config
6722       (gnus-run-hooks 'gnus-exit-group-hook)
6723       (gnus-summary-update-info))
6724     (gnus-close-group group)
6725     ;; Make sure where we were, and go to next newsgroup.
6726     (set-buffer gnus-group-buffer)
6727     (unless quit-config
6728       (gnus-group-jump-to-group group))
6729     (gnus-run-hooks 'gnus-summary-exit-hook)
6730     (unless (or quit-config
6731                 ;; If this group has disappeared from the summary
6732                 ;; buffer, don't skip forwards.
6733                 (not (string= group (gnus-group-group-name))))
6734       (gnus-group-next-unread-group 1))
6735     (setq group-point (point))
6736     (if temporary
6737         nil                             ;Nothing to do.
6738       ;; If we have several article buffers, we kill them at exit.
6739       (unless gnus-single-article-buffer
6740         (gnus-kill-buffer gnus-article-buffer)
6741         (gnus-kill-buffer gnus-original-article-buffer)
6742         (setq gnus-article-current nil))
6743       (set-buffer buf)
6744       (if (not gnus-kill-summary-on-exit)
6745           (progn
6746             (gnus-deaden-summary)
6747             (setq mode nil))
6748         ;; We set all buffer-local variables to nil.  It is unclear why
6749         ;; this is needed, but if we don't, buffer-local variables are
6750         ;; not garbage-collected, it seems.  This would the lead to en
6751         ;; ever-growing Emacs.
6752         (gnus-summary-clear-local-variables)
6753         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6754           (gnus-summary-clear-local-variables))
6755         (when (get-buffer gnus-article-buffer)
6756           (bury-buffer gnus-article-buffer))
6757         ;; Return to group mode buffer.
6758         (when (eq mode 'gnus-summary-mode)
6759           (gnus-kill-buffer buf)))
6760       (setq gnus-current-select-method gnus-select-method)
6761       (set-buffer gnus-group-buffer)
6762       (if quit-config
6763           (gnus-handle-ephemeral-exit quit-config)
6764         (goto-char group-point)
6765         ;; If gnus-group-buffer is already displayed, make sure we also move
6766         ;; the cursor in the window that displays it.
6767         (let ((win (get-buffer-window (current-buffer) 0)))
6768           (if win (set-window-point win (point))))
6769         (unless leave-hidden
6770           (gnus-configure-windows 'group 'force)))
6771       ;; Clear the current group name.
6772       (unless quit-config
6773         (setq gnus-newsgroup-name nil)))))
6774
6775 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6776 (defun gnus-summary-exit-no-update (&optional no-questions)
6777   "Quit reading current newsgroup without updating read article info."
6778   (interactive)
6779   (let* ((group gnus-newsgroup-name)
6780          (gnus-group-is-exiting-p t)
6781          (gnus-group-is-exiting-without-update-p t)
6782          (quit-config (gnus-group-quit-config group)))
6783     (when (or no-questions
6784               gnus-expert-user
6785               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6786       (gnus-async-halt-prefetch)
6787       (run-hooks 'gnus-summary-prepare-exit-hook)
6788       (when (gnus-buffer-live-p gnus-article-buffer)
6789         (save-excursion
6790           (set-buffer gnus-article-buffer)
6791           (mm-destroy-parts gnus-article-mime-handles)
6792           ;; Set it to nil for safety reason.
6793           (setq gnus-article-mime-handle-alist nil)
6794           (setq gnus-article-mime-handles nil)))
6795       ;; If we have several article buffers, we kill them at exit.
6796       (unless gnus-single-article-buffer
6797         (gnus-kill-buffer gnus-article-buffer)
6798         (gnus-kill-buffer gnus-original-article-buffer)
6799         (setq gnus-article-current nil))
6800       (if (not gnus-kill-summary-on-exit)
6801           (gnus-deaden-summary)
6802         (gnus-close-group group)
6803         (gnus-summary-clear-local-variables)
6804         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6805           (gnus-summary-clear-local-variables))
6806         (gnus-kill-buffer gnus-summary-buffer))
6807       (unless gnus-single-article-buffer
6808         (setq gnus-article-current nil))
6809       (when gnus-use-trees
6810         (gnus-tree-close group))
6811       (gnus-async-prefetch-remove-group group)
6812       (when (get-buffer gnus-article-buffer)
6813         (bury-buffer gnus-article-buffer))
6814       ;; Return to the group buffer.
6815       (gnus-configure-windows 'group 'force)
6816       ;; Clear the current group name.
6817       (setq gnus-newsgroup-name nil)
6818       (unless (gnus-ephemeral-group-p group)
6819         (gnus-group-update-group group))
6820       (when (equal (gnus-group-group-name) group)
6821         (gnus-group-next-unread-group 1))
6822       (when quit-config
6823         (gnus-handle-ephemeral-exit quit-config)))))
6824
6825 (defun gnus-handle-ephemeral-exit (quit-config)
6826   "Handle movement when leaving an ephemeral group.
6827 The state which existed when entering the ephemeral is reset."
6828   (if (not (buffer-name (car quit-config)))
6829       (gnus-configure-windows 'group 'force)
6830     (set-buffer (car quit-config))
6831     (cond ((eq major-mode 'gnus-summary-mode)
6832            (gnus-set-global-variables))
6833           ((eq major-mode 'gnus-article-mode)
6834            (save-excursion
6835              ;; The `gnus-summary-buffer' variable may point
6836              ;; to the old summary buffer when using a single
6837              ;; article buffer.
6838              (unless (gnus-buffer-live-p gnus-summary-buffer)
6839                (set-buffer gnus-group-buffer))
6840              (set-buffer gnus-summary-buffer)
6841              (gnus-set-global-variables))))
6842     (if (or (eq (cdr quit-config) 'article)
6843             (eq (cdr quit-config) 'pick))
6844         (progn
6845           ;; The current article may be from the ephemeral group
6846           ;; thus it is best that we reload this article
6847           ;;
6848           ;; If we're exiting from a large digest, this can be
6849           ;; extremely slow.  So, it's better not to reload it. -- jh.
6850           ;;(gnus-summary-show-article)
6851           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6852               (gnus-configure-windows 'pick 'force)
6853             (gnus-configure-windows (cdr quit-config) 'force)))
6854       (gnus-configure-windows (cdr quit-config) 'force))
6855     (when (eq major-mode 'gnus-summary-mode)
6856       (gnus-summary-next-subject 1 nil t)
6857       (gnus-summary-recenter)
6858       (gnus-summary-position-point))))
6859
6860 ;;; Dead summaries.
6861
6862 (defvar gnus-dead-summary-mode-map nil)
6863
6864 (unless gnus-dead-summary-mode-map
6865   (setq gnus-dead-summary-mode-map (make-keymap))
6866   (suppress-keymap gnus-dead-summary-mode-map)
6867   (substitute-key-definition
6868    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6869   (dolist (key '("\C-d" "\r" "\177" [delete]))
6870     (define-key gnus-dead-summary-mode-map
6871       key 'gnus-summary-wake-up-the-dead))
6872   (dolist (key '("q" "Q"))
6873     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6874
6875 (defvar gnus-dead-summary-mode nil
6876   "Minor mode for Gnus summary buffers.")
6877
6878 (defun gnus-dead-summary-mode (&optional arg)
6879   "Minor mode for Gnus summary buffers."
6880   (interactive "P")
6881   (when (eq major-mode 'gnus-summary-mode)
6882     (make-local-variable 'gnus-dead-summary-mode)
6883     (setq gnus-dead-summary-mode
6884           (if (null arg) (not gnus-dead-summary-mode)
6885             (> (prefix-numeric-value arg) 0)))
6886     (when gnus-dead-summary-mode
6887       (add-minor-mode
6888        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6889
6890 (defun gnus-deaden-summary ()
6891   "Make the current summary buffer into a dead summary buffer."
6892   ;; Kill any previous dead summary buffer.
6893   (when (and gnus-dead-summary
6894              (buffer-name gnus-dead-summary))
6895     (with-current-buffer gnus-dead-summary
6896       (when gnus-dead-summary-mode
6897         (kill-buffer (current-buffer)))))
6898   ;; Make this the current dead summary.
6899   (setq gnus-dead-summary (current-buffer))
6900   (gnus-dead-summary-mode 1)
6901   (let ((name (buffer-name)))
6902     (when (string-match "Summary" name)
6903       (rename-buffer
6904        (concat (substring name 0 (match-beginning 0)) "Dead "
6905                (substring name (match-beginning 0)))
6906        t)
6907       (bury-buffer))))
6908
6909 (defun gnus-kill-or-deaden-summary (buffer)
6910   "Kill or deaden the summary BUFFER."
6911   (save-excursion
6912     (when (and (buffer-name buffer)
6913                (not gnus-single-article-buffer))
6914       (with-current-buffer buffer
6915         (gnus-kill-buffer gnus-article-buffer)
6916         (gnus-kill-buffer gnus-original-article-buffer)))
6917     (cond
6918      ;; Kill the buffer.
6919      (gnus-kill-summary-on-exit
6920       (when (and gnus-use-trees
6921                  (gnus-buffer-exists-p buffer))
6922         (save-excursion
6923           (set-buffer buffer)
6924           (gnus-tree-close gnus-newsgroup-name)))
6925       (gnus-kill-buffer buffer))
6926      ;; Deaden the buffer.
6927      ((gnus-buffer-exists-p buffer)
6928       (save-excursion
6929         (set-buffer buffer)
6930         (gnus-deaden-summary))))))
6931
6932 (defun gnus-summary-wake-up-the-dead (&rest args)
6933   "Wake up the dead summary buffer."
6934   (interactive)
6935   (gnus-dead-summary-mode -1)
6936   (let ((name (buffer-name)))
6937     (when (string-match "Dead " name)
6938       (rename-buffer
6939        (concat (substring name 0 (match-beginning 0))
6940                (substring name (match-end 0)))
6941        t)))
6942   (gnus-message 3 "This dead summary is now alive again"))
6943
6944 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6945 (defun gnus-summary-fetch-faq (&optional faq-dir)
6946   "Fetch the FAQ for the current group.
6947 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6948 in."
6949   (interactive
6950    (list
6951     (when current-prefix-arg
6952       (completing-read
6953        "FAQ dir: " (and (listp gnus-group-faq-directory)
6954                         (mapcar 'list
6955                                 gnus-group-faq-directory))))))
6956   (let (gnus-faq-buffer)
6957     (when (setq gnus-faq-buffer
6958                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6959       (gnus-configure-windows 'summary-faq))))
6960
6961 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6962 (defun gnus-summary-describe-group (&optional force)
6963   "Describe the current newsgroup."
6964   (interactive "P")
6965   (gnus-group-describe-group force gnus-newsgroup-name))
6966
6967 (defun gnus-summary-describe-briefly ()
6968   "Describe summary mode commands briefly."
6969   (interactive)
6970   (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")))
6971
6972 ;; Walking around group mode buffer from summary mode.
6973
6974 (defun gnus-summary-next-group (&optional no-article target-group backward)
6975   "Exit current newsgroup and then select next unread newsgroup.
6976 If prefix argument NO-ARTICLE is non-nil, no article is selected
6977 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6978 previous group instead."
6979   (interactive "P")
6980   ;; Stop pre-fetching.
6981   (gnus-async-halt-prefetch)
6982   (let ((current-group gnus-newsgroup-name)
6983         (current-buffer (current-buffer))
6984         entered)
6985     ;; First we semi-exit this group to update Xrefs and all variables.
6986     ;; We can't do a real exit, because the window conf must remain
6987     ;; the same in case the user is prompted for info, and we don't
6988     ;; want the window conf to change before that...
6989     (gnus-summary-exit t)
6990     (while (not entered)
6991       ;; Then we find what group we are supposed to enter.
6992       (set-buffer gnus-group-buffer)
6993       (gnus-group-jump-to-group current-group)
6994       (setq target-group
6995             (or target-group
6996                 (if (eq gnus-keep-same-level 'best)
6997                     (gnus-summary-best-group gnus-newsgroup-name)
6998                   (gnus-summary-search-group backward gnus-keep-same-level))))
6999       (if (not target-group)
7000           ;; There are no further groups, so we return to the group
7001           ;; buffer.
7002           (progn
7003             (gnus-message 5 "Returning to the group buffer")
7004             (setq entered t)
7005             (when (gnus-buffer-live-p current-buffer)
7006               (set-buffer current-buffer)
7007               (gnus-summary-exit))
7008             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7009         ;; We try to enter the target group.
7010         (gnus-group-jump-to-group target-group)
7011         (let ((unreads (gnus-group-group-unread)))
7012           (if (and (or (eq t unreads)
7013                        (and unreads (not (zerop unreads))))
7014                    (gnus-summary-read-group
7015                     target-group nil no-article
7016                     (and (buffer-name current-buffer) current-buffer)
7017                     nil backward))
7018               (setq entered t)
7019             (setq current-group target-group
7020                   target-group nil)))))))
7021
7022 (defun gnus-summary-prev-group (&optional no-article)
7023   "Exit current newsgroup and then select previous unread newsgroup.
7024 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7025   (interactive "P")
7026   (gnus-summary-next-group no-article nil t))
7027
7028 ;; Walking around summary lines.
7029
7030 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7031   "Go to the first subject satisfying any non-nil constraint.
7032 If UNREAD is non-nil, the article should be unread.
7033 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7034 If UNSEEN is non-nil, the article should be unseen.
7035 Returns the article selected or nil if there are no matching articles."
7036   (interactive "P")
7037   (cond
7038    ;; Empty summary.
7039    ((null gnus-newsgroup-data)
7040     (gnus-message 3 "No articles in the group")
7041     nil)
7042    ;; Pick the first article.
7043    ((not (or unread undownloaded unseen))
7044     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7045     (gnus-data-number (car gnus-newsgroup-data)))
7046    ;; Find the first unread article.
7047    (t
7048     (let ((data gnus-newsgroup-data))
7049       (while (and data
7050                   (let ((num (gnus-data-number (car data))))
7051                     (or (memq num gnus-newsgroup-unfetched)
7052                         (not (or (and unread
7053                                       (memq num gnus-newsgroup-unreads))
7054                                  (and undownloaded
7055                                       (memq num gnus-newsgroup-undownloaded))
7056                                  (and unseen
7057                                       (memq num gnus-newsgroup-unseen)))))))
7058         (setq data (cdr data)))
7059       (prog1
7060           (if data
7061               (progn
7062                 (goto-char (gnus-data-pos (car data)))
7063                 (gnus-data-number (car data)))
7064             (gnus-message 3 "No more%s articles"
7065                           (let* ((r (when unread " unread"))
7066                                  (d (when undownloaded " undownloaded"))
7067                                  (s (when unseen " unseen"))
7068                                  (l (delq nil (list r d s))))
7069                             (cond ((= 3 (length l))
7070                                    (concat r "," d ", or" s))
7071                                   ((= 2 (length l))
7072                                    (concat (car l) ", or" (cadr l)))
7073                                   ((= 1 (length l))
7074                                    (car l))
7075                                   (t
7076                                    ""))))
7077             nil
7078             )
7079         (gnus-summary-position-point))))))
7080
7081 (defun gnus-summary-next-subject (n &optional unread dont-display)
7082   "Go to next N'th summary line.
7083 If N is negative, go to the previous N'th subject line.
7084 If UNREAD is non-nil, only unread articles are selected.
7085 The difference between N and the actual number of steps taken is
7086 returned."
7087   (interactive "p")
7088   (let ((backward (< n 0))
7089         (n (abs n)))
7090     (while (and (> n 0)
7091                 (if backward
7092                     (gnus-summary-find-prev unread)
7093                   (gnus-summary-find-next unread)))
7094       (unless (zerop (setq n (1- n)))
7095         (gnus-summary-show-thread)))
7096     (when (/= 0 n)
7097       (gnus-message 7 "No more%s articles"
7098                     (if unread " unread" "")))
7099     (unless dont-display
7100       (gnus-summary-recenter)
7101       (gnus-summary-position-point))
7102     n))
7103
7104 (defun gnus-summary-next-unread-subject (n)
7105   "Go to next N'th unread summary line."
7106   (interactive "p")
7107   (gnus-summary-next-subject n t))
7108
7109 (defun gnus-summary-prev-subject (n &optional unread)
7110   "Go to previous N'th summary line.
7111 If optional argument UNREAD is non-nil, only unread article is selected."
7112   (interactive "p")
7113   (gnus-summary-next-subject (- n) unread))
7114
7115 (defun gnus-summary-prev-unread-subject (n)
7116   "Go to previous N'th unread summary line."
7117   (interactive "p")
7118   (gnus-summary-next-subject (- n) t))
7119
7120 (defun gnus-summary-goto-subjects (articles)
7121   "Insert the subject header for ARTICLES in the current buffer."
7122   (save-excursion
7123     (dolist (article articles)
7124       (gnus-summary-goto-subject article t)))
7125   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7126   (gnus-summary-position-point))
7127
7128 (defun gnus-summary-goto-subject (article &optional force silent)
7129   "Go the subject line of ARTICLE.
7130 If FORCE, also allow jumping to articles not currently shown."
7131   (interactive "nArticle number: ")
7132   (unless (numberp article)
7133     (error "Article %s is not a number" article))
7134   (let ((b (point))
7135         (data (gnus-data-find article)))
7136     ;; We read in the article if we have to.
7137     (and (not data)
7138          force
7139          (gnus-summary-insert-subject
7140           article
7141           (if (or (numberp force) (vectorp force)) force)
7142           t)
7143          (setq data (gnus-data-find article)))
7144     (goto-char b)
7145     (if (not data)
7146         (progn
7147           (unless silent
7148             (gnus-message 3 "Can't find article %d" article))
7149           nil)
7150       (let ((pt (gnus-data-pos data)))
7151         (goto-char pt)
7152         (gnus-summary-set-article-display-arrow pt))
7153       (gnus-summary-position-point)
7154       article)))
7155
7156 ;; Walking around summary lines with displaying articles.
7157
7158 (defun gnus-summary-expand-window (&optional arg)
7159   "Make the summary buffer take up the entire Emacs frame.
7160 Given a prefix, will force an `article' buffer configuration."
7161   (interactive "P")
7162   (if arg
7163       (gnus-configure-windows 'article 'force)
7164     (gnus-configure-windows 'summary 'force)))
7165
7166 (defun gnus-summary-display-article (article &optional all-header)
7167   "Display ARTICLE in article buffer."
7168   (when (gnus-buffer-live-p gnus-article-buffer)
7169     (with-current-buffer gnus-article-buffer
7170       (mm-enable-multibyte)))
7171   (gnus-set-global-variables)
7172   (when (gnus-buffer-live-p gnus-article-buffer)
7173     (with-current-buffer gnus-article-buffer
7174       (setq gnus-article-charset gnus-newsgroup-charset)
7175       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7176       (mm-enable-multibyte)))
7177   (if (null article)
7178       nil
7179     (prog1
7180         (if gnus-summary-display-article-function
7181             (funcall gnus-summary-display-article-function article all-header)
7182           (gnus-article-prepare article all-header))
7183       (gnus-run-hooks 'gnus-select-article-hook)
7184       (when (and gnus-current-article
7185                  (not (zerop gnus-current-article)))
7186         (gnus-summary-goto-subject gnus-current-article))
7187       (gnus-summary-recenter)
7188       (when (and gnus-use-trees gnus-show-threads)
7189         (gnus-possibly-generate-tree article)
7190         (gnus-highlight-selected-tree article))
7191       ;; Successfully display article.
7192       (gnus-article-set-window-start
7193        (cdr (assq article gnus-newsgroup-bookmarks))))))
7194
7195 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7196   "Select the current article.
7197 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7198 non-nil, the article will be re-fetched even if it already present in
7199 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7200 be displayed."
7201   ;; Make sure we are in the summary buffer to work around bbdb bug.
7202   (unless (eq major-mode 'gnus-summary-mode)
7203     (set-buffer gnus-summary-buffer))
7204   (let ((article (or article (gnus-summary-article-number)))
7205         (all-headers (not (not all-headers))) ;Must be t or nil.
7206         gnus-summary-display-article-function)
7207     (and (not pseudo)
7208          (gnus-summary-article-pseudo-p article)
7209          (error "This is a pseudo-article"))
7210     (save-excursion
7211       (set-buffer gnus-summary-buffer)
7212       (if (or (and gnus-single-article-buffer
7213                    (or (null gnus-current-article)
7214                        (null gnus-article-current)
7215                        (null (get-buffer gnus-article-buffer))
7216                        (not (eq article (cdr gnus-article-current)))
7217                        (not (equal (car gnus-article-current)
7218                                    gnus-newsgroup-name))))
7219               (and (not gnus-single-article-buffer)
7220                    (or (null gnus-current-article)
7221                        (not (eq gnus-current-article article))))
7222               force)
7223           ;; The requested article is different from the current article.
7224           (progn
7225             (gnus-summary-display-article article all-headers)
7226             (when (gnus-buffer-live-p gnus-article-buffer)
7227               (with-current-buffer gnus-article-buffer
7228                 (if (not gnus-article-decoded-p) ;; a local variable
7229                     (mm-disable-multibyte))))
7230             (gnus-article-set-window-start
7231              (cdr (assq article gnus-newsgroup-bookmarks)))
7232             article)
7233         'old))))
7234
7235 (defun gnus-summary-force-verify-and-decrypt ()
7236   "Display buttons for signed/encrypted parts and verify/decrypt them."
7237   (interactive)
7238   (let ((mm-verify-option 'known)
7239         (mm-decrypt-option 'known)
7240         (gnus-article-emulate-mime t)
7241         (gnus-buttonized-mime-types (append (list "multipart/signed"
7242                                                   "multipart/encrypted")
7243                                             gnus-buttonized-mime-types)))
7244     (gnus-summary-select-article nil 'force)))
7245
7246 (defun gnus-summary-set-current-mark (&optional current-mark)
7247   "Obsolete function."
7248   nil)
7249
7250 (defun gnus-summary-next-article (&optional unread subject backward push)
7251   "Select the next article.
7252 If UNREAD, only unread articles are selected.
7253 If SUBJECT, only articles with SUBJECT are selected.
7254 If BACKWARD, the previous article is selected instead of the next."
7255   (interactive "P")
7256   (cond
7257    ;; Is there such an article?
7258    ((and (gnus-summary-search-forward unread subject backward)
7259          (or (gnus-summary-display-article (gnus-summary-article-number))
7260              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7261     (gnus-summary-position-point))
7262    ;; If not, we try the first unread, if that is wanted.
7263    ((and subject
7264          gnus-auto-select-same
7265          (gnus-summary-first-unread-article))
7266     (gnus-summary-position-point)
7267     (gnus-message 6 "Wrapped"))
7268    ;; Try to get next/previous article not displayed in this group.
7269    ((and gnus-auto-extend-newsgroup
7270          (not unread) (not subject))
7271     (gnus-summary-goto-article
7272      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7273      nil (count-lines (point-min) (point))))
7274    ;; Go to next/previous group.
7275    (t
7276     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7277       (gnus-summary-jump-to-group gnus-newsgroup-name))
7278     (let ((cmd last-command-char)
7279           (point
7280            (with-current-buffer gnus-group-buffer
7281              (point)))
7282           (group
7283            (if (eq gnus-keep-same-level 'best)
7284                (gnus-summary-best-group gnus-newsgroup-name)
7285              (gnus-summary-search-group backward gnus-keep-same-level))))
7286       ;; For some reason, the group window gets selected.  We change
7287       ;; it back.
7288       (select-window (get-buffer-window (current-buffer)))
7289       ;; Select next unread newsgroup automagically.
7290       (cond
7291        ((or (not gnus-auto-select-next)
7292             (not cmd))
7293         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7294        ((or (eq gnus-auto-select-next 'quietly)
7295             (and (eq gnus-auto-select-next 'slightly-quietly)
7296                  push)
7297             (and (eq gnus-auto-select-next 'almost-quietly)
7298                  (gnus-summary-last-article-p)))
7299         ;; Select quietly.
7300         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7301             (gnus-summary-exit)
7302           (gnus-message 7 "No more%s articles (%s)..."
7303                         (if unread " unread" "")
7304                         (if group (concat "selecting " group)
7305                           "exiting"))
7306           (gnus-summary-next-group nil group backward)))
7307        (t
7308         (when (gnus-key-press-event-p last-input-event)
7309           (gnus-summary-walk-group-buffer
7310            gnus-newsgroup-name cmd unread backward point))))))))
7311
7312 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7313   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7314                       (?\C-p (gnus-group-prev-unread-group 1))))
7315         (cursor-in-echo-area t)
7316         keve key group ended prompt)
7317     (save-excursion
7318       (set-buffer gnus-group-buffer)
7319       (goto-char start)
7320       (setq group
7321             (if (eq gnus-keep-same-level 'best)
7322                 (gnus-summary-best-group gnus-newsgroup-name)
7323               (gnus-summary-search-group backward gnus-keep-same-level))))
7324     (while (not ended)
7325       (setq prompt
7326             (format
7327              "No more%s articles%s " (if unread " unread" "")
7328              (if (and group
7329                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7330                  (format " (Type %s for %s [%s])"
7331                          (single-key-description cmd)
7332                          (gnus-group-decoded-name group)
7333                          (gnus-group-unread group))
7334                (format " (Type %s to exit %s)"
7335                        (single-key-description cmd)
7336                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7337       ;; Confirm auto selection.
7338       (setq key (car (setq keve (gnus-read-event-char prompt)))
7339             ended t)
7340       (cond
7341        ((assq key keystrokes)
7342         (let ((obuf (current-buffer)))
7343           (switch-to-buffer gnus-group-buffer)
7344           (when group
7345             (gnus-group-jump-to-group group))
7346           (eval (cadr (assq key keystrokes)))
7347           (setq group (gnus-group-group-name))
7348           (switch-to-buffer obuf))
7349         (setq ended nil))
7350        ((equal key cmd)
7351         (if (or (not group)
7352                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7353             (gnus-summary-exit)
7354           (gnus-summary-next-group nil group backward)))
7355        (t
7356         (push (cdr keve) unread-command-events))))))
7357
7358 (defun gnus-summary-next-unread-article ()
7359   "Select unread article after current one."
7360   (interactive)
7361   (gnus-summary-next-article
7362    (or (not (eq gnus-summary-goto-unread 'never))
7363        (gnus-summary-last-article-p (gnus-summary-article-number)))
7364    (and gnus-auto-select-same
7365         (gnus-summary-article-subject))))
7366
7367 (defun gnus-summary-prev-article (&optional unread subject)
7368   "Select the article after the current one.
7369 If UNREAD is non-nil, only unread articles are selected."
7370   (interactive "P")
7371   (gnus-summary-next-article unread subject t))
7372
7373 (defun gnus-summary-prev-unread-article ()
7374   "Select unread article before current one."
7375   (interactive)
7376   (gnus-summary-prev-article
7377    (or (not (eq gnus-summary-goto-unread 'never))
7378        (gnus-summary-first-article-p (gnus-summary-article-number)))
7379    (and gnus-auto-select-same
7380         (gnus-summary-article-subject))))
7381
7382 (defun gnus-summary-next-page (&optional lines circular stop)
7383   "Show next page of the selected article.
7384 If at the end of the current article, select the next article.
7385 LINES says how many lines should be scrolled up.
7386
7387 If CIRCULAR is non-nil, go to the start of the article instead of
7388 selecting the next article when reaching the end of the current
7389 article.
7390
7391 If STOP is non-nil, just stop when reaching the end of the message.
7392
7393 Also see the variable `gnus-article-skip-boring'."
7394   (interactive "P")
7395   (setq gnus-summary-buffer (current-buffer))
7396   (gnus-set-global-variables)
7397   (let ((article (gnus-summary-article-number))
7398         (article-window (get-buffer-window gnus-article-buffer t))
7399         endp)
7400     ;; If the buffer is empty, we have no article.
7401     (unless article
7402       (error "No article to select"))
7403     (gnus-configure-windows 'article)
7404     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7405         (if (and (eq gnus-summary-goto-unread 'never)
7406                  (not (gnus-summary-last-article-p article)))
7407             (gnus-summary-next-article)
7408           (gnus-summary-next-unread-article))
7409       (if (or (null gnus-current-article)
7410               (null gnus-article-current)
7411               (/= article (cdr gnus-article-current))
7412               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7413           ;; Selected subject is different from current article's.
7414           (gnus-summary-display-article article)
7415         (when article-window
7416           (gnus-eval-in-buffer-window gnus-article-buffer
7417             (setq endp (or (gnus-article-next-page lines)
7418                            (gnus-article-only-boring-p))))
7419           (when endp
7420             (cond (stop
7421                    (gnus-message 3 "End of message"))
7422                   (circular
7423                    (gnus-summary-beginning-of-article))
7424                   (lines
7425                    (gnus-message 3 "End of message"))
7426                   ((null lines)
7427                    (if (and (eq gnus-summary-goto-unread 'never)
7428                             (not (gnus-summary-last-article-p article)))
7429                        (gnus-summary-next-article)
7430                      (gnus-summary-next-unread-article))))))))
7431     (gnus-summary-recenter)
7432     (gnus-summary-position-point)))
7433
7434 (defun gnus-summary-prev-page (&optional lines move)
7435   "Show previous page of selected article.
7436 Argument LINES specifies lines to be scrolled down.
7437 If MOVE, move to the previous unread article if point is at
7438 the beginning of the buffer."
7439   (interactive "P")
7440   (let ((article (gnus-summary-article-number))
7441         (article-window (get-buffer-window gnus-article-buffer t))
7442         endp)
7443     (gnus-configure-windows 'article)
7444     (if (or (null gnus-current-article)
7445             (null gnus-article-current)
7446             (/= article (cdr gnus-article-current))
7447             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7448         ;; Selected subject is different from current article's.
7449         (gnus-summary-display-article article)
7450       (gnus-summary-recenter)
7451       (when article-window
7452         (gnus-eval-in-buffer-window gnus-article-buffer
7453           (setq endp (gnus-article-prev-page lines)))
7454         (when (and move endp)
7455           (cond (lines
7456                  (gnus-message 3 "Beginning of message"))
7457                 ((null lines)
7458                  (if (and (eq gnus-summary-goto-unread 'never)
7459                           (not (gnus-summary-first-article-p article)))
7460                      (gnus-summary-prev-article)
7461                    (gnus-summary-prev-unread-article))))))))
7462   (gnus-summary-position-point))
7463
7464 (defun gnus-summary-prev-page-or-article (&optional lines)
7465   "Show previous page of selected article.
7466 Argument LINES specifies lines to be scrolled down.
7467 If at the beginning of the article, go to the next article."
7468   (interactive "P")
7469   (gnus-summary-prev-page lines t))
7470
7471 (defun gnus-summary-scroll-up (lines)
7472   "Scroll up (or down) one line current article.
7473 Argument LINES specifies lines to be scrolled up (or down if negative)."
7474   (interactive "p")
7475   (gnus-configure-windows 'article)
7476   (gnus-summary-show-thread)
7477   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7478     (gnus-eval-in-buffer-window gnus-article-buffer
7479       (cond ((> lines 0)
7480              (when (gnus-article-next-page lines)
7481                (gnus-message 3 "End of message")))
7482             ((< lines 0)
7483              (gnus-article-prev-page (- lines))))))
7484   (gnus-summary-recenter)
7485   (gnus-summary-position-point))
7486
7487 (defun gnus-summary-scroll-down (lines)
7488   "Scroll down (or up) one line current article.
7489 Argument LINES specifies lines to be scrolled down (or up if negative)."
7490   (interactive "p")
7491   (gnus-summary-scroll-up (- lines)))
7492
7493 (defun gnus-summary-next-same-subject ()
7494   "Select next article which has the same subject as current one."
7495   (interactive)
7496   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7497
7498 (defun gnus-summary-prev-same-subject ()
7499   "Select previous article which has the same subject as current one."
7500   (interactive)
7501   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7502
7503 (defun gnus-summary-next-unread-same-subject ()
7504   "Select next unread article which has the same subject as current one."
7505   (interactive)
7506   (gnus-summary-next-article t (gnus-summary-article-subject)))
7507
7508 (defun gnus-summary-prev-unread-same-subject ()
7509   "Select previous unread article which has the same subject as current one."
7510   (interactive)
7511   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7512
7513 (defun gnus-summary-first-unread-article ()
7514   "Select the first unread article.
7515 Return nil if there are no unread articles."
7516   (interactive)
7517   (prog1
7518       (when (gnus-summary-first-subject t)
7519         (gnus-summary-show-thread)
7520         (gnus-summary-first-subject t)
7521         (gnus-summary-display-article (gnus-summary-article-number)))
7522     (gnus-summary-position-point)))
7523
7524 (defun gnus-summary-first-unread-subject ()
7525   "Place the point on the subject line of the first unread article.
7526 Return nil if there are no unread articles."
7527   (interactive)
7528   (prog1
7529       (when (gnus-summary-first-subject t)
7530         (gnus-summary-show-thread)
7531         (gnus-summary-first-subject t))
7532     (gnus-summary-position-point)))
7533
7534 (defun gnus-summary-first-unseen-subject ()
7535   "Place the point on the subject line of the first unseen article.
7536 Return nil if there are no unseen articles."
7537   (interactive)
7538   (prog1
7539       (when (gnus-summary-first-subject nil nil t)
7540         (gnus-summary-show-thread)
7541         (gnus-summary-first-subject nil nil t))
7542     (gnus-summary-position-point)))
7543
7544 (defun gnus-summary-first-unseen-or-unread-subject ()
7545   "Place the point on the subject line of the first unseen article or,
7546 if all article have been seen, on the subject line of the first unread
7547 article."
7548   (interactive)
7549   (prog1
7550       (unless (when (gnus-summary-first-subject nil nil t)
7551                 (gnus-summary-show-thread)
7552                 (gnus-summary-first-subject nil nil t))
7553         (when (gnus-summary-first-subject t)
7554           (gnus-summary-show-thread)
7555           (gnus-summary-first-subject t)))
7556     (gnus-summary-position-point)))
7557
7558 (defun gnus-summary-first-article ()
7559   "Select the first article.
7560 Return nil if there are no articles."
7561   (interactive)
7562   (prog1
7563       (when (gnus-summary-first-subject)
7564         (gnus-summary-show-thread)
7565         (gnus-summary-first-subject)
7566         (gnus-summary-display-article (gnus-summary-article-number)))
7567     (gnus-summary-position-point)))
7568
7569 (defun gnus-summary-best-unread-article (&optional arg)
7570   "Select the unread article with the highest score.
7571 If given a prefix argument, select the next unread article that has a
7572 score higher than the default score."
7573   (interactive "P")
7574   (let ((article (if arg
7575                      (gnus-summary-better-unread-subject)
7576                    (gnus-summary-best-unread-subject))))
7577     (if article
7578         (gnus-summary-goto-article article)
7579       (error "No unread articles"))))
7580
7581 (defun gnus-summary-best-unread-subject ()
7582   "Select the unread subject with the highest score."
7583   (interactive)
7584   (let ((best -1000000)
7585         (data gnus-newsgroup-data)
7586         article score)
7587     (while data
7588       (and (gnus-data-unread-p (car data))
7589            (> (setq score
7590                     (gnus-summary-article-score (gnus-data-number (car data))))
7591               best)
7592            (setq best score
7593                  article (gnus-data-number (car data))))
7594       (setq data (cdr data)))
7595     (when article
7596       (gnus-summary-goto-subject article))
7597     (gnus-summary-position-point)
7598     article))
7599
7600 (defun gnus-summary-better-unread-subject ()
7601   "Select the first unread subject that has a score over the default score."
7602   (interactive)
7603   (let ((data gnus-newsgroup-data)
7604         article score)
7605     (while (and (setq article (gnus-data-number (car data)))
7606                 (or (gnus-data-read-p (car data))
7607                     (not (> (gnus-summary-article-score article)
7608                             gnus-summary-default-score))))
7609       (setq data (cdr data)))
7610     (when article
7611       (gnus-summary-goto-subject article))
7612     (gnus-summary-position-point)
7613     article))
7614
7615 (defun gnus-summary-last-subject ()
7616   "Go to the last displayed subject line in the group."
7617   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7618     (when article
7619       (gnus-summary-goto-subject article))))
7620
7621 (defun gnus-summary-goto-article (article &optional all-headers force)
7622   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7623 If ALL-HEADERS is non-nil, no header lines are hidden.
7624 If FORCE, go to the article even if it isn't displayed.  If FORCE
7625 is a number, it is the line the article is to be displayed on."
7626   (interactive
7627    (list
7628     (completing-read
7629      "Article number or Message-ID: "
7630      (mapcar (lambda (number) (list (int-to-string number)))
7631              gnus-newsgroup-limit))
7632     current-prefix-arg
7633     t))
7634   (prog1
7635       (if (and (stringp article)
7636                (string-match "@\\|%40" article))
7637           (gnus-summary-refer-article article)
7638         (when (stringp article)
7639           (setq article (string-to-number article)))
7640         (if (gnus-summary-goto-subject article force)
7641             (gnus-summary-display-article article all-headers)
7642           (gnus-message 4 "Couldn't go to article %s" article) nil))
7643     (gnus-summary-position-point)))
7644
7645 (defun gnus-summary-goto-last-article ()
7646   "Go to the previously read article."
7647   (interactive)
7648   (prog1
7649       (when gnus-last-article
7650         (gnus-summary-goto-article gnus-last-article nil t))
7651     (gnus-summary-position-point)))
7652
7653 (defun gnus-summary-pop-article (number)
7654   "Pop one article off the history and go to the previous.
7655 NUMBER articles will be popped off."
7656   (interactive "p")
7657   (let (to)
7658     (setq gnus-newsgroup-history
7659           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7660     (if to
7661         (gnus-summary-goto-article (car to) nil t)
7662       (error "Article history empty")))
7663   (gnus-summary-position-point))
7664
7665 ;; Summary commands and functions for limiting the summary buffer.
7666
7667 (defun gnus-summary-limit-to-articles (n)
7668   "Limit the summary buffer to the next N articles.
7669 If not given a prefix, use the process marked articles instead."
7670   (interactive "P")
7671   (prog1
7672       (let ((articles (gnus-summary-work-articles n)))
7673         (setq gnus-newsgroup-processable nil)
7674         (gnus-summary-limit articles))
7675     (gnus-summary-position-point)))
7676
7677 (defun gnus-summary-pop-limit (&optional total)
7678   "Restore the previous limit.
7679 If given a prefix, remove all limits."
7680   (interactive "P")
7681   (when total
7682     (setq gnus-newsgroup-limits
7683           (list (mapcar (lambda (h) (mail-header-number h))
7684                         gnus-newsgroup-headers))))
7685   (unless gnus-newsgroup-limits
7686     (error "No limit to pop"))
7687   (prog1
7688       (gnus-summary-limit nil 'pop)
7689     (gnus-summary-position-point)))
7690
7691 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7692   "Limit the summary buffer to articles that have subjects that match a regexp.
7693 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7694   (interactive
7695    (list (read-string (if current-prefix-arg
7696                           "Exclude subject (regexp): "
7697                         "Limit to subject (regexp): "))
7698          nil current-prefix-arg))
7699   (unless header
7700     (setq header "subject"))
7701   (when (not (equal "" subject))
7702     (prog1
7703         (let ((articles (gnus-summary-find-matching
7704                          (or header "subject") subject 'all nil nil
7705                          not-matching)))
7706           (unless articles
7707             (error "Found no matches for \"%s\"" subject))
7708           (gnus-summary-limit articles))
7709       (gnus-summary-position-point))))
7710
7711 (defun gnus-summary-limit-to-author (from &optional not-matching)
7712   "Limit the summary buffer to articles that have authors that match a regexp.
7713 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7714   (interactive
7715    (list (read-string (if current-prefix-arg
7716                           "Exclude author (regexp): "
7717                         "Limit to author (regexp): "))
7718          current-prefix-arg))
7719   (gnus-summary-limit-to-subject from "from" not-matching))
7720
7721 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7722   "Limit the summary buffer to articles with the given RECIPIENT.
7723
7724 If NOT-MATCHING, exclude RECIPIENT.
7725
7726 To and Cc headers are checked.  You need to include them in
7727 `nnmail-extra-headers'."
7728   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7729   (interactive
7730    (list (read-string (format "%s recipient (regexp): "
7731                               (if current-prefix-arg "Exclude" "Limit to")))
7732          current-prefix-arg))
7733   (when (not (equal "" recipient))
7734     (prog1 (let* ((to
7735                    (if (memq 'To nnmail-extra-headers)
7736                        (gnus-summary-find-matching
7737                         (cons 'extra 'To) recipient 'all nil nil
7738                         not-matching)
7739                      (gnus-message
7740                       1 "`To' isn't present in `nnmail-extra-headers'")
7741                      (sit-for 1)
7742                      nil))
7743                   (cc
7744                    (if (memq 'Cc nnmail-extra-headers)
7745                        (gnus-summary-find-matching
7746                         (cons 'extra 'Cc) recipient 'all nil nil
7747                         not-matching)
7748                      (gnus-message
7749                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7750                      (sit-for 1)
7751                      nil))
7752                   (articles
7753                    (if not-matching
7754                        ;; We need the numbers that are in both lists:
7755                        (mapcar (lambda (a)
7756                                  (and (memq a to) a))
7757                                cc)
7758                      (nconc to cc))))
7759              (unless articles
7760                (error "Found no matches for \"%s\"" recipient))
7761              (gnus-summary-limit articles))
7762       (gnus-summary-position-point))))
7763
7764 (defun gnus-summary-limit-to-age (age &optional younger-p)
7765   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7766 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7767 articles that are younger than AGE days."
7768   (interactive
7769    (let ((younger current-prefix-arg)
7770          (days-got nil)
7771          days)
7772      (while (not days-got)
7773        (setq days (if younger
7774                       (read-string "Limit to articles younger than (in days, older when negative): ")
7775                     (read-string
7776                      "Limit to articles older than (in days, younger when negative): ")))
7777        (when (> (length days) 0)
7778          (setq days (read days)))
7779        (if (numberp days)
7780            (progn
7781              (setq days-got t)
7782              (when (< days 0)
7783                (setq younger (not younger))
7784                (setq days (* days -1))))
7785          (message "Please enter a number.")
7786          (sleep-for 1)))
7787      (list days younger)))
7788   (prog1
7789       (let ((data gnus-newsgroup-data)
7790             (cutoff (days-to-time age))
7791             articles d date is-younger)
7792         (while (setq d (pop data))
7793           (when (and (vectorp (gnus-data-header d))
7794                      (setq date (mail-header-date (gnus-data-header d))))
7795             (setq is-younger (time-less-p
7796                               (time-since (condition-case ()
7797                                               (date-to-time date)
7798                                             (error '(0 0))))
7799                               cutoff))
7800             (when (if younger-p
7801                       is-younger
7802                     (not is-younger))
7803               (push (gnus-data-number d) articles))))
7804         (gnus-summary-limit (nreverse articles)))
7805     (gnus-summary-position-point)))
7806
7807 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7808   "Limit the summary buffer to articles that match an 'extra' header."
7809   (interactive
7810    (let ((header
7811           (intern
7812            (gnus-completing-read-with-default
7813             (symbol-name (car gnus-extra-headers))
7814             (if current-prefix-arg
7815                 "Exclude extra header"
7816               "Limit extra header")
7817             (mapcar (lambda (x)
7818                       (cons (symbol-name x) x))
7819                     gnus-extra-headers)
7820             nil
7821             t))))
7822      (list header
7823            (read-string (format "%s header %s (regexp): "
7824                                 (if current-prefix-arg "Exclude" "Limit to")
7825                                 header))
7826            current-prefix-arg)))
7827   (when (not (equal "" regexp))
7828     (prog1
7829         (let ((articles (gnus-summary-find-matching
7830                          (cons 'extra header) regexp 'all nil nil
7831                          not-matching)))
7832           (unless articles
7833             (error "Found no matches for \"%s\"" regexp))
7834           (gnus-summary-limit articles))
7835       (gnus-summary-position-point))))
7836
7837 (defun gnus-summary-limit-to-display-predicate ()
7838   "Limit the summary buffer to the predicated in the `display' group parameter."
7839   (interactive)
7840   (unless gnus-newsgroup-display
7841     (error "There is no `display' group parameter"))
7842   (let (articles)
7843     (dolist (number gnus-newsgroup-articles)
7844       (when (funcall gnus-newsgroup-display)
7845         (push number articles)))
7846     (gnus-summary-limit articles))
7847   (gnus-summary-position-point))
7848
7849 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7850 (make-obsolete
7851  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7852
7853 (defun gnus-summary-limit-to-unread (&optional all)
7854   "Limit the summary buffer to articles that are not marked as read.
7855 If ALL is non-nil, limit strictly to unread articles."
7856   (interactive "P")
7857   (if all
7858       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7859     (gnus-summary-limit-to-marks
7860      ;; Concat all the marks that say that an article is read and have
7861      ;; those removed.
7862      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7863            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7864            gnus-low-score-mark gnus-expirable-mark
7865            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7866            gnus-duplicate-mark gnus-souped-mark)
7867      'reverse)))
7868
7869 (defun gnus-summary-limit-to-replied (&optional unreplied)
7870   "Limit the summary buffer to replied articles.
7871 If UNREPLIED (the prefix), limit to unreplied articles."
7872   (interactive "P")
7873   (if unreplied
7874       (gnus-summary-limit
7875        (gnus-set-difference gnus-newsgroup-articles
7876         gnus-newsgroup-replied))
7877     (gnus-summary-limit gnus-newsgroup-replied))
7878   (gnus-summary-position-point))
7879
7880 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7881 (make-obsolete 'gnus-summary-delete-marked-with
7882                'gnus-summary-limit-exclude-marks)
7883
7884 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7885   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7886 If REVERSE, limit the summary buffer to articles that are marked
7887 with MARKS.  MARKS can either be a string of marks or a list of marks.
7888 Returns how many articles were removed."
7889   (interactive "sMarks: ")
7890   (gnus-summary-limit-to-marks marks t))
7891
7892 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7893   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7894 If REVERSE (the prefix), limit the summary buffer to articles that are
7895 not marked with MARKS.  MARKS can either be a string of marks or a
7896 list of marks.
7897 Returns how many articles were removed."
7898   (interactive "sMarks: \nP")
7899   (prog1
7900       (let ((data gnus-newsgroup-data)
7901             (marks (if (listp marks) marks
7902                      (append marks nil))) ; Transform to list.
7903             articles)
7904         (while data
7905           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7906                   (memq (gnus-data-mark (car data)) marks))
7907             (push (gnus-data-number (car data)) articles))
7908           (setq data (cdr data)))
7909         (gnus-summary-limit articles))
7910     (gnus-summary-position-point)))
7911
7912 (defun gnus-summary-limit-to-score (score)
7913   "Limit to articles with score at or above SCORE."
7914   (interactive "NLimit to articles with score of at least: ")
7915   (let ((data gnus-newsgroup-data)
7916         articles)
7917     (while data
7918       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7919                 score)
7920         (push (gnus-data-number (car data)) articles))
7921       (setq data (cdr data)))
7922     (prog1
7923         (gnus-summary-limit articles)
7924       (gnus-summary-position-point))))
7925
7926 (defun gnus-summary-limit-to-unseen ()
7927   "Limit to unseen articles."
7928   (interactive)
7929   (prog1
7930       (gnus-summary-limit gnus-newsgroup-unseen)
7931     (gnus-summary-position-point)))
7932
7933 (defun gnus-summary-limit-include-thread (id)
7934   "Display all the hidden articles that is in the thread with ID in it.
7935 When called interactively, ID is the Message-ID of the current
7936 article."
7937   (interactive (list (mail-header-id (gnus-summary-article-header))))
7938   (let ((articles (gnus-articles-in-thread
7939                    (gnus-id-to-thread (gnus-root-id id)))))
7940     (prog1
7941         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7942       (gnus-summary-limit-include-matching-articles
7943        "subject"
7944        (regexp-quote (gnus-simplify-subject-re
7945                       (mail-header-subject (gnus-id-to-header id)))))
7946       (gnus-summary-position-point))))
7947
7948 (defun gnus-summary-limit-include-matching-articles (header regexp)
7949   "Display all the hidden articles that have HEADERs that match REGEXP."
7950   (interactive (list (read-string "Match on header: ")
7951                      (read-string "Regexp: ")))
7952   (let ((articles (gnus-find-matching-articles header regexp)))
7953     (prog1
7954         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7955       (gnus-summary-position-point))))
7956
7957 (defun gnus-summary-insert-dormant-articles ()
7958   "Insert all the dormant articles for this group into the current buffer."
7959   (interactive)
7960   (let ((gnus-verbose (max 6 gnus-verbose)))
7961     (if (not gnus-newsgroup-dormant)
7962         (gnus-message 3 "No cached articles for this group")
7963       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7964
7965 (defun gnus-summary-limit-include-dormant ()
7966   "Display all the hidden articles that are marked as dormant.
7967 Note that this command only works on a subset of the articles currently
7968 fetched for this group."
7969   (interactive)
7970   (unless gnus-newsgroup-dormant
7971     (error "There are no dormant articles in this group"))
7972   (prog1
7973       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7974     (gnus-summary-position-point)))
7975
7976 (defun gnus-summary-limit-exclude-dormant ()
7977   "Hide all dormant articles."
7978   (interactive)
7979   (prog1
7980       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7981     (gnus-summary-position-point)))
7982
7983 (defun gnus-summary-limit-exclude-childless-dormant ()
7984   "Hide all dormant articles that have no children."
7985   (interactive)
7986   (let ((data (gnus-data-list t))
7987         articles d children)
7988     ;; Find all articles that are either not dormant or have
7989     ;; children.
7990     (while (setq d (pop data))
7991       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7992                 (and (setq children
7993                            (gnus-article-children (gnus-data-number d)))
7994                      (let (found)
7995                        (while children
7996                          (when (memq (car children) articles)
7997                            (setq children nil
7998                                  found t))
7999                          (pop children))
8000                        found)))
8001         (push (gnus-data-number d) articles)))
8002     ;; Do the limiting.
8003     (prog1
8004         (gnus-summary-limit articles)
8005       (gnus-summary-position-point))))
8006
8007 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8008   "Mark all unread excluded articles as read.
8009 If ALL, mark even excluded ticked and dormants as read."
8010   (interactive "P")
8011   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8012   (let ((articles (gnus-sorted-ndifference
8013                    (sort
8014                     (mapcar (lambda (h) (mail-header-number h))
8015                             gnus-newsgroup-headers)
8016                     '<)
8017                    gnus-newsgroup-limit))
8018         article)
8019     (setq gnus-newsgroup-unreads
8020           (gnus-sorted-intersection gnus-newsgroup-unreads
8021                                     gnus-newsgroup-limit))
8022     (if all
8023         (setq gnus-newsgroup-dormant nil
8024               gnus-newsgroup-marked nil
8025               gnus-newsgroup-reads
8026               (nconc
8027                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8028                gnus-newsgroup-reads))
8029       (while (setq article (pop articles))
8030         (unless (or (memq article gnus-newsgroup-dormant)
8031                     (memq article gnus-newsgroup-marked))
8032           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8033
8034 (defun gnus-summary-limit (articles &optional pop)
8035   (if pop
8036       ;; We pop the previous limit off the stack and use that.
8037       (setq articles (car gnus-newsgroup-limits)
8038             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8039     ;; We use the new limit, so we push the old limit on the stack.
8040     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8041   ;; Set the limit.
8042   (setq gnus-newsgroup-limit articles)
8043   (let ((total (length gnus-newsgroup-data))
8044         (data (gnus-data-find-list (gnus-summary-article-number)))
8045         (gnus-summary-mark-below nil)   ; Inhibit this.
8046         found)
8047     ;; This will do all the work of generating the new summary buffer
8048     ;; according to the new limit.
8049     (gnus-summary-prepare)
8050     ;; Hide any threads, possibly.
8051     (gnus-summary-maybe-hide-threads)
8052     ;; Try to return to the article you were at, or one in the
8053     ;; neighborhood.
8054     (when data
8055       ;; We try to find some article after the current one.
8056       (while data
8057         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8058           (setq data nil
8059                 found t))
8060         (setq data (cdr data))))
8061     (unless found
8062       ;; If there is no data, that means that we were after the last
8063       ;; article.  The same goes when we can't find any articles
8064       ;; after the current one.
8065       (goto-char (point-max))
8066       (gnus-summary-find-prev))
8067     (gnus-set-mode-line 'summary)
8068     ;; We return how many articles were removed from the summary
8069     ;; buffer as a result of the new limit.
8070     (- total (length gnus-newsgroup-data))))
8071
8072 (defsubst gnus-invisible-cut-children (threads)
8073   (let ((num 0))
8074     (while threads
8075       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8076         (incf num))
8077       (pop threads))
8078     (< num 2)))
8079
8080 (defsubst gnus-cut-thread (thread)
8081   "Go forwards in the thread until we find an article that we want to display."
8082   (when (or (eq gnus-fetch-old-headers 'some)
8083             (eq gnus-fetch-old-headers 'invisible)
8084             (numberp gnus-fetch-old-headers)
8085             (eq gnus-build-sparse-threads 'some)
8086             (eq gnus-build-sparse-threads 'more))
8087     ;; Deal with old-fetched headers and sparse threads.
8088     (while (and
8089             thread
8090             (or
8091              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8092              (gnus-summary-article-ancient-p
8093               (mail-header-number (car thread))))
8094             (if (or (<= (length (cdr thread)) 1)
8095                     (eq gnus-fetch-old-headers 'invisible))
8096                 (setq gnus-newsgroup-limit
8097                       (delq (mail-header-number (car thread))
8098                             gnus-newsgroup-limit)
8099                       thread (cadr thread))
8100               (when (gnus-invisible-cut-children (cdr thread))
8101                 (let ((th (cdr thread)))
8102                   (while th
8103                     (if (memq (mail-header-number (caar th))
8104                               gnus-newsgroup-limit)
8105                         (setq thread (car th)
8106                               th nil)
8107                       (setq th (cdr th))))))))))
8108   thread)
8109
8110 (defun gnus-cut-threads (threads)
8111   "Cut off all uninteresting articles from the beginning of THREADS."
8112   (when (or (eq gnus-fetch-old-headers 'some)
8113             (eq gnus-fetch-old-headers 'invisible)
8114             (numberp gnus-fetch-old-headers)
8115             (eq gnus-build-sparse-threads 'some)
8116             (eq gnus-build-sparse-threads 'more))
8117     (let ((th threads))
8118       (while th
8119         (setcar th (gnus-cut-thread (car th)))
8120         (setq th (cdr th)))))
8121   ;; Remove nixed out threads.
8122   (delq nil threads))
8123
8124 (defun gnus-summary-initial-limit (&optional show-if-empty)
8125   "Figure out what the initial limit is supposed to be on group entry.
8126 This entails weeding out unwanted dormants, low-scored articles,
8127 fetch-old-headers verbiage, and so on."
8128   ;; Most groups have nothing to remove.
8129   (if (or gnus-inhibit-limiting
8130           (and (null gnus-newsgroup-dormant)
8131                (eq gnus-newsgroup-display 'gnus-not-ignore)
8132                (not (eq gnus-fetch-old-headers 'some))
8133                (not (numberp gnus-fetch-old-headers))
8134                (not (eq gnus-fetch-old-headers 'invisible))
8135                (null gnus-summary-expunge-below)
8136                (not (eq gnus-build-sparse-threads 'some))
8137                (not (eq gnus-build-sparse-threads 'more))
8138                (null gnus-thread-expunge-below)
8139                (not gnus-use-nocem)))
8140       ()                                ; Do nothing.
8141     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8142     (setq gnus-newsgroup-limit nil)
8143     (mapatoms
8144      (lambda (node)
8145        (unless (car (symbol-value node))
8146          ;; These threads have no parents -- they are roots.
8147          (let ((nodes (cdr (symbol-value node)))
8148                thread)
8149            (while nodes
8150              (if (and gnus-thread-expunge-below
8151                       (< (gnus-thread-total-score (car nodes))
8152                          gnus-thread-expunge-below))
8153                  (gnus-expunge-thread (pop nodes))
8154                (setq thread (pop nodes))
8155                (gnus-summary-limit-children thread))))))
8156      gnus-newsgroup-dependencies)
8157     ;; If this limitation resulted in an empty group, we might
8158     ;; pop the previous limit and use it instead.
8159     (when (and (not gnus-newsgroup-limit)
8160                show-if-empty)
8161       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8162     gnus-newsgroup-limit))
8163
8164 (defun gnus-summary-limit-children (thread)
8165   "Return 1 if this subthread is visible and 0 if it is not."
8166   ;; First we get the number of visible children to this thread.  This
8167   ;; is done by recursing down the thread using this function, so this
8168   ;; will really go down to a leaf article first, before slowly
8169   ;; working its way up towards the root.
8170   (when thread
8171     (let* ((max-lisp-eval-depth 5000)
8172            (children
8173            (if (cdr thread)
8174                (apply '+ (mapcar 'gnus-summary-limit-children
8175                                  (cdr thread)))
8176              0))
8177           (number (mail-header-number (car thread)))
8178           score)
8179       (if (and
8180            (not (memq number gnus-newsgroup-marked))
8181            (or
8182             ;; If this article is dormant and has absolutely no visible
8183             ;; children, then this article isn't visible.
8184             (and (memq number gnus-newsgroup-dormant)
8185                  (zerop children))
8186             ;; If this is "fetch-old-headered" and there is no
8187             ;; visible children, then we don't want this article.
8188             (and (or (eq gnus-fetch-old-headers 'some)
8189                      (numberp gnus-fetch-old-headers))
8190                  (gnus-summary-article-ancient-p number)
8191                  (zerop children))
8192             ;; If this is "fetch-old-headered" and `invisible', then
8193             ;; we don't want this article.
8194             (and (eq gnus-fetch-old-headers 'invisible)
8195                  (gnus-summary-article-ancient-p number))
8196             ;; If this is a sparsely inserted article with no children,
8197             ;; we don't want it.
8198             (and (eq gnus-build-sparse-threads 'some)
8199                  (gnus-summary-article-sparse-p number)
8200                  (zerop children))
8201             ;; If we use expunging, and this article is really
8202             ;; low-scored, then we don't want this article.
8203             (when (and gnus-summary-expunge-below
8204                        (< (setq score
8205                                 (or (cdr (assq number gnus-newsgroup-scored))
8206                                     gnus-summary-default-score))
8207                           gnus-summary-expunge-below))
8208               ;; We increase the expunge-tally here, but that has
8209               ;; nothing to do with the limits, really.
8210               (incf gnus-newsgroup-expunged-tally)
8211               ;; We also mark as read here, if that's wanted.
8212               (when (and gnus-summary-mark-below
8213                          (< score gnus-summary-mark-below))
8214                 (setq gnus-newsgroup-unreads
8215                       (delq number gnus-newsgroup-unreads))
8216                 (if gnus-newsgroup-auto-expire
8217                     (push number gnus-newsgroup-expirable)
8218                   (push (cons number gnus-low-score-mark)
8219                         gnus-newsgroup-reads)))
8220               t)
8221             ;; Do the `display' group parameter.
8222             (and gnus-newsgroup-display
8223                  (not (funcall gnus-newsgroup-display)))
8224             ;; Check NoCeM things.
8225             (when (and gnus-use-nocem
8226                        (gnus-nocem-unwanted-article-p
8227                         (mail-header-id (car thread))))
8228               (setq gnus-newsgroup-unreads
8229                     (delq number gnus-newsgroup-unreads))
8230               t)))
8231           ;; Nope, invisible article.
8232           0
8233         ;; Ok, this article is to be visible, so we add it to the limit
8234         ;; and return 1.
8235         (push number gnus-newsgroup-limit)
8236         1))))
8237
8238 (defun gnus-expunge-thread (thread)
8239   "Mark all articles in THREAD as read."
8240   (let* ((number (mail-header-number (car thread))))
8241     (incf gnus-newsgroup-expunged-tally)
8242     ;; We also mark as read here, if that's wanted.
8243     (setq gnus-newsgroup-unreads
8244           (delq number gnus-newsgroup-unreads))
8245     (if gnus-newsgroup-auto-expire
8246         (push number gnus-newsgroup-expirable)
8247       (push (cons number gnus-low-score-mark)
8248             gnus-newsgroup-reads)))
8249   ;; Go recursively through all subthreads.
8250   (mapcar 'gnus-expunge-thread (cdr thread)))
8251
8252 ;; Summary article oriented commands
8253
8254 (defun gnus-summary-refer-parent-article (n)
8255   "Refer parent article N times.
8256 If N is negative, go to ancestor -N instead.
8257 The difference between N and the number of articles fetched is returned."
8258   (interactive "p")
8259   (let ((skip 1)
8260         error header ref)
8261     (when (not (natnump n))
8262       (setq skip (abs n)
8263             n 1))
8264     (while (and (> n 0)
8265                 (not error))
8266       (setq header (gnus-summary-article-header))
8267       (if (and (eq (mail-header-number header)
8268                    (cdr gnus-article-current))
8269                (equal gnus-newsgroup-name
8270                       (car gnus-article-current)))
8271           ;; If we try to find the parent of the currently
8272           ;; displayed article, then we take a look at the actual
8273           ;; References header, since this is slightly more
8274           ;; reliable than the References field we got from the
8275           ;; server.
8276           (save-excursion
8277             (set-buffer gnus-original-article-buffer)
8278             (nnheader-narrow-to-headers)
8279             (unless (setq ref (message-fetch-field "references"))
8280               (when (setq ref (message-fetch-field "in-reply-to"))
8281                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8282             (widen))
8283         (setq ref
8284               ;; It's not the current article, so we take a bet on
8285               ;; the value we got from the server.
8286               (mail-header-references header)))
8287       (if (and ref
8288                (not (equal ref "")))
8289           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8290             (gnus-message 1 "Couldn't find parent"))
8291         (gnus-message 1 "No references in article %d"
8292                       (gnus-summary-article-number))
8293         (setq error t))
8294       (decf n))
8295     (gnus-summary-position-point)
8296     n))
8297
8298 (defun gnus-summary-refer-references ()
8299   "Fetch all articles mentioned in the References header.
8300 Return the number of articles fetched."
8301   (interactive)
8302   (let ((ref (mail-header-references (gnus-summary-article-header)))
8303         (current (gnus-summary-article-number))
8304         (n 0))
8305     (if (or (not ref)
8306             (equal ref ""))
8307         (error "No References in the current article")
8308       ;; For each Message-ID in the References header...
8309       (while (string-match "<[^>]*>" ref)
8310         (incf n)
8311         ;; ... fetch that article.
8312         (gnus-summary-refer-article
8313          (prog1 (match-string 0 ref)
8314            (setq ref (substring ref (match-end 0))))))
8315       (gnus-summary-goto-subject current)
8316       (gnus-summary-position-point)
8317       n)))
8318
8319 (defun gnus-summary-refer-thread (&optional limit)
8320   "Fetch all articles in the current thread.
8321 If LIMIT (the numerical prefix), fetch that many old headers instead
8322 of what's specified by the `gnus-refer-thread-limit' variable."
8323   (interactive "P")
8324   (let ((id (mail-header-id (gnus-summary-article-header)))
8325         (limit (if limit (prefix-numeric-value limit)
8326                  gnus-refer-thread-limit)))
8327     (unless (eq gnus-fetch-old-headers 'invisible)
8328       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8329       ;; Retrieve the headers and read them in.
8330       (if (eq (if (numberp limit)
8331                   (gnus-retrieve-headers
8332                    (list (min
8333                           (+ (mail-header-number
8334                               (gnus-summary-article-header))
8335                              limit)
8336                           gnus-newsgroup-end))
8337                    gnus-newsgroup-name (* limit 2))
8338                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8339                 ;; headers.
8340                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8341                                        gnus-newsgroup-name limit))
8342               'nov)
8343           (gnus-build-all-threads)
8344         (error "Can't fetch thread from back ends that don't support NOV"))
8345       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8346     (gnus-summary-limit-include-thread id)))
8347
8348 (defun gnus-summary-refer-article (message-id)
8349   "Fetch an article specified by MESSAGE-ID."
8350   (interactive "sMessage-ID: ")
8351   (when (and (stringp message-id)
8352              (not (zerop (length message-id))))
8353     (setq message-id (gnus-replace-in-string message-id " " ""))
8354     ;; Construct the correct Message-ID if necessary.
8355     ;; Suggested by tale@pawl.rpi.edu.
8356     (unless (string-match "^<" message-id)
8357       (setq message-id (concat "<" message-id)))
8358     (unless (string-match ">$" message-id)
8359       (setq message-id (concat message-id ">")))
8360     ;; People often post MIDs from URLs, so unhex it:
8361     (unless (string-match "@" message-id)
8362       (setq message-id (gnus-url-unhex-string message-id)))
8363     (let* ((header (gnus-id-to-header message-id))
8364            (sparse (and header
8365                         (gnus-summary-article-sparse-p
8366                          (mail-header-number header))
8367                         (memq (mail-header-number header)
8368                               gnus-newsgroup-limit)))
8369            number)
8370       (cond
8371        ;; If the article is present in the buffer we just go to it.
8372        ((and header
8373              (or (not (gnus-summary-article-sparse-p
8374                        (mail-header-number header)))
8375                  sparse))
8376         (prog1
8377             (gnus-summary-goto-article
8378              (mail-header-number header) nil t)
8379           (when sparse
8380             (gnus-summary-update-article (mail-header-number header)))))
8381        (t
8382         ;; We fetch the article.
8383         (catch 'found
8384           (dolist (gnus-override-method (gnus-refer-article-methods))
8385             (when (and (gnus-check-server gnus-override-method)
8386                        ;; Fetch the header,
8387                        (setq number (gnus-summary-insert-subject message-id)))
8388               ;; and display the article.
8389               (gnus-summary-select-article nil nil nil number)
8390               (throw 'found t)))
8391           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8392
8393 (defun gnus-refer-article-methods ()
8394   "Return a list of referable methods."
8395   (cond
8396    ;; No method, so we default to current and native.
8397    ((null gnus-refer-article-method)
8398     (list gnus-current-select-method gnus-select-method))
8399    ;; Current.
8400    ((eq 'current gnus-refer-article-method)
8401     (list gnus-current-select-method))
8402    ;; List of select methods.
8403    ((not (and (symbolp (car gnus-refer-article-method))
8404               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8405     (let (out)
8406       (dolist (method gnus-refer-article-method)
8407         (push (if (eq 'current method)
8408                   gnus-current-select-method
8409                 method)
8410               out))
8411       (nreverse out)))
8412    ;; One single select method.
8413    (t
8414     (list gnus-refer-article-method))))
8415
8416 (defun gnus-summary-edit-parameters ()
8417   "Edit the group parameters of the current group."
8418   (interactive)
8419   (gnus-group-edit-group gnus-newsgroup-name 'params))
8420
8421 (defun gnus-summary-customize-parameters ()
8422   "Customize the group parameters of the current group."
8423   (interactive)
8424   (gnus-group-customize gnus-newsgroup-name))
8425
8426 (defun gnus-summary-enter-digest-group (&optional force)
8427   "Enter an nndoc group based on the current article.
8428 If FORCE, force a digest interpretation.  If not, try
8429 to guess what the document format is."
8430   (interactive "P")
8431   (let ((conf gnus-current-window-configuration))
8432     (save-window-excursion
8433       (save-excursion
8434         (let (gnus-article-prepare-hook
8435               gnus-display-mime-function
8436               gnus-break-pages)
8437           (gnus-summary-select-article))))
8438     (setq gnus-current-window-configuration conf)
8439     (let* ((name (format "%s-%d"
8440                          (gnus-group-prefixed-name
8441                           gnus-newsgroup-name (list 'nndoc ""))
8442                          (with-current-buffer gnus-summary-buffer
8443                            gnus-current-article)))
8444            (ogroup gnus-newsgroup-name)
8445            (params (append (gnus-info-params (gnus-get-info ogroup))
8446                            (list (cons 'to-group ogroup))
8447                            (list (cons 'parent-group ogroup))
8448                            (list (cons 'save-article-group ogroup))))
8449            (case-fold-search t)
8450            (buf (current-buffer))
8451            dig to-address)
8452       (save-excursion
8453         (set-buffer gnus-original-article-buffer)
8454         ;; Have the digest group inherit the main mail address of
8455         ;; the parent article.
8456         (when (setq to-address (or (gnus-fetch-field "reply-to")
8457                                    (gnus-fetch-field "from")))
8458           (setq params (append
8459                         (list (cons 'to-address
8460                                     (funcall gnus-decode-encoded-word-function
8461                                              to-address))))))
8462         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8463         (insert-buffer-substring gnus-original-article-buffer)
8464         ;; Remove lines that may lead nndoc to misinterpret the
8465         ;; document type.
8466         (narrow-to-region
8467          (goto-char (point-min))
8468          (or (search-forward "\n\n" nil t) (point)))
8469         (goto-char (point-min))
8470         (delete-matching-lines "^Path:\\|^From ")
8471         (widen))
8472       (unwind-protect
8473           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8474                     (gnus-newsgroup-ephemeral-ignored-charsets
8475                      gnus-newsgroup-ignored-charsets))
8476                 (gnus-group-read-ephemeral-group
8477                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8478                               (nndoc-article-type
8479                                ,(if force 'mbox 'guess)))
8480                  t nil nil nil
8481                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8482                                                         "ADAPT")))))
8483               ;; Make all postings to this group go to the parent group.
8484               (nconc (gnus-info-params (gnus-get-info name))
8485                      params)
8486             ;; Couldn't select this doc group.
8487             (switch-to-buffer buf)
8488             (gnus-set-global-variables)
8489             (gnus-configure-windows 'summary)
8490             (gnus-message 3 "Article couldn't be entered?"))
8491         (kill-buffer dig)))))
8492
8493 (defun gnus-summary-read-document (n)
8494   "Open a new group based on the current article(s).
8495 This will allow you to read digests and other similar
8496 documents as newsgroups.
8497 Obeys the standard process/prefix convention."
8498   (interactive "P")
8499   (let* ((ogroup gnus-newsgroup-name)
8500          (params (append (gnus-info-params (gnus-get-info ogroup))
8501                          (list (cons 'to-group ogroup))))
8502          group egroup groups vgroup)
8503     (dolist (article (gnus-summary-work-articles n))
8504       (setq group (format "%s-%d" gnus-newsgroup-name article))
8505       (gnus-summary-remove-process-mark article)
8506       (when (gnus-summary-display-article article)
8507         (save-excursion
8508           (with-temp-buffer
8509             (insert-buffer-substring gnus-original-article-buffer)
8510             ;; Remove some headers that may lead nndoc to make
8511             ;; the wrong guess.
8512             (message-narrow-to-head)
8513             (goto-char (point-min))
8514             (delete-matching-lines "^Path:\\|^From ")
8515             (widen)
8516             (if (setq egroup
8517                       (gnus-group-read-ephemeral-group
8518                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8519                                      (nndoc-article-type guess))
8520                        t nil t))
8521                 (progn
8522             ;; Make all postings to this group go to the parent group.
8523                   (nconc (gnus-info-params (gnus-get-info egroup))
8524                          params)
8525                   (push egroup groups))
8526               ;; Couldn't select this doc group.
8527               (gnus-error 3 "Article couldn't be entered"))))))
8528     ;; Now we have selected all the documents.
8529     (cond
8530      ((not groups)
8531       (error "None of the articles could be interpreted as documents"))
8532      ((gnus-group-read-ephemeral-group
8533        (setq vgroup (format
8534                      "nnvirtual:%s-%s" gnus-newsgroup-name
8535                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8536        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8537        t
8538        (cons (current-buffer) 'summary)))
8539      (t
8540       (error "Couldn't select virtual nndoc group")))))
8541
8542 (defun gnus-summary-isearch-article (&optional regexp-p)
8543   "Do incremental search forward on the current article.
8544 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8545   (interactive "P")
8546   (gnus-summary-select-article)
8547   (gnus-configure-windows 'article)
8548   (gnus-eval-in-buffer-window gnus-article-buffer
8549     (save-restriction
8550       (widen)
8551       (isearch-forward regexp-p))))
8552
8553 (defun gnus-summary-search-article-forward (regexp &optional backward)
8554   "Search for an article containing REGEXP forward.
8555 If BACKWARD, search backward instead."
8556   (interactive
8557    (list (read-string
8558           (format "Search article %s (regexp%s): "
8559                   (if current-prefix-arg "backward" "forward")
8560                   (if gnus-last-search-regexp
8561                       (concat ", default " gnus-last-search-regexp)
8562                     "")))
8563          current-prefix-arg))
8564   (if (string-equal regexp "")
8565       (setq regexp (or gnus-last-search-regexp ""))
8566     (setq gnus-last-search-regexp regexp)
8567     (setq gnus-article-before-search gnus-current-article))
8568   ;; Intentionally set gnus-last-article.
8569   (setq gnus-last-article gnus-article-before-search)
8570   (let ((gnus-last-article gnus-last-article))
8571     (if (gnus-summary-search-article regexp backward)
8572         (gnus-summary-show-thread)
8573       (signal 'search-failed (list regexp)))))
8574
8575 (defun gnus-summary-search-article-backward (regexp)
8576   "Search for an article containing REGEXP backward."
8577   (interactive
8578    (list (read-string
8579           (format "Search article backward (regexp%s): "
8580                   (if gnus-last-search-regexp
8581                       (concat ", default " gnus-last-search-regexp)
8582                     "")))))
8583   (gnus-summary-search-article-forward regexp 'backward))
8584
8585 (defun gnus-summary-search-article (regexp &optional backward)
8586   "Search for an article containing REGEXP.
8587 Optional argument BACKWARD means do search for backward.
8588 `gnus-select-article-hook' is not called during the search."
8589   ;; We have to require this here to make sure that the following
8590   ;; dynamic binding isn't shadowed by autoloading.
8591   (require 'gnus-async)
8592   (require 'gnus-art)
8593   (let ((gnus-select-article-hook nil)  ;Disable hook.
8594         (gnus-article-prepare-hook nil)
8595         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8596         (gnus-use-article-prefetch nil)
8597         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8598         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8599         (gnus-visual nil)
8600         (gnus-keep-backlog nil)
8601         (gnus-break-pages nil)
8602         (gnus-summary-display-arrow nil)
8603         (gnus-updated-mode-lines nil)
8604         (gnus-auto-center-summary nil)
8605         (sum (current-buffer))
8606         (gnus-display-mime-function nil)
8607         (found nil)
8608         point)
8609     (gnus-save-hidden-threads
8610       (gnus-summary-select-article)
8611       (set-buffer gnus-article-buffer)
8612       (goto-char (window-point (get-buffer-window (current-buffer))))
8613       (when backward
8614         (forward-line -1))
8615       (while (not found)
8616         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8617         (if (if backward
8618                 (re-search-backward regexp nil t)
8619               (re-search-forward regexp nil t))
8620             ;; We found the regexp.
8621             (progn
8622               (setq found 'found)
8623               (beginning-of-line)
8624               (set-window-start
8625                (get-buffer-window (current-buffer))
8626                (point))
8627               (forward-line 1)
8628               (set-window-point
8629                (get-buffer-window (current-buffer))
8630                (point))
8631               (set-buffer sum)
8632               (setq point (point)))
8633           ;; We didn't find it, so we go to the next article.
8634           (set-buffer sum)
8635           (setq found 'not)
8636           (while (eq found 'not)
8637             (if (not (if backward (gnus-summary-find-prev)
8638                        (gnus-summary-find-next)))
8639                 ;; No more articles.
8640                 (setq found t)
8641               ;; Select the next article and adjust point.
8642               (unless (gnus-summary-article-sparse-p
8643                        (gnus-summary-article-number))
8644                 (setq found nil)
8645                 (gnus-summary-select-article)
8646                 (set-buffer gnus-article-buffer)
8647                 (widen)
8648                 (goto-char (if backward (point-max) (point-min))))))))
8649       (gnus-message 7 ""))
8650     ;; Return whether we found the regexp.
8651     (when (eq found 'found)
8652       (goto-char point)
8653       (gnus-summary-show-thread)
8654       (gnus-summary-goto-subject gnus-current-article)
8655       (gnus-summary-position-point)
8656       t)))
8657
8658 (defun gnus-find-matching-articles (header regexp)
8659   "Return a list of all articles that match REGEXP on HEADER.
8660 This search includes all articles in the current group that Gnus has
8661 fetched headers for, whether they are displayed or not."
8662   (let ((articles nil)
8663         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8664         (case-fold-search t))
8665     (dolist (header gnus-newsgroup-headers)
8666       (when (string-match regexp (funcall func header))
8667         (push (mail-header-number header) articles)))
8668     (nreverse articles)))
8669
8670 (defun gnus-summary-find-matching (header regexp &optional backward unread
8671                                           not-case-fold not-matching)
8672   "Return a list of all articles that match REGEXP on HEADER.
8673 The search stars on the current article and goes forwards unless
8674 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8675 If UNREAD is non-nil, only unread articles will
8676 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8677 in the comparisons. If NOT-MATCHING, return a list of all articles that
8678 not match REGEXP on HEADER."
8679   (let ((case-fold-search (not not-case-fold))
8680         articles d func)
8681     (if (consp header)
8682         (if (eq (car header) 'extra)
8683             (setq func
8684                   `(lambda (h)
8685                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8686                          "")))
8687           (error "%s is an invalid header" header))
8688       (unless (fboundp (intern (concat "mail-header-" header)))
8689         (error "%s is not a valid header" header))
8690       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8691     (dolist (d (if (eq backward 'all)
8692                    gnus-newsgroup-data
8693                  (gnus-data-find-list
8694                   (gnus-summary-article-number)
8695                   (gnus-data-list backward))))
8696       (when (and (or (not unread)       ; We want all articles...
8697                      (gnus-data-unread-p d)) ; Or just unreads.
8698                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8699                  (if not-matching
8700                      (not (string-match
8701                            regexp
8702                            (funcall func (gnus-data-header d))))
8703                    (string-match regexp
8704                                  (funcall func (gnus-data-header d)))))
8705         (push (gnus-data-number d) articles))) ; Success!
8706     (nreverse articles)))
8707
8708 (defun gnus-summary-execute-command (header regexp command &optional backward)
8709   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8710 If HEADER is an empty string (or nil), the match is done on the entire
8711 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8712   (interactive
8713    (list (let ((completion-ignore-case t))
8714            (completing-read
8715             "Header name: "
8716             (mapcar (lambda (header) (list (format "%s" header)))
8717                     (append
8718                      '("Number" "Subject" "From" "Lines" "Date"
8719                        "Message-ID" "Xref" "References" "Body")
8720                      gnus-extra-headers))
8721             nil 'require-match))
8722          (read-string "Regexp: ")
8723          (read-key-sequence "Command: ")
8724          current-prefix-arg))
8725   (when (equal header "Body")
8726     (setq header ""))
8727   ;; Hidden thread subtrees must be searched as well.
8728   (gnus-summary-show-all-threads)
8729   ;; We don't want to change current point nor window configuration.
8730   (save-excursion
8731     (save-window-excursion
8732       (let (gnus-visual
8733             gnus-treat-strip-trailing-blank-lines
8734             gnus-treat-strip-leading-blank-lines
8735             gnus-treat-strip-multiple-blank-lines
8736             gnus-treat-hide-boring-headers
8737             gnus-treat-fold-newsgroups
8738             gnus-article-prepare-hook)
8739         (gnus-message 6 "Executing %s..." (key-description command))
8740         ;; We'd like to execute COMMAND interactively so as to give arguments.
8741         (gnus-execute header regexp
8742                       `(call-interactively ',(key-binding command))
8743                       backward)
8744         (gnus-message 6 "Executing %s...done" (key-description command))))))
8745
8746 (defun gnus-summary-beginning-of-article ()
8747   "Scroll the article back to the beginning."
8748   (interactive)
8749   (gnus-summary-select-article)
8750   (gnus-configure-windows 'article)
8751   (gnus-eval-in-buffer-window gnus-article-buffer
8752     (widen)
8753     (goto-char (point-min))
8754     (when gnus-break-pages
8755       (gnus-narrow-to-page))))
8756
8757 (defun gnus-summary-end-of-article ()
8758   "Scroll to the end of the article."
8759   (interactive)
8760   (gnus-summary-select-article)
8761   (gnus-configure-windows 'article)
8762   (gnus-eval-in-buffer-window gnus-article-buffer
8763     (widen)
8764     (goto-char (point-max))
8765     (recenter -3)
8766     (when gnus-break-pages
8767       (when (re-search-backward page-delimiter nil t)
8768         (narrow-to-region (match-end 0) (point-max)))
8769       (gnus-narrow-to-page))))
8770
8771 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8772   "Truncate to LEN and quote all \"(\"'s in STRING."
8773   (gnus-replace-in-string (if (and len (> (length string) len))
8774                               (substring string 0 len)
8775                             string)
8776                           "[()]" "\\\\\\&"))
8777
8778 (defun gnus-summary-print-article (&optional filename n)
8779   "Generate and print a PostScript image of the process-marked (mail) articles.
8780
8781 If used interactively, print the current article if none are
8782 process-marked.  With prefix arg, prompt the user for the name of the
8783 file to save in.
8784
8785 When used from Lisp, accept two optional args FILENAME and N.  N means
8786 to print the next N articles.  If N is negative, print the N previous
8787 articles.  If N is nil and articles have been marked with the process
8788 mark, print these instead.
8789
8790 If the optional first argument FILENAME is nil, send the image to the
8791 printer.  If FILENAME is a string, save the PostScript image in a file with
8792 that name.  If FILENAME is a number, prompt the user for the name of the file
8793 to save in."
8794   (interactive (list (ps-print-preprint current-prefix-arg)))
8795   (dolist (article (gnus-summary-work-articles n))
8796     (gnus-summary-select-article nil nil 'pseudo article)
8797     (gnus-eval-in-buffer-window gnus-article-buffer
8798       (gnus-print-buffer))
8799     (gnus-summary-remove-process-mark article))
8800   (ps-despool filename))
8801
8802 (defun gnus-print-buffer ()
8803   (let ((buffer (generate-new-buffer " *print*")))
8804     (unwind-protect
8805         (progn
8806           (copy-to-buffer buffer (point-min) (point-max))
8807           (set-buffer buffer)
8808           (gnus-remove-text-with-property 'gnus-decoration)
8809           (when (gnus-visual-p 'article-highlight 'highlight)
8810             ;; Copy-to-buffer doesn't copy overlay.  So redo
8811             ;; highlight.
8812             (let ((gnus-article-buffer buffer))
8813               (gnus-article-highlight-citation t)
8814               (gnus-article-highlight-signature)
8815               (gnus-article-emphasize)
8816               (gnus-article-delete-invisible-text)))
8817           (let ((ps-left-header
8818                  (list
8819                   (concat "("
8820                           (gnus-summary-print-truncate-and-quote
8821                            (mail-header-subject gnus-current-headers)
8822                            66) ")")
8823                   (concat "("
8824                           (gnus-summary-print-truncate-and-quote
8825                            (mail-header-from gnus-current-headers)
8826                            45) ")")))
8827                 (ps-right-header
8828                  (list
8829                   "/pagenumberstring load"
8830                   (concat "("
8831                           (mail-header-date gnus-current-headers) ")"))))
8832             (gnus-run-hooks 'gnus-ps-print-hook)
8833             (save-excursion
8834               (if window-system
8835                   (ps-spool-buffer-with-faces)
8836                 (ps-spool-buffer)))))
8837       (kill-buffer buffer))))
8838
8839 (defun gnus-summary-show-article (&optional arg)
8840   "Force redisplaying of the current article.
8841 If ARG (the prefix) is a number, show the article with the charset
8842 defined in `gnus-summary-show-article-charset-alist', or the charset
8843 input.
8844 If ARG (the prefix) is non-nil and not a number, show the raw article
8845 without any article massaging functions being run.  Normally, the key
8846 strokes are `C-u g'."
8847   (interactive "P")
8848   (cond
8849    ((numberp arg)
8850     (gnus-summary-show-article t)
8851     (let ((gnus-newsgroup-charset
8852            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8853                (mm-read-coding-system
8854                 "View as charset: " ;; actually it is coding system.
8855                 (with-current-buffer gnus-article-buffer
8856                   (mm-detect-coding-region (point) (point-max))))))
8857           (gnus-newsgroup-ignored-charsets 'gnus-all))
8858       (gnus-summary-select-article nil 'force)
8859       (let ((deps gnus-newsgroup-dependencies)
8860             head header lines)
8861         (save-excursion
8862           (set-buffer gnus-original-article-buffer)
8863           (save-restriction
8864             (message-narrow-to-head)
8865             (setq head (buffer-string))
8866             (goto-char (point-min))
8867             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8868               (goto-char (point-max))
8869               (widen)
8870               (setq lines (1- (count-lines (point) (point-max))))))
8871           (with-temp-buffer
8872             (insert (format "211 %d Article retrieved.\n"
8873                             (cdr gnus-article-current)))
8874             (insert head)
8875             (if lines (insert (format "Lines: %d\n" lines)))
8876             (insert ".\n")
8877             (let ((nntp-server-buffer (current-buffer)))
8878               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8879         (gnus-data-set-header
8880          (gnus-data-find (cdr gnus-article-current))
8881          header)
8882         (gnus-summary-update-article-line
8883          (cdr gnus-article-current) header)
8884         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8885           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8886    ((not arg)
8887     ;; Select the article the normal way.
8888     (gnus-summary-select-article nil 'force))
8889    (t
8890     ;; We have to require this here to make sure that the following
8891     ;; dynamic binding isn't shadowed by autoloading.
8892     (require 'gnus-async)
8893     (require 'gnus-art)
8894     ;; Bind the article treatment functions to nil.
8895     (let ((gnus-have-all-headers t)
8896           gnus-article-prepare-hook
8897           gnus-article-decode-hook
8898           gnus-display-mime-function
8899           gnus-break-pages)
8900       ;; Destroy any MIME parts.
8901       (when (gnus-buffer-live-p gnus-article-buffer)
8902         (save-excursion
8903           (set-buffer gnus-article-buffer)
8904           (mm-destroy-parts gnus-article-mime-handles)
8905           ;; Set it to nil for safety reason.
8906           (setq gnus-article-mime-handle-alist nil)
8907           (setq gnus-article-mime-handles nil)))
8908       (gnus-summary-select-article nil 'force))))
8909   (gnus-summary-goto-subject gnus-current-article)
8910   (gnus-summary-position-point))
8911
8912 (defun gnus-summary-show-raw-article ()
8913   "Show the raw article without any article massaging functions being run."
8914   (interactive)
8915   (gnus-summary-show-article t))
8916
8917 (defun gnus-summary-verbose-headers (&optional arg)
8918   "Toggle permanent full header display.
8919 If ARG is a positive number, turn header display on.
8920 If ARG is a negative number, turn header display off."
8921   (interactive "P")
8922   (setq gnus-show-all-headers
8923         (cond ((or (not (numberp arg))
8924                    (zerop arg))
8925                (not gnus-show-all-headers))
8926               ((natnump arg)
8927                t)))
8928   (gnus-summary-show-article))
8929
8930 (defun gnus-summary-toggle-header (&optional arg)
8931   "Show the headers if they are hidden, or hide them if they are shown.
8932 If ARG is a positive number, show the entire header.
8933 If ARG is a negative number, hide the unwanted header lines."
8934   (interactive "P")
8935   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8936                      (get-buffer-window gnus-article-buffer t))))
8937     (with-current-buffer gnus-article-buffer
8938       (widen)
8939       (article-narrow-to-head)
8940       (let* ((buffer-read-only nil)
8941              (inhibit-point-motion-hooks t)
8942              (hidden (if (numberp arg)
8943                          (>= arg 0)
8944                        (or (not (looking-at "[^ \t\n]+:"))
8945                            (gnus-article-hidden-text-p 'headers))))
8946              s e)
8947         (delete-region (point-min) (point-max))
8948         (with-current-buffer gnus-original-article-buffer
8949           (goto-char (setq s (point-min)))
8950           (setq e (if (search-forward "\n\n" nil t)
8951                       (1- (point))
8952                     (point-max))))
8953         (insert-buffer-substring gnus-original-article-buffer s e)
8954         (run-hooks 'gnus-article-decode-hook)
8955         (if hidden
8956             (let ((gnus-treat-hide-headers nil)
8957                   (gnus-treat-hide-boring-headers nil))
8958               (gnus-delete-wash-type 'headers)
8959               (gnus-treat-article 'head))
8960           (gnus-treat-article 'head))
8961         (widen)
8962         (if window
8963             (set-window-start window (goto-char (point-min))))
8964         (if gnus-break-pages
8965             (gnus-narrow-to-page)
8966           (when (gnus-visual-p 'page-marker)
8967             (let ((buffer-read-only nil))
8968               (gnus-remove-text-with-property 'gnus-prev)
8969               (gnus-remove-text-with-property 'gnus-next))))
8970         (gnus-set-mode-line 'article)))))
8971
8972 (defun gnus-summary-show-all-headers ()
8973   "Make all header lines visible."
8974   (interactive)
8975   (gnus-summary-toggle-header 1))
8976
8977 (defun gnus-summary-caesar-message (&optional arg)
8978   "Caesar rotate the current article by 13.
8979 The numerical prefix specifies how many places to rotate each letter
8980 forward."
8981   (interactive "P")
8982   (gnus-summary-select-article)
8983   (let ((mail-header-separator ""))
8984     (gnus-eval-in-buffer-window gnus-article-buffer
8985       (save-restriction
8986         (widen)
8987         (let ((start (window-start))
8988               buffer-read-only)
8989           (message-caesar-buffer-body arg)
8990           (set-window-start (get-buffer-window (current-buffer)) start)))))
8991   ;; Create buttons and stuff...
8992   (gnus-treat-article nil))
8993
8994 (defun gnus-summary-idna-message (&optional arg)
8995   "Decode IDNA encoded domain names in the current articles.
8996 IDNA encoded domain names looks like `xn--bar'.  If a string
8997 remain unencoded after running this function, it is likely an
8998 invalid IDNA string (`xn--bar' is invalid).
8999
9000 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9001 installed for this command to work."
9002   (interactive "P")
9003   (if (not (and (condition-case nil (require 'idna)
9004                   (file-error))
9005                 (mm-coding-system-p 'utf-8)
9006                 (executable-find (symbol-value 'idna-program))))
9007       (gnus-message
9008        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9009     (gnus-summary-select-article)
9010     (let ((mail-header-separator ""))
9011       (gnus-eval-in-buffer-window gnus-article-buffer
9012         (save-restriction
9013           (widen)
9014           (let ((start (window-start))
9015                 buffer-read-only)
9016             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9017               (replace-match (idna-to-unicode (match-string 1))))
9018             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9019
9020 (autoload 'unmorse-region "morse"
9021   "Convert morse coded text in region to ordinary ASCII text."
9022   t)
9023
9024 (defun gnus-summary-morse-message (&optional arg)
9025   "Morse decode the current article."
9026   (interactive "P")
9027   (gnus-summary-select-article)
9028   (let ((mail-header-separator ""))
9029     (gnus-eval-in-buffer-window gnus-article-buffer
9030       (save-excursion
9031         (save-restriction
9032           (widen)
9033           (let ((pos (window-start))
9034                 buffer-read-only)
9035             (goto-char (point-min))
9036             (when (message-goto-body)
9037               (gnus-narrow-to-body))
9038             (goto-char (point-min))
9039             (while (search-forward "·" (point-max) t)
9040               (replace-match "."))
9041             (unmorse-region (point-min) (point-max))
9042             (widen)
9043             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9044
9045 (defun gnus-summary-stop-page-breaking ()
9046   "Stop page breaking in the current article."
9047   (interactive)
9048   (gnus-summary-select-article)
9049   (gnus-eval-in-buffer-window gnus-article-buffer
9050     (widen)
9051     (when (gnus-visual-p 'page-marker)
9052       (let ((buffer-read-only nil))
9053         (gnus-remove-text-with-property 'gnus-prev)
9054         (gnus-remove-text-with-property 'gnus-next))
9055       (setq gnus-page-broken nil))))
9056
9057 (defun gnus-summary-move-article (&optional n to-newsgroup
9058                                             select-method action)
9059   "Move the current article to a different newsgroup.
9060 If N is a positive number, move the N next articles.
9061 If N is a negative number, move the N previous articles.
9062 If N is nil and any articles have been marked with the process mark,
9063 move those articles instead.
9064 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9065 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9066 re-spool using this method.
9067
9068 When called interactively with TO-NEWSGROUP being nil, the value of
9069 the variable `gnus-move-split-methods' is used for finding a default
9070 for the target newsgroup.
9071
9072 For this function to work, both the current newsgroup and the
9073 newsgroup that you want to move to have to support the `request-move'
9074 and `request-accept' functions.
9075
9076 ACTION can be either `move' (the default), `crosspost' or `copy'."
9077   (interactive "P")
9078   (unless action
9079     (setq action 'move))
9080   ;; Check whether the source group supports the required functions.
9081   (cond ((and (eq action 'move)
9082               (not (gnus-check-backend-function
9083                     'request-move-article gnus-newsgroup-name)))
9084          (error "The current group does not support article moving"))
9085         ((and (eq action 'crosspost)
9086               (not (gnus-check-backend-function
9087                     'request-replace-article gnus-newsgroup-name)))
9088          (error "The current group does not support article editing")))
9089   (let ((articles (gnus-summary-work-articles n))
9090         (prefix (if (gnus-check-backend-function
9091                      'request-move-article gnus-newsgroup-name)
9092                     (funcall gnus-move-group-prefix-function
9093                              gnus-newsgroup-name)
9094                   ""))
9095         (names '((move "Move" "Moving")
9096                  (copy "Copy" "Copying")
9097                  (crosspost "Crosspost" "Crossposting")))
9098         (copy-buf (save-excursion
9099                     (nnheader-set-temp-buffer " *copy article*")))
9100         art-group to-method new-xref article to-groups articles-to-update-marks)
9101     (unless (assq action names)
9102       (error "Unknown action %s" action))
9103     ;; Read the newsgroup name.
9104     (when (and (not to-newsgroup)
9105                (not select-method))
9106       (if (and gnus-move-split-methods
9107                (not
9108                 (and (memq gnus-current-article articles)
9109                      (gnus-buffer-live-p gnus-original-article-buffer))))
9110           ;; When `gnus-move-split-methods' is non-nil, we have to
9111           ;; select an article to give `gnus-read-move-group-name' an
9112           ;; opportunity to suggest an appropriate default.  However,
9113           ;; we needn't render or mark the article.
9114           (let ((gnus-display-mime-function nil)
9115                 (gnus-article-prepare-hook nil)
9116                 (gnus-mark-article-hook nil))
9117             (gnus-summary-select-article nil nil nil (car articles))))
9118       (setq to-newsgroup
9119             (gnus-read-move-group-name
9120              (cadr (assq action names))
9121              (symbol-value (intern (format "gnus-current-%s-group" action)))
9122              articles prefix))
9123       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9124     (setq to-method (or select-method
9125                         (gnus-server-to-method
9126                          (gnus-group-method to-newsgroup))))
9127     ;; Check the method we are to move this article to...
9128     (unless (gnus-check-backend-function
9129              'request-accept-article (car to-method))
9130       (error "%s does not support article copying" (car to-method)))
9131     (unless (gnus-check-server to-method)
9132       (error "Can't open server %s" (car to-method)))
9133     (gnus-message 6 "%s to %s: %s..."
9134                   (caddr (assq action names))
9135                   (or (car select-method) to-newsgroup) articles)
9136     (while articles
9137       (setq article (pop articles))
9138       (setq
9139        art-group
9140        (cond
9141         ;; Move the article.
9142         ((eq action 'move)
9143          ;; Remove this article from future suppression.
9144          (gnus-dup-unsuppress-article article)
9145          (let* ((from-method (gnus-find-method-for-group
9146                               gnus-newsgroup-name))
9147                 (to-method (gnus-find-method-for-group
9148                             to-newsgroup))
9149                 (move-is-internal (gnus-method-equal from-method to-method)))
9150          (gnus-request-move-article
9151           article                       ; Article to move
9152           gnus-newsgroup-name           ; From newsgroup
9153           (nth 1 (gnus-find-method-for-group
9154                   gnus-newsgroup-name)) ; Server
9155           (list 'gnus-request-accept-article
9156                 to-newsgroup (list 'quote select-method)
9157                 (not articles) t)       ; Accept form
9158           (not articles)                ; Only save nov last time
9159           move-is-internal)))           ; is this move internal?
9160         ;; Copy the article.
9161         ((eq action 'copy)
9162          (save-excursion
9163            (set-buffer copy-buf)
9164            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9165              (save-restriction
9166                (nnheader-narrow-to-headers)
9167                (dolist (hdr gnus-copy-article-ignored-headers)
9168                  (message-remove-header hdr t)))
9169              (gnus-request-accept-article
9170               to-newsgroup select-method (not articles) t))))
9171         ;; Crosspost the article.
9172         ((eq action 'crosspost)
9173          (let ((xref (message-tokenize-header
9174                       (mail-header-xref (gnus-summary-article-header article))
9175                       " ")))
9176            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9177                                   ":" (number-to-string article)))
9178            (unless xref
9179              (setq xref (list (system-name))))
9180            (setq new-xref
9181                  (concat
9182                   (mapconcat 'identity
9183                              (delete "Xref:" (delete new-xref xref))
9184                              " ")
9185                   " " new-xref))
9186            (save-excursion
9187              (set-buffer copy-buf)
9188              ;; First put the article in the destination group.
9189              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9190              (when (consp (setq art-group
9191                                 (gnus-request-accept-article
9192                                  to-newsgroup select-method (not articles))))
9193                (setq new-xref (concat new-xref " " (car art-group)
9194                                       ":"
9195                                       (number-to-string (cdr art-group))))
9196                ;; Now we have the new Xrefs header, so we insert
9197                ;; it and replace the new article.
9198                (nnheader-replace-header "Xref" new-xref)
9199                (gnus-request-replace-article
9200                 (cdr art-group) to-newsgroup (current-buffer))
9201                art-group))))))
9202       (cond
9203        ((not art-group)
9204         (gnus-message 1 "Couldn't %s article %s: %s"
9205                       (cadr (assq action names)) article
9206                       (nnheader-get-report (car to-method))))
9207        ((eq art-group 'junk)
9208         (when (eq action 'move)
9209           (gnus-summary-mark-article article gnus-canceled-mark)
9210           (gnus-message 4 "Deleted article %s" article)
9211           ;; run the delete hook
9212           (run-hook-with-args 'gnus-summary-article-delete-hook
9213                               action
9214                               (gnus-data-header
9215                                (assoc article (gnus-data-list nil)))
9216                               gnus-newsgroup-name nil
9217                               select-method)))
9218        (t
9219         (let* ((pto-group (gnus-group-prefixed-name
9220                            (car art-group) to-method))
9221                (info (gnus-get-info pto-group))
9222                (to-group (gnus-info-group info))
9223                to-marks)
9224           ;; Update the group that has been moved to.
9225           (when (and info
9226                      (memq action '(move copy)))
9227             (unless (member to-group to-groups)
9228               (push to-group to-groups))
9229
9230             (unless (memq article gnus-newsgroup-unreads)
9231               (push 'read to-marks)
9232               (gnus-info-set-read
9233                info (gnus-add-to-range (gnus-info-read info)
9234                                        (list (cdr art-group)))))
9235
9236             ;; See whether the article is to be put in the cache.
9237             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9238                              gnus-article-mark-lists
9239                            (delete '(expirable . expire)
9240                                    (copy-sequence gnus-article-mark-lists))))
9241                   (to-article (cdr art-group)))
9242
9243               ;; Enter the article into the cache in the new group,
9244               ;; if that is required.
9245               (when gnus-use-cache
9246                 (gnus-cache-possibly-enter-article
9247                  to-group to-article
9248                  (memq article gnus-newsgroup-marked)
9249                  (memq article gnus-newsgroup-dormant)
9250                  (memq article gnus-newsgroup-unreads)))
9251
9252               (when gnus-preserve-marks
9253                 ;; Copy any marks over to the new group.
9254                 (when (and (equal to-group gnus-newsgroup-name)
9255                            (not (memq article gnus-newsgroup-unreads)))
9256                   ;; Mark this article as read in this group.
9257                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9258                   (setcdr (gnus-active to-group) to-article)
9259                   (setcdr gnus-newsgroup-active to-article))
9260
9261                 (while marks
9262                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9263                     (when (memq article (symbol-value
9264                                          (intern (format "gnus-newsgroup-%s"
9265                                                          (caar marks)))))
9266                       (push (cdar marks) to-marks)
9267                       ;; If the other group is the same as this group,
9268                       ;; then we have to add the mark to the list.
9269                       (when (equal to-group gnus-newsgroup-name)
9270                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9271                              (cons to-article
9272                                    (symbol-value
9273                                     (intern (format "gnus-newsgroup-%s"
9274                                                     (caar marks)))))))
9275                       ;; Copy the marks to other group.
9276                       (gnus-add-marked-articles
9277                        to-group (cdar marks) (list to-article) info)))
9278                   (setq marks (cdr marks)))
9279
9280                 (gnus-request-set-mark
9281                  to-group (list (list (list to-article) 'add to-marks))))
9282
9283               (gnus-dribble-enter
9284                (concat "(gnus-group-set-info '"
9285                        (gnus-prin1-to-string (gnus-get-info to-group))
9286                        ")"))))
9287
9288           ;; Update the Xref header in this article to point to
9289           ;; the new crossposted article we have just created.
9290           (when (eq action 'crosspost)
9291             (save-excursion
9292               (set-buffer copy-buf)
9293               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9294               (nnheader-replace-header "Xref" new-xref)
9295               (gnus-request-replace-article
9296                article gnus-newsgroup-name (current-buffer))))
9297
9298           ;; run the move/copy/crosspost/respool hook
9299           (run-hook-with-args 'gnus-summary-article-move-hook
9300                               action
9301                               (gnus-data-header
9302                                (assoc article (gnus-data-list nil)))
9303                               gnus-newsgroup-name
9304                               to-newsgroup
9305                               select-method))
9306
9307         ;;;!!!Why is this necessary?
9308         (set-buffer gnus-summary-buffer)
9309         
9310         (gnus-summary-goto-subject article)
9311         (when (eq action 'move)
9312           (gnus-summary-mark-article article gnus-canceled-mark))))
9313       (push article articles-to-update-marks))
9314
9315     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9316     ;; Re-activate all groups that have been moved to.
9317     (save-excursion
9318       (set-buffer gnus-group-buffer)
9319       (let ((gnus-group-marked to-groups))
9320         (gnus-group-get-new-news-this-group nil t)))
9321     
9322     (gnus-kill-buffer copy-buf)
9323     (gnus-summary-position-point)
9324     (gnus-set-mode-line 'summary)))
9325
9326 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9327   "Copy the current article to some other group.
9328 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9329 When called interactively, if TO-NEWSGROUP is nil, use the value of
9330 the variable `gnus-move-split-methods' for finding a default target
9331 newsgroup.
9332 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9333 re-spool using this method."
9334   (interactive "P")
9335   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9336
9337 (defun gnus-summary-crosspost-article (&optional n)
9338   "Crosspost the current article to some other group."
9339   (interactive "P")
9340   (gnus-summary-move-article n nil nil 'crosspost))
9341
9342 (defcustom gnus-summary-respool-default-method nil
9343   "Default method type for respooling an article.
9344 If nil, use to the current newsgroup method."
9345   :type 'symbol
9346   :group 'gnus-summary-mail)
9347
9348 (defcustom gnus-summary-display-while-building nil
9349   "If non-nil, show and update the summary buffer as it's being built.
9350 If the value is t, update the buffer after every line is inserted.  If
9351 the value is an integer (N), update the display every N lines."
9352   :version "22.1"
9353   :group 'gnus-thread
9354   :type '(choice (const :tag "off" nil)
9355                  number
9356                  (const :tag "frequently" t)))
9357
9358 (defun gnus-summary-respool-article (&optional n method)
9359   "Respool the current article.
9360 The article will be squeezed through the mail spooling process again,
9361 which means that it will be put in some mail newsgroup or other
9362 depending on `nnmail-split-methods'.
9363 If N is a positive number, respool the N next articles.
9364 If N is a negative number, respool the N previous articles.
9365 If N is nil and any articles have been marked with the process mark,
9366 respool those articles instead.
9367
9368 Respooling can be done both from mail groups and \"real\" newsgroups.
9369 In the former case, the articles in question will be moved from the
9370 current group into whatever groups they are destined to.  In the
9371 latter case, they will be copied into the relevant groups."
9372   (interactive
9373    (list current-prefix-arg
9374          (let* ((methods (gnus-methods-using 'respool))
9375                 (methname
9376                  (symbol-name (or gnus-summary-respool-default-method
9377                                   (car (gnus-find-method-for-group
9378                                         gnus-newsgroup-name)))))
9379                 (method
9380                  (gnus-completing-read-with-default
9381                   methname "Backend to use when respooling"
9382                   methods nil t nil 'gnus-mail-method-history))
9383                 ms)
9384            (cond
9385             ((zerop (length (setq ms (gnus-servers-using-backend
9386                                       (intern method)))))
9387              (list (intern method) ""))
9388             ((= 1 (length ms))
9389              (car ms))
9390             (t
9391              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9392                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9393                            ms-alist))))))))
9394   (unless method
9395     (error "No method given for respooling"))
9396   (if (assoc (symbol-name
9397               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9398              (gnus-methods-using 'respool))
9399       (gnus-summary-move-article n nil method)
9400     (gnus-summary-copy-article n nil method)))
9401
9402 (defun gnus-summary-import-article (file &optional edit)
9403   "Import an arbitrary file into a mail newsgroup."
9404   (interactive "fImport file: \nP")
9405   (let ((group gnus-newsgroup-name)
9406         (now (current-time))
9407         atts lines group-art)
9408     (unless (gnus-check-backend-function 'request-accept-article group)
9409       (error "%s does not support article importing" group))
9410     (or (file-readable-p file)
9411         (not (file-regular-p file))
9412         (error "Can't read %s" file))
9413     (save-excursion
9414       (set-buffer (gnus-get-buffer-create " *import file*"))
9415       (erase-buffer)
9416       (nnheader-insert-file-contents file)
9417       (goto-char (point-min))
9418       (if (nnheader-article-p)
9419           (save-restriction
9420             (goto-char (point-min))
9421             (search-forward "\n\n" nil t)
9422             (narrow-to-region (point-min) (1- (point)))
9423             (goto-char (point-min))
9424             (unless (re-search-forward "^date:" nil t)
9425               (goto-char (point-max))
9426               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9427        ;; This doesn't look like an article, so we fudge some headers.
9428         (setq atts (file-attributes file)
9429               lines (count-lines (point-min) (point-max)))
9430         (insert "From: " (read-string "From: ") "\n"
9431                 "Subject: " (read-string "Subject: ") "\n"
9432                 "Date: " (message-make-date (nth 5 atts)) "\n"
9433                 "Message-ID: " (message-make-message-id) "\n"
9434                 "Lines: " (int-to-string lines) "\n"
9435                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9436       (setq group-art (gnus-request-accept-article group nil t))
9437       (kill-buffer (current-buffer)))
9438     (setq gnus-newsgroup-active (gnus-activate-group group))
9439     (forward-line 1)
9440     (gnus-summary-goto-article (cdr group-art) nil t)
9441     (when edit
9442       (gnus-summary-edit-article))))
9443
9444 (defun gnus-summary-create-article ()
9445   "Create an article in a mail newsgroup."
9446   (interactive)
9447   (let ((group gnus-newsgroup-name)
9448         (now (current-time))
9449         group-art)
9450     (unless (gnus-check-backend-function 'request-accept-article group)
9451       (error "%s does not support article importing" group))
9452     (save-excursion
9453       (set-buffer (gnus-get-buffer-create " *import file*"))
9454       (erase-buffer)
9455       (goto-char (point-min))
9456       ;; This doesn't look like an article, so we fudge some headers.
9457       (insert "From: " (read-string "From: ") "\n"
9458               "Subject: " (read-string "Subject: ") "\n"
9459               "Date: " (message-make-date now) "\n"
9460               "Message-ID: " (message-make-message-id) "\n")
9461       (setq group-art (gnus-request-accept-article group nil t))
9462       (kill-buffer (current-buffer)))
9463     (setq gnus-newsgroup-active (gnus-activate-group group))
9464     (forward-line 1)
9465     (gnus-summary-goto-article (cdr group-art) nil t)
9466     (gnus-summary-edit-article)))
9467
9468 (defun gnus-summary-article-posted-p ()
9469   "Say whether the current (mail) article is available from news as well.
9470 This will be the case if the article has both been mailed and posted."
9471   (interactive)
9472   (let ((id (mail-header-references (gnus-summary-article-header)))
9473         (gnus-override-method (car (gnus-refer-article-methods))))
9474     (if (gnus-request-head id "")
9475         (gnus-message 2 "The current message was found on %s"
9476                       gnus-override-method)
9477       (gnus-message 2 "The current message couldn't be found on %s"
9478                     gnus-override-method)
9479       nil)))
9480
9481 (defun gnus-summary-expire-articles (&optional now)
9482   "Expire all articles that are marked as expirable in the current group."
9483   (interactive)
9484   (when (and (not gnus-group-is-exiting-without-update-p)
9485              (gnus-check-backend-function
9486               'request-expire-articles gnus-newsgroup-name))
9487     ;; This backend supports expiry.
9488     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9489            (expirable (if total
9490                           (progn
9491                             ;; We need to update the info for
9492                             ;; this group for `gnus-list-of-read-articles'
9493                             ;; to give us the right answer.
9494                             (gnus-run-hooks 'gnus-exit-group-hook)
9495                             (gnus-summary-update-info)
9496                             (gnus-list-of-read-articles gnus-newsgroup-name))
9497                         (setq gnus-newsgroup-expirable
9498                               (sort gnus-newsgroup-expirable '<))))
9499            (expiry-wait (if now 'immediate
9500                           (gnus-group-find-parameter
9501                            gnus-newsgroup-name 'expiry-wait)))
9502            (nnmail-expiry-target
9503             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9504                 nnmail-expiry-target))
9505            es)
9506       (when expirable
9507         ;; There are expirable articles in this group, so we run them
9508         ;; through the expiry process.
9509         (gnus-message 6 "Expiring articles...")
9510         (unless (gnus-check-group gnus-newsgroup-name)
9511           (error "Can't open server for %s" gnus-newsgroup-name))
9512         ;; The list of articles that weren't expired is returned.
9513         (save-excursion
9514           (if expiry-wait
9515               (let ((nnmail-expiry-wait-function nil)
9516                     (nnmail-expiry-wait expiry-wait))
9517                 (setq es (gnus-request-expire-articles
9518                           expirable gnus-newsgroup-name)))
9519             (setq es (gnus-request-expire-articles
9520                       expirable gnus-newsgroup-name)))
9521           (unless total
9522             (setq gnus-newsgroup-expirable es))
9523           ;; We go through the old list of expirable, and mark all
9524           ;; really expired articles as nonexistent.
9525           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9526             (let ((gnus-use-cache nil))
9527               (dolist (article expirable)
9528                 (when (and (not (memq article es))
9529                            (gnus-data-find article))
9530                   (gnus-summary-mark-article article gnus-canceled-mark)
9531                   (run-hook-with-args 'gnus-summary-article-expire-hook
9532                                       'delete
9533                                       (gnus-data-header
9534                                        (assoc article (gnus-data-list nil)))
9535                                       gnus-newsgroup-name
9536                                       nil
9537                                       nil))))))
9538         (gnus-message 6 "Expiring articles...done")))))
9539
9540 (defun gnus-summary-expire-articles-now ()
9541   "Expunge all expirable articles in the current group.
9542 This means that *all* articles that are marked as expirable will be
9543 deleted forever, right now."
9544   (interactive)
9545   (or gnus-expert-user
9546       (gnus-yes-or-no-p
9547        "Are you really, really, really sure you want to delete all these messages? ")
9548       (error "Phew!"))
9549   (gnus-summary-expire-articles t))
9550
9551 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9552 (defun gnus-summary-delete-article (&optional n)
9553   "Delete the N next (mail) articles.
9554 This command actually deletes articles.  This is not a marking
9555 command.  The article will disappear forever from your life, never to
9556 return.
9557
9558 If N is negative, delete backwards.
9559 If N is nil and articles have been marked with the process mark,
9560 delete these instead.
9561
9562 If `gnus-novice-user' is non-nil you will be asked for
9563 confirmation before the articles are deleted."
9564   (interactive "P")
9565   (unless (gnus-check-backend-function 'request-expire-articles
9566                                        gnus-newsgroup-name)
9567     (error "The current newsgroup does not support article deletion"))
9568   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9569     (error "Couldn't open server"))
9570   ;; Compute the list of articles to delete.
9571   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9572         (nnmail-expiry-target 'delete)
9573         not-deleted)
9574     (if (and gnus-novice-user
9575              (not (gnus-yes-or-no-p
9576                    (format "Do you really want to delete %s forever? "
9577                            (if (> (length articles) 1)
9578                                (format "these %s articles" (length articles))
9579                              "this article")))))
9580         ()
9581       ;; Delete the articles.
9582       (setq not-deleted (gnus-request-expire-articles
9583                          articles gnus-newsgroup-name 'force))
9584       (while articles
9585         (gnus-summary-remove-process-mark (car articles))
9586         ;; The backend might not have been able to delete the article
9587         ;; after all.
9588         (unless (memq (car articles) not-deleted)
9589           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9590         (let* ((article (car articles))
9591                (ghead  (gnus-data-header
9592                                     (assoc article (gnus-data-list nil)))))
9593           (run-hook-with-args 'gnus-summary-article-delete-hook
9594                               'delete ghead gnus-newsgroup-name nil
9595                               nil))
9596         (setq articles (cdr articles)))
9597       (when not-deleted
9598         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9599     (gnus-summary-position-point)
9600     (gnus-set-mode-line 'summary)
9601     not-deleted))
9602
9603 (defun gnus-summary-edit-article (&optional arg)
9604   "Edit the current article.
9605 This will have permanent effect only in mail groups.
9606 If ARG is nil, edit the decoded articles.
9607 If ARG is 1, edit the raw articles.
9608 If ARG is 2, edit the raw articles even in read-only groups.
9609 If ARG is 3, edit the articles with the current handles.
9610 Otherwise, allow editing of articles even in read-only
9611 groups."
9612   (interactive "P")
9613   (let (force raw current-handles)
9614     (cond
9615      ((null arg))
9616      ((eq arg 1)
9617       (setq raw t))
9618      ((eq arg 2)
9619       (setq raw t
9620             force t))
9621      ((eq arg 3)
9622       (setq current-handles
9623             (and (gnus-buffer-live-p gnus-article-buffer)
9624                  (with-current-buffer gnus-article-buffer
9625                    (prog1
9626                        gnus-article-mime-handles
9627                      (setq gnus-article-mime-handles nil))))))
9628      (t
9629       (setq force t)))
9630     (when (and raw (not force)
9631                (member gnus-newsgroup-name '("nndraft:delayed"
9632                                              "nndraft:drafts"
9633                                              "nndraft:queue")))
9634       (error "Can't edit the raw article in group %s"
9635              gnus-newsgroup-name))
9636     (save-excursion
9637       (set-buffer gnus-summary-buffer)
9638       (let ((mail-parse-charset gnus-newsgroup-charset)
9639             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9640         (gnus-set-global-variables)
9641         (when (and (not force)
9642                    (gnus-group-read-only-p))
9643           (error "The current newsgroup does not support article editing"))
9644         (gnus-summary-show-article t)
9645         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9646           (with-current-buffer gnus-article-buffer
9647             (mm-enable-multibyte)))
9648         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9649             (setq raw t))
9650         (gnus-article-edit-article
9651          (if raw 'ignore
9652            `(lambda ()
9653               (let ((mbl mml-buffer-list))
9654                 (setq mml-buffer-list nil)
9655                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9656                   (mime-to-mml ,'current-handles))
9657                 (let ((mbl1 mml-buffer-list))
9658                   (setq mml-buffer-list mbl)
9659                   (set (make-local-variable 'mml-buffer-list) mbl1))
9660                 (gnus-make-local-hook 'kill-buffer-hook)
9661                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9662          `(lambda (no-highlight)
9663             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9664                   (message-options message-options)
9665                   (message-options-set-recipient)
9666                   (mail-parse-ignored-charsets
9667                    ',gnus-newsgroup-ignored-charsets))
9668               ,(if (not raw) '(progn
9669                                 (mml-to-mime)
9670                                 (mml-destroy-buffers)
9671                                 (remove-hook 'kill-buffer-hook
9672                                              'mml-destroy-buffers t)
9673                                 (kill-local-variable 'mml-buffer-list)))
9674               (gnus-summary-edit-article-done
9675                ,(or (mail-header-references gnus-current-headers) "")
9676                ,(gnus-group-read-only-p)
9677                ,gnus-summary-buffer no-highlight))))))))
9678
9679 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9680
9681 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9682                                                  no-highlight)
9683   "Make edits to the current article permanent."
9684   (interactive)
9685   (save-excursion
9686    ;; The buffer restriction contains the entire article if it exists.
9687     (when (article-goto-body)
9688       (let ((lines (count-lines (point) (point-max)))
9689             (length (- (point-max) (point)))
9690             (case-fold-search t)
9691             (body (copy-marker (point))))
9692         (goto-char (point-min))
9693         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9694           (delete-region (match-beginning 1) (match-end 1))
9695           (insert (number-to-string length)))
9696         (goto-char (point-min))
9697         (when (re-search-forward
9698                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9699           (delete-region (match-beginning 1) (match-end 1))
9700           (insert (number-to-string length)))
9701         (goto-char (point-min))
9702         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9703           (delete-region (match-beginning 1) (match-end 1))
9704           (insert (number-to-string lines))))))
9705   ;; Replace the article.
9706   (let ((buf (current-buffer)))
9707     (with-temp-buffer
9708       (insert-buffer-substring buf)
9709
9710       (if (and (not read-only)
9711                (not (gnus-request-replace-article
9712                      (cdr gnus-article-current) (car gnus-article-current)
9713                      (current-buffer) t)))
9714           (error "Couldn't replace article")
9715         ;; Update the summary buffer.
9716         (if (and references
9717                  (equal (message-tokenize-header references " ")
9718                         (message-tokenize-header
9719                          (or (message-fetch-field "references") "") " ")))
9720             ;; We only have to update this line.
9721             (save-excursion
9722               (save-restriction
9723                 (message-narrow-to-head)
9724                 (let ((head (buffer-substring-no-properties
9725                              (point-min) (point-max)))
9726                       header)
9727                   (with-temp-buffer
9728                     (insert (format "211 %d Article retrieved.\n"
9729                                     (cdr gnus-article-current)))
9730                     (insert head)
9731                     (insert ".\n")
9732                     (let ((nntp-server-buffer (current-buffer)))
9733                       (setq header (car (gnus-get-newsgroup-headers
9734                                          nil t))))
9735                     (save-excursion
9736                       (set-buffer gnus-summary-buffer)
9737                       (gnus-data-set-header
9738                        (gnus-data-find (cdr gnus-article-current))
9739                        header)
9740                       (gnus-summary-update-article-line
9741                        (cdr gnus-article-current) header)
9742                       (if (gnus-summary-goto-subject
9743                            (cdr gnus-article-current) nil t)
9744                           (gnus-summary-update-secondary-mark
9745                            (cdr gnus-article-current))))))))
9746           ;; Update threads.
9747           (set-buffer (or buffer gnus-summary-buffer))
9748           (gnus-summary-update-article (cdr gnus-article-current))
9749           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9750               (gnus-summary-update-secondary-mark
9751                (cdr gnus-article-current))))
9752         ;; Prettify the article buffer again.
9753         (unless no-highlight
9754           (save-excursion
9755             (set-buffer gnus-article-buffer)
9756             ;;;!!! Fix this -- article should be rehighlighted.
9757             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9758             (set-buffer gnus-original-article-buffer)
9759             (gnus-request-article
9760              (cdr gnus-article-current)
9761              (car gnus-article-current) (current-buffer))))
9762         ;; Prettify the summary buffer line.
9763         (when (gnus-visual-p 'summary-highlight 'highlight)
9764           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9765
9766 (defun gnus-summary-edit-wash (key)
9767   "Perform editing command KEY in the article buffer."
9768   (interactive
9769    (list
9770     (progn
9771       (message "%s" (concat (this-command-keys) "- "))
9772       (read-char))))
9773   (message "")
9774   (gnus-summary-edit-article)
9775   (execute-kbd-macro (concat (this-command-keys) key))
9776   (gnus-article-edit-done))
9777
9778 ;;; Respooling
9779
9780 (defun gnus-summary-respool-query (&optional silent trace)
9781   "Query where the respool algorithm would put this article."
9782   (interactive)
9783   (let (gnus-mark-article-hook)
9784     (gnus-summary-select-article)
9785     (save-excursion
9786       (set-buffer gnus-original-article-buffer)
9787       (let ((groups (nnmail-article-group 'identity trace)))
9788         (unless silent
9789           (if groups
9790               (message "This message would go to %s"
9791                        (mapconcat 'car groups ", "))
9792             (message "This message would go to no groups"))
9793           groups)))))
9794
9795 (defun gnus-summary-respool-trace ()
9796   "Trace where the respool algorithm would put this article.
9797 Display a buffer showing all fancy splitting patterns which matched."
9798   (interactive)
9799   (gnus-summary-respool-query nil t))
9800
9801 ;; Summary marking commands.
9802
9803 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9804   "Mark articles which has the same subject as read, and then select the next.
9805 If UNMARK is positive, remove any kind of mark.
9806 If UNMARK is negative, tick articles."
9807   (interactive "P")
9808   (when unmark
9809     (setq unmark (prefix-numeric-value unmark)))
9810   (let ((count
9811          (gnus-summary-mark-same-subject
9812           (gnus-summary-article-subject) unmark)))
9813     ;; Select next unread article.  If auto-select-same mode, should
9814     ;; select the first unread article.
9815     (gnus-summary-next-article t (and gnus-auto-select-same
9816                                       (gnus-summary-article-subject)))
9817     (gnus-message 7 "%d article%s marked as %s"
9818                   count (if (= count 1) " is" "s are")
9819                   (if unmark "unread" "read"))))
9820
9821 (defun gnus-summary-kill-same-subject (&optional unmark)
9822   "Mark articles which has the same subject as read.
9823 If UNMARK is positive, remove any kind of mark.
9824 If UNMARK is negative, tick articles."
9825   (interactive "P")
9826   (when unmark
9827     (setq unmark (prefix-numeric-value unmark)))
9828   (let ((count
9829          (gnus-summary-mark-same-subject
9830           (gnus-summary-article-subject) unmark)))
9831     ;; If marked as read, go to next unread subject.
9832     (when (null unmark)
9833       ;; Go to next unread subject.
9834       (gnus-summary-next-subject 1 t))
9835     (gnus-message 7 "%d articles are marked as %s"
9836                   count (if unmark "unread" "read"))))
9837
9838 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9839   "Mark articles with same SUBJECT as read, and return marked number.
9840 If optional argument UNMARK is positive, remove any kinds of marks.
9841 If optional argument UNMARK is negative, mark articles as unread instead."
9842   (let ((count 1))
9843     (save-excursion
9844       (cond
9845        ((null unmark)                   ; Mark as read.
9846         (while (and
9847                 (progn
9848                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9849                   (gnus-summary-show-thread) t)
9850                 (gnus-summary-find-subject subject))
9851           (setq count (1+ count))))
9852        ((> unmark 0)                    ; Tick.
9853         (while (and
9854                 (progn
9855                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9856                   (gnus-summary-show-thread) t)
9857                 (gnus-summary-find-subject subject))
9858           (setq count (1+ count))))
9859        (t                               ; Mark as unread.
9860         (while (and
9861                 (progn
9862                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9863                   (gnus-summary-show-thread) t)
9864                 (gnus-summary-find-subject subject))
9865           (setq count (1+ count)))))
9866       (gnus-set-mode-line 'summary)
9867       ;; Return the number of marked articles.
9868       count)))
9869
9870 (defun gnus-summary-mark-as-processable (n &optional unmark)
9871   "Set the process mark on the next N articles.
9872 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9873 the process mark instead.  The difference between N and the actual
9874 number of articles marked is returned."
9875   (interactive "P")
9876   (if (and (null n) (gnus-region-active-p))
9877       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9878     (setq n (prefix-numeric-value n))
9879     (let ((backward (< n 0))
9880           (n (abs n)))
9881       (while (and
9882               (> n 0)
9883               (if unmark
9884                   (gnus-summary-remove-process-mark
9885                    (gnus-summary-article-number))
9886                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9887               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9888         (setq n (1- n)))
9889       (when (/= 0 n)
9890         (gnus-message 7 "No more articles"))
9891       (gnus-summary-recenter)
9892       (gnus-summary-position-point)
9893       n)))
9894
9895 (defun gnus-summary-unmark-as-processable (n)
9896   "Remove the process mark from the next N articles.
9897 If N is negative, unmark backward instead.  The difference between N and
9898 the actual number of articles unmarked is returned."
9899   (interactive "P")
9900   (gnus-summary-mark-as-processable n t))
9901
9902 (defun gnus-summary-unmark-all-processable ()
9903   "Remove the process mark from all articles."
9904   (interactive)
9905   (save-excursion
9906     (while gnus-newsgroup-processable
9907       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9908   (gnus-summary-position-point))
9909
9910 (defun gnus-summary-add-mark (article type)
9911   "Mark ARTICLE with a mark of TYPE."
9912   (let ((vtype (car (assq type gnus-article-mark-lists)))
9913         var)
9914     (if (not vtype)
9915         (error "No such mark type: %s" type)
9916       (setq var (intern (format "gnus-newsgroup-%s" type)))
9917       (set var (cons article (symbol-value var)))
9918       (if (memq type '(processable cached replied forwarded recent saved))
9919           (gnus-summary-update-secondary-mark article)
9920         ;;; !!! This is bogus.  We should find out what primary
9921         ;;; !!! mark we want to set.
9922         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9923
9924 (defun gnus-summary-mark-as-expirable (n)
9925   "Mark N articles forward as expirable.
9926 If N is negative, mark backward instead.  The difference between N and
9927 the actual number of articles marked is returned."
9928   (interactive "p")
9929   (gnus-summary-mark-forward n gnus-expirable-mark))
9930
9931 (defun gnus-summary-mark-as-spam (n)
9932   "Mark N articles forward as spam.
9933 If N is negative, mark backward instead.  The difference between N and
9934 the actual number of articles marked is returned."
9935   (interactive "p")
9936   (gnus-summary-mark-forward n gnus-spam-mark))
9937
9938 (defun gnus-summary-mark-article-as-replied (article)
9939   "Mark ARTICLE as replied to and update the summary line.
9940 ARTICLE can also be a list of articles."
9941   (interactive (list (gnus-summary-article-number)))
9942   (let ((articles (if (listp article) article (list article))))
9943     (dolist (article articles)
9944       (unless (numberp article)
9945         (error "%s is not a number" article))
9946       (push article gnus-newsgroup-replied)
9947       (let ((buffer-read-only nil))
9948         (when (gnus-summary-goto-subject article nil t)
9949           (gnus-summary-update-secondary-mark article))))))
9950
9951 (defun gnus-summary-mark-article-as-forwarded (article)
9952   "Mark ARTICLE as forwarded and update the summary line.
9953 ARTICLE can also be a list of articles."
9954   (let ((articles (if (listp article) article (list article))))
9955     (dolist (article articles)
9956       (push article gnus-newsgroup-forwarded)
9957       (let ((buffer-read-only nil))
9958         (when (gnus-summary-goto-subject article nil t)
9959           (gnus-summary-update-secondary-mark article))))))
9960
9961 (defun gnus-summary-set-bookmark (article)
9962   "Set a bookmark in current article."
9963   (interactive (list (gnus-summary-article-number)))
9964   (when (or (not (get-buffer gnus-article-buffer))
9965             (not gnus-current-article)
9966             (not gnus-article-current)
9967             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9968     (error "No current article selected"))
9969   ;; Remove old bookmark, if one exists.
9970   (gnus-pull article gnus-newsgroup-bookmarks)
9971   ;; Set the new bookmark, which is on the form
9972   ;; (article-number . line-number-in-body).
9973   (push
9974    (cons article
9975          (with-current-buffer gnus-article-buffer
9976            (count-lines
9977             (min (point)
9978                  (save-excursion
9979                    (article-goto-body)
9980                    (point)))
9981             (point))))
9982    gnus-newsgroup-bookmarks)
9983   (gnus-message 6 "A bookmark has been added to the current article."))
9984
9985 (defun gnus-summary-remove-bookmark (article)
9986   "Remove the bookmark from the current article."
9987   (interactive (list (gnus-summary-article-number)))
9988   ;; Remove old bookmark, if one exists.
9989   (if (not (assq article gnus-newsgroup-bookmarks))
9990       (gnus-message 6 "No bookmark in current article.")
9991     (gnus-pull article gnus-newsgroup-bookmarks)
9992     (gnus-message 6 "Removed bookmark.")))
9993
9994 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9995 (defun gnus-summary-mark-as-dormant (n)
9996   "Mark N articles forward as dormant.
9997 If N is negative, mark backward instead.  The difference between N and
9998 the actual number of articles marked is returned."
9999   (interactive "p")
10000   (gnus-summary-mark-forward n gnus-dormant-mark))
10001
10002 (defun gnus-summary-set-process-mark (article)
10003   "Set the process mark on ARTICLE and update the summary line."
10004   (setq gnus-newsgroup-processable
10005         (cons article
10006               (delq article gnus-newsgroup-processable)))
10007   (when (gnus-summary-goto-subject article)
10008     (gnus-summary-show-thread)
10009     (gnus-summary-goto-subject article)
10010     (gnus-summary-update-secondary-mark article)))
10011
10012 (defun gnus-summary-remove-process-mark (&rest articles)
10013   "Remove the process mark from ARTICLES and update the summary line."
10014   (dolist (article articles)
10015     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10016     (when (gnus-summary-goto-subject article)
10017       (gnus-summary-show-thread)
10018       (gnus-summary-goto-subject article)
10019       (gnus-summary-update-secondary-mark article))))
10020
10021 (defun gnus-summary-set-saved-mark (article)
10022   "Set the process mark on ARTICLE and update the summary line."
10023   (push article gnus-newsgroup-saved)
10024   (when (gnus-summary-goto-subject article)
10025     (gnus-summary-update-secondary-mark article)))
10026
10027 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10028   "Mark N articles as read forwards.
10029 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10030 The difference between N and the actual number of articles marked is
10031 returned.
10032 If NO-EXPIRE, auto-expiry will be inhibited."
10033   (interactive "p")
10034   (gnus-summary-show-thread)
10035   (let ((backward (< n 0))
10036         (gnus-summary-goto-unread
10037          (and gnus-summary-goto-unread
10038               (not (eq gnus-summary-goto-unread 'never))
10039               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10040                                     gnus-ticked-mark gnus-dormant-mark)))))
10041         (n (abs n))
10042         (mark (or mark gnus-del-mark)))
10043     (while (and (> n 0)
10044                 (gnus-summary-mark-article nil mark no-expire)
10045                 (zerop (gnus-summary-next-subject
10046                         (if backward -1 1)
10047                         (and gnus-summary-goto-unread
10048                              (not (eq gnus-summary-goto-unread 'never)))
10049                         t)))
10050       (setq n (1- n)))
10051     (when (/= 0 n)
10052       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10053     (gnus-summary-recenter)
10054     (gnus-summary-position-point)
10055     (gnus-set-mode-line 'summary)
10056     n))
10057
10058 (defun gnus-summary-mark-article-as-read (mark)
10059   "Mark the current article quickly as read with MARK."
10060   (let ((article (gnus-summary-article-number)))
10061     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10062     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10063     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10064     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10065     (push (cons article mark) gnus-newsgroup-reads)
10066     ;; Possibly remove from cache, if that is used.
10067     (when gnus-use-cache
10068       (gnus-cache-enter-remove-article article))
10069     ;; Allow the backend to change the mark.
10070     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10071     ;; Check for auto-expiry.
10072     (when (and gnus-newsgroup-auto-expire
10073                (memq mark gnus-auto-expirable-marks))
10074       (setq mark gnus-expirable-mark)
10075       ;; Let the backend know about the mark change.
10076       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10077       (push article gnus-newsgroup-expirable))
10078     ;; Set the mark in the buffer.
10079     (gnus-summary-update-mark mark 'unread)
10080     t))
10081
10082 (defun gnus-summary-mark-article-as-unread (mark)
10083   "Mark the current article quickly as unread with MARK."
10084   (let* ((article (gnus-summary-article-number))
10085          (old-mark (gnus-summary-article-mark article)))
10086     ;; Allow the backend to change the mark.
10087     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10088     (if (eq mark old-mark)
10089         t
10090       (if (<= article 0)
10091           (progn
10092             (gnus-error 1 "Can't mark negative article numbers")
10093             nil)
10094         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10095         (setq gnus-newsgroup-spam-marked
10096               (delq article gnus-newsgroup-spam-marked))
10097         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10098         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10099         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10100         (cond ((= mark gnus-ticked-mark)
10101                (setq gnus-newsgroup-marked
10102                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10103                                               article)))
10104               ((= mark gnus-spam-mark)
10105                (setq gnus-newsgroup-spam-marked
10106                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10107                                               article)))
10108               ((= mark gnus-dormant-mark)
10109                (setq gnus-newsgroup-dormant
10110                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10111                                               article)))
10112               (t
10113                (setq gnus-newsgroup-unreads
10114                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10115                                               article))))
10116         (gnus-pull article gnus-newsgroup-reads)
10117
10118         ;; See whether the article is to be put in the cache.
10119         (and gnus-use-cache
10120              (vectorp (gnus-summary-article-header article))
10121              (save-excursion
10122                (gnus-cache-possibly-enter-article
10123                 gnus-newsgroup-name article
10124                 (= mark gnus-ticked-mark)
10125                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10126
10127         ;; Fix the mark.
10128         (gnus-summary-update-mark mark 'unread)
10129         t))))
10130
10131 (defun gnus-summary-mark-article (&optional article mark no-expire)
10132   "Mark ARTICLE with MARK.  MARK can be any character.
10133 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10134 `??' (dormant) and `?E' (expirable).
10135 If MARK is nil, then the default character `?r' is used.
10136 If ARTICLE is nil, then the article on the current line will be
10137 marked.
10138 If NO-EXPIRE, auto-expiry will be inhibited."
10139   ;; The mark might be a string.
10140   (when (stringp mark)
10141     (setq mark (aref mark 0)))
10142   ;; If no mark is given, then we check auto-expiring.
10143   (when (null mark)
10144     (setq mark gnus-del-mark))
10145   (when (and (not no-expire)
10146              gnus-newsgroup-auto-expire
10147              (memq mark gnus-auto-expirable-marks))
10148     (setq mark gnus-expirable-mark))
10149   (let ((article (or article (gnus-summary-article-number)))
10150         (old-mark (gnus-summary-article-mark article)))
10151     ;; Allow the backend to change the mark.
10152     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10153     (if (eq mark old-mark)
10154         t
10155       (unless article
10156         (error "No article on current line"))
10157       (if (not (if (or (= mark gnus-unread-mark)
10158                        (= mark gnus-ticked-mark)
10159                        (= mark gnus-spam-mark)
10160                        (= mark gnus-dormant-mark))
10161                    (gnus-mark-article-as-unread article mark)
10162                  (gnus-mark-article-as-read article mark)))
10163           t
10164         ;; See whether the article is to be put in the cache.
10165         (and gnus-use-cache
10166              (not (= mark gnus-canceled-mark))
10167              (vectorp (gnus-summary-article-header article))
10168              (save-excursion
10169                (gnus-cache-possibly-enter-article
10170                 gnus-newsgroup-name article
10171                 (= mark gnus-ticked-mark)
10172                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10173
10174         (when (gnus-summary-goto-subject article nil t)
10175           (let ((buffer-read-only nil))
10176             (gnus-summary-show-thread)
10177             ;; Fix the mark.
10178             (gnus-summary-update-mark mark 'unread)
10179             t))))))
10180
10181 (defun gnus-summary-update-secondary-mark (article)
10182   "Update the secondary (read, process, cache) mark."
10183   (gnus-summary-update-mark
10184    (cond ((memq article gnus-newsgroup-processable)
10185           gnus-process-mark)
10186          ((memq article gnus-newsgroup-cached)
10187           gnus-cached-mark)
10188          ((memq article gnus-newsgroup-replied)
10189           gnus-replied-mark)
10190          ((memq article gnus-newsgroup-forwarded)
10191           gnus-forwarded-mark)
10192          ((memq article gnus-newsgroup-saved)
10193           gnus-saved-mark)
10194          ((memq article gnus-newsgroup-recent)
10195           gnus-recent-mark)
10196          ((memq article gnus-newsgroup-unseen)
10197           gnus-unseen-mark)
10198          (t gnus-no-mark))
10199    'replied)
10200   (when (gnus-visual-p 'summary-highlight 'highlight)
10201     (gnus-run-hooks 'gnus-summary-update-hook))
10202   t)
10203
10204 (defun gnus-summary-update-download-mark (article)
10205   "Update the download mark."
10206   (gnus-summary-update-mark
10207    (cond ((memq article gnus-newsgroup-undownloaded)
10208           gnus-undownloaded-mark)
10209          (gnus-newsgroup-agentized
10210           gnus-downloaded-mark)
10211          (t
10212           gnus-no-mark))
10213    'download)
10214   (gnus-summary-update-line t)
10215   t)
10216
10217 (defun gnus-summary-update-mark (mark type)
10218   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10219         (buffer-read-only nil))
10220     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10221     (when forward
10222       (when (looking-at "\r")
10223         (incf forward))
10224       (when (<= (+ forward (point)) (point-max))
10225         ;; Go to the right position on the line.
10226         (goto-char (+ forward (point)))
10227         ;; Replace the old mark with the new mark.
10228         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10229         ;; Optionally update the marks by some user rule.
10230         (when (eq type 'unread)
10231           (gnus-data-set-mark
10232            (gnus-data-find (gnus-summary-article-number)) mark)
10233           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10234
10235 (defun gnus-mark-article-as-read (article &optional mark)
10236   "Enter ARTICLE in the pertinent lists and remove it from others."
10237   ;; Make the article expirable.
10238   (let ((mark (or mark gnus-del-mark)))
10239     (setq gnus-newsgroup-expirable
10240           (if (= mark gnus-expirable-mark)
10241               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10242             (delq article gnus-newsgroup-expirable)))
10243     ;; Remove from unread and marked lists.
10244     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10245     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10246     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10247     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10248     (push (cons article mark) gnus-newsgroup-reads)
10249     ;; Possibly remove from cache, if that is used.
10250     (when gnus-use-cache
10251       (gnus-cache-enter-remove-article article))
10252     t))
10253
10254 (defun gnus-mark-article-as-unread (article &optional mark)
10255   "Enter ARTICLE in the pertinent lists and remove it from others."
10256   (let ((mark (or mark gnus-ticked-mark)))
10257     (if (<= article 0)
10258         (progn
10259           (gnus-error 1 "Can't mark negative article numbers")
10260           nil)
10261       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10262             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10263             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10264             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10265             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10266
10267       ;; Unsuppress duplicates?
10268       (when gnus-suppress-duplicates
10269         (gnus-dup-unsuppress-article article))
10270
10271       (cond ((= mark gnus-ticked-mark)
10272              (setq gnus-newsgroup-marked
10273                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10274             ((= mark gnus-spam-mark)
10275              (setq gnus-newsgroup-spam-marked
10276                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10277                                             article)))
10278             ((= mark gnus-dormant-mark)
10279              (setq gnus-newsgroup-dormant
10280                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10281             (t
10282              (setq gnus-newsgroup-unreads
10283                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10284       (gnus-pull article gnus-newsgroup-reads)
10285       t)))
10286
10287 (defalias 'gnus-summary-mark-as-unread-forward
10288   'gnus-summary-tick-article-forward)
10289 (make-obsolete 'gnus-summary-mark-as-unread-forward
10290                'gnus-summary-tick-article-forward)
10291 (defun gnus-summary-tick-article-forward (n)
10292   "Tick N articles forwards.
10293 If N is negative, tick backwards instead.
10294 The difference between N and the number of articles ticked is returned."
10295   (interactive "p")
10296   (gnus-summary-mark-forward n gnus-ticked-mark))
10297
10298 (defalias 'gnus-summary-mark-as-unread-backward
10299   'gnus-summary-tick-article-backward)
10300 (make-obsolete 'gnus-summary-mark-as-unread-backward
10301                'gnus-summary-tick-article-backward)
10302 (defun gnus-summary-tick-article-backward (n)
10303   "Tick N articles backwards.
10304 The difference between N and the number of articles ticked is returned."
10305   (interactive "p")
10306   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10307
10308 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10309 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10310 (defun gnus-summary-tick-article (&optional article clear-mark)
10311   "Mark current article as unread.
10312 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10313 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10314   (interactive)
10315   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10316                                        gnus-ticked-mark)))
10317
10318 (defun gnus-summary-mark-as-read-forward (n)
10319   "Mark N articles as read forwards.
10320 If N is negative, mark backwards instead.
10321 The difference between N and the actual number of articles marked is
10322 returned."
10323   (interactive "p")
10324   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10325
10326 (defun gnus-summary-mark-as-read-backward (n)
10327   "Mark the N articles as read backwards.
10328 The difference between N and the actual number of articles marked is
10329 returned."
10330   (interactive "p")
10331   (gnus-summary-mark-forward
10332    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10333
10334 (defun gnus-summary-mark-as-read (&optional article mark)
10335   "Mark current article as read.
10336 ARTICLE specifies the article to be marked as read.
10337 MARK specifies a string to be inserted at the beginning of the line."
10338   (gnus-summary-mark-article article mark))
10339
10340 (defun gnus-summary-clear-mark-forward (n)
10341   "Clear marks from N articles forward.
10342 If N is negative, clear backward instead.
10343 The difference between N and the number of marks cleared is returned."
10344   (interactive "p")
10345   (gnus-summary-mark-forward n gnus-unread-mark))
10346
10347 (defun gnus-summary-clear-mark-backward (n)
10348   "Clear marks from N articles backward.
10349 The difference between N and the number of marks cleared is returned."
10350   (interactive "p")
10351   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10352
10353 (defun gnus-summary-mark-unread-as-read ()
10354   "Intended to be used by `gnus-summary-mark-article-hook'."
10355   (when (memq gnus-current-article gnus-newsgroup-unreads)
10356     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10357
10358 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10359   "Intended to be used by `gnus-summary-mark-article-hook'."
10360   (let ((mark (gnus-summary-article-mark)))
10361     (when (or (gnus-unread-mark-p mark)
10362               (gnus-read-mark-p mark))
10363       (gnus-summary-mark-article gnus-current-article
10364                                  (or new-mark gnus-read-mark)))))
10365
10366 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10367   "Intended to be used by `gnus-summary-mark-article-hook'."
10368   (let ((mark (gnus-summary-article-mark)))
10369     (when (or (gnus-unread-mark-p mark)
10370               (gnus-read-mark-p mark))
10371       (gnus-summary-mark-article (gnus-summary-article-number)
10372                                  (or new-mark gnus-read-mark)))))
10373
10374 (defun gnus-summary-mark-unread-as-ticked ()
10375   "Intended to be used by `gnus-summary-mark-article-hook'."
10376   (when (memq gnus-current-article gnus-newsgroup-unreads)
10377     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10378
10379 (defun gnus-summary-mark-region-as-read (point mark all)
10380   "Mark all unread articles between point and mark as read.
10381 If given a prefix, mark all articles between point and mark as read,
10382 even ticked and dormant ones."
10383   (interactive "r\nP")
10384   (save-excursion
10385     (let (article)
10386       (goto-char point)
10387       (beginning-of-line)
10388       (while (and
10389               (< (point) mark)
10390               (progn
10391                 (when (or all
10392                           (memq (setq article (gnus-summary-article-number))
10393                                 gnus-newsgroup-unreads))
10394                   (gnus-summary-mark-article article gnus-del-mark))
10395                 t)
10396               (gnus-summary-find-next))))))
10397
10398 (defun gnus-summary-mark-below (score mark)
10399   "Mark articles with score less than SCORE with MARK."
10400   (interactive "P\ncMark: ")
10401   (setq score (if score
10402                   (prefix-numeric-value score)
10403                 (or gnus-summary-default-score 0)))
10404   (save-excursion
10405     (set-buffer gnus-summary-buffer)
10406     (goto-char (point-min))
10407     (while
10408         (progn
10409           (and (< (gnus-summary-article-score) score)
10410                (gnus-summary-mark-article nil mark))
10411           (gnus-summary-find-next)))))
10412
10413 (defun gnus-summary-kill-below (&optional score)
10414   "Mark articles with score below SCORE as read."
10415   (interactive "P")
10416   (gnus-summary-mark-below score gnus-killed-mark))
10417
10418 (defun gnus-summary-clear-above (&optional score)
10419   "Clear all marks from articles with score above SCORE."
10420   (interactive "P")
10421   (gnus-summary-mark-above score gnus-unread-mark))
10422
10423 (defun gnus-summary-tick-above (&optional score)
10424   "Tick all articles with score above SCORE."
10425   (interactive "P")
10426   (gnus-summary-mark-above score gnus-ticked-mark))
10427
10428 (defun gnus-summary-mark-above (score mark)
10429   "Mark articles with score over SCORE with MARK."
10430   (interactive "P\ncMark: ")
10431   (setq score (if score
10432                   (prefix-numeric-value score)
10433                 (or gnus-summary-default-score 0)))
10434   (save-excursion
10435     (set-buffer gnus-summary-buffer)
10436     (goto-char (point-min))
10437     (while (and (progn
10438                   (when (> (gnus-summary-article-score) score)
10439                     (gnus-summary-mark-article nil mark))
10440                   t)
10441                 (gnus-summary-find-next)))))
10442
10443 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10444 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10445 (defun gnus-summary-limit-include-expunged (&optional no-error)
10446   "Display all the hidden articles that were expunged for low scores."
10447   (interactive)
10448   (let ((buffer-read-only nil))
10449     (let ((scored gnus-newsgroup-scored)
10450           headers h)
10451       (while scored
10452         (unless (gnus-summary-article-header (caar scored))
10453           (and (setq h (gnus-number-to-header (caar scored)))
10454                (< (cdar scored) gnus-summary-expunge-below)
10455                (push h headers)))
10456         (setq scored (cdr scored)))
10457       (if (not headers)
10458           (when (not no-error)
10459             (error "No expunged articles hidden"))
10460         (goto-char (point-min))
10461         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10462         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10463         (mapcar (lambda (x) (push (mail-header-number x)
10464                                   gnus-newsgroup-limit))
10465                 headers)
10466         (gnus-summary-prepare-unthreaded (nreverse headers))
10467         (goto-char (point-min))
10468         (gnus-summary-position-point)
10469         t))))
10470
10471 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10472   "Mark all unread articles in this newsgroup as read.
10473 If prefix argument ALL is non-nil, ticked and dormant articles will
10474 also be marked as read.
10475 If QUIETLY is non-nil, no questions will be asked.
10476
10477 If TO-HERE is non-nil, it should be a point in the buffer.  All
10478 articles before (after, if REVERSE is set) this point will be marked
10479 as read.
10480
10481 Note that this function will only catch up the unread article
10482 in the current summary buffer limitation.
10483
10484 The number of articles marked as read is returned."
10485   (interactive "P")
10486   (prog1
10487       (save-excursion
10488         (when (or quietly
10489                   (not gnus-interactive-catchup) ;Without confirmation?
10490                   gnus-expert-user
10491                   (gnus-y-or-n-p
10492                    (if all
10493                        "Mark absolutely all articles as read? "
10494                      "Mark all unread articles as read? ")))
10495           (if (and not-mark
10496                    (not gnus-newsgroup-adaptive)
10497                    (not gnus-newsgroup-auto-expire)
10498                    (not gnus-suppress-duplicates)
10499                    (or (not gnus-use-cache)
10500                        (eq gnus-use-cache 'passive)))
10501               (progn
10502                 (when all
10503                   (setq gnus-newsgroup-marked nil
10504                         gnus-newsgroup-spam-marked nil
10505                         gnus-newsgroup-dormant nil))
10506                 (setq gnus-newsgroup-unreads
10507                       (gnus-sorted-nunion
10508                        (gnus-intersection gnus-newsgroup-unreads
10509                                           gnus-newsgroup-downloadable)
10510                        gnus-newsgroup-unfetched)))
10511             ;; We actually mark all articles as canceled, which we
10512             ;; have to do when using auto-expiry or adaptive scoring.
10513             (gnus-summary-show-all-threads)
10514             (if (and to-here reverse)
10515                 (progn
10516                   (goto-char to-here)
10517                   (gnus-summary-mark-current-read-and-unread-as-read
10518                    gnus-catchup-mark)
10519                   (while (gnus-summary-find-next (not all))
10520                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10521               (when (gnus-summary-first-subject (not all))
10522                 (while (and
10523                         (if to-here (< (point) to-here) t)
10524                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10525                         (gnus-summary-find-next (not all))))))
10526             (gnus-set-mode-line 'summary))
10527           t))
10528     (gnus-summary-position-point)))
10529
10530 (defun gnus-summary-catchup-to-here (&optional all)
10531   "Mark all unticked articles before the current one as read.
10532 If ALL is non-nil, also mark ticked and dormant articles as read."
10533   (interactive "P")
10534   (save-excursion
10535     (gnus-save-hidden-threads
10536       (let ((beg (point)))
10537         ;; We check that there are unread articles.
10538         (when (or all (gnus-summary-find-prev))
10539           (gnus-summary-catchup all t beg)))))
10540   (gnus-summary-position-point))
10541
10542 (defun gnus-summary-catchup-from-here (&optional all)
10543   "Mark all unticked articles after (and including) the current one as read.
10544 If ALL is non-nil, also mark ticked and dormant articles as read."
10545   (interactive "P")
10546   (save-excursion
10547     (gnus-save-hidden-threads
10548       (let ((beg (point)))
10549         ;; We check that there are unread articles.
10550         (when (or all (gnus-summary-find-next))
10551           (gnus-summary-catchup all t beg nil t)))))
10552   (gnus-summary-position-point))
10553
10554 (defun gnus-summary-catchup-all (&optional quietly)
10555   "Mark all articles in this newsgroup as read.
10556 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10557 instead, which marks only unread articles as read."
10558   (interactive "P")
10559   (gnus-summary-catchup t quietly))
10560
10561 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10562   "Mark all unread articles in this group as read, then exit.
10563 If prefix argument ALL is non-nil, all articles are marked as read.
10564 If QUIETLY is non-nil, no questions will be asked."
10565   (interactive "P")
10566   (when (gnus-summary-catchup all quietly nil 'fast)
10567     ;; Select next newsgroup or exit.
10568     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10569              (eq gnus-auto-select-next 'quietly))
10570         (gnus-summary-next-group nil)
10571       (gnus-summary-exit))))
10572
10573 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10574   "Mark all articles in this newsgroup as read, and then exit.
10575 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10576 instead, which marks only unread articles as read."
10577   (interactive "P")
10578   (gnus-summary-catchup-and-exit t quietly))
10579
10580 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10581   "Mark all articles in this group as read and select the next group.
10582 If given a prefix, mark all articles, unread as well as ticked, as
10583 read."
10584   (interactive "P")
10585   (save-excursion
10586     (gnus-summary-catchup all))
10587   (gnus-summary-next-group))
10588
10589 ;;;
10590 ;;; with article
10591 ;;;
10592
10593 (defmacro gnus-with-article (article &rest forms)
10594   "Select ARTICLE and perform FORMS in the original article buffer.
10595 Then replace the article with the result."
10596   `(progn
10597      ;; We don't want the article to be marked as read.
10598      (let (gnus-mark-article-hook)
10599        (gnus-summary-select-article t t nil ,article))
10600      (set-buffer gnus-original-article-buffer)
10601      ,@forms
10602      (if (not (gnus-check-backend-function
10603                'request-replace-article (car gnus-article-current)))
10604          (gnus-message 5 "Read-only group; not replacing")
10605        (unless (gnus-request-replace-article
10606                 ,article (car gnus-article-current)
10607                 (current-buffer) t)
10608          (error "Couldn't replace article")))
10609      ;; The cache and backlog have to be flushed somewhat.
10610      (when gnus-keep-backlog
10611        (gnus-backlog-remove-article
10612         (car gnus-article-current) (cdr gnus-article-current)))
10613      (when gnus-use-cache
10614        (gnus-cache-update-article
10615         (car gnus-article-current) (cdr gnus-article-current)))))
10616
10617 (put 'gnus-with-article 'lisp-indent-function 1)
10618 (put 'gnus-with-article 'edebug-form-spec '(form body))
10619
10620 ;; Thread-based commands.
10621
10622 (defun gnus-summary-articles-in-thread (&optional article)
10623   "Return a list of all articles in the current thread.
10624 If ARTICLE is non-nil, return all articles in the thread that starts
10625 with that article."
10626   (let* ((article (or article (gnus-summary-article-number)))
10627          (data (gnus-data-find-list article))
10628          (top-level (gnus-data-level (car data)))
10629          (top-subject
10630           (cond ((null gnus-thread-operation-ignore-subject)
10631                  (gnus-simplify-subject-re
10632                   (mail-header-subject (gnus-data-header (car data)))))
10633                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10634                  (gnus-simplify-subject-fuzzy
10635                   (mail-header-subject (gnus-data-header (car data)))))
10636                 (t nil)))
10637          (end-point (save-excursion
10638                       (if (gnus-summary-go-to-next-thread)
10639                           (point) (point-max))))
10640          articles)
10641     (while (and data
10642                 (< (gnus-data-pos (car data)) end-point))
10643       (when (or (not top-subject)
10644                 (string= top-subject
10645                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10646                              (gnus-simplify-subject-fuzzy
10647                               (mail-header-subject
10648                                (gnus-data-header (car data))))
10649                            (gnus-simplify-subject-re
10650                             (mail-header-subject
10651                              (gnus-data-header (car data)))))))
10652         (push (gnus-data-number (car data)) articles))
10653       (unless (and (setq data (cdr data))
10654                    (> (gnus-data-level (car data)) top-level))
10655         (setq data nil)))
10656     ;; Return the list of articles.
10657     (nreverse articles)))
10658
10659 (defun gnus-summary-rethread-current ()
10660   "Rethread the thread the current article is part of."
10661   (interactive)
10662   (let* ((gnus-show-threads t)
10663          (article (gnus-summary-article-number))
10664          (id (mail-header-id (gnus-summary-article-header)))
10665          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10666     (unless id
10667       (error "No article on the current line"))
10668     (gnus-rebuild-thread id)
10669     (gnus-summary-goto-subject article)))
10670
10671 (defun gnus-summary-reparent-thread ()
10672   "Make the current article child of the marked (or previous) article.
10673
10674 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10675 is non-nil or the Subject: of both articles are the same."
10676   (interactive)
10677   (unless (not (gnus-group-read-only-p))
10678     (error "The current newsgroup does not support article editing"))
10679   (unless (<= (length gnus-newsgroup-processable) 1)
10680     (error "No more than one article may be marked"))
10681   (save-window-excursion
10682     (let ((gnus-article-buffer " *reparent*")
10683           (current-article (gnus-summary-article-number))
10684           ;; First grab the marked article, otherwise one line up.
10685           (parent-article (if (not (null gnus-newsgroup-processable))
10686                               (car gnus-newsgroup-processable)
10687                             (save-excursion
10688                               (if (eq (forward-line -1) 0)
10689                                   (gnus-summary-article-number)
10690                                 (error "Beginning of summary buffer"))))))
10691       (unless (not (eq current-article parent-article))
10692         (error "An article may not be self-referential"))
10693       (let ((message-id (mail-header-id
10694                          (gnus-summary-article-header parent-article))))
10695         (unless (and message-id (not (equal message-id "")))
10696           (error "No message-id in desired parent"))
10697         (gnus-with-article current-article
10698           (save-restriction
10699             (goto-char (point-min))
10700             (message-narrow-to-head)
10701             (if (re-search-forward "^References: " nil t)
10702                 (progn
10703                   (re-search-forward "^[^ \t]" nil t)
10704                   (forward-line -1)
10705                   (end-of-line)
10706                   (insert " " message-id))
10707               (insert "References: " message-id "\n"))))
10708         (set-buffer gnus-summary-buffer)
10709         (gnus-summary-unmark-all-processable)
10710         (gnus-summary-update-article current-article)
10711         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10712             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10713         (gnus-summary-rethread-current)
10714         (gnus-message 3 "Article %d is now the child of article %d"
10715                       current-article parent-article)))))
10716
10717 (defun gnus-summary-toggle-threads (&optional arg)
10718   "Toggle showing conversation threads.
10719 If ARG is positive number, turn showing conversation threads on."
10720   (interactive "P")
10721   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10722     (setq gnus-show-threads
10723           (if (null arg) (not gnus-show-threads)
10724             (> (prefix-numeric-value arg) 0)))
10725     (gnus-summary-prepare)
10726     (gnus-summary-goto-subject current)
10727     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10728     (gnus-summary-position-point)))
10729
10730 (defun gnus-summary-show-all-threads ()
10731   "Show all threads."
10732   (interactive)
10733   (save-excursion
10734     (let ((buffer-read-only nil))
10735       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10736   (gnus-summary-position-point))
10737
10738 (defun gnus-summary-show-thread ()
10739   "Show thread subtrees.
10740 Returns nil if no thread was there to be shown."
10741   (interactive)
10742   (let ((buffer-read-only nil)
10743         (orig (point))
10744         (end (point-at-eol))
10745         ;; Leave point at bol
10746         (beg (progn (beginning-of-line) (point))))
10747     (prog1
10748         ;; Any hidden lines here?
10749         (search-forward "\r" end t)
10750       (subst-char-in-region beg end ?\^M ?\n t)
10751       (goto-char orig)
10752       (gnus-summary-position-point))))
10753
10754 (defun gnus-summary-maybe-hide-threads ()
10755   "If requested, hide the threads that should be hidden."
10756   (when (and gnus-show-threads
10757              gnus-thread-hide-subtree)
10758     (gnus-summary-hide-all-threads
10759      (if (or (consp gnus-thread-hide-subtree)
10760              (functionp gnus-thread-hide-subtree))
10761          (gnus-make-predicate gnus-thread-hide-subtree)
10762        nil))))
10763
10764 ;;; Hiding predicates.
10765
10766 (defun gnus-article-unread-p (header)
10767   (memq (mail-header-number header) gnus-newsgroup-unreads))
10768
10769 (defun gnus-article-unseen-p (header)
10770   (memq (mail-header-number header) gnus-newsgroup-unseen))
10771
10772 (defun gnus-map-articles (predicate articles)
10773   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10774   (apply 'gnus-or (mapcar predicate
10775                           (mapcar 'gnus-summary-article-header articles))))
10776
10777 (defun gnus-summary-hide-all-threads (&optional predicate)
10778   "Hide all thread subtrees.
10779 If PREDICATE is supplied, threads that satisfy this predicate
10780 will not be hidden."
10781   (interactive)
10782   (save-excursion
10783     (goto-char (point-min))
10784     (let ((end nil))
10785       (while (not end)
10786         (when (or (not predicate)
10787                   (gnus-map-articles
10788                    predicate (gnus-summary-article-children)))
10789             (gnus-summary-hide-thread))
10790         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10791   (gnus-summary-position-point))
10792
10793 (defun gnus-summary-hide-thread ()
10794   "Hide thread subtrees.
10795 If PREDICATE is supplied, threads that satisfy this predicate
10796 will not be hidden.
10797 Returns nil if no threads were there to be hidden."
10798   (interactive)
10799   (let ((buffer-read-only nil)
10800         (start (point))
10801         (article (gnus-summary-article-number)))
10802     (goto-char start)
10803     ;; Go forward until either the buffer ends or the subthread
10804     ;; ends.
10805     (when (and (not (eobp))
10806                (or (zerop (gnus-summary-next-thread 1 t))
10807                    (goto-char (point-max))))
10808       (prog1
10809           (if (and (> (point) start)
10810                    (search-backward "\n" start t))
10811               (progn
10812                 (subst-char-in-region start (point) ?\n ?\^M)
10813                 (gnus-summary-goto-subject article))
10814             (goto-char start)
10815             nil)))))
10816
10817 (defun gnus-summary-go-to-next-thread (&optional previous)
10818   "Go to the same level (or less) next thread.
10819 If PREVIOUS is non-nil, go to previous thread instead.
10820 Return the article number moved to, or nil if moving was impossible."
10821   (let ((level (gnus-summary-thread-level))
10822         (way (if previous -1 1))
10823         (beg (point)))
10824     (forward-line way)
10825     (while (and (not (eobp))
10826                 (< level (gnus-summary-thread-level)))
10827       (forward-line way))
10828     (if (eobp)
10829         (progn
10830           (goto-char beg)
10831           nil)
10832       (setq beg (point))
10833       (prog1
10834           (gnus-summary-article-number)
10835         (goto-char beg)))))
10836
10837 (defun gnus-summary-next-thread (n &optional silent)
10838   "Go to the same level next N'th thread.
10839 If N is negative, search backward instead.
10840 Returns the difference between N and the number of skips actually
10841 done.
10842
10843 If SILENT, don't output messages."
10844   (interactive "p")
10845   (let ((backward (< n 0))
10846         (n (abs n)))
10847     (while (and (> n 0)
10848                 (gnus-summary-go-to-next-thread backward))
10849       (decf n))
10850     (unless silent
10851       (gnus-summary-position-point))
10852     (when (and (not silent) (/= 0 n))
10853       (gnus-message 7 "No more threads"))
10854     n))
10855
10856 (defun gnus-summary-prev-thread (n)
10857   "Go to the same level previous N'th thread.
10858 Returns the difference between N and the number of skips actually
10859 done."
10860   (interactive "p")
10861   (gnus-summary-next-thread (- n)))
10862
10863 (defun gnus-summary-go-down-thread ()
10864   "Go down one level in the current thread."
10865   (let ((children (gnus-summary-article-children)))
10866     (when children
10867       (gnus-summary-goto-subject (car children)))))
10868
10869 (defun gnus-summary-go-up-thread ()
10870   "Go up one level in the current thread."
10871   (let ((parent (gnus-summary-article-parent)))
10872     (when parent
10873       (gnus-summary-goto-subject parent))))
10874
10875 (defun gnus-summary-down-thread (n)
10876   "Go down thread N steps.
10877 If N is negative, go up instead.
10878 Returns the difference between N and how many steps down that were
10879 taken."
10880   (interactive "p")
10881   (let ((up (< n 0))
10882         (n (abs n)))
10883     (while (and (> n 0)
10884                 (if up (gnus-summary-go-up-thread)
10885                   (gnus-summary-go-down-thread)))
10886       (setq n (1- n)))
10887     (gnus-summary-position-point)
10888     (when (/= 0 n)
10889       (gnus-message 7 "Can't go further"))
10890     n))
10891
10892 (defun gnus-summary-up-thread (n)
10893   "Go up thread N steps.
10894 If N is negative, go down instead.
10895 Returns the difference between N and how many steps down that were
10896 taken."
10897   (interactive "p")
10898   (gnus-summary-down-thread (- n)))
10899
10900 (defun gnus-summary-top-thread ()
10901   "Go to the top of the thread."
10902   (interactive)
10903   (while (gnus-summary-go-up-thread))
10904   (gnus-summary-article-number))
10905
10906 (defun gnus-summary-kill-thread (&optional unmark)
10907   "Mark articles under current thread as read.
10908 If the prefix argument is positive, remove any kinds of marks.
10909 If the prefix argument is negative, tick articles instead."
10910   (interactive "P")
10911   (when unmark
10912     (setq unmark (prefix-numeric-value unmark)))
10913   (let ((articles (gnus-summary-articles-in-thread)))
10914     (save-excursion
10915       ;; Expand the thread.
10916       (gnus-summary-show-thread)
10917       ;; Mark all the articles.
10918       (while articles
10919         (gnus-summary-goto-subject (car articles))
10920         (cond ((null unmark)
10921                (gnus-summary-mark-article-as-read gnus-killed-mark))
10922               ((> unmark 0)
10923                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10924               (t
10925                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10926         (setq articles (cdr articles))))
10927     ;; Hide killed subtrees.
10928     (and (null unmark)
10929          gnus-thread-hide-killed
10930          (gnus-summary-hide-thread))
10931     ;; If marked as read, go to next unread subject.
10932     (when (null unmark)
10933       ;; Go to next unread subject.
10934       (gnus-summary-next-subject 1 t)))
10935   (gnus-set-mode-line 'summary))
10936
10937 ;; Summary sorting commands
10938
10939 (defun gnus-summary-sort-by-number (&optional reverse)
10940   "Sort the summary buffer by article number.
10941 Argument REVERSE means reverse order."
10942   (interactive "P")
10943   (gnus-summary-sort 'number reverse))
10944
10945 (defun gnus-summary-sort-by-random (&optional reverse)
10946   "Randomize the order in the summary buffer.
10947 Argument REVERSE means to randomize in reverse order."
10948   (interactive "P")
10949   (gnus-summary-sort 'random reverse))
10950
10951 (defun gnus-summary-sort-by-author (&optional reverse)
10952   "Sort the summary buffer by author name alphabetically.
10953 If `case-fold-search' is non-nil, case of letters is ignored.
10954 Argument REVERSE means reverse order."
10955   (interactive "P")
10956   (gnus-summary-sort 'author reverse))
10957
10958 (defun gnus-summary-sort-by-recipient (&optional reverse)
10959   "Sort the summary buffer by recipient name alphabetically.
10960 If `case-fold-search' is non-nil, case of letters is ignored.
10961 Argument REVERSE means reverse order."
10962   (interactive "P")
10963   (gnus-summary-sort 'recipient reverse))
10964
10965 (defun gnus-summary-sort-by-subject (&optional reverse)
10966   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10967 If `case-fold-search' is non-nil, case of letters is ignored.
10968 Argument REVERSE means reverse order."
10969   (interactive "P")
10970   (gnus-summary-sort 'subject reverse))
10971
10972 (defun gnus-summary-sort-by-date (&optional reverse)
10973   "Sort the summary buffer by date.
10974 Argument REVERSE means reverse order."
10975   (interactive "P")
10976   (gnus-summary-sort 'date reverse))
10977
10978 (defun gnus-summary-sort-by-score (&optional reverse)
10979   "Sort the summary buffer by score.
10980 Argument REVERSE means reverse order."
10981   (interactive "P")
10982   (gnus-summary-sort 'score reverse))
10983
10984 (defun gnus-summary-sort-by-lines (&optional reverse)
10985   "Sort the summary buffer by the number of lines.
10986 Argument REVERSE means reverse order."
10987   (interactive "P")
10988   (gnus-summary-sort 'lines reverse))
10989
10990 (defun gnus-summary-sort-by-chars (&optional reverse)
10991   "Sort the summary buffer by article length.
10992 Argument REVERSE means reverse order."
10993   (interactive "P")
10994   (gnus-summary-sort 'chars reverse))
10995
10996 (defun gnus-summary-sort-by-original (&optional reverse)
10997   "Sort the summary buffer using the default sorting method.
10998 Argument REVERSE means reverse order."
10999   (interactive "P")
11000   (let* ((buffer-read-only)
11001          (gnus-summary-prepare-hook nil))
11002     ;; We do the sorting by regenerating the threads.
11003     (gnus-summary-prepare)
11004     ;; Hide subthreads if needed.
11005     (gnus-summary-maybe-hide-threads)))
11006
11007 (defun gnus-summary-sort (predicate reverse)
11008   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11009   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11010          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11011          (gnus-thread-sort-functions
11012           (if (not reverse)
11013               thread
11014             `(lambda (t1 t2)
11015                (,thread t2 t1))))
11016          (gnus-sort-gathered-threads-function
11017           gnus-thread-sort-functions)
11018          (gnus-article-sort-functions
11019           (if (not reverse)
11020               article
11021             `(lambda (t1 t2)
11022                (,article t2 t1))))
11023          (buffer-read-only)
11024          (gnus-summary-prepare-hook nil))
11025     ;; We do the sorting by regenerating the threads.
11026     (gnus-summary-prepare)
11027     ;; Hide subthreads if needed.
11028     (gnus-summary-maybe-hide-threads)))
11029
11030 ;; Summary saving commands.
11031
11032 (defun gnus-summary-save-article (&optional n not-saved)
11033   "Save the current article using the default saver function.
11034 If N is a positive number, save the N next articles.
11035 If N is a negative number, save the N previous articles.
11036 If N is nil and any articles have been marked with the process mark,
11037 save those articles instead.
11038 The variable `gnus-default-article-saver' specifies the saver function."
11039   (interactive "P")
11040   (let* ((articles (gnus-summary-work-articles n))
11041          (save-buffer (save-excursion
11042                         (nnheader-set-temp-buffer " *Gnus Save*")))
11043          (num (length articles))
11044          header file)
11045     (dolist (article articles)
11046       (setq header (gnus-summary-article-header article))
11047       (if (not (vectorp header))
11048           ;; This is a pseudo-article.
11049           (if (assq 'name header)
11050               (gnus-copy-file (cdr (assq 'name header)))
11051             (gnus-message 1 "Article %d is unsaveable" article))
11052         ;; This is a real article.
11053         (save-window-excursion
11054           (let ((gnus-display-mime-function nil)
11055                 (gnus-article-prepare-hook nil))
11056             (gnus-summary-select-article t nil nil article)))
11057         (save-excursion
11058           (set-buffer save-buffer)
11059           (erase-buffer)
11060           (insert-buffer-substring gnus-original-article-buffer))
11061         (setq file (gnus-article-save save-buffer file num))
11062         (gnus-summary-remove-process-mark article)
11063         (unless not-saved
11064           (gnus-summary-set-saved-mark article))))
11065     (gnus-kill-buffer save-buffer)
11066     (gnus-summary-position-point)
11067     (gnus-set-mode-line 'summary)
11068     n))
11069
11070 (defun gnus-summary-pipe-output (&optional arg headers)
11071   "Pipe the current article to a subprocess.
11072 If N is a positive number, pipe the N next articles.
11073 If N is a negative number, pipe the N previous articles.
11074 If N is nil and any articles have been marked with the process mark,
11075 pipe those articles instead.
11076 If HEADERS (the symbolic prefix), include the headers, too."
11077   (interactive (gnus-interactive "P\ny"))
11078   (require 'gnus-art)
11079   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11080         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11081     (gnus-summary-save-article arg t))
11082   (let ((buffer (get-buffer "*Shell Command Output*")))
11083     (when (and buffer
11084                (not (zerop (buffer-size buffer))))
11085       (gnus-configure-windows 'pipe))))
11086
11087 (defun gnus-summary-save-article-mail (&optional arg)
11088   "Append the current article to an mail file.
11089 If N is a positive number, save the N next articles.
11090 If N is a negative number, save the N previous articles.
11091 If N is nil and any articles have been marked with the process mark,
11092 save those articles instead."
11093   (interactive "P")
11094   (require 'gnus-art)
11095   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11096     (gnus-summary-save-article arg)))
11097
11098 (defun gnus-summary-save-article-rmail (&optional arg)
11099   "Append the current article to an rmail file.
11100 If N is a positive number, save the N next articles.
11101 If N is a negative number, save the N previous articles.
11102 If N is nil and any articles have been marked with the process mark,
11103 save those articles instead."
11104   (interactive "P")
11105   (require 'gnus-art)
11106   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11107     (gnus-summary-save-article arg)))
11108
11109 (defun gnus-summary-save-article-file (&optional arg)
11110   "Append the current article to a file.
11111 If N is a positive number, save the N next articles.
11112 If N is a negative number, save the N previous articles.
11113 If N is nil and any articles have been marked with the process mark,
11114 save those articles instead."
11115   (interactive "P")
11116   (require 'gnus-art)
11117   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11118     (gnus-summary-save-article arg)))
11119
11120 (defun gnus-summary-write-article-file (&optional arg)
11121   "Write the current article to a file, deleting the previous file.
11122 If N is a positive number, save the N next articles.
11123 If N is a negative number, save the N previous articles.
11124 If N is nil and any articles have been marked with the process mark,
11125 save those articles instead."
11126   (interactive "P")
11127   (require 'gnus-art)
11128   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11129     (gnus-summary-save-article arg)))
11130
11131 (defun gnus-summary-save-article-body-file (&optional arg)
11132   "Append the current article body to a file.
11133 If N is a positive number, save the N next articles.
11134 If N is a negative number, save the N previous articles.
11135 If N is nil and any articles have been marked with the process mark,
11136 save those articles instead."
11137   (interactive "P")
11138   (require 'gnus-art)
11139   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11140     (gnus-summary-save-article arg)))
11141
11142 (defun gnus-summary-muttprint (&optional arg)
11143   "Print the current article using Muttprint.
11144 If N is a positive number, save the N next articles.
11145 If N is a negative number, save the N previous articles.
11146 If N is nil and any articles have been marked with the process mark,
11147 save those articles instead."
11148   (interactive "P")
11149   (require 'gnus-art)
11150   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11151     (gnus-summary-save-article arg t)))
11152
11153 (defun gnus-summary-pipe-message (program)
11154   "Pipe the current article through PROGRAM."
11155   (interactive "sProgram: ")
11156   (gnus-summary-select-article)
11157   (let ((mail-header-separator ""))
11158     (gnus-eval-in-buffer-window gnus-article-buffer
11159       (save-restriction
11160         (widen)
11161         (let ((start (window-start))
11162               buffer-read-only)
11163           (message-pipe-buffer-body program)
11164           (set-window-start (get-buffer-window (current-buffer)) start))))))
11165
11166 (defun gnus-get-split-value (methods)
11167   "Return a value based on the split METHODS."
11168   (let (split-name method result match)
11169     (when methods
11170       (save-excursion
11171         (set-buffer gnus-original-article-buffer)
11172         (save-restriction
11173           (nnheader-narrow-to-headers)
11174           (while (and methods (not split-name))
11175             (goto-char (point-min))
11176             (setq method (pop methods))
11177             (setq match (car method))
11178             (when (cond
11179                    ((stringp match)
11180                     ;; Regular expression.
11181                     (ignore-errors
11182                       (re-search-forward match nil t)))
11183                    ((functionp match)
11184                     ;; Function.
11185                     (save-restriction
11186                       (widen)
11187                       (setq result (funcall match gnus-newsgroup-name))))
11188                    ((consp match)
11189                     ;; Form.
11190                     (save-restriction
11191                       (widen)
11192                       (setq result (eval match)))))
11193               (setq split-name (cdr method))
11194               (cond ((stringp result)
11195                      (push (expand-file-name
11196                             result gnus-article-save-directory)
11197                            split-name))
11198                     ((consp result)
11199                      (setq split-name (append result split-name)))))))))
11200     (nreverse split-name)))
11201
11202 (defun gnus-valid-move-group-p (group)
11203   (and (boundp group)
11204        (symbol-name group)
11205        (symbol-value group)
11206        (gnus-get-function (gnus-find-method-for-group
11207                            (symbol-name group)) 'request-accept-article t)))
11208
11209 (defun gnus-read-move-group-name (prompt default articles prefix)
11210   "Read a group name."
11211   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11212          (minibuffer-confirm-incomplete nil) ; XEmacs
11213          (prom
11214           (format "%s %s to"
11215                   prompt
11216                   (if (> (length articles) 1)
11217                       (format "these %d articles" (length articles))
11218                     "this article")))
11219          (to-newsgroup
11220           (cond
11221            ((null split-name)
11222             (gnus-completing-read-with-default
11223              default prom
11224              gnus-active-hashtb
11225              'gnus-valid-move-group-p
11226              nil prefix
11227              'gnus-group-history))
11228            ((= 1 (length split-name))
11229             (gnus-completing-read-with-default
11230              (car split-name) prom
11231              gnus-active-hashtb
11232              'gnus-valid-move-group-p
11233              nil nil
11234              'gnus-group-history))
11235            (t
11236             (gnus-completing-read-with-default
11237              nil prom
11238              (mapcar 'list (nreverse split-name))
11239              nil nil nil
11240              'gnus-group-history))))
11241          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11242     (when to-newsgroup
11243       (if (or (string= to-newsgroup "")
11244               (string= to-newsgroup prefix))
11245           (setq to-newsgroup default))
11246       (unless to-newsgroup
11247         (error "No group name entered"))
11248       (or (gnus-active to-newsgroup)
11249           (gnus-activate-group to-newsgroup nil nil to-method)
11250           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11251                                      to-newsgroup))
11252               (or (and (gnus-request-create-group to-newsgroup to-method)
11253                        (gnus-activate-group
11254                         to-newsgroup nil nil to-method)
11255                        (gnus-subscribe-group to-newsgroup))
11256                   (error "Couldn't create group %s" to-newsgroup)))
11257           (error "No such group: %s" to-newsgroup)))
11258     to-newsgroup))
11259
11260 (defun gnus-summary-save-parts (type dir n &optional reverse)
11261   "Save parts matching TYPE to DIR.
11262 If REVERSE, save parts that do not match TYPE."
11263   (interactive
11264    (list (read-string "Save parts of type: "
11265                       (or (car gnus-summary-save-parts-type-history)
11266                           gnus-summary-save-parts-default-mime)
11267                       'gnus-summary-save-parts-type-history)
11268          (setq gnus-summary-save-parts-last-directory
11269                (read-file-name "Save to directory: "
11270                                gnus-summary-save-parts-last-directory
11271                                nil t))
11272          current-prefix-arg))
11273   (gnus-summary-iterate n
11274     (let ((gnus-display-mime-function nil)
11275           (gnus-inhibit-treatment t))
11276       (gnus-summary-select-article))
11277     (save-excursion
11278       (set-buffer gnus-article-buffer)
11279       (let ((handles (or gnus-article-mime-handles
11280                          (mm-dissect-buffer nil gnus-article-loose-mime)
11281                          (and gnus-article-emulate-mime
11282                               (mm-uu-dissect)))))
11283         (when handles
11284           (gnus-summary-save-parts-1 type dir handles reverse)
11285           (unless gnus-article-mime-handles ;; Don't destroy this case.
11286             (mm-destroy-parts handles)))))))
11287
11288 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11289   (if (stringp (car handle))
11290       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11291               (cdr handle))
11292     (when (if reverse
11293               (not (string-match type (mm-handle-media-type handle)))
11294             (string-match type (mm-handle-media-type handle)))
11295       (let ((file (expand-file-name
11296                    (gnus-map-function
11297                     mm-file-name-rewrite-functions
11298                     (file-name-nondirectory
11299                      (or
11300                       (mail-content-type-get
11301                        (mm-handle-disposition handle) 'filename)
11302                       (mail-content-type-get
11303                        (mm-handle-type handle) 'name)
11304                       (concat gnus-newsgroup-name
11305                               "." (number-to-string
11306                                    (cdr gnus-article-current))))))
11307                    dir)))
11308         (unless (file-exists-p file)
11309           (mm-save-part-to-file handle file))))))
11310
11311 ;; Summary extract commands
11312
11313 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11314   (let ((buffer-read-only nil)
11315         (article (gnus-summary-article-number))
11316         after-article b e)
11317     (unless (gnus-summary-goto-subject article)
11318       (error "No such article: %d" article))
11319     (gnus-summary-position-point)
11320     ;; If all commands are to be bunched up on one line, we collect
11321     ;; them here.
11322     (unless gnus-view-pseudos-separately
11323       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11324             files action)
11325         (while ps
11326           (setq action (cdr (assq 'action (car ps))))
11327           (setq files (list (cdr (assq 'name (car ps)))))
11328           (while (and ps (cdr ps)
11329                       (string= (or action "1")
11330                                (or (cdr (assq 'action (cadr ps))) "2")))
11331             (push (cdr (assq 'name (cadr ps))) files)
11332             (setcdr ps (cddr ps)))
11333           (when files
11334             (when (not (string-match "%s" action))
11335               (push " " files))
11336             (push " " files)
11337             (when (assq 'execute (car ps))
11338               (setcdr (assq 'execute (car ps))
11339                       (funcall (if (string-match "%s" action)
11340                                    'format 'concat)
11341                                action
11342                                (mapconcat
11343                                 (lambda (f)
11344                                   (if (equal f " ")
11345                                       f
11346                                     (shell-quote-argument f)))
11347                                 files " ")))))
11348           (setq ps (cdr ps)))))
11349     (if (and gnus-view-pseudos (not not-view))
11350         (while pslist
11351           (when (assq 'execute (car pslist))
11352             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11353                                   (eq gnus-view-pseudos 'not-confirm)))
11354           (setq pslist (cdr pslist)))
11355       (save-excursion
11356         (while pslist
11357           (setq after-article (or (cdr (assq 'article (car pslist)))
11358                                   (gnus-summary-article-number)))
11359           (gnus-summary-goto-subject after-article)
11360           (forward-line 1)
11361           (setq b (point))
11362           (insert "    " (file-name-nondirectory
11363                           (cdr (assq 'name (car pslist))))
11364                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11365           (setq e (point))
11366           (forward-line -1)             ; back to `b'
11367           (gnus-add-text-properties
11368            b (1- e) (list 'gnus-number gnus-reffed-article-number
11369                           gnus-mouse-face-prop gnus-mouse-face))
11370           (gnus-data-enter
11371            after-article gnus-reffed-article-number
11372            gnus-unread-mark b (car pslist) 0 (- e b))
11373           (setq gnus-newsgroup-unreads
11374                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11375                                          gnus-reffed-article-number))
11376           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11377           (setq pslist (cdr pslist)))))))
11378
11379 (defun gnus-pseudos< (p1 p2)
11380   (let ((c1 (cdr (assq 'action p1)))
11381         (c2 (cdr (assq 'action p2))))
11382     (and c1 c2 (string< c1 c2))))
11383
11384 (defun gnus-request-pseudo-article (props)
11385   (cond ((assq 'execute props)
11386          (gnus-execute-command (cdr (assq 'execute props)))))
11387   (let ((gnus-current-article (gnus-summary-article-number)))
11388     (gnus-run-hooks 'gnus-mark-article-hook)))
11389
11390 (defun gnus-execute-command (command &optional automatic)
11391   (save-excursion
11392     (gnus-article-setup-buffer)
11393     (set-buffer gnus-article-buffer)
11394     (setq buffer-read-only nil)
11395     (let ((command (if automatic command
11396                      (read-string "Command: " (cons command 0)))))
11397       (erase-buffer)
11398       (insert "$ " command "\n\n")
11399       (if gnus-view-pseudo-asynchronously
11400           (start-process "gnus-execute" (current-buffer) shell-file-name
11401                          shell-command-switch command)
11402         (call-process shell-file-name nil t nil
11403                       shell-command-switch command)))))
11404
11405 ;; Summary kill commands.
11406
11407 (defun gnus-summary-edit-global-kill (article)
11408   "Edit the \"global\" kill file."
11409   (interactive (list (gnus-summary-article-number)))
11410   (gnus-group-edit-global-kill article))
11411
11412 (defun gnus-summary-edit-local-kill ()
11413   "Edit a local kill file applied to the current newsgroup."
11414   (interactive)
11415   (setq gnus-current-headers (gnus-summary-article-header))
11416   (gnus-group-edit-local-kill
11417    (gnus-summary-article-number) gnus-newsgroup-name))
11418
11419 ;;; Header reading.
11420
11421 (defun gnus-read-header (id &optional header)
11422   "Read the headers of article ID and enter them into the Gnus system."
11423   (let ((group gnus-newsgroup-name)
11424         (gnus-override-method
11425          (or
11426           gnus-override-method
11427           (and (gnus-news-group-p gnus-newsgroup-name)
11428                (car (gnus-refer-article-methods)))))
11429         where)
11430     ;; First we check to see whether the header in question is already
11431     ;; fetched.
11432     (if (stringp id)
11433         ;; This is a Message-ID.
11434         (setq header (or header (gnus-id-to-header id)))
11435       ;; This is an article number.
11436       (setq header (or header (gnus-summary-article-header id))))
11437     (if (and header
11438              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11439         ;; We have found the header.
11440         header
11441       ;; We have to really fetch the header to this article.
11442       (save-excursion
11443         (set-buffer nntp-server-buffer)
11444         (when (setq where (gnus-request-head id group))
11445           (nnheader-fold-continuation-lines)
11446           (goto-char (point-max))
11447           (insert ".\n")
11448           (goto-char (point-min))
11449           (insert "211 ")
11450           (princ (cond
11451                   ((numberp id) id)
11452                   ((cdr where) (cdr where))
11453                   (header (mail-header-number header))
11454                   (t gnus-reffed-article-number))
11455                  (current-buffer))
11456           (insert " Article retrieved.\n"))
11457         (if (or (not where)
11458                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11459             ()                          ; Malformed head.
11460           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11461             (when (and (stringp id)
11462                        (not (string= (gnus-group-real-name group)
11463                                      (car where))))
11464               ;; If we fetched by Message-ID and the article came
11465               ;; from a different group, we fudge some bogus article
11466               ;; numbers for this article.
11467               (mail-header-set-number header gnus-reffed-article-number))
11468             (save-excursion
11469               (set-buffer gnus-summary-buffer)
11470               (decf gnus-reffed-article-number)
11471               (gnus-remove-header (mail-header-number header))
11472               (push header gnus-newsgroup-headers)
11473               (setq gnus-current-headers header)
11474               (push (mail-header-number header) gnus-newsgroup-limit)))
11475           header)))))
11476
11477 (defun gnus-remove-header (number)
11478   "Remove header NUMBER from `gnus-newsgroup-headers'."
11479   (if (and gnus-newsgroup-headers
11480            (= number (mail-header-number (car gnus-newsgroup-headers))))
11481       (pop gnus-newsgroup-headers)
11482     (let ((headers gnus-newsgroup-headers))
11483       (while (and (cdr headers)
11484                   (not (= number (mail-header-number (cadr headers)))))
11485         (pop headers))
11486       (when (cdr headers)
11487         (setcdr headers (cddr headers))))))
11488
11489 ;;;
11490 ;;; summary highlights
11491 ;;;
11492
11493 (defun gnus-highlight-selected-summary ()
11494   "Highlight selected article in summary buffer."
11495   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11496   (when gnus-summary-selected-face
11497     (save-excursion
11498       (let* ((beg (point-at-bol))
11499              (end (point-at-eol))
11500              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11501              (from (if (get-text-property beg gnus-mouse-face-prop)
11502                        beg
11503                      (or (next-single-property-change
11504                           beg gnus-mouse-face-prop nil end)
11505                          beg)))
11506              (to
11507               (if (= from end)
11508                   (- from 2)
11509                 (or (next-single-property-change
11510                      from gnus-mouse-face-prop nil end)
11511                     end))))
11512         ;; If no mouse-face prop on line we will have to = from = end,
11513         ;; so we highlight the entire line instead.
11514         (when (= (+ to 2) from)
11515           (setq from beg)
11516           (setq to end))
11517         (if gnus-newsgroup-selected-overlay
11518             ;; Move old overlay.
11519             (gnus-move-overlay
11520              gnus-newsgroup-selected-overlay from to (current-buffer))
11521           ;; Create new overlay.
11522           (gnus-overlay-put
11523            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11524            'face gnus-summary-selected-face))))))
11525
11526 (defvar gnus-summary-highlight-line-cached nil)
11527 (defvar gnus-summary-highlight-line-trigger nil)
11528
11529 (defun gnus-summary-highlight-line-0 ()
11530   (if (and (eq gnus-summary-highlight-line-trigger
11531                gnus-summary-highlight)
11532            gnus-summary-highlight-line-cached)
11533       gnus-summary-highlight-line-cached
11534     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11535           gnus-summary-highlight-line-cached
11536           (let* ((cond (list 'cond))
11537                  (c cond)
11538                  (list gnus-summary-highlight))
11539             (while list
11540               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11541                               nil))
11542               (setq c (cdr c)
11543                     list (cdr list)))
11544             (gnus-byte-compile (list 'lambda nil cond))))))
11545
11546 (defun gnus-summary-highlight-line ()
11547   "Highlight current line according to `gnus-summary-highlight'."
11548   (let* ((beg (point-at-bol))
11549          (article (or (gnus-summary-article-number) gnus-current-article))
11550          (score (or (cdr (assq article
11551                                gnus-newsgroup-scored))
11552                     gnus-summary-default-score 0))
11553          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11554          (inhibit-read-only t)
11555          (default gnus-summary-default-score)
11556          (default-high gnus-summary-default-high-score)
11557          (default-low gnus-summary-default-low-score)
11558          (uncached (and gnus-summary-use-undownloaded-faces
11559                         (memq article gnus-newsgroup-undownloaded)
11560                         (not (memq article gnus-newsgroup-cached)))))
11561     (let ((face (funcall (gnus-summary-highlight-line-0))))
11562       (unless (eq face (get-text-property beg 'face))
11563         (gnus-put-text-property-excluding-characters-with-faces
11564          beg (point-at-eol) 'face
11565          (setq face (if (boundp face) (symbol-value face) face)))
11566         (when gnus-summary-highlight-line-function
11567           (funcall gnus-summary-highlight-line-function article face))))))
11568
11569 (defun gnus-update-read-articles (group unread &optional compute)
11570   "Update the list of read articles in GROUP.
11571 UNREAD is a sorted list."
11572   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11573         (info (gnus-get-info group))
11574         (prev 1)
11575         read)
11576     (if (or (not info) (not active))
11577         ;; There is no info on this group if it was, in fact,
11578         ;; killed.  Gnus stores no information on killed groups, so
11579         ;; there's nothing to be done.
11580         ;; One could store the information somewhere temporarily,
11581         ;; perhaps...  Hmmm...
11582         ()
11583       ;; Remove any negative articles numbers.
11584       (while (and unread (< (car unread) 0))
11585         (setq unread (cdr unread)))
11586       ;; Remove any expired article numbers
11587       (while (and unread (< (car unread) (car active)))
11588         (setq unread (cdr unread)))
11589       ;; Compute the ranges of read articles by looking at the list of
11590       ;; unread articles.
11591       (while unread
11592         (when (/= (car unread) prev)
11593           (push (if (= prev (1- (car unread))) prev
11594                   (cons prev (1- (car unread))))
11595                 read))
11596         (setq prev (1+ (car unread)))
11597         (setq unread (cdr unread)))
11598       (when (<= prev (cdr active))
11599         (push (cons prev (cdr active)) read))
11600       (setq read (if (> (length read) 1) (nreverse read) read))
11601       (if compute
11602           read
11603         (save-excursion
11604           (let (setmarkundo)
11605             ;; Propagate the read marks to the backend.
11606             (when (gnus-check-backend-function 'request-set-mark group)
11607               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11608                     (add (gnus-remove-from-range read (gnus-info-read info))))
11609                 (when (or add del)
11610                   (unless (gnus-check-group group)
11611                     (error "Can't open server for %s" group))
11612                   (gnus-request-set-mark
11613                    group (delq nil (list (if add (list add 'add '(read)))
11614                                          (if del (list del 'del '(read))))))
11615                   (setq setmarkundo
11616                         `(gnus-request-set-mark
11617                           ,group
11618                           ',(delq nil (list
11619                                        (if del (list del 'add '(read)))
11620                                        (if add (list add 'del '(read))))))))))
11621             (set-buffer gnus-group-buffer)
11622             (gnus-undo-register
11623               `(progn
11624                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11625                  (gnus-info-set-read ',info ',(gnus-info-read info))
11626                  (gnus-get-unread-articles-in-group ',info
11627                                                     (gnus-active ,group))
11628                  (gnus-group-update-group ,group t)
11629                  ,setmarkundo))))
11630         ;; Enter this list into the group info.
11631         (gnus-info-set-read info read)
11632         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11633         (gnus-get-unread-articles-in-group info (gnus-active group))
11634         t))))
11635
11636 (defun gnus-offer-save-summaries ()
11637   "Offer to save all active summary buffers."
11638   (let (buffers)
11639     ;; Go through all buffers and find all summaries.
11640     (dolist (buffer (buffer-list))
11641       (when (and (setq buffer (buffer-name buffer))
11642                  (string-match "Summary" buffer)
11643                  (with-current-buffer buffer
11644                    ;; We check that this is, indeed, a summary buffer.
11645                    (and (eq major-mode 'gnus-summary-mode)
11646                         ;; Also make sure this isn't bogus.
11647                         gnus-newsgroup-prepared
11648                         ;; Also make sure that this isn't a
11649                         ;; dead summary buffer.
11650                         (not gnus-dead-summary-mode))))
11651         (push buffer buffers)))
11652     ;; Go through all these summary buffers and offer to save them.
11653     (when buffers
11654       (save-excursion
11655         (map-y-or-n-p
11656          "Update summary buffer %s? "
11657          (lambda (buf)
11658            (switch-to-buffer buf)
11659            (gnus-summary-exit))
11660          buffers)))))
11661
11662 (defun gnus-summary-setup-default-charset ()
11663   "Setup newsgroup default charset."
11664   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11665       (setq gnus-newsgroup-charset nil)
11666     (let* ((ignored-charsets
11667             (or gnus-newsgroup-ephemeral-ignored-charsets
11668                 (append
11669                  (and gnus-newsgroup-name
11670                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11671                  gnus-newsgroup-ignored-charsets))))
11672       (setq gnus-newsgroup-charset
11673             (or gnus-newsgroup-ephemeral-charset
11674                 (and gnus-newsgroup-name
11675                      (gnus-parameter-charset gnus-newsgroup-name))
11676                 gnus-default-charset))
11677       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11678            ignored-charsets))))
11679
11680 ;;;
11681 ;;; Mime Commands
11682 ;;;
11683
11684 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11685   "Display the current article buffer fully MIME-buttonized.
11686 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11687 treated as multipart/mixed."
11688   (interactive "P")
11689   (require 'gnus-art)
11690   (let ((gnus-unbuttonized-mime-types nil)
11691         (gnus-mime-display-multipart-as-mixed show-all-parts))
11692     (gnus-summary-show-article)))
11693
11694 (defun gnus-summary-repair-multipart (article)
11695   "Add a Content-Type header to a multipart article without one."
11696   (interactive (list (gnus-summary-article-number)))
11697   (gnus-with-article article
11698     (message-narrow-to-head)
11699     (message-remove-header "Mime-Version")
11700     (goto-char (point-max))
11701     (insert "Mime-Version: 1.0\n")
11702     (widen)
11703     (when (search-forward "\n--" nil t)
11704       (let ((separator (buffer-substring (point) (point-at-eol))))
11705         (message-narrow-to-head)
11706         (message-remove-header "Content-Type")
11707         (goto-char (point-max))
11708         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11709                         separator))
11710         (widen))))
11711   (let (gnus-mark-article-hook)
11712     (gnus-summary-select-article t t nil article)))
11713
11714 (defun gnus-summary-toggle-display-buttonized ()
11715   "Toggle the buttonizing of the article buffer."
11716   (interactive)
11717   (require 'gnus-art)
11718   (if (setq gnus-inhibit-mime-unbuttonizing
11719             (not gnus-inhibit-mime-unbuttonizing))
11720       (let ((gnus-unbuttonized-mime-types nil))
11721         (gnus-summary-show-article))
11722     (gnus-summary-show-article)))
11723
11724 ;;;
11725 ;;; Generic summary marking commands
11726 ;;;
11727
11728 (defvar gnus-summary-marking-alist
11729   '((read gnus-del-mark "d")
11730     (unread gnus-unread-mark "u")
11731     (ticked gnus-ticked-mark "!")
11732     (dormant gnus-dormant-mark "?")
11733     (expirable gnus-expirable-mark "e"))
11734   "An alist of names/marks/keystrokes.")
11735
11736 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11737 (defvar gnus-summary-mark-map)
11738
11739 (defun gnus-summary-make-all-marking-commands ()
11740   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11741   (dolist (elem gnus-summary-marking-alist)
11742     (apply 'gnus-summary-make-marking-command elem)))
11743
11744 (defun gnus-summary-make-marking-command (name mark keystroke)
11745   (let ((map (make-sparse-keymap)))
11746     (define-key gnus-summary-generic-mark-map keystroke map)
11747     (dolist (lway `((next "next" next nil "n")
11748                     (next-unread "next unread" next t "N")
11749                     (prev "previous" prev nil "p")
11750                     (prev-unread "previous unread" prev t "P")
11751                     (nomove "" nil nil ,keystroke)))
11752       (let ((func (gnus-summary-make-marking-command-1
11753                    mark (car lway) lway name)))
11754         (setq func (eval func))
11755         (define-key map (nth 4 lway) func)))))
11756
11757 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11758   `(defun ,(intern
11759             (format "gnus-summary-put-mark-as-%s%s"
11760                     name (if (eq way 'nomove)
11761                              ""
11762                            (concat "-" (symbol-name way)))))
11763      (n)
11764      ,(format
11765        "Mark the current article as %s%s.
11766 If N, the prefix, then repeat N times.
11767 If N is negative, move in reverse order.
11768 The difference between N and the actual number of articles marked is
11769 returned."
11770        name (cadr lway))
11771      (interactive "p")
11772      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11773
11774 (defun gnus-summary-generic-mark (n mark move unread)
11775   "Mark N articles with MARK."
11776   (unless (eq major-mode 'gnus-summary-mode)
11777     (error "This command can only be used in the summary buffer"))
11778   (gnus-summary-show-thread)
11779   (let ((nummove
11780          (cond
11781           ((eq move 'next) 1)
11782           ((eq move 'prev) -1)
11783           (t 0))))
11784     (if (zerop nummove)
11785         (setq n 1)
11786       (when (< n 0)
11787         (setq n (abs n)
11788               nummove (* -1 nummove))))
11789     (while (and (> n 0)
11790                 (gnus-summary-mark-article nil mark)
11791                 (zerop (gnus-summary-next-subject nummove unread t)))
11792       (setq n (1- n)))
11793     (when (/= 0 n)
11794       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11795     (gnus-summary-recenter)
11796     (gnus-summary-position-point)
11797     (gnus-set-mode-line 'summary)
11798     n))
11799
11800 (defun gnus-summary-insert-articles (articles)
11801   (when (setq articles
11802               (gnus-sorted-difference articles
11803                                       (mapcar (lambda (h)
11804                                                 (mail-header-number h))
11805                                               gnus-newsgroup-headers)))
11806     (setq gnus-newsgroup-headers
11807           (gnus-merge 'list
11808                       gnus-newsgroup-headers
11809                       (gnus-fetch-headers articles)
11810                       'gnus-article-sort-by-number))
11811     ;; Suppress duplicates?
11812     (when gnus-suppress-duplicates
11813       (gnus-dup-suppress-articles))
11814
11815     ;; We might want to build some more threads first.
11816     (when (and gnus-fetch-old-headers
11817                (eq gnus-headers-retrieved-by 'nov))
11818       (if (eq gnus-fetch-old-headers 'invisible)
11819           (gnus-build-all-threads)
11820         (gnus-build-old-threads)))
11821     ;; Let the Gnus agent mark articles as read.
11822     (when gnus-agent
11823       (gnus-agent-get-undownloaded-list))
11824     ;; Remove list identifiers from subject
11825     (when gnus-list-identifiers
11826       (gnus-summary-remove-list-identifiers))
11827     ;; First and last article in this newsgroup.
11828     (when gnus-newsgroup-headers
11829       (setq gnus-newsgroup-begin
11830             (mail-header-number (car gnus-newsgroup-headers))
11831             gnus-newsgroup-end
11832             (mail-header-number
11833              (gnus-last-element gnus-newsgroup-headers))))
11834     (when gnus-use-scoring
11835       (gnus-possibly-score-headers))))
11836
11837 (defun gnus-summary-insert-old-articles (&optional all)
11838   "Insert all old articles in this group.
11839 If ALL is non-nil, already read articles become readable.
11840 If ALL is a number, fetch this number of articles."
11841   (interactive "P")
11842   (prog1
11843       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11844             older len)
11845         (setq older
11846               ;; Some nntp servers lie about their active range.  When
11847               ;; this happens, the active range can be in the millions.
11848               ;; Use a compressed range to avoid creating a huge list.
11849               (gnus-range-difference (list gnus-newsgroup-active) old))
11850         (setq len (gnus-range-length older))
11851         (cond
11852          ((null older) nil)
11853          ((numberp all)
11854           (if (< all len)
11855               (let ((older-range (nreverse older)))
11856                 (setq older nil)
11857
11858                 (while (> all 0)
11859                   (let* ((r (pop older-range))
11860                          (min (if (numberp r) r (car r)))
11861                          (max (if (numberp r) r (cdr r))))
11862                     (while (and (<= min max)
11863                                 (> all 0))
11864                       (push max older)
11865                       (setq all (1- all)
11866                             max (1- max))))))
11867             (setq older (gnus-uncompress-range older))))
11868          (all
11869           (setq older (gnus-uncompress-range older)))
11870          (t
11871           (when (and (numberp gnus-large-newsgroup)
11872                    (> len gnus-large-newsgroup))
11873               (let* ((cursor-in-echo-area nil)
11874                      (initial (gnus-parameter-large-newsgroup-initial
11875                                gnus-newsgroup-name))
11876                      (input
11877                       (read-string
11878                        (format
11879                         "How many articles from %s (%s %d): "
11880                         (gnus-limit-string
11881                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11882                         (if initial "max" "default")
11883                         len)
11884                        (if initial
11885                            (cons (number-to-string initial)
11886                                  0)))))
11887                 (unless (string-match "^[ \t]*$" input)
11888                   (setq all (string-to-number input))
11889                   (if (< all len)
11890                       (let ((older-range (nreverse older)))
11891                         (setq older nil)
11892
11893                         (while (> all 0)
11894                           (let* ((r (pop older-range))
11895                                  (min (if (numberp r) r (car r)))
11896                                  (max (if (numberp r) r (cdr r))))
11897                             (while (and (<= min max)
11898                                         (> all 0))
11899                               (push max older)
11900                               (setq all (1- all)
11901                                     max (1- max))))))))))
11902           (setq older (gnus-uncompress-range older))))
11903         (if (not older)
11904             (message "No old news.")
11905           (gnus-summary-insert-articles older)
11906           (gnus-summary-limit (gnus-sorted-nunion old older))))
11907     (gnus-summary-position-point)))
11908
11909 (defun gnus-summary-insert-new-articles ()
11910   "Insert all new articles in this group."
11911   (interactive)
11912   (prog1
11913       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11914             (old-active gnus-newsgroup-active)
11915             (nnmail-fetched-sources (list t))
11916             i new)
11917         (setq gnus-newsgroup-active
11918               (gnus-activate-group gnus-newsgroup-name 'scan))
11919         (setq i (cdr gnus-newsgroup-active))
11920         (while (> i (cdr old-active))
11921           (push i new)
11922           (decf i))
11923         (if (not new)
11924             (message "No gnus is bad news")
11925           (gnus-summary-insert-articles new)
11926           (setq gnus-newsgroup-unreads
11927                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11928           (gnus-summary-limit (gnus-sorted-nunion old new))))
11929     (gnus-summary-position-point)))
11930
11931 (gnus-summary-make-all-marking-commands)
11932
11933 (gnus-ems-redefine)
11934
11935 (provide 'gnus-sum)
11936
11937 (run-hooks 'gnus-sum-load-hook)
11938
11939 ;; Local Variables:
11940 ;; coding: iso-8859-1
11941 ;; End:
11942
11943 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11944 ;;; gnus-sum.el ends here