* gnus-agent.el (gnus-agent-possibly-synchronize-flags): A server
[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  
1060        (not (string= user-mail-address ""))
1061        (regexp-quote user-mail-address))
1062   "*Regexp of From headers that may be suppressed in favor of To headers."
1063   :version "21.1"
1064   :group 'gnus-summary
1065   :type 'regexp)
1066
1067 (defcustom gnus-summary-to-prefix "-> "
1068   "*String prefixed to the To field in the summary line when
1069 using `gnus-ignored-from-addresses'."
1070   :version "22.1"
1071   :group 'gnus-summary
1072   :type 'string)
1073
1074 (defcustom gnus-summary-newsgroup-prefix "=> "
1075   "*String prefixed to the Newsgroup field in the summary
1076 line when using `gnus-ignored-from-addresses'."
1077   :version "22.1"
1078   :group 'gnus-summary
1079   :type 'string)
1080
1081 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1082   "List of charsets that should be ignored.
1083 When these charsets are used in the \"charset\" parameter, the
1084 default charset will be used instead."
1085   :version "21.1"
1086   :type '(repeat symbol)
1087   :group 'gnus-charset)
1088
1089 (gnus-define-group-parameter
1090  ignored-charsets
1091  :type list
1092  :function-document
1093  "Return the ignored charsets of GROUP."
1094  :variable gnus-group-ignored-charsets-alist
1095  :variable-default
1096  '(("alt\\.chinese\\.text" iso-8859-1))
1097  :variable-document
1098  "Alist of regexps (to match group names) and charsets that should be ignored.
1099 When these charsets are used in the \"charset\" parameter, the
1100 default charset will be used instead."
1101  :variable-group gnus-charset
1102  :variable-type '(repeat (cons (regexp :tag "Group")
1103                                (repeat symbol)))
1104  :parameter-type '(choice :tag "Ignored charsets"
1105                           :value nil
1106                           (repeat (symbol)))
1107  :parameter-document       "\
1108 List of charsets that should be ignored.
1109
1110 When these charsets are used in the \"charset\" parameter, the
1111 default charset will be used instead.")
1112
1113 (defcustom gnus-group-highlight-words-alist nil
1114   "Alist of group regexps and highlight regexps.
1115 This variable uses the same syntax as `gnus-emphasis-alist'."
1116   :version "21.1"
1117   :type '(repeat (cons (regexp :tag "Group")
1118                        (repeat (list (regexp :tag "Highlight regexp")
1119                                      (number :tag "Group for entire word" 0)
1120                                      (number :tag "Group for displayed part" 0)
1121                                      (symbol :tag "Face"
1122                                              gnus-emphasis-highlight-words)))))
1123   :group 'gnus-summary-visual)
1124
1125 (defcustom gnus-summary-show-article-charset-alist
1126   nil
1127   "Alist of number and charset.
1128 The article will be shown with the charset corresponding to the
1129 numbered argument.
1130 For example: ((1 . cn-gb-2312) (2 . big5))."
1131   :version "21.1"
1132   :type '(repeat (cons (number :tag "Argument" 1)
1133                        (symbol :tag "Charset")))
1134   :group 'gnus-charset)
1135
1136 (defcustom gnus-preserve-marks t
1137   "Whether marks are preserved when moving, copying and respooling messages."
1138   :version "21.1"
1139   :type 'boolean
1140   :group 'gnus-summary-marks)
1141
1142 (defcustom gnus-alter-articles-to-read-function nil
1143   "Function to be called to alter the list of articles to be selected."
1144   :type '(choice (const nil) function)
1145   :group 'gnus-summary)
1146
1147 (defcustom gnus-orphan-score nil
1148   "*All orphans get this score added.  Set in the score file."
1149   :group 'gnus-score-default
1150   :type '(choice (const nil)
1151                  integer))
1152
1153 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1154   "*A regexp to match MIME parts when saving multiple parts of a
1155 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1156 This regexp will be used by default when prompting the user for which
1157 type of files to save."
1158   :group 'gnus-summary
1159   :type 'regexp)
1160
1161 (defcustom gnus-read-all-available-headers nil
1162   "Whether Gnus should parse all headers made available to it.
1163 This is mostly relevant for slow back ends where the user may
1164 wish to widen the summary buffer to include all headers
1165 that were fetched.  Say, for nnultimate groups."
1166   :version "22.1"
1167   :group 'gnus-summary
1168   :type '(choice boolean regexp))
1169
1170 (defcustom gnus-summary-muttprint-program "muttprint"
1171   "Command (and optional arguments) used to run Muttprint."
1172   :version "22.1"
1173   :group 'gnus-summary
1174   :type 'string)
1175
1176 (defcustom gnus-article-loose-mime t
1177   "If non-nil, don't require MIME-Version header.
1178 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1179 supply the MIME-Version header or deliberately strip it from the mail.
1180 If non-nil (the default), Gnus will treat some articles as MIME
1181 even if the MIME-Version header is missing."
1182   :version "22.1"
1183   :type 'boolean
1184   :group 'gnus-article-mime)
1185
1186 (defcustom gnus-article-emulate-mime t
1187   "If non-nil, use MIME emulation for uuencode and the like.
1188 This means that Gnus will search message bodies for text that look
1189 like uuencoded bits, yEncoded bits, and so on, and present that using
1190 the normal Gnus MIME machinery."
1191   :version "22.1"
1192   :type 'boolean
1193   :group 'gnus-article-mime)
1194
1195 ;;; Internal variables
1196
1197 (defvar gnus-summary-display-cache nil)
1198 (defvar gnus-article-mime-handles nil)
1199 (defvar gnus-article-decoded-p nil)
1200 (defvar gnus-article-charset nil)
1201 (defvar gnus-article-ignored-charsets nil)
1202 (defvar gnus-scores-exclude-files nil)
1203 (defvar gnus-page-broken nil)
1204
1205 (defvar gnus-original-article nil)
1206 (defvar gnus-article-internal-prepare-hook nil)
1207 (defvar gnus-newsgroup-process-stack nil)
1208
1209 (defvar gnus-thread-indent-array nil)
1210 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1211 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1212   "Function called to sort the articles within a thread after it has been gathered together.")
1213
1214 (defvar gnus-summary-save-parts-type-history nil)
1215 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1216
1217 ;; Avoid highlighting in kill files.
1218 (defvar gnus-summary-inhibit-highlight nil)
1219 (defvar gnus-newsgroup-selected-overlay nil)
1220 (defvar gnus-inhibit-limiting nil)
1221 (defvar gnus-newsgroup-adaptive-score-file nil)
1222 (defvar gnus-current-score-file nil)
1223 (defvar gnus-current-move-group nil)
1224 (defvar gnus-current-copy-group nil)
1225 (defvar gnus-current-crosspost-group nil)
1226 (defvar gnus-newsgroup-display nil)
1227
1228 (defvar gnus-newsgroup-dependencies nil)
1229 (defvar gnus-newsgroup-adaptive nil)
1230 (defvar gnus-summary-display-article-function nil)
1231 (defvar gnus-summary-highlight-line-function nil
1232   "Function called after highlighting a summary line.")
1233
1234 (defvar gnus-summary-line-format-alist
1235   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1236     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1237     (?s gnus-tmp-subject-or-nil ?s)
1238     (?n gnus-tmp-name ?s)
1239     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1240         ?s)
1241     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1242             gnus-tmp-from) ?s)
1243     (?F gnus-tmp-from ?s)
1244     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1245     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1246     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1247     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1248     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1249     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1250     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1251     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1252     (?L gnus-tmp-lines ?s)
1253     (?O gnus-tmp-downloaded ?c)
1254     (?I gnus-tmp-indentation ?s)
1255     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1256     (?R gnus-tmp-replied ?c)
1257     (?\[ gnus-tmp-opening-bracket ?c)
1258     (?\] gnus-tmp-closing-bracket ?c)
1259     (?\> (make-string gnus-tmp-level ? ) ?s)
1260     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1261     (?i gnus-tmp-score ?d)
1262     (?z gnus-tmp-score-char ?c)
1263     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1264     (?U gnus-tmp-unread ?c)
1265     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1266         ?s)
1267     (?t (gnus-summary-number-of-articles-in-thread
1268          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1269         ?d)
1270     (?e (gnus-summary-number-of-articles-in-thread
1271          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1272         ?c)
1273     (?u gnus-tmp-user-defined ?s)
1274     (?P (gnus-pick-line-number) ?d)
1275     (?B gnus-tmp-thread-tree-header-string ?s)
1276     (user-date (gnus-user-date
1277                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1278   "An alist of format specifications that can appear in summary lines.
1279 These are paired with what variables they correspond with, along with
1280 the type of the variable (string, integer, character, etc).")
1281
1282 (defvar gnus-summary-dummy-line-format-alist
1283   `((?S gnus-tmp-subject ?s)
1284     (?N gnus-tmp-number ?d)
1285     (?u gnus-tmp-user-defined ?s)))
1286
1287 (defvar gnus-summary-mode-line-format-alist
1288   `((?G gnus-tmp-group-name ?s)
1289     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1290     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1291     (?A gnus-tmp-article-number ?d)
1292     (?Z gnus-tmp-unread-and-unselected ?s)
1293     (?V gnus-version ?s)
1294     (?U gnus-tmp-unread-and-unticked ?d)
1295     (?S gnus-tmp-subject ?s)
1296     (?e gnus-tmp-unselected ?d)
1297     (?u gnus-tmp-user-defined ?s)
1298     (?d (length gnus-newsgroup-dormant) ?d)
1299     (?t (length gnus-newsgroup-marked) ?d)
1300     (?h (length gnus-newsgroup-spam-marked) ?d)
1301     (?r (length gnus-newsgroup-reads) ?d)
1302     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1303     (?E gnus-newsgroup-expunged-tally ?d)
1304     (?s (gnus-current-score-file-nondirectory) ?s)))
1305
1306 (defvar gnus-last-search-regexp nil
1307   "Default regexp for article search command.")
1308
1309 (defvar gnus-last-shell-command nil
1310   "Default shell command on article.")
1311
1312 (defvar gnus-newsgroup-agentized nil
1313   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1314 (defvar gnus-newsgroup-begin nil)
1315 (defvar gnus-newsgroup-end nil)
1316 (defvar gnus-newsgroup-last-rmail nil)
1317 (defvar gnus-newsgroup-last-mail nil)
1318 (defvar gnus-newsgroup-last-folder nil)
1319 (defvar gnus-newsgroup-last-file nil)
1320 (defvar gnus-newsgroup-auto-expire nil)
1321 (defvar gnus-newsgroup-active nil)
1322
1323 (defvar gnus-newsgroup-data nil)
1324 (defvar gnus-newsgroup-data-reverse nil)
1325 (defvar gnus-newsgroup-limit nil)
1326 (defvar gnus-newsgroup-limits nil)
1327 (defvar gnus-summary-use-undownloaded-faces nil)
1328
1329 (defvar gnus-newsgroup-unreads nil
1330   "Sorted list of unread articles in the current newsgroup.")
1331
1332 (defvar gnus-newsgroup-unselected nil
1333   "Sorted list of unselected unread articles in the current newsgroup.")
1334
1335 (defvar gnus-newsgroup-reads nil
1336   "Alist of read articles and article marks in the current newsgroup.")
1337
1338 (defvar gnus-newsgroup-expunged-tally nil)
1339
1340 (defvar gnus-newsgroup-marked nil
1341   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1342
1343 (defvar gnus-newsgroup-spam-marked nil
1344   "List of ranges of articles that have been marked as spam.")
1345
1346 (defvar gnus-newsgroup-killed nil
1347   "List of ranges of articles that have been through the scoring process.")
1348
1349 (defvar gnus-newsgroup-cached nil
1350   "Sorted list of articles that come from the article cache.")
1351
1352 (defvar gnus-newsgroup-saved nil
1353   "List of articles that have been saved.")
1354
1355 (defvar gnus-newsgroup-kill-headers nil)
1356
1357 (defvar gnus-newsgroup-replied nil
1358   "List of articles that have been replied to in the current newsgroup.")
1359
1360 (defvar gnus-newsgroup-forwarded nil
1361   "List of articles that have been forwarded in the current newsgroup.")
1362
1363 (defvar gnus-newsgroup-recent nil
1364   "List of articles that have are recent in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-expirable nil
1367   "Sorted list of articles in the current newsgroup that can be expired.")
1368
1369 (defvar gnus-newsgroup-processable nil
1370   "List of articles in the current newsgroup that can be processed.")
1371
1372 (defvar gnus-newsgroup-downloadable nil
1373   "Sorted list of articles in the current newsgroup that can be processed.")
1374
1375 (defvar gnus-newsgroup-unfetched nil
1376   "Sorted list of articles in the current newsgroup whose headers have
1377 not been fetched into the agent.
1378
1379 This list will always be a subset of gnus-newsgroup-undownloaded.")
1380
1381 (defvar gnus-newsgroup-undownloaded nil
1382   "List of articles in the current newsgroup that haven't been downloaded.")
1383
1384 (defvar gnus-newsgroup-unsendable nil
1385   "List of articles in the current newsgroup that won't be sent.")
1386
1387 (defvar gnus-newsgroup-bookmarks nil
1388   "List of articles in the current newsgroup that have bookmarks.")
1389
1390 (defvar gnus-newsgroup-dormant nil
1391   "Sorted list of dormant articles in the current newsgroup.")
1392
1393 (defvar gnus-newsgroup-unseen nil
1394   "List of unseen articles in the current newsgroup.")
1395
1396 (defvar gnus-newsgroup-seen nil
1397   "Range of seen articles in the current newsgroup.")
1398
1399 (defvar gnus-newsgroup-articles nil
1400   "List of articles in the current newsgroup.")
1401
1402 (defvar gnus-newsgroup-scored nil
1403   "List of scored articles in the current newsgroup.")
1404
1405 (defvar gnus-newsgroup-headers nil
1406   "List of article headers in the current newsgroup.")
1407
1408 (defvar gnus-newsgroup-threads nil)
1409
1410 (defvar gnus-newsgroup-prepared nil
1411   "Whether the current group has been prepared properly.")
1412
1413 (defvar gnus-newsgroup-ancient nil
1414   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1415
1416 (defvar gnus-newsgroup-sparse nil)
1417
1418 (defvar gnus-current-article nil)
1419 (defvar gnus-article-current nil)
1420 (defvar gnus-current-headers nil)
1421 (defvar gnus-have-all-headers nil)
1422 (defvar gnus-last-article nil)
1423 (defvar gnus-newsgroup-history nil)
1424 (defvar gnus-newsgroup-charset nil)
1425 (defvar gnus-newsgroup-ephemeral-charset nil)
1426 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1427
1428 (defvar gnus-article-before-search nil)
1429
1430 (defvar gnus-summary-local-variables
1431   '(gnus-newsgroup-name
1432     gnus-newsgroup-begin gnus-newsgroup-end
1433     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1434     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1435     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1436     gnus-newsgroup-unselected gnus-newsgroup-marked
1437     gnus-newsgroup-spam-marked
1438     gnus-newsgroup-reads gnus-newsgroup-saved
1439     gnus-newsgroup-replied gnus-newsgroup-forwarded
1440     gnus-newsgroup-recent
1441     gnus-newsgroup-expirable
1442     gnus-newsgroup-processable gnus-newsgroup-killed
1443     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1444     gnus-newsgroup-unfetched
1445     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1446     gnus-newsgroup-seen gnus-newsgroup-articles
1447     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1448     gnus-newsgroup-headers gnus-newsgroup-threads
1449     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1450     gnus-current-article gnus-current-headers gnus-have-all-headers
1451     gnus-last-article gnus-article-internal-prepare-hook
1452     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1453     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1454     gnus-thread-expunge-below
1455     gnus-score-alist gnus-current-score-file
1456     (gnus-summary-expunge-below . global)
1457     (gnus-summary-mark-below . global)
1458     (gnus-orphan-score . global)
1459     gnus-newsgroup-active gnus-scores-exclude-files
1460     gnus-newsgroup-history gnus-newsgroup-ancient
1461     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1462     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1463     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1464     (gnus-newsgroup-expunged-tally . 0)
1465     gnus-cache-removable-articles gnus-newsgroup-cached
1466     gnus-newsgroup-data gnus-newsgroup-data-reverse
1467     gnus-newsgroup-limit gnus-newsgroup-limits
1468     gnus-newsgroup-charset gnus-newsgroup-display
1469     gnus-summary-use-undownloaded-faces)
1470   "Variables that are buffer-local to the summary buffers.")
1471
1472 (defvar gnus-newsgroup-variables nil
1473   "A list of variables that have separate values in different newsgroups.
1474 A list of newsgroup (summary buffer) local variables, or cons of
1475 variables and their default expressions to be evalled (when the default
1476 values are not nil), that should be made global while the summary buffer
1477 is active.
1478
1479 Note: The default expressions will be evaluated (using function `eval')
1480 before assignment to the local variable rather than just assigned to it.
1481 If the default expression is the symbol `global', that symbol will not
1482 be evaluated but the global value of the local variable will be used
1483 instead.
1484
1485 These variables can be used to set variables in the group parameters
1486 while still allowing them to affect operations done in other buffers.
1487 For example:
1488
1489 \(setq gnus-newsgroup-variables
1490      '(message-use-followup-to
1491        (gnus-visible-headers .
1492          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1493 ")
1494
1495 ;; Byte-compiler warning.
1496 (eval-when-compile
1497   ;; Bind features so that require will believe that gnus-sum has
1498   ;; already been loaded (avoids infinite recursion)
1499   (let ((features (cons 'gnus-sum features)))
1500     ;; Several of the declarations in gnus-sum are needed to load the
1501     ;; following files. Right now, these definitions have been
1502     ;; compiled but not defined (evaluated).  We could either do a
1503     ;; eval-and-compile about all of the declarations or evaluate the
1504     ;; source file.
1505     (if (boundp 'gnus-newsgroup-variables)
1506         nil
1507       (load "gnus-sum.el" t t t))
1508     (require 'gnus)
1509     (require 'gnus-art)))
1510
1511 ;; MIME stuff.
1512
1513 (defvar gnus-decode-encoded-word-methods
1514   '(mail-decode-encoded-word-string)
1515   "List of methods used to decode encoded words.
1516
1517 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1518 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1519 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1520 whose names match REGEXP.
1521
1522 For example:
1523 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1524  mail-decode-encoded-word-string
1525  (\"chinese\" . rfc1843-decode-string))")
1526
1527 (defvar gnus-decode-encoded-word-methods-cache nil)
1528
1529 (defun gnus-multi-decode-encoded-word-string (string)
1530   "Apply the functions from `gnus-encoded-word-methods' that match."
1531   (unless (and gnus-decode-encoded-word-methods-cache
1532                (eq gnus-newsgroup-name
1533                    (car gnus-decode-encoded-word-methods-cache)))
1534     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1535     (mapcar (lambda (x)
1536               (if (symbolp x)
1537                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1538                 (if (and gnus-newsgroup-name
1539                          (string-match (car x) gnus-newsgroup-name))
1540                     (nconc gnus-decode-encoded-word-methods-cache
1541                            (list (cdr x))))))
1542             gnus-decode-encoded-word-methods))
1543   (let ((xlist gnus-decode-encoded-word-methods-cache))
1544     (pop xlist)
1545     (while xlist
1546       (setq string (funcall (pop xlist) string))))
1547   string)
1548
1549 ;; Subject simplification.
1550
1551 (defun gnus-simplify-whitespace (str)
1552   "Remove excessive whitespace from STR."
1553   ;; Multiple spaces.
1554   (while (string-match "[ \t][ \t]+" str)
1555     (setq str (concat (substring str 0 (match-beginning 0))
1556                         " "
1557                         (substring str (match-end 0)))))
1558   ;; Leading spaces.
1559   (when (string-match "^[ \t]+" str)
1560     (setq str (substring str (match-end 0))))
1561   ;; Trailing spaces.
1562   (when (string-match "[ \t]+$" str)
1563     (setq str (substring str 0 (match-beginning 0))))
1564   str)
1565
1566 (defun gnus-simplify-all-whitespace (str)
1567   "Remove all whitespace from STR."
1568   (while (string-match "[ \t\n]+" str)
1569     (setq str (replace-match "" nil nil str)))
1570   str)
1571
1572 (defsubst gnus-simplify-subject-re (subject)
1573   "Remove \"Re:\" from subject lines."
1574   (if (string-match message-subject-re-regexp subject)
1575       (substring subject (match-end 0))
1576     subject))
1577
1578 (defun gnus-simplify-subject (subject &optional re-only)
1579   "Remove `Re:' and words in parentheses.
1580 If RE-ONLY is non-nil, strip leading `Re:'s only."
1581   (let ((case-fold-search t))           ;Ignore case.
1582     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1583     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1584       (setq subject (substring subject (match-end 0))))
1585     ;; Remove uninteresting prefixes.
1586     (when (and (not re-only)
1587                gnus-simplify-ignored-prefixes
1588                (string-match gnus-simplify-ignored-prefixes subject))
1589       (setq subject (substring subject (match-end 0))))
1590     ;; Remove words in parentheses from end.
1591     (unless re-only
1592       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1593         (setq subject (substring subject 0 (match-beginning 0)))))
1594     ;; Return subject string.
1595     subject))
1596
1597 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1598 ;; all whitespace.
1599 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1600   (goto-char (point-min))
1601   (while (re-search-forward regexp nil t)
1602     (replace-match (or newtext ""))))
1603
1604 (defun gnus-simplify-buffer-fuzzy ()
1605   "Simplify string in the buffer fuzzily.
1606 The string in the accessible portion of the current buffer is simplified.
1607 It is assumed to be a single-line subject.
1608 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1609 matter is removed.  Additional things can be deleted by setting
1610 `gnus-simplify-subject-fuzzy-regexp'."
1611   (let ((case-fold-search t)
1612         (modified-tick))
1613     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1614
1615     (while (not (eq modified-tick (buffer-modified-tick)))
1616       (setq modified-tick (buffer-modified-tick))
1617       (cond
1618        ((listp gnus-simplify-subject-fuzzy-regexp)
1619         (mapcar 'gnus-simplify-buffer-fuzzy-step
1620                 gnus-simplify-subject-fuzzy-regexp))
1621        (gnus-simplify-subject-fuzzy-regexp
1622         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1623       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1624       (gnus-simplify-buffer-fuzzy-step
1625        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1626       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1627
1628     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1629     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1630     (gnus-simplify-buffer-fuzzy-step " $")
1631     (gnus-simplify-buffer-fuzzy-step "^ +")))
1632
1633 (defun gnus-simplify-subject-fuzzy (subject)
1634   "Simplify a subject string fuzzily.
1635 See `gnus-simplify-buffer-fuzzy' for details."
1636   (save-excursion
1637     (gnus-set-work-buffer)
1638     (let ((case-fold-search t))
1639       ;; Remove uninteresting prefixes.
1640       (when (and gnus-simplify-ignored-prefixes
1641                  (string-match gnus-simplify-ignored-prefixes subject))
1642         (setq subject (substring subject (match-end 0))))
1643       (insert subject)
1644       (inline (gnus-simplify-buffer-fuzzy))
1645       (buffer-string))))
1646
1647 (defsubst gnus-simplify-subject-fully (subject)
1648   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1649   (cond
1650    (gnus-simplify-subject-functions
1651     (gnus-map-function gnus-simplify-subject-functions subject))
1652    ((null gnus-summary-gather-subject-limit)
1653     (gnus-simplify-subject-re subject))
1654    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1655     (gnus-simplify-subject-fuzzy subject))
1656    ((numberp gnus-summary-gather-subject-limit)
1657     (gnus-limit-string (gnus-simplify-subject-re subject)
1658                        gnus-summary-gather-subject-limit))
1659    (t
1660     subject)))
1661
1662 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1663   "Check whether two subjects are equal.
1664 If optional argument SIMPLE-FIRST is t, first argument is already
1665 simplified."
1666   (cond
1667    ((null simple-first)
1668     (equal (gnus-simplify-subject-fully s1)
1669            (gnus-simplify-subject-fully s2)))
1670    (t
1671     (equal s1
1672            (gnus-simplify-subject-fully s2)))))
1673
1674 (defun gnus-summary-bubble-group ()
1675   "Increase the score of the current group.
1676 This is a handy function to add to `gnus-summary-exit-hook' to
1677 increase the score of each group you read."
1678   (gnus-group-add-score gnus-newsgroup-name))
1679
1680 \f
1681 ;;;
1682 ;;; Gnus summary mode
1683 ;;;
1684
1685 (put 'gnus-summary-mode 'mode-class 'special)
1686
1687 (defvar gnus-article-commands-menu)
1688
1689 ;; Non-orthogonal keys
1690
1691 (gnus-define-keys gnus-summary-mode-map
1692   " " gnus-summary-next-page
1693   "\177" gnus-summary-prev-page
1694   [delete] gnus-summary-prev-page
1695   [backspace] gnus-summary-prev-page
1696   "\r" gnus-summary-scroll-up
1697   "\M-\r" gnus-summary-scroll-down
1698   "n" gnus-summary-next-unread-article
1699   "p" gnus-summary-prev-unread-article
1700   "N" gnus-summary-next-article
1701   "P" gnus-summary-prev-article
1702   "\M-\C-n" gnus-summary-next-same-subject
1703   "\M-\C-p" gnus-summary-prev-same-subject
1704   "\M-n" gnus-summary-next-unread-subject
1705   "\M-p" gnus-summary-prev-unread-subject
1706   "." gnus-summary-first-unread-article
1707   "," gnus-summary-best-unread-article
1708   "\M-s" gnus-summary-search-article-forward
1709   "\M-r" gnus-summary-search-article-backward
1710   "<" gnus-summary-beginning-of-article
1711   ">" gnus-summary-end-of-article
1712   "j" gnus-summary-goto-article
1713   "^" gnus-summary-refer-parent-article
1714   "\M-^" gnus-summary-refer-article
1715   "u" gnus-summary-tick-article-forward
1716   "!" gnus-summary-tick-article-forward
1717   "U" gnus-summary-tick-article-backward
1718   "d" gnus-summary-mark-as-read-forward
1719   "D" gnus-summary-mark-as-read-backward
1720   "E" gnus-summary-mark-as-expirable
1721   "\M-u" gnus-summary-clear-mark-forward
1722   "\M-U" gnus-summary-clear-mark-backward
1723   "k" gnus-summary-kill-same-subject-and-select
1724   "\C-k" gnus-summary-kill-same-subject
1725   "\M-\C-k" gnus-summary-kill-thread
1726   "\M-\C-l" gnus-summary-lower-thread
1727   "e" gnus-summary-edit-article
1728   "#" gnus-summary-mark-as-processable
1729   "\M-#" gnus-summary-unmark-as-processable
1730   "\M-\C-t" gnus-summary-toggle-threads
1731   "\M-\C-s" gnus-summary-show-thread
1732   "\M-\C-h" gnus-summary-hide-thread
1733   "\M-\C-f" gnus-summary-next-thread
1734   "\M-\C-b" gnus-summary-prev-thread
1735   [(meta down)] gnus-summary-next-thread
1736   [(meta up)] gnus-summary-prev-thread
1737   "\M-\C-u" gnus-summary-up-thread
1738   "\M-\C-d" gnus-summary-down-thread
1739   "&" gnus-summary-execute-command
1740   "c" gnus-summary-catchup-and-exit
1741   "\C-w" gnus-summary-mark-region-as-read
1742   "\C-t" gnus-summary-toggle-truncation
1743   "?" gnus-summary-mark-as-dormant
1744   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1745   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1746   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1747   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1748   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1749   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1750   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1751   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1752   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1753   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1754   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1755   "=" gnus-summary-expand-window
1756   "\C-x\C-s" gnus-summary-reselect-current-group
1757   "\M-g" gnus-summary-rescan-group
1758   "w" gnus-summary-stop-page-breaking
1759   "\C-c\C-r" gnus-summary-caesar-message
1760   "f" gnus-summary-followup
1761   "F" gnus-summary-followup-with-original
1762   "C" gnus-summary-cancel-article
1763   "r" gnus-summary-reply
1764   "R" gnus-summary-reply-with-original
1765   "\C-c\C-f" gnus-summary-mail-forward
1766   "o" gnus-summary-save-article
1767   "\C-o" gnus-summary-save-article-mail
1768   "|" gnus-summary-pipe-output
1769   "\M-k" gnus-summary-edit-local-kill
1770   "\M-K" gnus-summary-edit-global-kill
1771   ;; "V" gnus-version
1772   "\C-c\C-d" gnus-summary-describe-group
1773   "q" gnus-summary-exit
1774   "Q" gnus-summary-exit-no-update
1775   "\C-c\C-i" gnus-info-find-node
1776   gnus-mouse-2 gnus-mouse-pick-article
1777   [follow-link] mouse-face
1778   "m" gnus-summary-mail-other-window
1779   "a" gnus-summary-post-news
1780   "i" gnus-summary-news-other-window
1781   "x" gnus-summary-limit-to-unread
1782   "s" gnus-summary-isearch-article
1783   "t" gnus-summary-toggle-header
1784   "g" gnus-summary-show-article
1785   "l" gnus-summary-goto-last-article
1786   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1787   "\C-d" gnus-summary-enter-digest-group
1788   "\M-\C-d" gnus-summary-read-document
1789   "\M-\C-e" gnus-summary-edit-parameters
1790   "\M-\C-a" gnus-summary-customize-parameters
1791   "\C-c\C-b" gnus-bug
1792   "*" gnus-cache-enter-article
1793   "\M-*" gnus-cache-remove-article
1794   "\M-&" gnus-summary-universal-argument
1795   "\C-l" gnus-recenter
1796   "I" gnus-summary-increase-score
1797   "L" gnus-summary-lower-score
1798   "\M-i" gnus-symbolic-argument
1799   "h" gnus-summary-select-article-buffer
1800
1801   "b" gnus-article-view-part
1802   "\M-t" gnus-summary-toggle-display-buttonized
1803
1804   "V" gnus-summary-score-map
1805   "X" gnus-uu-extract-map
1806   "S" gnus-summary-send-map)
1807
1808 ;; Sort of orthogonal keymap
1809 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1810   "t" gnus-summary-tick-article-forward
1811   "!" gnus-summary-tick-article-forward
1812   "d" gnus-summary-mark-as-read-forward
1813   "r" gnus-summary-mark-as-read-forward
1814   "c" gnus-summary-clear-mark-forward
1815   " " gnus-summary-clear-mark-forward
1816   "e" gnus-summary-mark-as-expirable
1817   "x" gnus-summary-mark-as-expirable
1818   "?" gnus-summary-mark-as-dormant
1819   "b" gnus-summary-set-bookmark
1820   "B" gnus-summary-remove-bookmark
1821   "#" gnus-summary-mark-as-processable
1822   "\M-#" gnus-summary-unmark-as-processable
1823   "S" gnus-summary-limit-include-expunged
1824   "C" gnus-summary-catchup
1825   "H" gnus-summary-catchup-to-here
1826   "h" gnus-summary-catchup-from-here
1827   "\C-c" gnus-summary-catchup-all
1828   "k" gnus-summary-kill-same-subject-and-select
1829   "K" gnus-summary-kill-same-subject
1830   "P" gnus-uu-mark-map)
1831
1832 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1833   "c" gnus-summary-clear-above
1834   "u" gnus-summary-tick-above
1835   "m" gnus-summary-mark-above
1836   "k" gnus-summary-kill-below)
1837
1838 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1839   "/" gnus-summary-limit-to-subject
1840   "n" gnus-summary-limit-to-articles
1841   "w" gnus-summary-pop-limit
1842   "s" gnus-summary-limit-to-subject
1843   "a" gnus-summary-limit-to-author
1844   "u" gnus-summary-limit-to-unread
1845   "m" gnus-summary-limit-to-marks
1846   "M" gnus-summary-limit-exclude-marks
1847   "v" gnus-summary-limit-to-score
1848   "*" gnus-summary-limit-include-cached
1849   "D" gnus-summary-limit-include-dormant
1850   "T" gnus-summary-limit-include-thread
1851   "d" gnus-summary-limit-exclude-dormant
1852   "t" gnus-summary-limit-to-age
1853   "." gnus-summary-limit-to-unseen
1854   "x" gnus-summary-limit-to-extra
1855   "p" gnus-summary-limit-to-display-predicate
1856   "E" gnus-summary-limit-include-expunged
1857   "c" gnus-summary-limit-exclude-childless-dormant
1858   "C" gnus-summary-limit-mark-excluded-as-read
1859   "o" gnus-summary-insert-old-articles
1860   "N" gnus-summary-insert-new-articles
1861   "r" gnus-summary-limit-to-replied
1862   "R" gnus-summary-limit-to-recipient)
1863
1864 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1865   "n" gnus-summary-next-unread-article
1866   "p" gnus-summary-prev-unread-article
1867   "N" gnus-summary-next-article
1868   "P" gnus-summary-prev-article
1869   "\C-n" gnus-summary-next-same-subject
1870   "\C-p" gnus-summary-prev-same-subject
1871   "\M-n" gnus-summary-next-unread-subject
1872   "\M-p" gnus-summary-prev-unread-subject
1873   "f" gnus-summary-first-unread-article
1874   "b" gnus-summary-best-unread-article
1875   "j" gnus-summary-goto-article
1876   "g" gnus-summary-goto-subject
1877   "l" gnus-summary-goto-last-article
1878   "o" gnus-summary-pop-article)
1879
1880 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1881   "k" gnus-summary-kill-thread
1882   "l" gnus-summary-lower-thread
1883   "i" gnus-summary-raise-thread
1884   "T" gnus-summary-toggle-threads
1885   "t" gnus-summary-rethread-current
1886   "^" gnus-summary-reparent-thread
1887   "s" gnus-summary-show-thread
1888   "S" gnus-summary-show-all-threads
1889   "h" gnus-summary-hide-thread
1890   "H" gnus-summary-hide-all-threads
1891   "n" gnus-summary-next-thread
1892   "p" gnus-summary-prev-thread
1893   "u" gnus-summary-up-thread
1894   "o" gnus-summary-top-thread
1895   "d" gnus-summary-down-thread
1896   "#" gnus-uu-mark-thread
1897   "\M-#" gnus-uu-unmark-thread)
1898
1899 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1900   "g" gnus-summary-prepare
1901   "c" gnus-summary-insert-cached-articles
1902   "d" gnus-summary-insert-dormant-articles)
1903
1904 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1905   "c" gnus-summary-catchup-and-exit
1906   "C" gnus-summary-catchup-all-and-exit
1907   "E" gnus-summary-exit-no-update
1908   "Q" gnus-summary-exit
1909   "Z" gnus-summary-exit
1910   "n" gnus-summary-catchup-and-goto-next-group
1911   "p" gnus-summary-catchup-and-goto-prev-group
1912   "R" gnus-summary-reselect-current-group
1913   "G" gnus-summary-rescan-group
1914   "N" gnus-summary-next-group
1915   "s" gnus-summary-save-newsrc
1916   "P" gnus-summary-prev-group)
1917
1918 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1919   " " gnus-summary-next-page
1920   "n" gnus-summary-next-page
1921   "\177" gnus-summary-prev-page
1922   [delete] gnus-summary-prev-page
1923   "p" gnus-summary-prev-page
1924   "\r" gnus-summary-scroll-up
1925   "\M-\r" gnus-summary-scroll-down
1926   "<" gnus-summary-beginning-of-article
1927   ">" gnus-summary-end-of-article
1928   "b" gnus-summary-beginning-of-article
1929   "e" gnus-summary-end-of-article
1930   "^" gnus-summary-refer-parent-article
1931   "r" gnus-summary-refer-parent-article
1932   "D" gnus-summary-enter-digest-group
1933   "R" gnus-summary-refer-references
1934   "T" gnus-summary-refer-thread
1935   "g" gnus-summary-show-article
1936   "s" gnus-summary-isearch-article
1937   "P" gnus-summary-print-article
1938   "M" gnus-mailing-list-insinuate
1939   "t" gnus-article-babel)
1940
1941 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1942   "b" gnus-article-add-buttons
1943   "B" gnus-article-add-buttons-to-head
1944   "o" gnus-article-treat-overstrike
1945   "e" gnus-article-emphasize
1946   "w" gnus-article-fill-cited-article
1947   "Q" gnus-article-fill-long-lines
1948   "C" gnus-article-capitalize-sentences
1949   "c" gnus-article-remove-cr
1950   "q" gnus-article-de-quoted-unreadable
1951   "6" gnus-article-de-base64-unreadable
1952   "Z" gnus-article-decode-HZ
1953   "A" gnus-article-treat-ansi-sequences
1954   "h" gnus-article-wash-html
1955   "u" gnus-article-unsplit-urls
1956   "s" gnus-summary-force-verify-and-decrypt
1957   "f" gnus-article-display-x-face
1958   "l" gnus-summary-stop-page-breaking
1959   "r" gnus-summary-caesar-message
1960   "m" gnus-summary-morse-message
1961   "t" gnus-summary-toggle-header
1962   "g" gnus-treat-smiley
1963   "v" gnus-summary-verbose-headers
1964   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1965   "p" gnus-article-verify-x-pgp-sig
1966   "d" gnus-article-treat-dumbquotes
1967   "i" gnus-summary-idna-message)
1968
1969 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1970   ;; mnemonic: deuglif*Y*
1971   "u" gnus-article-outlook-unwrap-lines
1972   "a" gnus-article-outlook-repair-attribution
1973   "c" gnus-article-outlook-rearrange-citation
1974   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1975
1976 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1977   "a" gnus-article-hide
1978   "h" gnus-article-hide-headers
1979   "b" gnus-article-hide-boring-headers
1980   "s" gnus-article-hide-signature
1981   "c" gnus-article-hide-citation
1982   "C" gnus-article-hide-citation-in-followups
1983   "l" gnus-article-hide-list-identifiers
1984   "B" gnus-article-strip-banner
1985   "P" gnus-article-hide-pem
1986   "\C-c" gnus-article-hide-citation-maybe)
1987
1988 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1989   "a" gnus-article-highlight
1990   "h" gnus-article-highlight-headers
1991   "c" gnus-article-highlight-citation
1992   "s" gnus-article-highlight-signature)
1993
1994 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1995   "f" gnus-article-treat-fold-headers
1996   "u" gnus-article-treat-unfold-headers
1997   "n" gnus-article-treat-fold-newsgroups)
1998
1999 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2000   "x" gnus-article-display-x-face
2001   "d" gnus-article-display-face
2002   "s" gnus-treat-smiley
2003   "D" gnus-article-remove-images
2004   "f" gnus-treat-from-picon
2005   "m" gnus-treat-mail-picon
2006   "n" gnus-treat-newsgroups-picon)
2007
2008 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2009   "w" gnus-article-decode-mime-words
2010   "c" gnus-article-decode-charset
2011   "v" gnus-mime-view-all-parts
2012   "b" gnus-article-view-part)
2013
2014 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2015   "z" gnus-article-date-ut
2016   "u" gnus-article-date-ut
2017   "l" gnus-article-date-local
2018   "p" gnus-article-date-english
2019   "e" gnus-article-date-lapsed
2020   "o" gnus-article-date-original
2021   "i" gnus-article-date-iso8601
2022   "s" gnus-article-date-user)
2023
2024 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2025   "t" gnus-article-remove-trailing-blank-lines
2026   "l" gnus-article-strip-leading-blank-lines
2027   "m" gnus-article-strip-multiple-blank-lines
2028   "a" gnus-article-strip-blank-lines
2029   "A" gnus-article-strip-all-blank-lines
2030   "s" gnus-article-strip-leading-space
2031   "e" gnus-article-strip-trailing-space
2032   "w" gnus-article-remove-leading-whitespace)
2033
2034 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2035   "v" gnus-version
2036   "f" gnus-summary-fetch-faq
2037   "d" gnus-summary-describe-group
2038   "h" gnus-summary-describe-briefly
2039   "i" gnus-info-find-node
2040   "c" gnus-group-fetch-charter
2041   "C" gnus-group-fetch-control)
2042
2043 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2044   "e" gnus-summary-expire-articles
2045   "\M-\C-e" gnus-summary-expire-articles-now
2046   "\177" gnus-summary-delete-article
2047   [delete] gnus-summary-delete-article
2048   [backspace] gnus-summary-delete-article
2049   "m" gnus-summary-move-article
2050   "r" gnus-summary-respool-article
2051   "w" gnus-summary-edit-article
2052   "c" gnus-summary-copy-article
2053   "B" gnus-summary-crosspost-article
2054   "q" gnus-summary-respool-query
2055   "t" gnus-summary-respool-trace
2056   "i" gnus-summary-import-article
2057   "I" gnus-summary-create-article
2058   "p" gnus-summary-article-posted-p)
2059
2060 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2061   "o" gnus-summary-save-article
2062   "m" gnus-summary-save-article-mail
2063   "F" gnus-summary-write-article-file
2064   "r" gnus-summary-save-article-rmail
2065   "f" gnus-summary-save-article-file
2066   "b" gnus-summary-save-article-body-file
2067   "h" gnus-summary-save-article-folder
2068   "v" gnus-summary-save-article-vm
2069   "p" gnus-summary-pipe-output
2070   "P" gnus-summary-muttprint
2071   "s" gnus-soup-add-article)
2072
2073 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2074   "b" gnus-summary-display-buttonized
2075   "m" gnus-summary-repair-multipart
2076   "v" gnus-article-view-part
2077   "o" gnus-article-save-part
2078   "O" gnus-article-save-part-and-strip
2079   "r" gnus-article-replace-part
2080   "d" gnus-article-delete-part
2081   "j" gnus-article-jump-to-part
2082   "c" gnus-article-copy-part
2083   "C" gnus-article-view-part-as-charset
2084   "e" gnus-article-view-part-externally
2085   "E" gnus-article-encrypt-body
2086   "i" gnus-article-inline-part
2087   "|" gnus-article-pipe-part)
2088
2089 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2090   "p" gnus-summary-mark-as-processable
2091   "u" gnus-summary-unmark-as-processable
2092   "U" gnus-summary-unmark-all-processable
2093   "v" gnus-uu-mark-over
2094   "s" gnus-uu-mark-series
2095   "r" gnus-uu-mark-region
2096   "g" gnus-uu-unmark-region
2097   "R" gnus-uu-mark-by-regexp
2098   "G" gnus-uu-unmark-by-regexp
2099   "t" gnus-uu-mark-thread
2100   "T" gnus-uu-unmark-thread
2101   "a" gnus-uu-mark-all
2102   "b" gnus-uu-mark-buffer
2103   "S" gnus-uu-mark-sparse
2104   "k" gnus-summary-kill-process-mark
2105   "y" gnus-summary-yank-process-mark
2106   "w" gnus-summary-save-process-mark
2107   "i" gnus-uu-invert-processable)
2108
2109 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2110   ;;"x" gnus-uu-extract-any
2111   "m" gnus-summary-save-parts
2112   "u" gnus-uu-decode-uu
2113   "U" gnus-uu-decode-uu-and-save
2114   "s" gnus-uu-decode-unshar
2115   "S" gnus-uu-decode-unshar-and-save
2116   "o" gnus-uu-decode-save
2117   "O" gnus-uu-decode-save
2118   "b" gnus-uu-decode-binhex
2119   "B" gnus-uu-decode-binhex
2120   "p" gnus-uu-decode-postscript
2121   "P" gnus-uu-decode-postscript-and-save)
2122
2123 (gnus-define-keys
2124     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2125   "u" gnus-uu-decode-uu-view
2126   "U" gnus-uu-decode-uu-and-save-view
2127   "s" gnus-uu-decode-unshar-view
2128   "S" gnus-uu-decode-unshar-and-save-view
2129   "o" gnus-uu-decode-save-view
2130   "O" gnus-uu-decode-save-view
2131   "b" gnus-uu-decode-binhex-view
2132   "B" gnus-uu-decode-binhex-view
2133   "p" gnus-uu-decode-postscript-view
2134   "P" gnus-uu-decode-postscript-and-save-view)
2135
2136 (defvar gnus-article-post-menu nil)
2137
2138 (defconst gnus-summary-menu-maxlen 20)
2139
2140 (defun gnus-summary-menu-split (menu)
2141   ;; If we have lots of elements, divide them into groups of 20
2142   ;; and make a pane (or submenu) for each one.
2143   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2144       (let ((menu menu) sublists next
2145             (i 1))
2146         (while menu
2147           ;; Pull off the next gnus-summary-menu-maxlen elements
2148           ;; and make them the next element of sublist.
2149           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2150           (if next
2151               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2152                       nil))
2153           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2154                                              (aref (car (last menu)) 0)) menu)
2155                                sublists))
2156           (setq i (1+ i))
2157           (setq menu next))
2158         (nreverse sublists))
2159     ;; Few elements--put them all in one pane.
2160     menu))
2161
2162 (defun gnus-summary-make-menu-bar ()
2163   (gnus-turn-off-edit-menu 'summary)
2164
2165   (unless (boundp 'gnus-summary-misc-menu)
2166
2167     (easy-menu-define
2168       gnus-summary-kill-menu gnus-summary-mode-map ""
2169       (cons
2170        "Score"
2171        (nconc
2172         (list
2173          ["Customize" gnus-score-customize t])
2174         (gnus-make-score-map 'increase)
2175         (gnus-make-score-map 'lower)
2176         '(("Mark"
2177            ["Kill below" gnus-summary-kill-below t]
2178            ["Mark above" gnus-summary-mark-above t]
2179            ["Tick above" gnus-summary-tick-above t]
2180            ["Clear above" gnus-summary-clear-above t])
2181           ["Current score" gnus-summary-current-score t]
2182           ["Set score" gnus-summary-set-score t]
2183           ["Switch current score file..." gnus-score-change-score-file t]
2184           ["Set mark below..." gnus-score-set-mark-below t]
2185           ["Set expunge below..." gnus-score-set-expunge-below t]
2186           ["Edit current score file" gnus-score-edit-current-scores t]
2187           ["Edit score file" gnus-score-edit-file t]
2188           ["Trace score" gnus-score-find-trace t]
2189           ["Find words" gnus-score-find-favourite-words t]
2190           ["Rescore buffer" gnus-summary-rescore t]
2191           ["Increase score..." gnus-summary-increase-score t]
2192           ["Lower score..." gnus-summary-lower-score t]))))
2193
2194     ;; Define both the Article menu in the summary buffer and the
2195     ;; equivalent Commands menu in the article buffer here for
2196     ;; consistency.
2197     (let ((innards
2198            `(("Hide"
2199               ["All" gnus-article-hide t]
2200               ["Headers" gnus-article-hide-headers t]
2201               ["Signature" gnus-article-hide-signature t]
2202               ["Citation" gnus-article-hide-citation t]
2203               ["List identifiers" gnus-article-hide-list-identifiers t]
2204               ["Banner" gnus-article-strip-banner t]
2205               ["Boring headers" gnus-article-hide-boring-headers t])
2206              ("Highlight"
2207               ["All" gnus-article-highlight t]
2208               ["Headers" gnus-article-highlight-headers t]
2209               ["Signature" gnus-article-highlight-signature t]
2210               ["Citation" gnus-article-highlight-citation t])
2211              ("MIME"
2212               ["Words" gnus-article-decode-mime-words t]
2213               ["Charset" gnus-article-decode-charset t]
2214               ["QP" gnus-article-de-quoted-unreadable t]
2215               ["Base64" gnus-article-de-base64-unreadable t]
2216               ["View MIME buttons" gnus-summary-display-buttonized t]
2217               ["View all" gnus-mime-view-all-parts t]
2218               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2219               ["Encrypt body" gnus-article-encrypt-body
2220                :active (not (gnus-group-read-only-p))
2221                ,@(if (featurep 'xemacs) nil
2222                    '(:help "Encrypt the message body on disk"))]
2223               ["Extract all parts..." gnus-summary-save-parts t]
2224               ("Multipart"
2225                ["Repair multipart" gnus-summary-repair-multipart t]
2226                ["Pipe part..." gnus-article-pipe-part t]
2227                ["Inline part" gnus-article-inline-part t]
2228                ["Encrypt body" gnus-article-encrypt-body
2229                 :active (not (gnus-group-read-only-p))
2230                ,@(if (featurep 'xemacs) nil
2231                    '(:help "Encrypt the message body on disk"))]
2232                ["View part externally" gnus-article-view-part-externally t]
2233                ["View part with charset..." gnus-article-view-part-as-charset t]
2234                ["Copy part" gnus-article-copy-part t]
2235                ["Save part..." gnus-article-save-part t]
2236                ["View part" gnus-article-view-part t]))
2237              ("Date"
2238               ["Local" gnus-article-date-local t]
2239               ["ISO8601" gnus-article-date-iso8601 t]
2240               ["UT" gnus-article-date-ut t]
2241               ["Original" gnus-article-date-original t]
2242               ["Lapsed" gnus-article-date-lapsed t]
2243               ["User-defined" gnus-article-date-user t])
2244              ("Display"
2245               ["Remove images" gnus-article-remove-images t]
2246               ["Toggle smiley" gnus-treat-smiley t]
2247               ["Show X-Face" gnus-article-display-x-face t]
2248               ["Show picons in From" gnus-treat-from-picon t]
2249               ["Show picons in mail headers" gnus-treat-mail-picon t]
2250               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2251               ("View as different encoding"
2252                ,@(gnus-summary-menu-split
2253                   (mapcar
2254                    (lambda (cs)
2255                      ;; Since easymenu under Emacs doesn't allow
2256                      ;; lambda forms for menu commands, we should
2257                      ;; provide intern'ed function symbols.
2258                      (let ((command (intern (format "\
2259 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2260                        (fset command
2261                              `(lambda ()
2262                                 (interactive)
2263                                 (let ((gnus-summary-show-article-charset-alist
2264                                        '((1 . ,cs))))
2265                                   (gnus-summary-show-article 1))))
2266                        `[,(symbol-name cs) ,command t]))
2267                    (sort (if (fboundp 'coding-system-list)
2268                              (coding-system-list)
2269                            (mapcar 'car mm-mime-mule-charset-alist))
2270                          'string<)))))
2271              ("Washing"
2272               ("Remove Blanks"
2273                ["Leading" gnus-article-strip-leading-blank-lines t]
2274                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2275                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2276                ["All of the above" gnus-article-strip-blank-lines t]
2277                ["All" gnus-article-strip-all-blank-lines t]
2278                ["Leading space" gnus-article-strip-leading-space t]
2279                ["Trailing space" gnus-article-strip-trailing-space t]
2280                ["Leading space in headers"
2281                 gnus-article-remove-leading-whitespace t])
2282               ["Overstrike" gnus-article-treat-overstrike t]
2283               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2284               ["Emphasis" gnus-article-emphasize t]
2285               ["Word wrap" gnus-article-fill-cited-article t]
2286               ["Fill long lines" gnus-article-fill-long-lines t]
2287               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2288               ["Remove CR" gnus-article-remove-cr t]
2289               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2290               ["Base64" gnus-article-de-base64-unreadable t]
2291               ["Rot 13" gnus-summary-caesar-message
2292                ,@(if (featurep 'xemacs) '(t)
2293                    '(:help "\"Caesar rotate\" article by 13"))]
2294               ["De-IDNA" gnus-summary-idna-message t]
2295               ["Morse decode" gnus-summary-morse-message t]
2296               ["Unix pipe..." gnus-summary-pipe-message t]
2297               ["Add buttons" gnus-article-add-buttons t]
2298               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2299               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2300               ["Verbose header" gnus-summary-verbose-headers t]
2301               ["Toggle header" gnus-summary-toggle-header t]
2302               ["Unfold headers" gnus-article-treat-unfold-headers t]
2303               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2304               ["Html" gnus-article-wash-html t]
2305               ["Unsplit URLs" gnus-article-unsplit-urls t]
2306               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2307               ["Decode HZ" gnus-article-decode-HZ t]
2308               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2309               ("(Outlook) Deuglify"
2310                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2311                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2312                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2313                ["Full (Outlook) deuglify"
2314                 gnus-article-outlook-deuglify-article t])
2315               )
2316              ("Output"
2317               ["Save in default format..." gnus-summary-save-article
2318                ,@(if (featurep 'xemacs) '(t)
2319                    '(:help "Save article using default method"))]
2320               ["Save in file..." gnus-summary-save-article-file
2321                ,@(if (featurep 'xemacs) '(t)
2322                    '(:help "Save article in file"))]
2323               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2324               ["Save in MH folder..." gnus-summary-save-article-folder t]
2325               ["Save in VM folder..." gnus-summary-save-article-vm t]
2326               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2327               ["Save body in file..." gnus-summary-save-article-body-file t]
2328               ["Pipe through a filter..." gnus-summary-pipe-output t]
2329               ["Add to SOUP packet" gnus-soup-add-article t]
2330               ["Print with Muttprint..." gnus-summary-muttprint t]
2331               ["Print" gnus-summary-print-article
2332                ,@(if (featurep 'xemacs) '(t)
2333                    '(:help "Generate and print a PostScript image"))])
2334              ("Copy, move,... (Backend)"
2335               ,@(if (featurep 'xemacs) nil
2336                   '(:help "Copying, moving, expiring articles..."))
2337               ["Respool article..." gnus-summary-respool-article t]
2338               ["Move article..." gnus-summary-move-article
2339                (gnus-check-backend-function
2340                 'request-move-article gnus-newsgroup-name)]
2341               ["Copy article..." gnus-summary-copy-article t]
2342               ["Crosspost article..." gnus-summary-crosspost-article
2343                (gnus-check-backend-function
2344                 'request-replace-article gnus-newsgroup-name)]
2345               ["Import file..." gnus-summary-import-article
2346                (gnus-check-backend-function
2347                 'request-accept-article gnus-newsgroup-name)]
2348               ["Create article..." gnus-summary-create-article
2349                (gnus-check-backend-function
2350                 'request-accept-article gnus-newsgroup-name)]
2351               ["Check if posted" gnus-summary-article-posted-p t]
2352               ["Edit article" gnus-summary-edit-article
2353                (not (gnus-group-read-only-p))]
2354               ["Delete article" gnus-summary-delete-article
2355                (gnus-check-backend-function
2356                 'request-expire-articles gnus-newsgroup-name)]
2357               ["Query respool" gnus-summary-respool-query t]
2358               ["Trace respool" gnus-summary-respool-trace t]
2359               ["Delete expirable articles" gnus-summary-expire-articles-now
2360                (gnus-check-backend-function
2361                 'request-expire-articles gnus-newsgroup-name)])
2362              ("Extract"
2363               ["Uudecode" gnus-uu-decode-uu
2364                ,@(if (featurep 'xemacs) '(t)
2365                    '(:help "Decode uuencoded article(s)"))]
2366               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2367               ["Unshar" gnus-uu-decode-unshar t]
2368               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2369               ["Save" gnus-uu-decode-save t]
2370               ["Binhex" gnus-uu-decode-binhex t]
2371               ["Postscript" gnus-uu-decode-postscript t]
2372               ["All MIME parts" gnus-summary-save-parts t])
2373              ("Cache"
2374               ["Enter article" gnus-cache-enter-article t]
2375               ["Remove article" gnus-cache-remove-article t])
2376              ["Translate" gnus-article-babel t]
2377              ["Select article buffer" gnus-summary-select-article-buffer t]
2378              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2379              ["Isearch article..." gnus-summary-isearch-article t]
2380              ["Beginning of the article" gnus-summary-beginning-of-article t]
2381              ["End of the article" gnus-summary-end-of-article t]
2382              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2383              ["Fetch referenced articles" gnus-summary-refer-references t]
2384              ["Fetch current thread" gnus-summary-refer-thread t]
2385              ["Fetch article with id..." gnus-summary-refer-article t]
2386              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2387              ["Redisplay" gnus-summary-show-article t]
2388              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2389       (easy-menu-define
2390         gnus-summary-article-menu gnus-summary-mode-map ""
2391         (cons "Article" innards))
2392
2393       (if (not (keymapp gnus-summary-article-menu))
2394           (easy-menu-define
2395             gnus-article-commands-menu gnus-article-mode-map ""
2396             (cons "Commands" innards))
2397         ;; in Emacs, don't share menu.
2398         (setq gnus-article-commands-menu
2399               (copy-keymap gnus-summary-article-menu))
2400         (define-key gnus-article-mode-map [menu-bar commands]
2401           (cons "Commands" gnus-article-commands-menu))))
2402
2403     (easy-menu-define
2404       gnus-summary-thread-menu gnus-summary-mode-map ""
2405       '("Threads"
2406         ["Find all messages in thread" gnus-summary-refer-thread t]
2407         ["Toggle threading" gnus-summary-toggle-threads t]
2408         ["Hide threads" gnus-summary-hide-all-threads t]
2409         ["Show threads" gnus-summary-show-all-threads t]
2410         ["Hide thread" gnus-summary-hide-thread t]
2411         ["Show thread" gnus-summary-show-thread t]
2412         ["Go to next thread" gnus-summary-next-thread t]
2413         ["Go to previous thread" gnus-summary-prev-thread t]
2414         ["Go down thread" gnus-summary-down-thread t]
2415         ["Go up thread" gnus-summary-up-thread t]
2416         ["Top of thread" gnus-summary-top-thread t]
2417         ["Mark thread as read" gnus-summary-kill-thread t]
2418         ["Lower thread score" gnus-summary-lower-thread t]
2419         ["Raise thread score" gnus-summary-raise-thread t]
2420         ["Rethread current" gnus-summary-rethread-current t]))
2421
2422     (easy-menu-define
2423       gnus-summary-post-menu gnus-summary-mode-map ""
2424       `("Post"
2425         ["Send a message (mail or news)" gnus-summary-post-news
2426          ,@(if (featurep 'xemacs) '(t)
2427              '(:help "Compose a new message (mail or news)"))]
2428         ["Followup" gnus-summary-followup
2429          ,@(if (featurep 'xemacs) '(t)
2430              '(:help "Post followup to this article"))]
2431         ["Followup and yank" gnus-summary-followup-with-original
2432          ,@(if (featurep 'xemacs) '(t)
2433              '(:help "Post followup to this article, quoting its contents"))]
2434         ["Supersede article" gnus-summary-supersede-article t]
2435         ["Cancel article" gnus-summary-cancel-article
2436          ,@(if (featurep 'xemacs) '(t)
2437              '(:help "Cancel an article you posted"))]
2438         ["Reply" gnus-summary-reply t]
2439         ["Reply and yank" gnus-summary-reply-with-original t]
2440         ["Wide reply" gnus-summary-wide-reply t]
2441         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2442          ,@(if (featurep 'xemacs) '(t)
2443              '(:help "Mail a reply, quoting this article"))]
2444         ["Very wide reply" gnus-summary-very-wide-reply t]
2445         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2446          ,@(if (featurep 'xemacs) '(t)
2447              '(:help "Mail a very wide reply, quoting this article"))]
2448         ["Mail forward" gnus-summary-mail-forward t]
2449         ["Post forward" gnus-summary-post-forward t]
2450         ["Digest and mail" gnus-uu-digest-mail-forward t]
2451         ["Digest and post" gnus-uu-digest-post-forward t]
2452         ["Resend message" gnus-summary-resend-message t]
2453         ["Resend message edit" gnus-summary-resend-message-edit t]
2454         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2455         ["Send a mail" gnus-summary-mail-other-window t]
2456         ["Create a local message" gnus-summary-news-other-window t]
2457         ["Uuencode and post" gnus-uu-post-news
2458          ,@(if (featurep 'xemacs) '(t)
2459              '(:help "Post a uuencoded article"))]
2460         ["Followup via news" gnus-summary-followup-to-mail t]
2461         ["Followup via news and yank"
2462          gnus-summary-followup-to-mail-with-original t]
2463         ;;("Draft"
2464         ;;["Send" gnus-summary-send-draft t]
2465         ;;["Send bounced" gnus-resend-bounced-mail t])
2466         ))
2467
2468     (cond
2469      ((not (keymapp gnus-summary-post-menu))
2470       (setq gnus-article-post-menu gnus-summary-post-menu))
2471      ((not gnus-article-post-menu)
2472       ;; Don't share post menu.
2473       (setq gnus-article-post-menu
2474             (copy-keymap gnus-summary-post-menu))))
2475     (define-key gnus-article-mode-map [menu-bar post]
2476       (cons "Post" gnus-article-post-menu))
2477
2478     (easy-menu-define
2479       gnus-summary-misc-menu gnus-summary-mode-map ""
2480       `("Gnus"
2481         ("Mark Read"
2482          ["Mark as read" gnus-summary-mark-as-read-forward t]
2483          ["Mark same subject and select"
2484           gnus-summary-kill-same-subject-and-select t]
2485          ["Mark same subject" gnus-summary-kill-same-subject t]
2486          ["Catchup" gnus-summary-catchup
2487           ,@(if (featurep 'xemacs) '(t)
2488               '(:help "Mark unread articles in this group as read"))]
2489          ["Catchup all" gnus-summary-catchup-all t]
2490          ["Catchup to here" gnus-summary-catchup-to-here t]
2491          ["Catchup from here" gnus-summary-catchup-from-here t]
2492          ["Catchup region" gnus-summary-mark-region-as-read
2493           (gnus-mark-active-p)]
2494          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2495         ("Mark Various"
2496          ["Tick" gnus-summary-tick-article-forward t]
2497          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2498          ["Remove marks" gnus-summary-clear-mark-forward t]
2499          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2500          ["Set bookmark" gnus-summary-set-bookmark t]
2501          ["Remove bookmark" gnus-summary-remove-bookmark t])
2502         ("Limit to"
2503          ["Marks..." gnus-summary-limit-to-marks t]
2504          ["Subject..." gnus-summary-limit-to-subject t]
2505          ["Author..." gnus-summary-limit-to-author t]
2506          ["Recipient..." gnus-summary-limit-to-recipient t]
2507          ["Age..." gnus-summary-limit-to-age t]
2508          ["Extra..." gnus-summary-limit-to-extra t]
2509          ["Score..." gnus-summary-limit-to-score t]
2510          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2511          ["Unread" gnus-summary-limit-to-unread t]
2512          ["Unseen" gnus-summary-limit-to-unseen t]
2513          ["Replied" gnus-summary-limit-to-replied t]
2514          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2515          ["Next articles" gnus-summary-limit-to-articles t]
2516          ["Pop limit" gnus-summary-pop-limit t]
2517          ["Show dormant" gnus-summary-limit-include-dormant t]
2518          ["Hide childless dormant"
2519           gnus-summary-limit-exclude-childless-dormant t]
2520          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2521          ["Hide marked" gnus-summary-limit-exclude-marks t]
2522          ["Show expunged" gnus-summary-limit-include-expunged t])
2523         ("Process Mark"
2524          ["Set mark" gnus-summary-mark-as-processable t]
2525          ["Remove mark" gnus-summary-unmark-as-processable t]
2526          ["Remove all marks" gnus-summary-unmark-all-processable t]
2527          ["Invert marks" gnus-uu-invert-processable t]
2528          ["Mark above" gnus-uu-mark-over t]
2529          ["Mark series" gnus-uu-mark-series t]
2530          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2531          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2532          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2533          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2534          ["Mark all" gnus-uu-mark-all t]
2535          ["Mark buffer" gnus-uu-mark-buffer t]
2536          ["Mark sparse" gnus-uu-mark-sparse t]
2537          ["Mark thread" gnus-uu-mark-thread t]
2538          ["Unmark thread" gnus-uu-unmark-thread t]
2539          ("Process Mark Sets"
2540           ["Kill" gnus-summary-kill-process-mark t]
2541           ["Yank" gnus-summary-yank-process-mark
2542            gnus-newsgroup-process-stack]
2543           ["Save" gnus-summary-save-process-mark t]
2544           ["Run command on marked..." gnus-summary-universal-argument t]))
2545         ("Scroll article"
2546          ["Page forward" gnus-summary-next-page
2547           ,@(if (featurep 'xemacs) '(t)
2548               '(:help "Show next page of article"))]
2549          ["Page backward" gnus-summary-prev-page
2550           ,@(if (featurep 'xemacs) '(t)
2551               '(:help "Show previous page of article"))]
2552          ["Line forward" gnus-summary-scroll-up t])
2553         ("Move"
2554          ["Next unread article" gnus-summary-next-unread-article t]
2555          ["Previous unread article" gnus-summary-prev-unread-article t]
2556          ["Next article" gnus-summary-next-article t]
2557          ["Previous article" gnus-summary-prev-article t]
2558          ["Next unread subject" gnus-summary-next-unread-subject t]
2559          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2560          ["Next article same subject" gnus-summary-next-same-subject t]
2561          ["Previous article same subject" gnus-summary-prev-same-subject t]
2562          ["First unread article" gnus-summary-first-unread-article t]
2563          ["Best unread article" gnus-summary-best-unread-article t]
2564          ["Go to subject number..." gnus-summary-goto-subject t]
2565          ["Go to article number..." gnus-summary-goto-article t]
2566          ["Go to the last article" gnus-summary-goto-last-article t]
2567          ["Pop article off history" gnus-summary-pop-article t])
2568         ("Sort"
2569          ["Sort by number" gnus-summary-sort-by-number t]
2570          ["Sort by author" gnus-summary-sort-by-author t]
2571          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2572          ["Sort by subject" gnus-summary-sort-by-subject t]
2573          ["Sort by date" gnus-summary-sort-by-date t]
2574          ["Sort by score" gnus-summary-sort-by-score t]
2575          ["Sort by lines" gnus-summary-sort-by-lines t]
2576          ["Sort by characters" gnus-summary-sort-by-chars t]
2577          ["Randomize" gnus-summary-sort-by-random t]
2578          ["Original sort" gnus-summary-sort-by-original t])
2579         ("Help"
2580          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2581          ["Describe group" gnus-summary-describe-group t]
2582          ["Fetch charter" gnus-group-fetch-charter
2583           ,@(if (featurep 'xemacs) nil
2584               '(:help "Display the charter of the current group"))]
2585          ["Fetch control message" gnus-group-fetch-control
2586           ,@(if (featurep 'xemacs) nil
2587               '(:help "Display the archived control message for the current group"))]
2588          ["Read manual" gnus-info-find-node t])
2589         ("Modes"
2590          ["Pick and read" gnus-pick-mode t]
2591          ["Binary" gnus-binary-mode t])
2592         ("Regeneration"
2593          ["Regenerate" gnus-summary-prepare t]
2594          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2595          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2596          ["Toggle threading" gnus-summary-toggle-threads t])
2597         ["See old articles" gnus-summary-insert-old-articles t]
2598         ["See new articles" gnus-summary-insert-new-articles t]
2599         ["Filter articles..." gnus-summary-execute-command t]
2600         ["Run command on articles..." gnus-summary-universal-argument t]
2601         ["Search articles forward..." gnus-summary-search-article-forward t]
2602         ["Search articles backward..." gnus-summary-search-article-backward t]
2603         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2604         ["Expand window" gnus-summary-expand-window t]
2605         ["Expire expirable articles" gnus-summary-expire-articles
2606          (gnus-check-backend-function
2607           'request-expire-articles gnus-newsgroup-name)]
2608         ["Edit local kill file" gnus-summary-edit-local-kill t]
2609         ["Edit main kill file" gnus-summary-edit-global-kill t]
2610         ["Edit group parameters" gnus-summary-edit-parameters t]
2611         ["Customize group parameters" gnus-summary-customize-parameters t]
2612         ["Send a bug report" gnus-bug t]
2613         ("Exit"
2614          ["Catchup and exit" gnus-summary-catchup-and-exit
2615           ,@(if (featurep 'xemacs) '(t)
2616               '(:help "Mark unread articles in this group as read, then exit"))]
2617          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2618          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2619          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2620          ["Exit group" gnus-summary-exit
2621           ,@(if (featurep 'xemacs) '(t)
2622               '(:help "Exit current group, return to group selection mode"))]
2623          ["Exit group without updating" gnus-summary-exit-no-update t]
2624          ["Exit and goto next group" gnus-summary-next-group t]
2625          ["Exit and goto prev group" gnus-summary-prev-group t]
2626          ["Reselect group" gnus-summary-reselect-current-group t]
2627          ["Rescan group" gnus-summary-rescan-group t]
2628          ["Update dribble" gnus-summary-save-newsrc t])))
2629
2630     (gnus-run-hooks 'gnus-summary-menu-hook)))
2631
2632 (defvar gnus-summary-tool-bar-map nil)
2633
2634 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2635 (defun gnus-summary-make-tool-bar ()
2636   (if (and (fboundp 'tool-bar-add-item-from-menu)
2637            (default-value 'tool-bar-mode)
2638            (not gnus-summary-tool-bar-map))
2639       (setq gnus-summary-tool-bar-map
2640             (let ((tool-bar-map (make-sparse-keymap))
2641                   (load-path (mm-image-load-path)))
2642               (tool-bar-add-item-from-menu
2643                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2644               (tool-bar-add-item-from-menu
2645                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2646               (tool-bar-add-item-from-menu
2647                'gnus-summary-post-news "post" gnus-summary-mode-map)
2648               (tool-bar-add-item-from-menu
2649                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2650               (tool-bar-add-item-from-menu
2651                'gnus-summary-followup "followup" gnus-summary-mode-map)
2652               (tool-bar-add-item-from-menu
2653                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2654               (tool-bar-add-item-from-menu
2655                'gnus-summary-reply "reply" gnus-summary-mode-map)
2656               (tool-bar-add-item-from-menu
2657                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2658               (tool-bar-add-item-from-menu
2659                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2660               (tool-bar-add-item-from-menu
2661                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2662               (tool-bar-add-item-from-menu
2663                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2664               (tool-bar-add-item-from-menu
2665                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2666               (tool-bar-add-item-from-menu
2667                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2668               (tool-bar-add-item-from-menu
2669                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2670               (tool-bar-add-item-from-menu
2671                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2672               (tool-bar-add-item-from-menu
2673                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2674               tool-bar-map)))
2675   (if gnus-summary-tool-bar-map
2676       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2677
2678 (defun gnus-score-set-default (var value)
2679   "A version of set that updates the GNU Emacs menu-bar."
2680   (set var value)
2681   ;; It is the message that forces the active status to be updated.
2682   (message ""))
2683
2684 (defun gnus-make-score-map (type)
2685   "Make a summary score map of type TYPE."
2686   (if t
2687       nil
2688     (let ((headers '(("author" "from" string)
2689                      ("subject" "subject" string)
2690                      ("article body" "body" string)
2691                      ("article head" "head" string)
2692                      ("xref" "xref" string)
2693                      ("extra header" "extra" string)
2694                      ("lines" "lines" number)
2695                      ("followups to author" "followup" string)))
2696           (types '((number ("less than" <)
2697                            ("greater than" >)
2698                            ("equal" =))
2699                    (string ("substring" s)
2700                            ("exact string" e)
2701                            ("fuzzy string" f)
2702                            ("regexp" r))))
2703           (perms '(("temporary" (current-time-string))
2704                    ("permanent" nil)
2705                    ("immediate" now)))
2706           header)
2707       (list
2708        (apply
2709         'nconc
2710         (list
2711          (if (eq type 'lower)
2712              "Lower score"
2713            "Increase score"))
2714         (let (outh)
2715           (while headers
2716             (setq header (car headers))
2717             (setq outh
2718                   (cons
2719                    (apply
2720                     'nconc
2721                     (list (car header))
2722                     (let ((ts (cdr (assoc (nth 2 header) types)))
2723                           outt)
2724                       (while ts
2725                         (setq outt
2726                               (cons
2727                                (apply
2728                                 'nconc
2729                                 (list (caar ts))
2730                                 (let ((ps perms)
2731                                       outp)
2732                                   (while ps
2733                                     (setq outp
2734                                           (cons
2735                                            (vector
2736                                             (caar ps)
2737                                             (list
2738                                              'gnus-summary-score-entry
2739                                              (nth 1 header)
2740                                              (if (or (string= (nth 1 header)
2741                                                               "head")
2742                                                      (string= (nth 1 header)
2743                                                               "body"))
2744                                                  ""
2745                                                (list 'gnus-summary-header
2746                                                      (nth 1 header)))
2747                                              (list 'quote (nth 1 (car ts)))
2748                                              (list 'gnus-score-delta-default
2749                                                    nil)
2750                                              (nth 1 (car ps))
2751                                              t)
2752                                             t)
2753                                            outp))
2754                                     (setq ps (cdr ps)))
2755                                   (list (nreverse outp))))
2756                                outt))
2757                         (setq ts (cdr ts)))
2758                       (list (nreverse outt))))
2759                    outh))
2760             (setq headers (cdr headers)))
2761           (list (nreverse outh))))))))
2762
2763 \f
2764
2765 (defun gnus-summary-mode (&optional group)
2766   "Major mode for reading articles.
2767
2768 All normal editing commands are switched off.
2769 \\<gnus-summary-mode-map>
2770 Each line in this buffer represents one article.  To read an
2771 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2772 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2773 respectively.
2774
2775 You can also post articles and send mail from this buffer.  To
2776 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2777 of an article, type `\\[gnus-summary-reply]'.
2778
2779 There are approx. one gazillion commands you can execute in this
2780 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2781
2782 The following commands are available:
2783
2784 \\{gnus-summary-mode-map}"
2785   (interactive)
2786   (kill-all-local-variables)
2787   (when (gnus-visual-p 'summary-menu 'menu)
2788     (gnus-summary-make-menu-bar)
2789     (gnus-summary-make-tool-bar))
2790   (gnus-summary-make-local-variables)
2791   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2792     (gnus-summary-make-local-variables))
2793   (gnus-make-thread-indent-array)
2794   (gnus-simplify-mode-line)
2795   (setq major-mode 'gnus-summary-mode)
2796   (setq mode-name "Summary")
2797   (make-local-variable 'minor-mode-alist)
2798   (use-local-map gnus-summary-mode-map)
2799   (buffer-disable-undo)
2800   (setq buffer-read-only t              ;Disable modification
2801         show-trailing-whitespace nil)
2802   (setq truncate-lines t)
2803   (setq selective-display t)
2804   (setq selective-display-ellipses t)   ;Display `...'
2805   (gnus-summary-set-display-table)
2806   (gnus-set-default-directory)
2807   (setq gnus-newsgroup-name group)
2808   (make-local-variable 'gnus-summary-line-format)
2809   (make-local-variable 'gnus-summary-line-format-spec)
2810   (make-local-variable 'gnus-summary-dummy-line-format)
2811   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2812   (make-local-variable 'gnus-summary-mark-positions)
2813   (gnus-make-local-hook 'pre-command-hook)
2814   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2815   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2816   (turn-on-gnus-mailing-list-mode)
2817   (mm-enable-multibyte)
2818   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2819   (gnus-update-summary-mark-positions))
2820
2821 (defun gnus-summary-make-local-variables ()
2822   "Make all the local summary buffer variables."
2823   (let (global)
2824     (dolist (local gnus-summary-local-variables)
2825       (if (consp local)
2826           (progn
2827             (if (eq (cdr local) 'global)
2828                 ;; Copy the global value of the variable.
2829                 (setq global (symbol-value (car local)))
2830               ;; Use the value from the list.
2831               (setq global (eval (cdr local))))
2832             (set (make-local-variable (car local)) global))
2833         ;; Simple nil-valued local variable.
2834         (set (make-local-variable local) nil)))))
2835
2836 (defun gnus-summary-clear-local-variables ()
2837   (let ((locals gnus-summary-local-variables))
2838     (while locals
2839       (if (consp (car locals))
2840           (and (symbolp (caar locals))
2841                (set (caar locals) nil))
2842         (and (symbolp (car locals))
2843              (set (car locals) nil)))
2844       (setq locals (cdr locals)))))
2845
2846 ;; Summary data functions.
2847
2848 (defmacro gnus-data-number (data)
2849   `(car ,data))
2850
2851 (defmacro gnus-data-set-number (data number)
2852   `(setcar ,data ,number))
2853
2854 (defmacro gnus-data-mark (data)
2855   `(nth 1 ,data))
2856
2857 (defmacro gnus-data-set-mark (data mark)
2858   `(setcar (nthcdr 1 ,data) ,mark))
2859
2860 (defmacro gnus-data-pos (data)
2861   `(nth 2 ,data))
2862
2863 (defmacro gnus-data-set-pos (data pos)
2864   `(setcar (nthcdr 2 ,data) ,pos))
2865
2866 (defmacro gnus-data-header (data)
2867   `(nth 3 ,data))
2868
2869 (defmacro gnus-data-set-header (data header)
2870   `(setf (nth 3 ,data) ,header))
2871
2872 (defmacro gnus-data-level (data)
2873   `(nth 4 ,data))
2874
2875 (defmacro gnus-data-unread-p (data)
2876   `(= (nth 1 ,data) gnus-unread-mark))
2877
2878 (defmacro gnus-data-read-p (data)
2879   `(/= (nth 1 ,data) gnus-unread-mark))
2880
2881 (defmacro gnus-data-pseudo-p (data)
2882   `(consp (nth 3 ,data)))
2883
2884 (defmacro gnus-data-find (number)
2885   `(assq ,number gnus-newsgroup-data))
2886
2887 (defmacro gnus-data-find-list (number &optional data)
2888   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2889      (memq (assq ,number bdata)
2890            bdata)))
2891
2892 (defmacro gnus-data-make (number mark pos header level)
2893   `(list ,number ,mark ,pos ,header ,level))
2894
2895 (defun gnus-data-enter (after-article number mark pos header level offset)
2896   (let ((data (gnus-data-find-list after-article)))
2897     (unless data
2898       (error "No such article: %d" after-article))
2899     (setcdr data (cons (gnus-data-make number mark pos header level)
2900                        (cdr data)))
2901     (setq gnus-newsgroup-data-reverse nil)
2902     (gnus-data-update-list (cddr data) offset)))
2903
2904 (defun gnus-data-enter-list (after-article list &optional offset)
2905   (when list
2906     (let ((data (and after-article (gnus-data-find-list after-article)))
2907           (ilist list))
2908       (if (not (or data
2909                    after-article))
2910           (let ((odata gnus-newsgroup-data))
2911             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2912             (when offset
2913               (gnus-data-update-list odata offset)))
2914         ;; Find the last element in the list to be spliced into the main
2915         ;; list.
2916         (setq list (last list))
2917         (if (not data)
2918             (progn
2919               (setcdr list gnus-newsgroup-data)
2920               (setq gnus-newsgroup-data ilist)
2921               (when offset
2922                 (gnus-data-update-list (cdr list) offset)))
2923           (setcdr list (cdr data))
2924           (setcdr data ilist)
2925           (when offset
2926             (gnus-data-update-list (cdr list) offset))))
2927       (setq gnus-newsgroup-data-reverse nil))))
2928
2929 (defun gnus-data-remove (article &optional offset)
2930   (let ((data gnus-newsgroup-data))
2931     (if (= (gnus-data-number (car data)) article)
2932         (progn
2933           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2934                 gnus-newsgroup-data-reverse nil)
2935           (when offset
2936             (gnus-data-update-list gnus-newsgroup-data offset)))
2937       (while (cdr data)
2938         (when (= (gnus-data-number (cadr data)) article)
2939           (setcdr data (cddr data))
2940           (when offset
2941             (gnus-data-update-list (cdr data) offset))
2942           (setq data nil
2943                 gnus-newsgroup-data-reverse nil))
2944         (setq data (cdr data))))))
2945
2946 (defmacro gnus-data-list (backward)
2947   `(if ,backward
2948        (or gnus-newsgroup-data-reverse
2949            (setq gnus-newsgroup-data-reverse
2950                  (reverse gnus-newsgroup-data)))
2951      gnus-newsgroup-data))
2952
2953 (defun gnus-data-update-list (data offset)
2954   "Add OFFSET to the POS of all data entries in DATA."
2955   (setq gnus-newsgroup-data-reverse nil)
2956   (while data
2957     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2958     (setq data (cdr data))))
2959
2960 (defun gnus-summary-article-pseudo-p (article)
2961   "Say whether this article is a pseudo article or not."
2962   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2963
2964 (defmacro gnus-summary-article-sparse-p (article)
2965   "Say whether this article is a sparse article or not."
2966   `(memq ,article gnus-newsgroup-sparse))
2967
2968 (defmacro gnus-summary-article-ancient-p (article)
2969   "Say whether this article is a sparse article or not."
2970   `(memq ,article gnus-newsgroup-ancient))
2971
2972 (defun gnus-article-parent-p (number)
2973   "Say whether this article is a parent or not."
2974   (let ((data (gnus-data-find-list number)))
2975     (and (cdr data)              ; There has to be an article after...
2976          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2977             (gnus-data-level (nth 1 data))))))
2978
2979 (defun gnus-article-children (number)
2980   "Return a list of all children to NUMBER."
2981   (let* ((data (gnus-data-find-list number))
2982          (level (gnus-data-level (car data)))
2983          children)
2984     (setq data (cdr data))
2985     (while (and data
2986                 (= (gnus-data-level (car data)) (1+ level)))
2987       (push (gnus-data-number (car data)) children)
2988       (setq data (cdr data)))
2989     children))
2990
2991 (defmacro gnus-summary-skip-intangible ()
2992   "If the current article is intangible, then jump to a different article."
2993   '(let ((to (get-text-property (point) 'gnus-intangible)))
2994      (and to (gnus-summary-goto-subject to))))
2995
2996 (defmacro gnus-summary-article-intangible-p ()
2997   "Say whether this article is intangible or not."
2998   '(get-text-property (point) 'gnus-intangible))
2999
3000 (defun gnus-article-read-p (article)
3001   "Say whether ARTICLE is read or not."
3002   (not (or (memq article gnus-newsgroup-marked)
3003            (memq article gnus-newsgroup-spam-marked)
3004            (memq article gnus-newsgroup-unreads)
3005            (memq article gnus-newsgroup-unselected)
3006            (memq article gnus-newsgroup-dormant))))
3007
3008 ;; Some summary mode macros.
3009
3010 (defmacro gnus-summary-article-number ()
3011   "The article number of the article on the current line.
3012 If there isn't an article number here, then we return the current
3013 article number."
3014   '(progn
3015      (gnus-summary-skip-intangible)
3016      (or (get-text-property (point) 'gnus-number)
3017          (gnus-summary-last-subject))))
3018
3019 (defmacro gnus-summary-article-header (&optional number)
3020   "Return the header of article NUMBER."
3021   `(gnus-data-header (gnus-data-find
3022                       ,(or number '(gnus-summary-article-number)))))
3023
3024 (defmacro gnus-summary-thread-level (&optional number)
3025   "Return the level of thread that starts with article NUMBER."
3026   `(if (and (eq gnus-summary-make-false-root 'dummy)
3027             (get-text-property (point) 'gnus-intangible))
3028        0
3029      (gnus-data-level (gnus-data-find
3030                        ,(or number '(gnus-summary-article-number))))))
3031
3032 (defmacro gnus-summary-article-mark (&optional number)
3033   "Return the mark of article NUMBER."
3034   `(gnus-data-mark (gnus-data-find
3035                     ,(or number '(gnus-summary-article-number)))))
3036
3037 (defmacro gnus-summary-article-pos (&optional number)
3038   "Return the position of the line of article NUMBER."
3039   `(gnus-data-pos (gnus-data-find
3040                    ,(or number '(gnus-summary-article-number)))))
3041
3042 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3043 (defmacro gnus-summary-article-subject (&optional number)
3044   "Return current subject string or nil if nothing."
3045   `(let ((headers
3046           ,(if number
3047                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3048              '(gnus-data-header (assq (gnus-summary-article-number)
3049                                       gnus-newsgroup-data)))))
3050      (and headers
3051           (vectorp headers)
3052           (mail-header-subject headers))))
3053
3054 (defmacro gnus-summary-article-score (&optional number)
3055   "Return current article score."
3056   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3057                   gnus-newsgroup-scored))
3058        gnus-summary-default-score 0))
3059
3060 (defun gnus-summary-article-children (&optional number)
3061   "Return a list of article numbers that are children of article NUMBER."
3062   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3063          (level (gnus-data-level (car data)))
3064          l children)
3065     (while (and (setq data (cdr data))
3066                 (> (setq l (gnus-data-level (car data))) level))
3067       (and (= (1+ level) l)
3068            (push (gnus-data-number (car data))
3069                  children)))
3070     (nreverse children)))
3071
3072 (defun gnus-summary-article-parent (&optional number)
3073   "Return the article number of the parent of article NUMBER."
3074   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3075                                     (gnus-data-list t)))
3076          (level (gnus-data-level (car data))))
3077     (if (zerop level)
3078         ()                              ; This is a root.
3079       ;; We search until we find an article with a level less than
3080       ;; this one.  That function has to be the parent.
3081       (while (and (setq data (cdr data))
3082                   (not (< (gnus-data-level (car data)) level))))
3083       (and data (gnus-data-number (car data))))))
3084
3085 (defun gnus-unread-mark-p (mark)
3086   "Say whether MARK is the unread mark."
3087   (= mark gnus-unread-mark))
3088
3089 (defun gnus-read-mark-p (mark)
3090   "Say whether MARK is one of the marks that mark as read.
3091 This is all marks except unread, ticked, dormant, and expirable."
3092   (not (or (= mark gnus-unread-mark)
3093            (= mark gnus-ticked-mark)
3094            (= mark gnus-spam-mark)
3095            (= mark gnus-dormant-mark)
3096            (= mark gnus-expirable-mark))))
3097
3098 (defmacro gnus-article-mark (number)
3099   "Return the MARK of article NUMBER.
3100 This macro should only be used when computing the mark the \"first\"
3101 time; i.e., when generating the summary lines.  After that,
3102 `gnus-summary-article-mark' should be used to examine the
3103 marks of articles."
3104   `(cond
3105     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3106     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3107     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3108     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3109     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3110     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3111     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3112     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3113            gnus-ancient-mark))))
3114
3115 ;; Saving hidden threads.
3116
3117 (defmacro gnus-save-hidden-threads (&rest forms)
3118   "Save hidden threads, eval FORMS, and restore the hidden threads."
3119   (let ((config (make-symbol "config")))
3120     `(let ((,config (gnus-hidden-threads-configuration)))
3121        (unwind-protect
3122            (save-excursion
3123              ,@forms)
3124          (gnus-restore-hidden-threads-configuration ,config)))))
3125 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3126 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3127
3128 (defun gnus-data-compute-positions ()
3129   "Compute the positions of all articles."
3130   (setq gnus-newsgroup-data-reverse nil)
3131   (let ((data gnus-newsgroup-data))
3132     (save-excursion
3133       (gnus-save-hidden-threads
3134         (gnus-summary-show-all-threads)
3135         (goto-char (point-min))
3136         (while data
3137           (while (get-text-property (point) 'gnus-intangible)
3138             (forward-line 1))
3139           (gnus-data-set-pos (car data) (+ (point) 3))
3140           (setq data (cdr data))
3141           (forward-line 1))))))
3142
3143 (defun gnus-hidden-threads-configuration ()
3144   "Return the current hidden threads configuration."
3145   (save-excursion
3146     (let (config)
3147       (goto-char (point-min))
3148       (while (search-forward "\r" nil t)
3149         (push (1- (point)) config))
3150       config)))
3151
3152 (defun gnus-restore-hidden-threads-configuration (config)
3153   "Restore hidden threads configuration from CONFIG."
3154   (save-excursion
3155     (let (point buffer-read-only)
3156       (while (setq point (pop config))
3157         (when (and (< point (point-max))
3158                    (goto-char point)
3159                    (eq (char-after) ?\n))
3160           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3161
3162 ;; Various summary mode internalish functions.
3163
3164 (defun gnus-mouse-pick-article (e)
3165   (interactive "e")
3166   (mouse-set-point e)
3167   (gnus-summary-next-page nil t))
3168
3169 (defun gnus-summary-set-display-table ()
3170   "Change the display table.
3171 Odd characters have a tendency to mess
3172 up nicely formatted displays - we make all possible glyphs
3173 display only a single character."
3174
3175   ;; We start from the standard display table, if any.
3176   (let ((table (or (copy-sequence standard-display-table)
3177                    (make-display-table)))
3178         (i 32))
3179     ;; Nix out all the control chars...
3180     (while (>= (setq i (1- i)) 0)
3181       (aset table i [??]))
3182    ;; ... but not newline and cr, of course.  (cr is necessary for the
3183     ;; selective display).
3184     (aset table ?\n nil)
3185     (aset table ?\r nil)
3186     ;; We keep TAB as well.
3187     (aset table ?\t nil)
3188     ;; We nix out any glyphs over 126 that are not set already.
3189     (let ((i 256))
3190       (while (>= (setq i (1- i)) 127)
3191         ;; Only modify if the entry is nil.
3192         (unless (aref table i)
3193           (aset table i [??]))))
3194     (setq buffer-display-table table)))
3195
3196 (defun gnus-summary-set-article-display-arrow (pos)
3197   "Update the overlay arrow to point to line at position POS."
3198   (when (and gnus-summary-display-arrow
3199              (boundp 'overlay-arrow-position)
3200              (boundp 'overlay-arrow-string))
3201     (save-excursion
3202       (goto-char pos)
3203       (beginning-of-line)
3204       (unless overlay-arrow-position
3205         (setq overlay-arrow-position (make-marker)))
3206       (setq overlay-arrow-string "=>"
3207             overlay-arrow-position (set-marker overlay-arrow-position
3208                                                (point)
3209                                                (current-buffer))))))
3210
3211 (defun gnus-summary-setup-buffer (group)
3212   "Initialize summary buffer."
3213   (let ((buffer (gnus-summary-buffer-name group))
3214         (dead-name (concat "*Dead Summary "
3215                            (gnus-group-decoded-name group) "*")))
3216     ;; If a dead summary buffer exists, we kill it.
3217     (when (gnus-buffer-live-p dead-name)
3218       (gnus-kill-buffer dead-name))
3219     (if (get-buffer buffer)
3220         (progn
3221           (set-buffer buffer)
3222           (setq gnus-summary-buffer (current-buffer))
3223           (not gnus-newsgroup-prepared))
3224       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3225       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3226       (gnus-summary-mode group)
3227       (when gnus-carpal
3228         (gnus-carpal-setup-buffer 'summary))
3229       (unless gnus-single-article-buffer
3230         (make-local-variable 'gnus-article-buffer)
3231         (make-local-variable 'gnus-article-current)
3232         (make-local-variable 'gnus-original-article-buffer))
3233       (setq gnus-newsgroup-name group)
3234       ;; Set any local variables in the group parameters.
3235       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3236       t)))
3237
3238 (defun gnus-set-global-variables ()
3239   "Set the global equivalents of the buffer-local variables.
3240 They are set to the latest values they had.  These reflect the summary
3241 buffer that was in action when the last article was fetched."
3242   (when (eq major-mode 'gnus-summary-mode)
3243     (setq gnus-summary-buffer (current-buffer))
3244     (let ((name gnus-newsgroup-name)
3245           (marked gnus-newsgroup-marked)
3246           (spam gnus-newsgroup-spam-marked)
3247           (unread gnus-newsgroup-unreads)
3248           (headers gnus-current-headers)
3249           (data gnus-newsgroup-data)
3250           (summary gnus-summary-buffer)
3251           (article-buffer gnus-article-buffer)
3252           (original gnus-original-article-buffer)
3253           (gac gnus-article-current)
3254           (reffed gnus-reffed-article-number)
3255           (score-file gnus-current-score-file)
3256           (default-charset gnus-newsgroup-charset)
3257           vlist)
3258       (let ((locals gnus-newsgroup-variables))
3259         (while locals
3260           (if (consp (car locals))
3261               (push (eval (caar locals)) vlist)
3262             (push (eval (car locals)) vlist))
3263           (setq locals (cdr locals)))
3264         (setq vlist (nreverse vlist)))
3265       (with-current-buffer gnus-group-buffer
3266         (setq gnus-newsgroup-name name
3267               gnus-newsgroup-marked marked
3268               gnus-newsgroup-spam-marked spam
3269               gnus-newsgroup-unreads unread
3270               gnus-current-headers headers
3271               gnus-newsgroup-data data
3272               gnus-article-current gac
3273               gnus-summary-buffer summary
3274               gnus-article-buffer article-buffer
3275               gnus-original-article-buffer original
3276               gnus-reffed-article-number reffed
3277               gnus-current-score-file score-file
3278               gnus-newsgroup-charset default-charset)
3279         (let ((locals gnus-newsgroup-variables))
3280           (while locals
3281             (if (consp (car locals))
3282                 (set (caar locals) (pop vlist))
3283               (set (car locals) (pop vlist)))
3284             (setq locals (cdr locals))))
3285         ;; The article buffer also has local variables.
3286         (when (gnus-buffer-live-p gnus-article-buffer)
3287           (set-buffer gnus-article-buffer)
3288           (setq gnus-summary-buffer summary))))))
3289
3290 (defun gnus-summary-article-unread-p (article)
3291   "Say whether ARTICLE is unread or not."
3292   (memq article gnus-newsgroup-unreads))
3293
3294 (defun gnus-summary-first-article-p (&optional article)
3295   "Return whether ARTICLE is the first article in the buffer."
3296   (if (not (setq article (or article (gnus-summary-article-number))))
3297       nil
3298     (eq article (caar gnus-newsgroup-data))))
3299
3300 (defun gnus-summary-last-article-p (&optional article)
3301   "Return whether ARTICLE is the last article in the buffer."
3302   (if (not (setq article (or article (gnus-summary-article-number))))
3303       ;; All non-existent numbers are the last article.  :-)
3304       t
3305     (not (cdr (gnus-data-find-list article)))))
3306
3307 (defun gnus-make-thread-indent-array ()
3308   (let ((n 200))
3309     (unless (and gnus-thread-indent-array
3310                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3311       (setq gnus-thread-indent-array (make-vector 201 "")
3312             gnus-thread-indent-array-level gnus-thread-indent-level)
3313       (while (>= n 0)
3314         (aset gnus-thread-indent-array n
3315               (make-string (* n gnus-thread-indent-level) ? ))
3316         (setq n (1- n))))))
3317
3318 (defun gnus-update-summary-mark-positions ()
3319   "Compute where the summary marks are to go."
3320   (save-excursion
3321     (when (gnus-buffer-exists-p gnus-summary-buffer)
3322       (set-buffer gnus-summary-buffer))
3323     (let ((spec gnus-summary-line-format-spec)
3324           pos)
3325       (save-excursion
3326         (gnus-set-work-buffer)
3327         (let ((gnus-tmp-unread ?Z)
3328               (gnus-replied-mark ?Z)
3329               (gnus-score-below-mark ?Z)
3330               (gnus-score-over-mark ?Z)
3331               (gnus-undownloaded-mark ?Z)
3332               (gnus-summary-line-format-spec spec)
3333               (gnus-newsgroup-downloadable '(0))
3334               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3335               case-fold-search ignores)
3336           ;; Here, all marks are bound to Z.
3337           (gnus-summary-insert-line header
3338                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3339           (goto-char (point-min))
3340           ;; Memorize the positions of the same characters as dummy marks.
3341           (while (re-search-forward "[A-D]" nil t)
3342             (push (point) ignores))
3343           (erase-buffer)
3344           ;; We use A-D as dummy marks in order to know column positions
3345           ;; where marks should be inserted.
3346           (setq gnus-tmp-unread ?A
3347                 gnus-replied-mark ?B
3348                 gnus-score-below-mark ?C
3349                 gnus-score-over-mark ?C
3350                 gnus-undownloaded-mark ?D)
3351           (gnus-summary-insert-line header
3352                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3353           ;; Ignore characters which aren't dummy marks.
3354           (dolist (p ignores)
3355             (delete-region (goto-char (1- p)) p)
3356             (insert ?Z))
3357           (goto-char (point-min))
3358           (setq pos (list (cons 'unread
3359                                 (and (search-forward "A" nil t)
3360                                      (- (point) (point-min) 1)))))
3361           (goto-char (point-min))
3362           (push (cons 'replied (and (search-forward "B" nil t)
3363                                     (- (point) (point-min) 1)))
3364                 pos)
3365           (goto-char (point-min))
3366           (push (cons 'score (and (search-forward "C" nil t)
3367                                   (- (point) (point-min) 1)))
3368                 pos)
3369           (goto-char (point-min))
3370           (push (cons 'download (and (search-forward "D" nil t)
3371                                      (- (point) (point-min) 1)))
3372                 pos)))
3373       (setq gnus-summary-mark-positions pos))))
3374
3375 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3376   "Insert a dummy root in the summary buffer."
3377   (beginning-of-line)
3378   (gnus-add-text-properties
3379    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3380    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3381
3382 (defun gnus-summary-extract-address-component (from)
3383   (or (car (funcall gnus-extract-address-components from))
3384       from))
3385
3386 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3387   (let ((mail-parse-charset gnus-newsgroup-charset)
3388         ; Is it really necessary to do this next part for each summary line?
3389         ; Luckily, doesn't seem to slow things down much.
3390         (mail-parse-ignored-charsets
3391          (with-current-buffer gnus-summary-buffer
3392            gnus-newsgroup-ignored-charsets)))
3393     (or
3394      (and gnus-ignored-from-addresses
3395           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3396           (let ((extra-headers (mail-header-extra header))
3397                 to
3398                 newsgroups)
3399             (cond
3400              ((setq to (cdr (assq 'To extra-headers)))
3401               (concat gnus-summary-to-prefix
3402                       (inline
3403                         (gnus-summary-extract-address-component
3404                          (funcall gnus-decode-encoded-word-function to)))))
3405              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3406               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3407      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3408
3409 (defun gnus-summary-insert-line (gnus-tmp-header
3410                                  gnus-tmp-level gnus-tmp-current
3411                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3412                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3413                                  &optional gnus-tmp-dummy gnus-tmp-score
3414                                  gnus-tmp-process)
3415   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3416          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3417          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3418          (gnus-tmp-score-char
3419           (if (or (null gnus-summary-default-score)
3420                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3421                       gnus-summary-zcore-fuzz))
3422               ?                         ;Whitespace
3423             (if (< gnus-tmp-score gnus-summary-default-score)
3424                 gnus-score-below-mark gnus-score-over-mark)))
3425          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3426          (gnus-tmp-replied
3427           (cond (gnus-tmp-process gnus-process-mark)
3428                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3429                  gnus-cached-mark)
3430                 (gnus-tmp-replied gnus-replied-mark)
3431                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3432                  gnus-forwarded-mark)
3433                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3434                  gnus-saved-mark)
3435                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3436                  gnus-recent-mark)
3437                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3438                  gnus-unseen-mark)
3439                 (t gnus-no-mark)))
3440          (gnus-tmp-downloaded
3441           (cond (undownloaded
3442                  gnus-undownloaded-mark)
3443                 (gnus-newsgroup-agentized
3444                  gnus-downloaded-mark)
3445                 (t
3446                  gnus-no-mark)))
3447          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3448          (gnus-tmp-name
3449           (cond
3450            ((string-match "<[^>]+> *$" gnus-tmp-from)
3451             (let ((beg (match-beginning 0)))
3452               (or (and (string-match "^\".+\"" gnus-tmp-from)
3453                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3454                   (substring gnus-tmp-from 0 beg))))
3455            ((string-match "(.+)" gnus-tmp-from)
3456             (substring gnus-tmp-from
3457                        (1+ (match-beginning 0)) (1- (match-end 0))))
3458            (t gnus-tmp-from)))
3459          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3460          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3461          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3462          (buffer-read-only nil))
3463     (when (string= gnus-tmp-name "")
3464       (setq gnus-tmp-name gnus-tmp-from))
3465     (unless (numberp gnus-tmp-lines)
3466       (setq gnus-tmp-lines -1))
3467     (if (= gnus-tmp-lines -1)
3468         (setq gnus-tmp-lines "?")
3469       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3470       (gnus-put-text-property
3471      (point)
3472      (progn (eval gnus-summary-line-format-spec) (point))
3473        'gnus-number gnus-tmp-number)
3474     (when (gnus-visual-p 'summary-highlight 'highlight)
3475       (forward-line -1)
3476       (gnus-run-hooks 'gnus-summary-update-hook)
3477       (forward-line 1))))
3478
3479 (defun gnus-summary-update-line (&optional dont-update)
3480   "Update summary line after change."
3481   (when (and gnus-summary-default-score
3482              (not gnus-summary-inhibit-highlight))
3483     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3484            (article (gnus-summary-article-number))
3485            (score (gnus-summary-article-score article)))
3486       (unless dont-update
3487         (if (and gnus-summary-mark-below
3488                  (< (gnus-summary-article-score)
3489                     gnus-summary-mark-below))
3490             ;; This article has a low score, so we mark it as read.
3491             (when (memq article gnus-newsgroup-unreads)
3492               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3493           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3494             ;; This article was previously marked as read on account
3495             ;; of a low score, but now it has risen, so we mark it as
3496             ;; unread.
3497             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3498         (gnus-summary-update-mark
3499          (if (or (null gnus-summary-default-score)
3500                  (<= (abs (- score gnus-summary-default-score))
3501                      gnus-summary-zcore-fuzz))
3502              ?                          ;Whitespace
3503            (if (< score gnus-summary-default-score)
3504                gnus-score-below-mark gnus-score-over-mark))
3505          'score))
3506       ;; Do visual highlighting.
3507       (when (gnus-visual-p 'summary-highlight 'highlight)
3508         (gnus-run-hooks 'gnus-summary-update-hook)))))
3509
3510 (defvar gnus-tmp-new-adopts nil)
3511
3512 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3513   "Return the number of articles in THREAD.
3514 This may be 0 in some cases -- if none of the articles in
3515 the thread are to be displayed."
3516   (let* ((number
3517          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3518           (cond
3519            ((not (listp thread))
3520             1)
3521            ((and (consp thread) (cdr thread))
3522             (apply
3523              '+ 1 (mapcar
3524                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3525            ((null thread)
3526             1)
3527            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3528             1)
3529            (t 0))))
3530     (when (and level (zerop level) gnus-tmp-new-adopts)
3531       (incf number
3532             (apply '+ (mapcar
3533                        'gnus-summary-number-of-articles-in-thread
3534                        gnus-tmp-new-adopts))))
3535     (if char
3536         (if (> number 1) gnus-not-empty-thread-mark
3537           gnus-empty-thread-mark)
3538       number)))
3539
3540 (defsubst gnus-summary-line-message-size (head)
3541   "Return pretty-printed version of message size.
3542 This function is intended to be used in
3543 `gnus-summary-line-format-alist'."
3544   (let ((c (or (mail-header-chars head) -1)))
3545     (cond ((< c 0) "n/a")               ; chars not available
3546           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3547           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3548           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3549           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3550
3551
3552 (defun gnus-summary-set-local-parameters (group)
3553   "Go through the local params of GROUP and set all variable specs in that list."
3554   (let ((vars '(quit-config)))          ; Ignore quit-config.
3555     (dolist (elem (gnus-group-find-parameter group))
3556       (and (consp elem)                 ; Has to be a cons.
3557            (consp (cdr elem))           ; The cdr has to be a list.
3558            (symbolp (car elem))         ; Has to be a symbol in there.
3559            (not (memq (car elem) vars))
3560            (ignore-errors               ; So we set it.
3561              (push (car elem) vars)
3562              (make-local-variable (car elem))
3563              (set (car elem) (eval (nth 1 elem))))))))
3564
3565 (defun gnus-summary-read-group (group &optional show-all no-article
3566                                       kill-buffer no-display backward
3567                                       select-articles)
3568   "Start reading news in newsgroup GROUP.
3569 If SHOW-ALL is non-nil, already read articles are also listed.
3570 If NO-ARTICLE is non-nil, no article is selected initially.
3571 If NO-DISPLAY, don't generate a summary buffer."
3572   (let (result)
3573     (while (and group
3574                 (null (setq result
3575                             (let ((gnus-auto-select-next nil))
3576                               (or (gnus-summary-read-group-1
3577                                    group show-all no-article
3578                                    kill-buffer no-display
3579                                    select-articles)
3580                                   (setq show-all nil
3581                                         select-articles nil)))))
3582                 (eq gnus-auto-select-next 'quietly))
3583       (set-buffer gnus-group-buffer)
3584       ;; The entry function called above goes to the next
3585       ;; group automatically, so we go two groups back
3586       ;; if we are searching for the previous group.
3587       (when backward
3588         (gnus-group-prev-unread-group 2))
3589       (if (not (equal group (gnus-group-group-name)))
3590           (setq group (gnus-group-group-name))
3591         (setq group nil)))
3592     result))
3593
3594 (defun gnus-summary-read-group-1 (group show-all no-article
3595                                         kill-buffer no-display
3596                                         &optional select-articles)
3597   ;; Killed foreign groups can't be entered.
3598   ;;  (when (and (not (gnus-group-native-p group))
3599   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3600   ;;    (error "Dead non-native groups can't be entered"))
3601   (gnus-message 5 "Retrieving newsgroup: %s..."
3602                 (gnus-group-decoded-name group))
3603   (let* ((new-group (gnus-summary-setup-buffer group))
3604          (quit-config (gnus-group-quit-config group))
3605          (did-select (and new-group (gnus-select-newsgroup
3606                                      group show-all select-articles))))
3607     (cond
3608      ;; This summary buffer exists already, so we just select it.
3609      ((not new-group)
3610       (gnus-set-global-variables)
3611       (when kill-buffer
3612         (gnus-kill-or-deaden-summary kill-buffer))
3613       (gnus-configure-windows 'summary 'force)
3614       (gnus-set-mode-line 'summary)
3615       (gnus-summary-position-point)
3616       (message "")
3617       t)
3618      ;; We couldn't select this group.
3619      ((null did-select)
3620       (when (and (eq major-mode 'gnus-summary-mode)
3621                  (not (equal (current-buffer) kill-buffer)))
3622         (kill-buffer (current-buffer))
3623         (if (not quit-config)
3624             (progn
3625               ;; Update the info -- marks might need to be removed,
3626               ;; for instance.
3627               (gnus-summary-update-info)
3628               (set-buffer gnus-group-buffer)
3629               (gnus-group-jump-to-group group)
3630               (gnus-group-next-unread-group 1))
3631           (gnus-handle-ephemeral-exit quit-config)))
3632       (let ((grpinfo (gnus-get-info group)))
3633         (if (null (gnus-info-read grpinfo))
3634             (gnus-message 3 "Group %s contains no messages"
3635                           (gnus-group-decoded-name group))
3636           (gnus-message 3 "Can't select group")))
3637       nil)
3638      ;; The user did a `C-g' while prompting for number of articles,
3639      ;; so we exit this group.
3640      ((eq did-select 'quit)
3641       (and (eq major-mode 'gnus-summary-mode)
3642            (not (equal (current-buffer) kill-buffer))
3643            (kill-buffer (current-buffer)))
3644       (when kill-buffer
3645         (gnus-kill-or-deaden-summary kill-buffer))
3646       (if (not quit-config)
3647           (progn
3648             (set-buffer gnus-group-buffer)
3649             (gnus-group-jump-to-group group)
3650             (gnus-group-next-unread-group 1)
3651             (gnus-configure-windows 'group 'force))
3652         (gnus-handle-ephemeral-exit quit-config))
3653       ;; Finally signal the quit.
3654       (signal 'quit nil))
3655      ;; The group was successfully selected.
3656      (t
3657       (gnus-set-global-variables)
3658       ;; Save the active value in effect when the group was entered.
3659       (setq gnus-newsgroup-active
3660             (gnus-copy-sequence
3661              (gnus-active gnus-newsgroup-name)))
3662       ;; You can change the summary buffer in some way with this hook.
3663       (gnus-run-hooks 'gnus-select-group-hook)
3664       (when (memq 'summary (gnus-update-format-specifications
3665                             nil 'summary 'summary-mode 'summary-dummy))
3666         ;; The format specification for the summary line was updated,
3667         ;; so we need to update the mark positions as well.
3668         (gnus-update-summary-mark-positions))
3669       ;; Do score processing.
3670       (when gnus-use-scoring
3671         (gnus-possibly-score-headers))
3672       ;; Check whether to fill in the gaps in the threads.
3673       (when gnus-build-sparse-threads
3674         (gnus-build-sparse-threads))
3675       ;; Find the initial limit.
3676       (if show-all
3677           (let ((gnus-newsgroup-dormant nil))
3678             (gnus-summary-initial-limit show-all))
3679         (gnus-summary-initial-limit show-all))
3680       ;; Generate the summary buffer.
3681       (unless no-display
3682         (gnus-summary-prepare))
3683       (when gnus-use-trees
3684         (gnus-tree-open group)
3685         (setq gnus-summary-highlight-line-function
3686               'gnus-tree-highlight-article))
3687       ;; If the summary buffer is empty, but there are some low-scored
3688       ;; articles or some excluded dormants, we include these in the
3689       ;; buffer.
3690       (when (and (zerop (buffer-size))
3691                  (not no-display))
3692         (cond (gnus-newsgroup-dormant
3693                (gnus-summary-limit-include-dormant))
3694               ((and gnus-newsgroup-scored show-all)
3695                (gnus-summary-limit-include-expunged t))))
3696       ;; Function `gnus-apply-kill-file' must be called in this hook.
3697       (gnus-run-hooks 'gnus-apply-kill-hook)
3698       (if (and (zerop (buffer-size))
3699                (not no-display))
3700           (progn
3701             ;; This newsgroup is empty.
3702             (gnus-summary-catchup-and-exit nil t)
3703             (gnus-message 6 "No unread news")
3704             (when kill-buffer
3705               (gnus-kill-or-deaden-summary kill-buffer))
3706             ;; Return nil from this function.
3707             nil)
3708         ;; Hide conversation thread subtrees.  We cannot do this in
3709         ;; gnus-summary-prepare-hook since kill processing may not
3710         ;; work with hidden articles.
3711         (gnus-summary-maybe-hide-threads)
3712         (when kill-buffer
3713           (gnus-kill-or-deaden-summary kill-buffer))
3714         (gnus-summary-auto-select-subject)
3715         ;; Show first unread article if requested.
3716         (if (and (not no-article)
3717                  (not no-display)
3718                  gnus-newsgroup-unreads
3719                  gnus-auto-select-first)
3720             (progn
3721               (gnus-configure-windows 'summary)
3722               (let ((art (gnus-summary-article-number)))
3723                 (unless (and (not gnus-plugged)
3724                              (or (memq art gnus-newsgroup-undownloaded)
3725                                  (memq art gnus-newsgroup-downloadable)))
3726                   (gnus-summary-goto-article art))))
3727           ;; Don't select any articles.
3728           (gnus-summary-position-point)
3729           (gnus-configure-windows 'summary 'force)
3730           (gnus-set-mode-line 'summary))
3731         (when (and gnus-auto-center-group
3732                    (get-buffer-window gnus-group-buffer t))
3733           ;; Gotta use windows, because recenter does weird stuff if
3734           ;; the current buffer ain't the displayed window.
3735           (let ((owin (selected-window)))
3736             (select-window (get-buffer-window gnus-group-buffer t))
3737             (when (gnus-group-goto-group group)
3738               (recenter))
3739             (select-window owin)))
3740         ;; Mark this buffer as "prepared".
3741         (setq gnus-newsgroup-prepared t)
3742         (gnus-run-hooks 'gnus-summary-prepared-hook)
3743         (unless (gnus-ephemeral-group-p group)
3744           (gnus-group-update-group group))
3745         t)))))
3746
3747 (defun gnus-summary-auto-select-subject ()
3748   "Select the subject line on initial group entry."
3749   (goto-char (point-min))
3750   (cond
3751    ((eq gnus-auto-select-subject 'best)
3752     (gnus-summary-best-unread-subject))
3753    ((eq gnus-auto-select-subject 'unread)
3754     (gnus-summary-first-unread-subject))
3755    ((eq gnus-auto-select-subject 'unseen)
3756     (gnus-summary-first-unseen-subject))
3757    ((eq gnus-auto-select-subject 'unseen-or-unread)
3758     (gnus-summary-first-unseen-or-unread-subject))
3759    ((eq gnus-auto-select-subject 'first)
3760     ;; Do nothing.
3761     )
3762    ((functionp gnus-auto-select-subject)
3763     (funcall gnus-auto-select-subject))))
3764
3765 (defun gnus-summary-prepare ()
3766   "Generate the summary buffer."
3767   (interactive)
3768   (let ((buffer-read-only nil))
3769     (erase-buffer)
3770     (setq gnus-newsgroup-data nil
3771           gnus-newsgroup-data-reverse nil)
3772     (gnus-run-hooks 'gnus-summary-generate-hook)
3773     ;; Generate the buffer, either with threads or without.
3774     (when gnus-newsgroup-headers
3775       (gnus-summary-prepare-threads
3776        (if gnus-show-threads
3777            (gnus-sort-gathered-threads
3778             (funcall gnus-summary-thread-gathering-function
3779                      (gnus-sort-threads
3780                       (gnus-cut-threads (gnus-make-threads)))))
3781          ;; Unthreaded display.
3782          (gnus-sort-articles gnus-newsgroup-headers))))
3783     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3784     ;; Call hooks for modifying summary buffer.
3785     (goto-char (point-min))
3786     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3787
3788 (defsubst gnus-general-simplify-subject (subject)
3789   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3790   (setq subject
3791         (cond
3792          ;; Truncate the subject.
3793          (gnus-simplify-subject-functions
3794           (gnus-map-function gnus-simplify-subject-functions subject))
3795          ((numberp gnus-summary-gather-subject-limit)
3796           (setq subject (gnus-simplify-subject-re subject))
3797           (if (> (length subject) gnus-summary-gather-subject-limit)
3798               (substring subject 0 gnus-summary-gather-subject-limit)
3799             subject))
3800          ;; Fuzzily simplify it.
3801          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3802           (gnus-simplify-subject-fuzzy subject))
3803          ;; Just remove the leading "Re:".
3804          (t
3805           (gnus-simplify-subject-re subject))))
3806
3807   (if (and gnus-summary-gather-exclude-subject
3808            (string-match gnus-summary-gather-exclude-subject subject))
3809       nil                         ; This article shouldn't be gathered
3810     subject))
3811
3812 (defun gnus-summary-simplify-subject-query ()
3813   "Query where the respool algorithm would put this article."
3814   (interactive)
3815   (gnus-summary-select-article)
3816   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3817
3818 (defun gnus-gather-threads-by-subject (threads)
3819   "Gather threads by looking at Subject headers."
3820   (if (not gnus-summary-make-false-root)
3821       threads
3822     (let ((hashtb (gnus-make-hashtable 1024))
3823           (prev threads)
3824           (result threads)
3825           subject hthread whole-subject)
3826       (while threads
3827         (setq subject (gnus-general-simplify-subject
3828                        (setq whole-subject (mail-header-subject
3829                                             (caar threads)))))
3830         (when subject
3831           (if (setq hthread (gnus-gethash subject hashtb))
3832               (progn
3833                 ;; We enter a dummy root into the thread, if we
3834                 ;; haven't done that already.
3835                 (unless (stringp (caar hthread))
3836                   (setcar hthread (list whole-subject (car hthread))))
3837                 ;; We add this new gathered thread to this gathered
3838                 ;; thread.
3839                 (setcdr (car hthread)
3840                         (nconc (cdar hthread) (list (car threads))))
3841                 ;; Remove it from the list of threads.
3842                 (setcdr prev (cdr threads))
3843                 (setq threads prev))
3844             ;; Enter this thread into the hash table.
3845             (gnus-sethash subject
3846                           (if gnus-summary-make-false-root-always
3847                               (progn
3848                                 ;; If you want a dummy root above all
3849                                 ;; threads...
3850                                 (setcar threads (list whole-subject
3851                                                       (car threads)))
3852                                 threads)
3853                             threads)
3854                           hashtb)))
3855         (setq prev threads)
3856         (setq threads (cdr threads)))
3857       result)))
3858
3859 (defun gnus-gather-threads-by-references (threads)
3860   "Gather threads by looking at References headers."
3861   (let ((idhashtb (gnus-make-hashtable 1024))
3862         (thhashtb (gnus-make-hashtable 1024))
3863         (prev threads)
3864         (result threads)
3865         ids references id gthread gid entered ref)
3866     (while threads
3867       (when (setq references (mail-header-references (caar threads)))
3868         (setq id (mail-header-id (caar threads))
3869               ids (inline (gnus-split-references references))
3870               entered nil)
3871         (while (setq ref (pop ids))
3872           (setq ids (delete ref ids))
3873           (if (not (setq gid (gnus-gethash ref idhashtb)))
3874               (progn
3875                 (gnus-sethash ref id idhashtb)
3876                 (gnus-sethash id threads thhashtb))
3877             (setq gthread (gnus-gethash gid thhashtb))
3878             (unless entered
3879               ;; We enter a dummy root into the thread, if we
3880               ;; haven't done that already.
3881               (unless (stringp (caar gthread))
3882                 (setcar gthread (list (mail-header-subject (caar gthread))
3883                                       (car gthread))))
3884               ;; We add this new gathered thread to this gathered
3885               ;; thread.
3886               (setcdr (car gthread)
3887                       (nconc (cdar gthread) (list (car threads)))))
3888             ;; Add it into the thread hash table.
3889             (gnus-sethash id gthread thhashtb)
3890             (setq entered t)
3891             ;; Remove it from the list of threads.
3892             (setcdr prev (cdr threads))
3893             (setq threads prev))))
3894       (setq prev threads)
3895       (setq threads (cdr threads)))
3896     result))
3897
3898 (defun gnus-sort-gathered-threads (threads)
3899   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3900   (let ((result threads))
3901     (while threads
3902       (when (stringp (caar threads))
3903         (setcdr (car threads)
3904                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3905       (setq threads (cdr threads)))
3906     result))
3907
3908 (defun gnus-thread-loop-p (root thread)
3909   "Say whether ROOT is in THREAD."
3910   (let ((stack (list thread))
3911         (infloop 0)
3912         th)
3913     (while (setq thread (pop stack))
3914       (setq th (cdr thread))
3915       (while (and th
3916                   (not (eq (caar th) root)))
3917         (pop th))
3918       (if th
3919           ;; We have found a loop.
3920           (let (ref-dep)
3921             (setcdr thread (delq (car th) (cdr thread)))
3922             (if (boundp (setq ref-dep (intern "none"
3923                                               gnus-newsgroup-dependencies)))
3924                 (setcdr (symbol-value ref-dep)
3925                         (nconc (cdr (symbol-value ref-dep))
3926                                (list (car th))))
3927               (set ref-dep (list nil (car th))))
3928             (setq infloop 1
3929                   stack nil))
3930         ;; Push all the subthreads onto the stack.
3931         (push (cdr thread) stack)))
3932     infloop))
3933
3934 (defun gnus-make-threads ()
3935   "Go through the dependency hashtb and find the roots.  Return all threads."
3936   (let (threads)
3937     (while (catch 'infloop
3938              (mapatoms
3939               (lambda (refs)
3940                 ;; Deal with self-referencing References loops.
3941                 (when (and (car (symbol-value refs))
3942                            (not (zerop
3943                                  (apply
3944                                   '+
3945                                   (mapcar
3946                                    (lambda (thread)
3947                                      (gnus-thread-loop-p
3948                                       (car (symbol-value refs)) thread))
3949                                    (cdr (symbol-value refs)))))))
3950                   (setq threads nil)
3951                   (throw 'infloop t))
3952                 (unless (car (symbol-value refs))
3953                   ;; These threads do not refer back to any other
3954                   ;; articles, so they're roots.
3955                   (setq threads (append (cdr (symbol-value refs)) threads))))
3956               gnus-newsgroup-dependencies)))
3957     threads))
3958
3959 ;; Build the thread tree.
3960 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3961   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3962
3963 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3964 if it was already present.
3965
3966 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3967 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3968 Message-IDs will be renamed to a unique Message-ID before being
3969 entered.
3970
3971 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3972   (let* ((id (mail-header-id header))
3973          (id-dep (and id (intern id dependencies)))
3974          parent-id ref ref-dep ref-header replaced)
3975     ;; Enter this `header' in the `dependencies' table.
3976     (cond
3977      ((not id-dep)
3978       (setq header nil))
3979      ;; The first two cases do the normal part: enter a new `header'
3980      ;; in the `dependencies' table.
3981      ((not (boundp id-dep))
3982       (set id-dep (list header)))
3983      ((null (car (symbol-value id-dep)))
3984       (setcar (symbol-value id-dep) header))
3985
3986      ;; From here the `header' was already present in the
3987      ;; `dependencies' table.
3988      (force-new
3989       ;; Overrides an existing entry;
3990       ;; just set the header part of the entry.
3991       (setcar (symbol-value id-dep) header)
3992       (setq replaced t))
3993
3994      ;; Renames the existing `header' to a unique Message-ID.
3995      ((not gnus-summary-ignore-duplicates)
3996       ;; An article with this Message-ID has already been seen.
3997       ;; We rename the Message-ID.
3998       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3999            (list header))
4000       (mail-header-set-id header id))
4001
4002      ;; The last case ignores an existing entry, except it adds any
4003      ;; additional Xrefs (in case the two articles came from different
4004      ;; servers.
4005      ;; Also sets `header' to `nil' meaning that the `dependencies'
4006      ;; table was *not* modified.
4007      (t
4008       (mail-header-set-xref
4009        (car (symbol-value id-dep))
4010        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4011                    "")
4012                (or (mail-header-xref header) "")))
4013       (setq header nil)))
4014
4015     (when (and header (not replaced))
4016       ;; First check that we are not creating a References loop.
4017       (setq parent-id (gnus-parent-id (mail-header-references header)))
4018       (setq ref parent-id)
4019       (while (and ref
4020                   (setq ref-dep (intern-soft ref dependencies))
4021                   (boundp ref-dep)
4022                   (setq ref-header (car (symbol-value ref-dep))))
4023         (if (string= id ref)
4024             ;; Yuk!  This is a reference loop.  Make the article be a
4025             ;; root article.
4026             (progn
4027               (mail-header-set-references (car (symbol-value id-dep)) "none")
4028               (setq ref nil)
4029               (setq parent-id nil))
4030           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4031       (setq ref-dep (intern (or parent-id "none") dependencies))
4032       (if (boundp ref-dep)
4033           (setcdr (symbol-value ref-dep)
4034                   (nconc (cdr (symbol-value ref-dep))
4035                          (list (symbol-value id-dep))))
4036         (set ref-dep (list nil (symbol-value id-dep)))))
4037     header))
4038
4039 (defun gnus-extract-message-id-from-in-reply-to (string)
4040   (if (string-match "<[^>]+>" string)
4041       (substring string (match-beginning 0) (match-end 0))
4042     nil))
4043
4044 (defun gnus-build-sparse-threads ()
4045   (let ((headers gnus-newsgroup-headers)
4046         (mail-parse-charset gnus-newsgroup-charset)
4047         (gnus-summary-ignore-duplicates t)
4048         header references generation relations
4049         subject child end new-child date)
4050     ;; First we create an alist of generations/relations, where
4051     ;; generations is how much we trust the relation, and the relation
4052     ;; is parent/child.
4053     (gnus-message 7 "Making sparse threads...")
4054     (save-excursion
4055       (nnheader-set-temp-buffer " *gnus sparse threads*")
4056       (while (setq header (pop headers))
4057         (when (and (setq references (mail-header-references header))
4058                    (not (string= references "")))
4059           (insert references)
4060           (setq child (mail-header-id header)
4061                 subject (mail-header-subject header)
4062                 date (mail-header-date header)
4063                 generation 0)
4064           (while (search-backward ">" nil t)
4065             (setq end (1+ (point)))
4066             (when (search-backward "<" nil t)
4067               (setq new-child (buffer-substring (point) end))
4068               (push (list (incf generation)
4069                           child (setq child new-child)
4070                           subject date)
4071                     relations)))
4072           (when child
4073             (push (list (1+ generation) child nil subject) relations))
4074           (erase-buffer)))
4075       (kill-buffer (current-buffer)))
4076     ;; Sort over trustworthiness.
4077     (mapcar
4078      (lambda (relation)
4079        (when (gnus-dependencies-add-header
4080               (make-full-mail-header
4081                gnus-reffed-article-number
4082                (nth 3 relation) "" (or (nth 4 relation) "")
4083                (nth 1 relation)
4084                (or (nth 2 relation) "") 0 0 "")
4085               gnus-newsgroup-dependencies nil)
4086          (push gnus-reffed-article-number gnus-newsgroup-limit)
4087          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4088          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4089                gnus-newsgroup-reads)
4090          (decf gnus-reffed-article-number)))
4091      (sort relations 'car-less-than-car))
4092     (gnus-message 7 "Making sparse threads...done")))
4093
4094 (defun gnus-build-old-threads ()
4095   ;; Look at all the articles that refer back to old articles, and
4096   ;; fetch the headers for the articles that aren't there.  This will
4097   ;; build complete threads - if the roots haven't been expired by the
4098   ;; server, that is.
4099   (let ((mail-parse-charset gnus-newsgroup-charset)
4100         id heads)
4101     (mapatoms
4102      (lambda (refs)
4103        (when (not (car (symbol-value refs)))
4104          (setq heads (cdr (symbol-value refs)))
4105          (while heads
4106            (if (memq (mail-header-number (caar heads))
4107                      gnus-newsgroup-dormant)
4108                (setq heads (cdr heads))
4109              (setq id (symbol-name refs))
4110              (while (and (setq id (gnus-build-get-header id))
4111                          (not (car (gnus-id-to-thread id)))))
4112              (setq heads nil)))))
4113      gnus-newsgroup-dependencies)))
4114
4115 (defsubst gnus-remove-odd-characters (string)
4116   "Translate STRING into something that doesn't contain weird characters."
4117   (mm-subst-char-in-string
4118    ?\r ?\-
4119    (mm-subst-char-in-string ?\n ?\- string t) t))
4120
4121 ;; This function has to be called with point after the article number
4122 ;; on the beginning of the line.
4123 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4124   (let ((eol (point-at-eol))
4125         (buffer (current-buffer))
4126         header references in-reply-to)
4127
4128     ;; overview: [num subject from date id refs chars lines misc]
4129     (unwind-protect
4130         (let (x)
4131           (narrow-to-region (point) eol)
4132           (unless (eobp)
4133             (forward-char))
4134
4135           (setq header
4136                 (make-full-mail-header
4137                  number                 ; number
4138                  (condition-case ()     ; subject
4139                      (gnus-remove-odd-characters
4140                       (funcall gnus-decode-encoded-word-function
4141                                (setq x (nnheader-nov-field))))
4142                    (error x))
4143                  (condition-case ()     ; from
4144                      (gnus-remove-odd-characters
4145                       (funcall gnus-decode-encoded-word-function
4146                                (setq x (nnheader-nov-field))))
4147                    (error x))
4148                  (nnheader-nov-field)   ; date
4149                  (nnheader-nov-read-message-id number)  ; id
4150                  (setq references (nnheader-nov-field)) ; refs
4151                  (nnheader-nov-read-integer) ; chars
4152                  (nnheader-nov-read-integer) ; lines
4153                  (unless (eobp)
4154                    (if (looking-at "Xref: ")
4155                        (goto-char (match-end 0)))
4156                    (nnheader-nov-field)) ; Xref
4157                  (nnheader-nov-parse-extra)))) ; extra
4158
4159       (widen))
4160
4161     (when (and (string= references "")
4162                (setq in-reply-to (mail-header-extra header))
4163                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4164       (mail-header-set-references
4165        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4166
4167     (when gnus-alter-header-function
4168       (funcall gnus-alter-header-function header))
4169     (gnus-dependencies-add-header header dependencies force-new)))
4170
4171 (defun gnus-build-get-header (id)
4172   "Look through the buffer of NOV lines and find the header to ID.
4173 Enter this line into the dependencies hash table, and return
4174 the id of the parent article (if any)."
4175   (let ((deps gnus-newsgroup-dependencies)
4176         found header)
4177     (prog1
4178         (save-excursion
4179           (set-buffer nntp-server-buffer)
4180           (let ((case-fold-search nil))
4181             (goto-char (point-min))
4182             (while (and (not found)
4183                         (search-forward id nil t))
4184               (beginning-of-line)
4185               (setq found (looking-at
4186                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4187                                    (regexp-quote id))))
4188               (or found (beginning-of-line 2)))
4189             (when found
4190               (beginning-of-line)
4191               (and
4192                (setq header (gnus-nov-parse-line
4193                              (read (current-buffer)) deps))
4194                (gnus-parent-id (mail-header-references header))))))
4195       (when header
4196         (let ((number (mail-header-number header)))
4197           (push number gnus-newsgroup-limit)
4198           (push header gnus-newsgroup-headers)
4199           (if (memq number gnus-newsgroup-unselected)
4200               (progn
4201                 (setq gnus-newsgroup-unreads
4202                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4203                                                number))
4204                 (setq gnus-newsgroup-unselected
4205                       (delq number gnus-newsgroup-unselected)))
4206             (push number gnus-newsgroup-ancient)))))))
4207
4208 (defun gnus-build-all-threads ()
4209   "Read all the headers."
4210   (let ((gnus-summary-ignore-duplicates t)
4211         (mail-parse-charset gnus-newsgroup-charset)
4212         (dependencies gnus-newsgroup-dependencies)
4213         header article)
4214     (save-excursion
4215       (set-buffer nntp-server-buffer)
4216       (let ((case-fold-search nil))
4217         (goto-char (point-min))
4218         (while (not (eobp))
4219           (ignore-errors
4220             (setq article (read (current-buffer))
4221                   header (gnus-nov-parse-line article dependencies)))
4222           (when header
4223             (with-current-buffer gnus-summary-buffer
4224               (push header gnus-newsgroup-headers)
4225               (if (memq (setq article (mail-header-number header))
4226                         gnus-newsgroup-unselected)
4227                   (progn
4228                     (setq gnus-newsgroup-unreads
4229                           (gnus-add-to-sorted-list
4230                            gnus-newsgroup-unreads article))
4231                     (setq gnus-newsgroup-unselected
4232                           (delq article gnus-newsgroup-unselected)))
4233                 (push article gnus-newsgroup-ancient)))
4234             (forward-line 1)))))))
4235
4236 (defun gnus-summary-update-article-line (article header)
4237   "Update the line for ARTICLE using HEADER."
4238   (let* ((id (mail-header-id header))
4239          (thread (gnus-id-to-thread id)))
4240     (unless thread
4241       (error "Article in no thread"))
4242     ;; Update the thread.
4243     (setcar thread header)
4244     (gnus-summary-goto-subject article)
4245     (let* ((datal (gnus-data-find-list article))
4246            (data (car datal))
4247            (buffer-read-only nil)
4248            (level (gnus-summary-thread-level)))
4249       (gnus-delete-line)
4250       (let ((inserted (- (point)
4251                          (progn
4252                            (gnus-summary-insert-line
4253                             header level nil
4254                             (memq article gnus-newsgroup-undownloaded)
4255                             (gnus-article-mark article)
4256                             (memq article gnus-newsgroup-replied)
4257                             (memq article gnus-newsgroup-expirable)
4258                             ;; Only insert the Subject string when it's different
4259                             ;; from the previous Subject string.
4260                             (if (and
4261                                  gnus-show-threads
4262                                  (gnus-subject-equal
4263                                   (condition-case ()
4264                                       (mail-header-subject
4265                                        (gnus-data-header
4266                                         (cadr
4267                                          (gnus-data-find-list
4268                                           article
4269                                           (gnus-data-list t)))))
4270                                     ;; Error on the side of excessive subjects.
4271                                     (error ""))
4272                                   (mail-header-subject header)))
4273                                 ""
4274                               (mail-header-subject header))
4275                             nil (cdr (assq article gnus-newsgroup-scored))
4276                             (memq article gnus-newsgroup-processable))
4277                            (point)))))
4278         (when (cdr datal)
4279           (gnus-data-update-list
4280            (cdr datal)
4281            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4282
4283 (defun gnus-summary-update-article (article &optional iheader)
4284   "Update ARTICLE in the summary buffer."
4285   (set-buffer gnus-summary-buffer)
4286   (let* ((header (gnus-summary-article-header article))
4287          (id (mail-header-id header))
4288          (data (gnus-data-find article))
4289          (thread (gnus-id-to-thread id))
4290          (references (mail-header-references header))
4291          (parent
4292           (gnus-id-to-thread
4293            (or (gnus-parent-id
4294                 (when (and references
4295                            (not (equal "" references)))
4296                   references))
4297                "none")))
4298          (buffer-read-only nil)
4299          (old (car thread)))
4300     (when thread
4301       (unless iheader
4302         (setcar thread nil)
4303         (when parent
4304           (delq thread parent)))
4305       (if (gnus-summary-insert-subject id header)
4306           ;; Set the (possibly) new article number in the data structure.
4307           (gnus-data-set-number data (gnus-id-to-article id))
4308         (setcar thread old)
4309         nil))))
4310
4311 (defun gnus-rebuild-thread (id &optional line)
4312   "Rebuild the thread containing ID.
4313 If LINE, insert the rebuilt thread starting on line LINE."
4314   (let ((buffer-read-only nil)
4315         old-pos current thread data)
4316     (if (not gnus-show-threads)
4317         (setq thread (list (car (gnus-id-to-thread id))))
4318       ;; Get the thread this article is part of.
4319       (setq thread (gnus-remove-thread id)))
4320     (setq old-pos (point-at-bol))
4321     (setq current (save-excursion
4322                     (and (re-search-backward "[\r\n]" nil t)
4323                          (gnus-summary-article-number))))
4324     ;; If this is a gathered thread, we have to go some re-gathering.
4325     (when (stringp (car thread))
4326       (let ((subject (car thread))
4327             roots thr)
4328         (setq thread (cdr thread))
4329         (while thread
4330           (unless (memq (setq thr (gnus-id-to-thread
4331                                    (gnus-root-id
4332                                     (mail-header-id (caar thread)))))
4333                         roots)
4334             (push thr roots))
4335           (setq thread (cdr thread)))
4336         ;; We now have all (unique) roots.
4337         (if (= (length roots) 1)
4338             ;; All the loose roots are now one solid root.
4339             (setq thread (car roots))
4340           (setq thread (cons subject (gnus-sort-threads roots))))))
4341     (let (threads)
4342       ;; We then insert this thread into the summary buffer.
4343       (when line
4344         (goto-char (point-min))
4345         (forward-line (1- line)))
4346       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4347         (if gnus-show-threads
4348             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4349           (gnus-summary-prepare-unthreaded thread))
4350         (setq data (nreverse gnus-newsgroup-data))
4351         (setq threads gnus-newsgroup-threads))
4352       ;; We splice the new data into the data structure.
4353       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4354       ;;!!! then we want to insert at the beginning of the buffer.
4355       ;;!!! That happens to be true with Gnus now, but that may
4356       ;;!!! change in the future.  Perhaps.
4357       (gnus-data-enter-list
4358        (if line nil current) data (- (point) old-pos))
4359       (setq gnus-newsgroup-threads
4360             (nconc threads gnus-newsgroup-threads))
4361       (gnus-data-compute-positions))))
4362
4363 (defun gnus-number-to-header (number)
4364   "Return the header for article NUMBER."
4365   (let ((headers gnus-newsgroup-headers))
4366     (while (and headers
4367                 (not (= number (mail-header-number (car headers)))))
4368       (pop headers))
4369     (when headers
4370       (car headers))))
4371
4372 (defun gnus-parent-headers (in-headers &optional generation)
4373   "Return the headers of the GENERATIONeth parent of HEADERS."
4374   (unless generation
4375     (setq generation 1))
4376   (let ((parent t)
4377         (headers in-headers)
4378         references)
4379     (while (and parent
4380                 (not (zerop generation))
4381                 (setq references (mail-header-references headers)))
4382       (setq headers (if (and references
4383                              (setq parent (gnus-parent-id references)))
4384                         (car (gnus-id-to-thread parent))
4385                       nil))
4386       (decf generation))
4387     (and (not (eq headers in-headers))
4388          headers)))
4389
4390 (defun gnus-id-to-thread (id)
4391   "Return the (sub-)thread where ID appears."
4392   (gnus-gethash id gnus-newsgroup-dependencies))
4393
4394 (defun gnus-id-to-article (id)
4395   "Return the article number of ID."
4396   (let ((thread (gnus-id-to-thread id)))
4397     (when (and thread
4398                (car thread))
4399       (mail-header-number (car thread)))))
4400
4401 (defun gnus-id-to-header (id)
4402   "Return the article headers of ID."
4403   (car (gnus-id-to-thread id)))
4404
4405 (defun gnus-article-displayed-root-p (article)
4406   "Say whether ARTICLE is a root(ish) article."
4407   (let ((level (gnus-summary-thread-level article))
4408         (refs (mail-header-references  (gnus-summary-article-header article)))
4409         particle)
4410     (cond
4411      ((null level) nil)
4412      ((zerop level) t)
4413      ((null refs) t)
4414      ((null (gnus-parent-id refs)) t)
4415      ((and (= 1 level)
4416            (null (setq particle (gnus-id-to-article
4417                                  (gnus-parent-id refs))))
4418            (null (gnus-summary-thread-level particle)))))))
4419
4420 (defun gnus-root-id (id)
4421   "Return the id of the root of the thread where ID appears."
4422   (let (last-id prev)
4423     (while (and id (setq prev (car (gnus-id-to-thread id))))
4424       (setq last-id id
4425             id (gnus-parent-id (mail-header-references prev))))
4426     last-id))
4427
4428 (defun gnus-articles-in-thread (thread)
4429   "Return the list of articles in THREAD."
4430   (cons (mail-header-number (car thread))
4431         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4432
4433 (defun gnus-remove-thread (id &optional dont-remove)
4434   "Remove the thread that has ID in it."
4435   (let (headers thread last-id)
4436     ;; First go up in this thread until we find the root.
4437     (setq last-id (gnus-root-id id)
4438           headers (message-flatten-list (gnus-id-to-thread last-id)))
4439     ;; We have now found the real root of this thread.  It might have
4440     ;; been gathered into some loose thread, so we have to search
4441     ;; through the threads to find the thread we wanted.
4442     (let ((threads gnus-newsgroup-threads)
4443           sub)
4444       (while threads
4445         (setq sub (car threads))
4446         (if (stringp (car sub))
4447             ;; This is a gathered thread, so we look at the roots
4448             ;; below it to find whether this article is in this
4449             ;; gathered root.
4450             (progn
4451               (setq sub (cdr sub))
4452               (while sub
4453                 (when (member (caar sub) headers)
4454                   (setq thread (car threads)
4455                         threads nil
4456                         sub nil))
4457                 (setq sub (cdr sub))))
4458           ;; It's an ordinary thread, so we check it.
4459           (when (eq (car sub) (car headers))
4460             (setq thread sub
4461                   threads nil)))
4462         (setq threads (cdr threads)))
4463       ;; If this article is in no thread, then it's a root.
4464       (if thread
4465           (unless dont-remove
4466             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4467         (setq thread (gnus-id-to-thread last-id)))
4468       (when thread
4469         (prog1
4470             thread                      ; We return this thread.
4471           (unless dont-remove
4472             (if (stringp (car thread))
4473                 (progn
4474                   ;; If we use dummy roots, then we have to remove the
4475                   ;; dummy root as well.
4476                   (when (eq gnus-summary-make-false-root 'dummy)
4477                     ;; We go to the dummy root by going to
4478                     ;; the first sub-"thread", and then one line up.
4479                     (gnus-summary-goto-article
4480                      (mail-header-number (caadr thread)))
4481                     (forward-line -1)
4482                     (gnus-delete-line)
4483                     (gnus-data-compute-positions))
4484                   (setq thread (cdr thread))
4485                   (while thread
4486                     (gnus-remove-thread-1 (car thread))
4487                     (setq thread (cdr thread))))
4488               (gnus-remove-thread-1 thread))))))))
4489
4490 (defun gnus-remove-thread-1 (thread)
4491   "Remove the thread THREAD recursively."
4492   (let ((number (mail-header-number (pop thread)))
4493         d)
4494     (setq thread (reverse thread))
4495     (while thread
4496       (gnus-remove-thread-1 (pop thread)))
4497     (when (setq d (gnus-data-find number))
4498       (goto-char (gnus-data-pos d))
4499       (gnus-summary-show-thread)
4500       (gnus-data-remove
4501        number
4502        (- (point-at-bol)
4503           (prog1
4504               (1+ (point-at-eol))
4505             (gnus-delete-line)))))))
4506
4507 (defun gnus-sort-threads-1 (threads func)
4508   (sort (mapcar (lambda (thread)
4509                   (cons (car thread)
4510                         (and (cdr thread)
4511                              (gnus-sort-threads-1 (cdr thread) func))))
4512                 threads) func))
4513
4514 (defun gnus-sort-threads (threads)
4515   "Sort THREADS."
4516   (if (not gnus-thread-sort-functions)
4517       threads
4518     (gnus-message 8 "Sorting threads...")
4519     (let ((max-lisp-eval-depth 5000))
4520       (prog1 (gnus-sort-threads-1
4521          threads
4522          (gnus-make-sort-function gnus-thread-sort-functions))
4523         (gnus-message 8 "Sorting threads...done")))))
4524
4525 (defun gnus-sort-articles (articles)
4526   "Sort ARTICLES."
4527   (when gnus-article-sort-functions
4528     (gnus-message 7 "Sorting articles...")
4529     (prog1
4530         (setq gnus-newsgroup-headers
4531               (sort articles (gnus-make-sort-function
4532                               gnus-article-sort-functions)))
4533       (gnus-message 7 "Sorting articles...done"))))
4534
4535 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4536 (defmacro gnus-thread-header (thread)
4537   "Return header of first article in THREAD.
4538 Note that THREAD must never, ever be anything else than a variable -
4539 using some other form will lead to serious barfage."
4540   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4541   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4542   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4543         (vector thread) 2))
4544
4545 (defsubst gnus-article-sort-by-number (h1 h2)
4546   "Sort articles by article number."
4547   (< (mail-header-number h1)
4548      (mail-header-number h2)))
4549
4550 (defun gnus-thread-sort-by-number (h1 h2)
4551   "Sort threads by root article number."
4552   (gnus-article-sort-by-number
4553    (gnus-thread-header h1) (gnus-thread-header h2)))
4554
4555 (defsubst gnus-article-sort-by-random (h1 h2)
4556   "Sort articles by article number."
4557   (zerop (random 2)))
4558
4559 (defun gnus-thread-sort-by-random (h1 h2)
4560   "Sort threads by root article number."
4561   (gnus-article-sort-by-random
4562    (gnus-thread-header h1) (gnus-thread-header h2)))
4563
4564 (defsubst gnus-article-sort-by-lines (h1 h2)
4565   "Sort articles by article Lines header."
4566   (< (mail-header-lines h1)
4567      (mail-header-lines h2)))
4568
4569 (defun gnus-thread-sort-by-lines (h1 h2)
4570   "Sort threads by root article Lines header."
4571   (gnus-article-sort-by-lines
4572    (gnus-thread-header h1) (gnus-thread-header h2)))
4573
4574 (defsubst gnus-article-sort-by-chars (h1 h2)
4575   "Sort articles by octet length."
4576   (< (mail-header-chars h1)
4577      (mail-header-chars h2)))
4578
4579 (defun gnus-thread-sort-by-chars (h1 h2)
4580   "Sort threads by root article octet length."
4581   (gnus-article-sort-by-chars
4582    (gnus-thread-header h1) (gnus-thread-header h2)))
4583
4584 (defsubst gnus-article-sort-by-author (h1 h2)
4585   "Sort articles by root author."
4586   (string-lessp
4587    (let ((extract (funcall
4588                    gnus-extract-address-components
4589                    (mail-header-from h1))))
4590      (or (car extract) (cadr extract) ""))
4591    (let ((extract (funcall
4592                    gnus-extract-address-components
4593                    (mail-header-from h2))))
4594      (or (car extract) (cadr extract) ""))))
4595
4596 (defun gnus-thread-sort-by-author (h1 h2)
4597   "Sort threads by root author."
4598   (gnus-article-sort-by-author
4599    (gnus-thread-header h1)  (gnus-thread-header h2)))
4600
4601 (defsubst gnus-article-sort-by-recipient (h1 h2)
4602   "Sort articles by recipient."
4603   (string-lessp
4604    (let ((extract (funcall
4605                    gnus-extract-address-components
4606                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4607      (or (car extract) (cadr extract)))
4608    (let ((extract (funcall
4609                    gnus-extract-address-components
4610                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4611      (or (car extract) (cadr extract)))))
4612
4613 (defun gnus-thread-sort-by-recipient (h1 h2)
4614   "Sort threads by root recipient."
4615   (gnus-article-sort-by-recipient
4616    (gnus-thread-header h1) (gnus-thread-header h2)))
4617
4618 (defsubst gnus-article-sort-by-subject (h1 h2)
4619   "Sort articles by root subject."
4620   (string-lessp
4621    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4622    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4623
4624 (defun gnus-thread-sort-by-subject (h1 h2)
4625   "Sort threads by root subject."
4626   (gnus-article-sort-by-subject
4627    (gnus-thread-header h1) (gnus-thread-header h2)))
4628
4629 (defsubst gnus-article-sort-by-date (h1 h2)
4630   "Sort articles by root article date."
4631   (time-less-p
4632    (gnus-date-get-time (mail-header-date h1))
4633    (gnus-date-get-time (mail-header-date h2))))
4634
4635 (defun gnus-thread-sort-by-date (h1 h2)
4636   "Sort threads by root article date."
4637   (gnus-article-sort-by-date
4638    (gnus-thread-header h1) (gnus-thread-header h2)))
4639
4640 (defsubst gnus-article-sort-by-score (h1 h2)
4641   "Sort articles by root article score.
4642 Unscored articles will be counted as having a score of zero."
4643   (> (or (cdr (assq (mail-header-number h1)
4644                     gnus-newsgroup-scored))
4645          gnus-summary-default-score 0)
4646      (or (cdr (assq (mail-header-number h2)
4647                     gnus-newsgroup-scored))
4648          gnus-summary-default-score 0)))
4649
4650 (defun gnus-thread-sort-by-score (h1 h2)
4651   "Sort threads by root article score."
4652   (gnus-article-sort-by-score
4653    (gnus-thread-header h1) (gnus-thread-header h2)))
4654
4655 (defun gnus-thread-sort-by-total-score (h1 h2)
4656   "Sort threads by the sum of all scores in the thread.
4657 Unscored articles will be counted as having a score of zero."
4658   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4659
4660 (defun gnus-thread-total-score (thread)
4661   ;; This function find the total score of THREAD.
4662   (cond
4663    ((null thread)
4664     0)
4665    ((consp thread)
4666     (if (stringp (car thread))
4667         (apply gnus-thread-score-function 0
4668                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4669       (gnus-thread-total-score-1 thread)))
4670    (t
4671     (gnus-thread-total-score-1 (list thread)))))
4672
4673 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4674   "Sort threads such that the thread with the most recently arrived article comes first."
4675   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4676
4677 (defun gnus-thread-highest-number (thread)
4678   "Return the highest article number in THREAD."
4679   (apply 'max (mapcar (lambda (header)
4680                         (mail-header-number header))
4681                       (message-flatten-list thread))))
4682
4683 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4684   "Sort threads such that the thread with the most recently dated article comes first."
4685   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4686
4687 (defun gnus-thread-latest-date (thread)
4688   "Return the highest article date in THREAD."
4689   (let ((previous-time 0))
4690     (apply 'max
4691            (mapcar
4692             (lambda (header)
4693               (setq previous-time
4694                     (condition-case ()
4695                         (time-to-seconds (mail-header-parse-date
4696                                           (mail-header-date header)))
4697                       (error previous-time))))
4698             (sort
4699              (message-flatten-list thread)
4700              (lambda (h1 h2)
4701                (< (mail-header-number h1)
4702                   (mail-header-number h2))))))))
4703
4704 (defun gnus-thread-total-score-1 (root)
4705   ;; This function find the total score of the thread below ROOT.
4706   (setq root (car root))
4707   (apply gnus-thread-score-function
4708          (or (append
4709               (mapcar 'gnus-thread-total-score
4710                       (cdr (gnus-id-to-thread (mail-header-id root))))
4711               (when (> (mail-header-number root) 0)
4712                 (list (or (cdr (assq (mail-header-number root)
4713                                      gnus-newsgroup-scored))
4714                           gnus-summary-default-score 0))))
4715              (list gnus-summary-default-score)
4716              '(0))))
4717
4718 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4719 (defvar gnus-tmp-prev-subject nil)
4720 (defvar gnus-tmp-false-parent nil)
4721 (defvar gnus-tmp-root-expunged nil)
4722 (defvar gnus-tmp-dummy-line nil)
4723
4724 (eval-when-compile (defvar gnus-tmp-header))
4725 (defun gnus-extra-header (type &optional header)
4726   "Return the extra header of TYPE."
4727   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4728       ""))
4729
4730 (defvar gnus-tmp-thread-tree-header-string "")
4731
4732 (defcustom gnus-sum-thread-tree-root "> "
4733   "With %B spec, used for the root of a thread.
4734 If nil, use subject instead."
4735   :version "22.1"
4736   :type '(radio (const :format "%v  " nil) string)
4737   :group 'gnus-thread)
4738
4739 (defcustom gnus-sum-thread-tree-false-root "> "
4740   "With %B spec, used for a false root of a thread.
4741 If nil, use subject instead."
4742   :version "22.1"
4743   :type '(radio (const :format "%v  " nil) string)
4744   :group 'gnus-thread)
4745
4746 (defcustom gnus-sum-thread-tree-single-indent ""
4747   "With %B spec, used for a thread with just one message.
4748 If nil, use subject instead."
4749   :version "22.1"
4750   :type '(radio (const :format "%v  " nil) string)
4751   :group 'gnus-thread)
4752
4753 (defcustom gnus-sum-thread-tree-vertical "| "
4754   "With %B spec, used for drawing a vertical line."
4755   :version "22.1"
4756   :type 'string
4757   :group 'gnus-thread)
4758
4759 (defcustom gnus-sum-thread-tree-indent "  "
4760   "With %B spec, used for indenting."
4761   :version "22.1"
4762   :type 'string
4763   :group 'gnus-thread)
4764
4765 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4766   "With %B spec, used for a leaf with brothers."
4767   :version "22.1"
4768   :type 'string
4769   :group 'gnus-thread)
4770
4771 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4772   "With %B spec, used for a leaf without brothers."
4773   :version "22.1"
4774   :type 'string
4775   :group 'gnus-thread)
4776
4777 (defun gnus-summary-prepare-threads (threads)
4778   "Prepare summary buffer from THREADS and indentation LEVEL.
4779 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4780 or a straight list of headers."
4781   (gnus-message 7 "Generating summary...")
4782
4783   (setq gnus-newsgroup-threads threads)
4784   (beginning-of-line)
4785
4786   (let ((gnus-tmp-level 0)
4787         (default-score (or gnus-summary-default-score 0))
4788         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4789         (building-line-count gnus-summary-display-while-building)
4790         (building-count (integerp gnus-summary-display-while-building))
4791         thread number subject stack state gnus-tmp-gathered beg-match
4792         new-roots gnus-tmp-new-adopts thread-end simp-subject
4793         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4794         gnus-tmp-replied gnus-tmp-subject-or-nil
4795         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4796         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4797         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4798         tree-stack)
4799
4800     (setq gnus-tmp-prev-subject nil
4801           gnus-tmp-thread-tree-header-string "")
4802
4803     (if (vectorp (car threads))
4804         ;; If this is a straight (sic) list of headers, then a
4805         ;; threaded summary display isn't required, so we just create
4806         ;; an unthreaded one.
4807         (gnus-summary-prepare-unthreaded threads)
4808
4809       ;; Do the threaded display.
4810
4811       (if gnus-summary-display-while-building
4812           (switch-to-buffer (buffer-name)))
4813       (while (or threads stack gnus-tmp-new-adopts new-roots)
4814
4815         (if (and (= gnus-tmp-level 0)
4816                  (or (not stack)
4817                      (= (caar stack) 0))
4818                  (not gnus-tmp-false-parent)
4819                  (or gnus-tmp-new-adopts new-roots))
4820             (if gnus-tmp-new-adopts
4821                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4822                       thread (list (car gnus-tmp-new-adopts))
4823                       gnus-tmp-header (caar thread)
4824                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4825               (when new-roots
4826                 (setq thread (list (car new-roots))
4827                       gnus-tmp-header (caar thread)
4828                       new-roots (cdr new-roots))))
4829
4830           (if threads
4831               ;; If there are some threads, we do them before the
4832               ;; threads on the stack.
4833               (setq thread threads
4834                     gnus-tmp-header (caar thread))
4835             ;; There were no current threads, so we pop something off
4836             ;; the stack.
4837             (setq state (car stack)
4838                   gnus-tmp-level (car state)
4839                   tree-stack (cadr state)
4840                   thread (caddr state)
4841                   stack (cdr stack)
4842                   gnus-tmp-header (caar thread))))
4843
4844         (setq gnus-tmp-false-parent nil)
4845         (setq gnus-tmp-root-expunged nil)
4846         (setq thread-end nil)
4847
4848         (if (stringp gnus-tmp-header)
4849             ;; The header is a dummy root.
4850             (cond
4851              ((eq gnus-summary-make-false-root 'adopt)
4852               ;; We let the first article adopt the rest.
4853               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4854                                                (cddar thread)))
4855               (setq gnus-tmp-gathered
4856                     (nconc (mapcar
4857                             (lambda (h) (mail-header-number (car h)))
4858                             (cddar thread))
4859                            gnus-tmp-gathered))
4860               (setq thread (cons (list (caar thread)
4861                                        (cadar thread))
4862                                  (cdr thread)))
4863               (setq gnus-tmp-level -1
4864                     gnus-tmp-false-parent t))
4865              ((eq gnus-summary-make-false-root 'empty)
4866               ;; We print adopted articles with empty subject fields.
4867               (setq gnus-tmp-gathered
4868                     (nconc (mapcar
4869                             (lambda (h) (mail-header-number (car h)))
4870                             (cddar thread))
4871                            gnus-tmp-gathered))
4872               (setq gnus-tmp-level -1))
4873              ((eq gnus-summary-make-false-root 'dummy)
4874               ;; We remember that we probably want to output a dummy
4875               ;; root.
4876               (setq gnus-tmp-dummy-line gnus-tmp-header)
4877               (setq gnus-tmp-prev-subject gnus-tmp-header))
4878              (t
4879               ;; We do not make a root for the gathered
4880               ;; sub-threads at all.
4881               (setq gnus-tmp-level -1)))
4882
4883           (setq number (mail-header-number gnus-tmp-header)
4884                 subject (mail-header-subject gnus-tmp-header)
4885                 simp-subject (gnus-simplify-subject-fully subject))
4886
4887           (cond
4888            ;; If the thread has changed subject, we might want to make
4889            ;; this subthread into a root.
4890            ((and (null gnus-thread-ignore-subject)
4891                  (not (zerop gnus-tmp-level))
4892                  gnus-tmp-prev-subject
4893                  (not (string= gnus-tmp-prev-subject simp-subject)))
4894             (setq new-roots (nconc new-roots (list (car thread)))
4895                   thread-end t
4896                   gnus-tmp-header nil))
4897            ;; If the article lies outside the current limit,
4898            ;; then we do not display it.
4899            ((not (memq number gnus-newsgroup-limit))
4900             (setq gnus-tmp-gathered
4901                   (nconc (mapcar
4902                           (lambda (h) (mail-header-number (car h)))
4903                           (cdar thread))
4904                          gnus-tmp-gathered))
4905             (setq gnus-tmp-new-adopts (if (cdar thread)
4906                                           (append gnus-tmp-new-adopts
4907                                                   (cdar thread))
4908                                         gnus-tmp-new-adopts)
4909                   thread-end t
4910                   gnus-tmp-header nil)
4911             (when (zerop gnus-tmp-level)
4912               (setq gnus-tmp-root-expunged t)))
4913            ;; Perhaps this article is to be marked as read?
4914            ((and gnus-summary-mark-below
4915                  (< (or (cdr (assq number gnus-newsgroup-scored))
4916                         default-score)
4917                     gnus-summary-mark-below)
4918                  ;; Don't touch sparse articles.
4919                  (not (gnus-summary-article-sparse-p number))
4920                  (not (gnus-summary-article-ancient-p number)))
4921             (setq gnus-newsgroup-unreads
4922                   (delq number gnus-newsgroup-unreads))
4923             (if gnus-newsgroup-auto-expire
4924                 (setq gnus-newsgroup-expirable
4925                       (gnus-add-to-sorted-list
4926                        gnus-newsgroup-expirable number))
4927               (push (cons number gnus-low-score-mark)
4928                     gnus-newsgroup-reads))))
4929
4930           (when gnus-tmp-header
4931             ;; We may have an old dummy line to output before this
4932             ;; article.
4933             (when (and gnus-tmp-dummy-line
4934                        (gnus-subject-equal
4935                         gnus-tmp-dummy-line
4936                         (mail-header-subject gnus-tmp-header)))
4937               (gnus-summary-insert-dummy-line
4938                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4939               (setq gnus-tmp-dummy-line nil))
4940
4941             ;; Compute the mark.
4942             (setq gnus-tmp-unread (gnus-article-mark number))
4943
4944             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4945                                   gnus-tmp-header gnus-tmp-level)
4946                   gnus-newsgroup-data)
4947
4948             ;; Actually insert the line.
4949             (setq
4950              gnus-tmp-subject-or-nil
4951              (cond
4952               ((and gnus-thread-ignore-subject
4953                     gnus-tmp-prev-subject
4954                     (not (string= gnus-tmp-prev-subject simp-subject)))
4955                subject)
4956               ((zerop gnus-tmp-level)
4957                (if (and (eq gnus-summary-make-false-root 'empty)
4958                         (memq number gnus-tmp-gathered)
4959                         gnus-tmp-prev-subject
4960                         (string= gnus-tmp-prev-subject simp-subject))
4961                    gnus-summary-same-subject
4962                  subject))
4963               (t gnus-summary-same-subject)))
4964             (if (and (eq gnus-summary-make-false-root 'adopt)
4965                      (= gnus-tmp-level 1)
4966                      (memq number gnus-tmp-gathered))
4967                 (setq gnus-tmp-opening-bracket ?\<
4968                       gnus-tmp-closing-bracket ?\>)
4969               (setq gnus-tmp-opening-bracket ?\[
4970                     gnus-tmp-closing-bracket ?\]))
4971             (setq
4972              gnus-tmp-indentation
4973              (aref gnus-thread-indent-array gnus-tmp-level)
4974              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4975              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4976                                 gnus-summary-default-score 0)
4977              gnus-tmp-score-char
4978              (if (or (null gnus-summary-default-score)
4979                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4980                          gnus-summary-zcore-fuzz))
4981                  ?                      ;Whitespace
4982                (if (< gnus-tmp-score gnus-summary-default-score)
4983                    gnus-score-below-mark gnus-score-over-mark))
4984              gnus-tmp-replied
4985              (cond ((memq number gnus-newsgroup-processable)
4986                     gnus-process-mark)
4987                    ((memq number gnus-newsgroup-cached)
4988                     gnus-cached-mark)
4989                    ((memq number gnus-newsgroup-replied)
4990                     gnus-replied-mark)
4991                    ((memq number gnus-newsgroup-forwarded)
4992                     gnus-forwarded-mark)
4993                    ((memq number gnus-newsgroup-saved)
4994                     gnus-saved-mark)
4995                    ((memq number gnus-newsgroup-recent)
4996                     gnus-recent-mark)
4997                    ((memq number gnus-newsgroup-unseen)
4998                     gnus-unseen-mark)
4999                    (t gnus-no-mark))
5000              gnus-tmp-downloaded
5001              (cond ((memq number gnus-newsgroup-undownloaded)
5002                     gnus-undownloaded-mark)
5003                    (gnus-newsgroup-agentized
5004                     gnus-downloaded-mark)
5005                    (t
5006                     gnus-no-mark))
5007              gnus-tmp-from (mail-header-from gnus-tmp-header)
5008              gnus-tmp-name
5009              (cond
5010               ((string-match "<[^>]+> *$" gnus-tmp-from)
5011                (setq beg-match (match-beginning 0))
5012                (or (and (string-match "^\".+\"" gnus-tmp-from)
5013                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5014                    (substring gnus-tmp-from 0 beg-match)))
5015               ((string-match "(.+)" gnus-tmp-from)
5016                (substring gnus-tmp-from
5017                           (1+ (match-beginning 0)) (1- (match-end 0))))
5018               (t gnus-tmp-from))
5019
5020              ;; Do the %B string
5021              gnus-tmp-thread-tree-header-string
5022              (cond
5023               ((not gnus-show-threads) "")
5024               ((zerop gnus-tmp-level)
5025                (cond ((cdar thread)
5026                       (or gnus-sum-thread-tree-root subject))
5027                      (gnus-tmp-new-adopts
5028                       (or gnus-sum-thread-tree-false-root subject))
5029                      (t
5030                       (or gnus-sum-thread-tree-single-indent subject))))
5031               (t
5032                (concat (apply 'concat
5033                               (mapcar (lambda (item)
5034                                         (if (= item 1)
5035                                             gnus-sum-thread-tree-vertical
5036                                           gnus-sum-thread-tree-indent))
5037                                       (cdr (reverse tree-stack))))
5038                        (if (nth 1 thread)
5039                            gnus-sum-thread-tree-leaf-with-other
5040                          gnus-sum-thread-tree-single-leaf)))))
5041             (when (string= gnus-tmp-name "")
5042               (setq gnus-tmp-name gnus-tmp-from))
5043             (unless (numberp gnus-tmp-lines)
5044               (setq gnus-tmp-lines -1))
5045             (if (= gnus-tmp-lines -1)
5046                 (setq gnus-tmp-lines "?")
5047               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5048               (gnus-put-text-property
5049              (point)
5050              (progn (eval gnus-summary-line-format-spec) (point))
5051                'gnus-number number)
5052             (when gnus-visual-p
5053               (forward-line -1)
5054               (gnus-run-hooks 'gnus-summary-update-hook)
5055               (forward-line 1))
5056
5057             (setq gnus-tmp-prev-subject simp-subject)))
5058
5059         (when (nth 1 thread)
5060           (push (list (max 0 gnus-tmp-level)
5061                       (copy-sequence tree-stack)
5062                       (nthcdr 1 thread))
5063                 stack))
5064         (push (if (nth 1 thread) 1 0) tree-stack)
5065         (incf gnus-tmp-level)
5066         (setq threads (if thread-end nil (cdar thread)))
5067         (if gnus-summary-display-while-building
5068             (if building-count
5069                 (progn
5070                   ;; use a set frequency
5071                   (setq building-line-count (1- building-line-count))
5072                   (when (= building-line-count 0)
5073                     (sit-for 0)
5074                     (setq building-line-count
5075                           gnus-summary-display-while-building)))
5076               ;; always
5077               (sit-for 0)))
5078         (unless threads
5079           (setq gnus-tmp-level 0)))))
5080   (gnus-message 7 "Generating summary...done"))
5081
5082 (defun gnus-summary-prepare-unthreaded (headers)
5083   "Generate an unthreaded summary buffer based on HEADERS."
5084   (let (header number mark)
5085
5086     (beginning-of-line)
5087
5088     (while headers
5089       ;; We may have to root out some bad articles...
5090       (when (memq (setq number (mail-header-number
5091                                 (setq header (pop headers))))
5092                   gnus-newsgroup-limit)
5093         ;; Mark article as read when it has a low score.
5094         (when (and gnus-summary-mark-below
5095                    (< (or (cdr (assq number gnus-newsgroup-scored))
5096                           gnus-summary-default-score 0)
5097                       gnus-summary-mark-below)
5098                    (not (gnus-summary-article-ancient-p number)))
5099           (setq gnus-newsgroup-unreads
5100                 (delq number gnus-newsgroup-unreads))
5101           (if gnus-newsgroup-auto-expire
5102               (push number gnus-newsgroup-expirable)
5103             (push (cons number gnus-low-score-mark)
5104                   gnus-newsgroup-reads)))
5105
5106         (setq mark (gnus-article-mark number))
5107         (push (gnus-data-make number mark (1+ (point)) header 0)
5108               gnus-newsgroup-data)
5109         (gnus-summary-insert-line
5110          header 0 number
5111          (memq number gnus-newsgroup-undownloaded)
5112          mark (memq number gnus-newsgroup-replied)
5113          (memq number gnus-newsgroup-expirable)
5114          (mail-header-subject header) nil
5115          (cdr (assq number gnus-newsgroup-scored))
5116          (memq number gnus-newsgroup-processable))))))
5117
5118 (defun gnus-summary-remove-list-identifiers ()
5119   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5120   (let ((regexp (if (consp gnus-list-identifiers)
5121                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5122                   gnus-list-identifiers))
5123         changed subject)
5124     (when regexp
5125       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5126       (dolist (header gnus-newsgroup-headers)
5127         (setq subject (mail-header-subject header)
5128               changed nil)
5129         (while (string-match regexp subject)
5130           (setq subject
5131                 (concat (substring subject 0 (match-beginning 1))
5132                         (substring subject (match-end 0)))
5133                 changed t))
5134         (when changed
5135           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5136             (setq subject
5137                   (concat (substring subject 0 (match-beginning 1))
5138                           (substring subject (match-end 1)))))
5139           (mail-header-set-subject header subject))))))
5140
5141 (defun gnus-fetch-headers (articles)
5142   "Fetch headers of ARTICLES."
5143   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5144     (gnus-message 5 "Fetching headers for %s..." name)
5145     (prog1
5146         (if (eq 'nov
5147                 (setq gnus-headers-retrieved-by
5148                       (gnus-retrieve-headers
5149                        articles gnus-newsgroup-name
5150                        ;; We might want to fetch old headers, but
5151                        ;; not if there is only 1 article.
5152                        (and (or (and
5153                                  (not (eq gnus-fetch-old-headers 'some))
5154                                  (not (numberp gnus-fetch-old-headers)))
5155                                 (> (length articles) 1))
5156                             gnus-fetch-old-headers))))
5157             (gnus-get-newsgroup-headers-xover
5158              articles nil nil gnus-newsgroup-name t)
5159           (gnus-get-newsgroup-headers))
5160       (gnus-message 5 "Fetching headers for %s...done" name))))
5161
5162 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5163   "Select newsgroup GROUP.
5164 If READ-ALL is non-nil, all articles in the group are selected.
5165 If SELECT-ARTICLES, only select those articles from GROUP."
5166   (let* ((entry (gnus-group-entry group))
5167          ;;!!! Dirty hack; should be removed.
5168          (gnus-summary-ignore-duplicates
5169           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5170               t
5171             gnus-summary-ignore-duplicates))
5172          (info (nth 2 entry))
5173          articles fetched-articles cached)
5174
5175     (unless (gnus-check-server
5176              (set (make-local-variable 'gnus-current-select-method)
5177                   (gnus-find-method-for-group group)))
5178       (error "Couldn't open server"))
5179
5180     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5181         (gnus-activate-group group)     ; Or we can activate it...
5182         (progn                          ; Or we bug out.
5183           (when (equal major-mode 'gnus-summary-mode)
5184             (gnus-kill-buffer (current-buffer)))
5185           (error "Couldn't activate group %s: %s"
5186                  group (gnus-status-message group))))
5187
5188     (unless (gnus-request-group group t)
5189       (when (equal major-mode 'gnus-summary-mode)
5190         (gnus-kill-buffer (current-buffer)))
5191       (error "Couldn't request group %s: %s"
5192              group (gnus-status-message group)))
5193
5194     (when gnus-agent
5195       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5196
5197       (setq gnus-summary-use-undownloaded-faces
5198             (gnus-agent-find-parameter
5199              group
5200              'agent-enable-undownloaded-faces)))
5201
5202     (setq gnus-newsgroup-name group
5203           gnus-newsgroup-unselected nil
5204           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5205
5206     (let ((display (gnus-group-find-parameter group 'display)))
5207       (setq gnus-newsgroup-display
5208             (cond
5209              ((not (zerop (or (car-safe read-all) 0)))
5210               ;; The user entered the group with C-u SPC/RET, let's show
5211               ;; all articles.
5212               'gnus-not-ignore)
5213              ((eq display 'all)
5214               'gnus-not-ignore)
5215              ((arrayp display)
5216               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5217              ((numberp display)
5218               ;; The following is probably the "correct" solution, but
5219               ;; it makes Gnus fetch all headers and then limit the
5220               ;; articles (which is slow), so instead we hack the
5221               ;; select-articles parameter instead. -- Simon Josefsson
5222               ;; <jas@kth.se>
5223               ;;
5224               ;; (gnus-byte-compile
5225               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5226               ;;                         display)))))
5227               (setq select-articles
5228                     (gnus-uncompress-range
5229                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5230                              (if (> tmp 0)
5231                                  tmp
5232                                1))
5233                            (cdr (gnus-active group)))))
5234               nil)
5235              (t
5236               nil))))
5237
5238     (gnus-summary-setup-default-charset)
5239
5240     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5241     (when (gnus-virtual-group-p group)
5242       (setq cached gnus-newsgroup-cached))
5243
5244     (setq gnus-newsgroup-unreads
5245           (gnus-sorted-ndifference
5246            (gnus-sorted-ndifference gnus-newsgroup-unreads
5247                                     gnus-newsgroup-marked)
5248            gnus-newsgroup-dormant))
5249
5250     (setq gnus-newsgroup-processable nil)
5251
5252     (gnus-update-read-articles group gnus-newsgroup-unreads)
5253
5254     ;; Adjust and set lists of article marks.
5255     (when info
5256       (gnus-adjust-marked-articles info))
5257     (if (setq articles select-articles)
5258         (setq gnus-newsgroup-unselected
5259               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5260       (setq articles (gnus-articles-to-read group read-all)))
5261
5262     (cond
5263      ((null articles)
5264       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5265       'quit)
5266      ((eq articles 0) nil)
5267      (t
5268       ;; Init the dependencies hash table.
5269       (setq gnus-newsgroup-dependencies
5270             (gnus-make-hashtable (length articles)))
5271       (gnus-set-global-variables)
5272       ;; Retrieve the headers and read them in.
5273
5274       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5275
5276       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5277       (when cached
5278         (setq gnus-newsgroup-cached cached))
5279
5280       ;; Suppress duplicates?
5281       (when gnus-suppress-duplicates
5282         (gnus-dup-suppress-articles))
5283
5284       ;; Set the initial limit.
5285       (setq gnus-newsgroup-limit (copy-sequence articles))
5286       ;; Remove canceled articles from the list of unread articles.
5287       (setq fetched-articles
5288             (mapcar (lambda (headers) (mail-header-number headers))
5289                     gnus-newsgroup-headers))
5290       (setq gnus-newsgroup-articles fetched-articles)
5291       (setq gnus-newsgroup-unreads
5292             (gnus-sorted-nintersection
5293              gnus-newsgroup-unreads fetched-articles))
5294       (gnus-compute-unseen-list)
5295
5296       ;; Removed marked articles that do not exist.
5297       (gnus-update-missing-marks
5298        (gnus-sorted-difference articles fetched-articles))
5299       ;; We might want to build some more threads first.
5300       (when (and gnus-fetch-old-headers
5301                  (eq gnus-headers-retrieved-by 'nov))
5302         (if (eq gnus-fetch-old-headers 'invisible)
5303             (gnus-build-all-threads)
5304           (gnus-build-old-threads)))
5305       ;; Let the Gnus agent mark articles as read.
5306       (when gnus-agent
5307         (gnus-agent-get-undownloaded-list))
5308       ;; Remove list identifiers from subject
5309       (when gnus-list-identifiers
5310         (gnus-summary-remove-list-identifiers))
5311       ;; Check whether auto-expire is to be done in this group.
5312       (setq gnus-newsgroup-auto-expire
5313             (gnus-group-auto-expirable-p group))
5314       ;; Set up the article buffer now, if necessary.
5315       (unless gnus-single-article-buffer
5316         (gnus-article-setup-buffer))
5317       ;; First and last article in this newsgroup.
5318       (when gnus-newsgroup-headers
5319         (setq gnus-newsgroup-begin
5320               (mail-header-number (car gnus-newsgroup-headers))
5321               gnus-newsgroup-end
5322               (mail-header-number
5323                (gnus-last-element gnus-newsgroup-headers))))
5324       ;; GROUP is successfully selected.
5325       (or gnus-newsgroup-headers t)))))
5326
5327 (defun gnus-compute-unseen-list ()
5328   ;; The `seen' marks are treated specially.
5329   (if (not gnus-newsgroup-seen)
5330       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5331     (setq gnus-newsgroup-unseen
5332           (gnus-inverse-list-range-intersection
5333            gnus-newsgroup-articles gnus-newsgroup-seen))))
5334
5335 (defun gnus-summary-display-make-predicate (display)
5336   (require 'gnus-agent)
5337   (when (= (length display) 1)
5338     (setq display (car display)))
5339   (unless gnus-summary-display-cache
5340     (dolist (elem (append '((unread . unread)
5341                             (read . read)
5342                             (unseen . unseen))
5343                           gnus-article-mark-lists))
5344       (push (cons (cdr elem)
5345                   (gnus-byte-compile
5346                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5347             gnus-summary-display-cache)))
5348   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5349         (gnus-category-predicate-cache gnus-summary-display-cache))
5350     (gnus-get-predicate display)))
5351
5352 ;; Uses the dynamically bound `number' variable.
5353 (eval-when-compile
5354   (defvar number))
5355 (defun gnus-article-marked-p (type &optional article)
5356   (let ((article (or article number)))
5357     (cond
5358      ((eq type 'tick)
5359       (memq article gnus-newsgroup-marked))
5360      ((eq type 'spam)
5361       (memq article gnus-newsgroup-spam-marked))
5362      ((eq type 'unsend)
5363       (memq article gnus-newsgroup-unsendable))
5364      ((eq type 'undownload)
5365       (memq article gnus-newsgroup-undownloaded))
5366      ((eq type 'download)
5367       (memq article gnus-newsgroup-downloadable))
5368      ((eq type 'unread)
5369       (memq article gnus-newsgroup-unreads))
5370      ((eq type 'read)
5371       (memq article gnus-newsgroup-reads))
5372      ((eq type 'dormant)
5373       (memq article gnus-newsgroup-dormant) )
5374      ((eq type 'expire)
5375       (memq article gnus-newsgroup-expirable))
5376      ((eq type 'reply)
5377       (memq article gnus-newsgroup-replied))
5378      ((eq type 'killed)
5379       (memq article gnus-newsgroup-killed))
5380      ((eq type 'bookmark)
5381       (assq article gnus-newsgroup-bookmarks))
5382      ((eq type 'score)
5383       (assq article gnus-newsgroup-scored))
5384      ((eq type 'save)
5385       (memq article gnus-newsgroup-saved))
5386      ((eq type 'cache)
5387       (memq article gnus-newsgroup-cached))
5388      ((eq type 'forward)
5389       (memq article gnus-newsgroup-forwarded))
5390      ((eq type 'seen)
5391       (not (memq article gnus-newsgroup-unseen)))
5392      ((eq type 'recent)
5393       (memq article gnus-newsgroup-recent))
5394      (t t))))
5395
5396 (defun gnus-articles-to-read (group &optional read-all)
5397   "Find out what articles the user wants to read."
5398   (let* ((articles
5399           ;; Select all articles if `read-all' is non-nil, or if there
5400           ;; are no unread articles.
5401           (if (or read-all
5402                   (and (zerop (length gnus-newsgroup-marked))
5403                        (zerop (length gnus-newsgroup-unreads)))
5404                   ;; Fetch all if the predicate is non-nil.
5405                   gnus-newsgroup-display)
5406               ;; We want to select the headers for all the articles in
5407               ;; the group, so we select either all the active
5408               ;; articles in the group, or (if that's nil), the
5409               ;; articles in the cache.
5410               (or
5411                (gnus-uncompress-range (gnus-active group))
5412                (gnus-cache-articles-in-group group))
5413             ;; Select only the "normal" subset of articles.
5414             (gnus-sorted-nunion
5415              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5416              gnus-newsgroup-unreads)))
5417          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5418          (scored (length scored-list))
5419          (number (length articles))
5420          (marked (+ (length gnus-newsgroup-marked)
5421                     (length gnus-newsgroup-dormant)))
5422          (select
5423           (cond
5424            ((numberp read-all)
5425             read-all)
5426            ((numberp gnus-newsgroup-display)
5427             gnus-newsgroup-display)
5428            (t
5429             (condition-case ()
5430                 (cond
5431                  ((and (or (<= scored marked) (= scored number))
5432                        (numberp gnus-large-newsgroup)
5433                        (> number gnus-large-newsgroup))
5434                   (let* ((cursor-in-echo-area nil)
5435                          (initial (gnus-parameter-large-newsgroup-initial
5436                                    gnus-newsgroup-name))
5437                          (input
5438                           (read-string
5439                            (format
5440                             "How many articles from %s (%s %d): "
5441                             (gnus-limit-string
5442                              (gnus-group-decoded-name gnus-newsgroup-name)
5443                              35)
5444                             (if initial "max" "default")
5445                             number)
5446                            (if initial
5447                                (cons (number-to-string initial)
5448                                      0)))))
5449                     (if (string-match "^[ \t]*$" input) number input)))
5450                  ((and (> scored marked) (< scored number)
5451                        (> (- scored number) 20))
5452                   (let ((input
5453                          (read-string
5454                           (format "%s %s (%d scored, %d total): "
5455                                   "How many articles from"
5456                                   (gnus-group-decoded-name group)
5457                                   scored number))))
5458                     (if (string-match "^[ \t]*$" input)
5459                         number input)))
5460                  (t number))
5461               (quit
5462                (message "Quit getting the articles to read")
5463                nil))))))
5464     (setq select (if (stringp select) (string-to-number select) select))
5465     (if (or (null select) (zerop select))
5466         select
5467       (if (and (not (zerop scored)) (<= (abs select) scored))
5468           (progn
5469             (setq articles (sort scored-list '<))
5470             (setq number (length articles)))
5471         (setq articles (copy-sequence articles)))
5472
5473       (when (< (abs select) number)
5474         (if (< select 0)
5475             ;; Select the N oldest articles.
5476             (setcdr (nthcdr (1- (abs select)) articles) nil)
5477           ;; Select the N most recent articles.
5478           (setq articles (nthcdr (- number select) articles))))
5479       (setq gnus-newsgroup-unselected
5480             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5481       (when gnus-alter-articles-to-read-function
5482         (setq articles
5483               (sort
5484                (funcall gnus-alter-articles-to-read-function
5485                         gnus-newsgroup-name articles)
5486                '<)))
5487       articles)))
5488
5489 (defun gnus-killed-articles (killed articles)
5490   (let (out)
5491     (while articles
5492       (when (inline (gnus-member-of-range (car articles) killed))
5493         (push (car articles) out))
5494       (setq articles (cdr articles)))
5495     out))
5496
5497 (defun gnus-uncompress-marks (marks)
5498   "Uncompress the mark ranges in MARKS."
5499   (let ((uncompressed '(score bookmark))
5500         out)
5501     (while marks
5502       (if (memq (caar marks) uncompressed)
5503           (push (car marks) out)
5504         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5505       (setq marks (cdr marks)))
5506     out))
5507
5508 (defun gnus-article-mark-to-type (mark)
5509   "Return the type of MARK."
5510   (or (cadr (assq mark gnus-article-special-mark-lists))
5511       'list))
5512
5513 (defun gnus-article-unpropagatable-p (mark)
5514   "Return whether MARK should be propagated to back end."
5515   (memq mark gnus-article-unpropagated-mark-lists))
5516
5517 (defun gnus-adjust-marked-articles (info)
5518   "Set all article lists and remove all marks that are no longer valid."
5519   (let* ((marked-lists (gnus-info-marks info))
5520          (active (gnus-active (gnus-info-group info)))
5521          (min (car active))
5522          (max (cdr active))
5523          (types gnus-article-mark-lists)
5524          marks var articles article mark mark-type
5525          bgn end)
5526
5527     (dolist (marks marked-lists)
5528       (setq mark (car marks)
5529             mark-type (gnus-article-mark-to-type mark)
5530             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5531
5532       ;; We set the variable according to the type of the marks list,
5533       ;; and then adjust the marks to a subset of the active articles.
5534       (cond
5535        ;; Adjust "simple" lists - compressed yet unsorted
5536        ((eq mark-type 'list)
5537         ;; Simultaneously uncompress and clip to active range
5538         ;; See gnus-uncompress-range for a description of possible marks
5539         (let (l lh)
5540           (if (not (cadr marks))
5541               (set var nil)
5542             (setq articles (if (numberp (cddr marks))
5543                                (list (cdr marks))
5544                              (cdr marks))
5545                   lh (cons nil nil)
5546                   l lh)
5547
5548             (while (setq article (pop articles))
5549               (cond ((consp article)
5550                      (setq bgn (max (car article) min)
5551                            end (min (cdr article) max))
5552                      (while (<= bgn end)
5553                        (setq l (setcdr l (cons bgn nil))
5554                              bgn (1+ bgn))))
5555                     ((and (<= min article)
5556                           (>= max article))
5557                      (setq l (setcdr l (cons article nil))))))
5558             (set var (cdr lh)))))
5559        ;; Adjust assocs.
5560        ((eq mark-type 'tuple)
5561         (set var (setq articles (cdr marks)))
5562         (when (not (listp (cdr (symbol-value var))))
5563           (set var (list (symbol-value var))))
5564         (when (not (listp (cdr articles)))
5565           (setq articles (list articles)))
5566         (while articles
5567           (when (or (not (consp (setq article (pop articles))))
5568                     (< (car article) min)
5569                     (> (car article) max))
5570             (set var (delq article (symbol-value var))))))
5571        ;; Adjust ranges (sloppily).
5572        ((eq mark-type 'range)
5573         (cond
5574          ((eq mark 'seen)
5575           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5576           ;; It should be (seen (NUM1 . NUM2)).
5577           (when (numberp (cddr marks))
5578             (setcdr marks (list (cdr marks))))
5579           (setq articles (cdr marks))
5580           (while (and articles
5581                       (or (and (consp (car articles))
5582                                (> min (cdar articles)))
5583                           (and (numberp (car articles))
5584                                (> min (car articles)))))
5585             (pop articles))
5586           (set var articles))))))))
5587
5588 (defun gnus-update-missing-marks (missing)
5589   "Go through the list of MISSING articles and remove them from the mark lists."
5590   (when missing
5591     (let (var m)
5592       ;; Go through all types.
5593       (dolist (elem gnus-article-mark-lists)
5594         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5595           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5596           (when (symbol-value var)
5597             ;; This list has articles.  So we delete all missing
5598             ;; articles from it.
5599             (setq m missing)
5600             (while m
5601               (set var (delq (pop m) (symbol-value var))))))))))
5602
5603 (defun gnus-update-marks ()
5604   "Enter the various lists of marked articles into the newsgroup info list."
5605   (let ((types gnus-article-mark-lists)
5606         (info (gnus-get-info gnus-newsgroup-name))
5607         type list newmarked symbol delta-marks)
5608     (when info
5609       ;; Add all marks lists to the list of marks lists.
5610       (while (setq type (pop types))
5611         (setq list (symbol-value
5612                     (setq symbol
5613                           (intern (format "gnus-newsgroup-%s" (car type))))))
5614
5615         (when list
5616           ;; Get rid of the entries of the articles that have the
5617           ;; default score.
5618           (when (and (eq (cdr type) 'score)
5619                      gnus-save-score
5620                      list)
5621             (let* ((arts list)
5622                    (prev (cons nil list))
5623                    (all prev))
5624               (while arts
5625                 (if (or (not (consp (car arts)))
5626                         (= (cdar arts) gnus-summary-default-score))
5627                     (setcdr prev (cdr arts))
5628                   (setq prev arts))
5629                 (setq arts (cdr arts)))
5630               (setq list (cdr all)))))
5631
5632         (when (eq (cdr type) 'seen)
5633           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5634
5635         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5636           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5637
5638         (when (and (gnus-check-backend-function
5639                     'request-set-mark gnus-newsgroup-name)
5640                    (not (gnus-article-unpropagatable-p (cdr type))))
5641           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5642                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5643                  (add (gnus-remove-from-range
5644                        (gnus-copy-sequence list) old)))
5645             (when add
5646               (push (list add 'add (list (cdr type))) delta-marks))
5647             (when del
5648               (push (list del 'del (list (cdr type))) delta-marks))))
5649
5650         (when list
5651           (push (cons (cdr type) list) newmarked)))
5652
5653       (when delta-marks
5654         (unless (gnus-check-group gnus-newsgroup-name)
5655           (error "Can't open server for %s" gnus-newsgroup-name))
5656         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5657
5658       ;; Enter these new marks into the info of the group.
5659       (if (nthcdr 3 info)
5660           (setcar (nthcdr 3 info) newmarked)
5661         ;; Add the marks lists to the end of the info.
5662         (when newmarked
5663           (setcdr (nthcdr 2 info) (list newmarked))))
5664
5665       ;; Cut off the end of the info if there's nothing else there.
5666       (let ((i 5))
5667         (while (and (> i 2)
5668                     (not (nth i info)))
5669           (when (nthcdr (decf i) info)
5670             (setcdr (nthcdr i info) nil)))))))
5671
5672 (defun gnus-set-mode-line (where)
5673   "Set the mode line of the article or summary buffers.
5674 If WHERE is `summary', the summary mode line format will be used."
5675   ;; Is this mode line one we keep updated?
5676   (when (and (memq where gnus-updated-mode-lines)
5677              (symbol-value
5678               (intern (format "gnus-%s-mode-line-format-spec" where))))
5679     (let (mode-string)
5680       (save-excursion
5681         ;; We evaluate this in the summary buffer since these
5682         ;; variables are buffer-local to that buffer.
5683         (set-buffer gnus-summary-buffer)
5684        ;; We bind all these variables that are used in the `eval' form
5685         ;; below.
5686         (let* ((mformat (symbol-value
5687                          (intern
5688                           (format "gnus-%s-mode-line-format-spec" where))))
5689                (gnus-tmp-group-name (gnus-group-decoded-name
5690                                      gnus-newsgroup-name))
5691                (gnus-tmp-article-number (or gnus-current-article 0))
5692                (gnus-tmp-unread gnus-newsgroup-unreads)
5693                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5694                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5695                (gnus-tmp-unread-and-unselected
5696                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5697                             (zerop gnus-tmp-unselected))
5698                        "")
5699                       ((zerop gnus-tmp-unselected)
5700                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5701                       (t (format "{%d(+%d) more}"
5702                                  gnus-tmp-unread-and-unticked
5703                                  gnus-tmp-unselected))))
5704                (gnus-tmp-subject
5705                 (if (and gnus-current-headers
5706                          (vectorp gnus-current-headers))
5707                     (gnus-mode-string-quote
5708                      (mail-header-subject gnus-current-headers))
5709                   ""))
5710                bufname-length max-len
5711                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5712           (setq mode-string (eval mformat))
5713           (setq bufname-length (if (string-match "%b" mode-string)
5714                                    (- (length
5715                                        (buffer-name
5716                                         (if (eq where 'summary)
5717                                             nil
5718                                           (get-buffer gnus-article-buffer))))
5719                                       2)
5720                                  0))
5721           (setq max-len (max 4 (if gnus-mode-non-string-length
5722                                    (- (window-width)
5723                                       gnus-mode-non-string-length
5724                                       bufname-length)
5725                                  (length mode-string))))
5726           ;; We might have to chop a bit of the string off...
5727           (when (> (length mode-string) max-len)
5728             (setq mode-string
5729                   (concat (truncate-string-to-width mode-string (- max-len 3))
5730                           "...")))
5731           ;; Pad the mode string a bit.
5732           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5733       ;; Update the mode line.
5734       (setq mode-line-buffer-identification
5735             (gnus-mode-line-buffer-identification (list mode-string)))
5736       (set-buffer-modified-p t))))
5737
5738 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5739   "Go through the HEADERS list and add all Xrefs to a hash table.
5740 The resulting hash table is returned, or nil if no Xrefs were found."
5741   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5742          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5743          (xref-hashtb (gnus-make-hashtable))
5744          start group entry number xrefs header)
5745     (while headers
5746       (setq header (pop headers))
5747       (when (and (setq xrefs (mail-header-xref header))
5748                  (not (memq (setq number (mail-header-number header))
5749                             unreads)))
5750         (setq start 0)
5751         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5752           (setq start (match-end 0))
5753           (setq group (if prefix
5754                           (concat prefix (substring xrefs (match-beginning 1)
5755                                                     (match-end 1)))
5756                         (substring xrefs (match-beginning 1) (match-end 1))))
5757           (setq number
5758                 (string-to-number (substring xrefs (match-beginning 2)
5759                                           (match-end 2))))
5760           (if (setq entry (gnus-gethash group xref-hashtb))
5761               (setcdr entry (cons number (cdr entry)))
5762             (gnus-sethash group (cons number nil) xref-hashtb)))))
5763     (and start xref-hashtb)))
5764
5765 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5766   "Look through all the headers and mark the Xrefs as read."
5767   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5768         name info xref-hashtb idlist method nth4)
5769     (save-excursion
5770       (set-buffer gnus-group-buffer)
5771       (when (setq xref-hashtb
5772                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5773         (mapatoms
5774          (lambda (group)
5775            (unless (string= from-newsgroup (setq name (symbol-name group)))
5776              (setq idlist (symbol-value group))
5777              ;; Dead groups are not updated.
5778              (and (prog1
5779                       (setq info (gnus-get-info name))
5780                     (when (stringp (setq nth4 (gnus-info-method info)))
5781                       (setq nth4 (gnus-server-to-method nth4))))
5782                   ;; Only do the xrefs if the group has the same
5783                   ;; select method as the group we have just read.
5784                   (or (gnus-methods-equal-p
5785                        nth4 (gnus-find-method-for-group from-newsgroup))
5786                       virtual
5787                       (equal nth4 (setq method (gnus-find-method-for-group
5788                                                 from-newsgroup)))
5789                       (and (equal (car nth4) (car method))
5790                            (equal (nth 1 nth4) (nth 1 method))))
5791                   gnus-use-cross-reference
5792                   (or (not (eq gnus-use-cross-reference t))
5793                       virtual
5794                       ;; Only do cross-references on subscribed
5795                       ;; groups, if that is what is wanted.
5796                       (<= (gnus-info-level info) gnus-level-subscribed))
5797                   (gnus-group-make-articles-read name idlist))))
5798          xref-hashtb)))))
5799
5800 (defun gnus-compute-read-articles (group articles)
5801   (let* ((entry (gnus-group-entry group))
5802          (info (nth 2 entry))
5803          (active (gnus-active group))
5804          ninfo)
5805     (when entry
5806       ;; First peel off all invalid article numbers.
5807       (when active
5808         (let ((ids articles)
5809               id first)
5810           (while (setq id (pop ids))
5811             (when (and first (> id (cdr active)))
5812               ;; We'll end up in this situation in one particular
5813               ;; obscure situation.  If you re-scan a group and get
5814               ;; a new article that is cross-posted to a different
5815               ;; group that has not been re-scanned, you might get
5816               ;; crossposted article that has a higher number than
5817               ;; Gnus believes possible.  So we re-activate this
5818               ;; group as well.  This might mean doing the
5819               ;; crossposting thingy will *increase* the number
5820               ;; of articles in some groups.  Tsk, tsk.
5821               (setq active (or (gnus-activate-group group) active)))
5822             (when (or (> id (cdr active))
5823                       (< id (car active)))
5824               (setq articles (delq id articles))))))
5825       ;; If the read list is nil, we init it.
5826       (if (and active
5827                (null (gnus-info-read info))
5828                (> (car active) 1))
5829           (setq ninfo (cons 1 (1- (car active))))
5830         (setq ninfo (gnus-info-read info)))
5831       ;; Then we add the read articles to the range.
5832       (gnus-add-to-range
5833        ninfo (setq articles (sort articles '<))))))
5834
5835 (defun gnus-group-make-articles-read (group articles)
5836   "Update the info of GROUP to say that ARTICLES are read."
5837   (let* ((num 0)
5838          (entry (gnus-group-entry group))
5839          (info (nth 2 entry))
5840          (active (gnus-active group))
5841          range)
5842     (when entry
5843       (setq range (gnus-compute-read-articles group articles))
5844       (with-current-buffer gnus-group-buffer
5845         (gnus-undo-register
5846           `(progn
5847              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5848              (gnus-info-set-read ',info ',(gnus-info-read info))
5849              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5850              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5851              (gnus-group-update-group ,group t))))
5852       ;; Add the read articles to the range.
5853       (gnus-info-set-read info range)
5854       (gnus-request-set-mark group (list (list range 'add '(read))))
5855       ;; Then we have to re-compute how many unread
5856       ;; articles there are in this group.
5857       (when active
5858         (cond
5859          ((not range)
5860           (setq num (- (1+ (cdr active)) (car active))))
5861          ((not (listp (cdr range)))
5862           (setq num (- (cdr active) (- (1+ (cdr range))
5863                                        (car range)))))
5864          (t
5865           (while range
5866             (if (numberp (car range))
5867                 (setq num (1+ num))
5868               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5869             (setq range (cdr range)))
5870           (setq num (- (cdr active) num))))
5871         ;; Update the number of unread articles.
5872         (setcar entry num)
5873         ;; Update the group buffer.
5874         (unless (gnus-ephemeral-group-p group)
5875           (gnus-group-update-group group t))))))
5876
5877 (defvar gnus-newsgroup-none-id 0)
5878
5879 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5880   (let ((cur nntp-server-buffer)
5881         (dependencies
5882          (or dependencies
5883              (with-current-buffer gnus-summary-buffer
5884                gnus-newsgroup-dependencies)))
5885         headers id end ref number
5886         (mail-parse-charset gnus-newsgroup-charset)
5887         (mail-parse-ignored-charsets
5888          (save-excursion (condition-case nil
5889                              (set-buffer gnus-summary-buffer)
5890                            (error))
5891                          gnus-newsgroup-ignored-charsets)))
5892     (save-excursion
5893       (set-buffer nntp-server-buffer)
5894       ;; Translate all TAB characters into SPACE characters.
5895       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5896       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5897       (ietf-drums-unfold-fws)
5898       (gnus-run-hooks 'gnus-parse-headers-hook)
5899       (let ((case-fold-search t)
5900             in-reply-to header p lines chars)
5901         (goto-char (point-min))
5902         ;; Search to the beginning of the next header.  Error messages
5903         ;; do not begin with 2 or 3.
5904         (while (re-search-forward "^[23][0-9]+ " nil t)
5905           (setq id nil
5906                 ref nil)
5907           ;; This implementation of this function, with nine
5908           ;; search-forwards instead of the one re-search-forward and
5909           ;; a case (which basically was the old function) is actually
5910           ;; about twice as fast, even though it looks messier.  You
5911           ;; can't have everything, I guess.  Speed and elegance
5912           ;; doesn't always go hand in hand.
5913           (setq
5914            header
5915            (vector
5916             ;; Number.
5917             (prog1
5918                 (setq number (read cur))
5919               (end-of-line)
5920               (setq p (point))
5921               (narrow-to-region (point)
5922                                 (or (and (search-forward "\n.\n" nil t)
5923                                          (- (point) 2))
5924                                     (point))))
5925             ;; Subject.
5926             (progn
5927               (goto-char p)
5928               (if (search-forward "\nsubject:" nil t)
5929                   (funcall gnus-decode-encoded-word-function
5930                            (nnheader-header-value))
5931                 "(none)"))
5932             ;; From.
5933             (progn
5934               (goto-char p)
5935               (if (search-forward "\nfrom:" nil t)
5936                   (funcall gnus-decode-encoded-word-function
5937                            (nnheader-header-value))
5938                 "(nobody)"))
5939             ;; Date.
5940             (progn
5941               (goto-char p)
5942               (if (search-forward "\ndate:" nil t)
5943                   (nnheader-header-value) ""))
5944             ;; Message-ID.
5945             (progn
5946               (goto-char p)
5947               (setq id (if (re-search-forward
5948                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5949                            ;; We do it this way to make sure the Message-ID
5950                            ;; is (somewhat) syntactically valid.
5951                            (buffer-substring (match-beginning 1)
5952                                              (match-end 1))
5953                          ;; If there was no message-id, we just fake one
5954                          ;; to make subsequent routines simpler.
5955                          (nnheader-generate-fake-message-id number))))
5956             ;; References.
5957             (progn
5958               (goto-char p)
5959               (if (search-forward "\nreferences:" nil t)
5960                   (progn
5961                     (setq end (point))
5962                     (prog1
5963                         (nnheader-header-value)
5964                       (setq ref
5965                             (buffer-substring
5966                              (progn
5967                                (end-of-line)
5968                                (search-backward ">" end t)
5969                                (1+ (point)))
5970                              (progn
5971                                (search-backward "<" end t)
5972                                (point))))))
5973                 ;; Get the references from the in-reply-to header if there
5974                 ;; were no references and the in-reply-to header looks
5975                 ;; promising.
5976                 (if (and (search-forward "\nin-reply-to:" nil t)
5977                          (setq in-reply-to (nnheader-header-value))
5978                          (string-match "<[^>]+>" in-reply-to))
5979                     (let (ref2)
5980                       (setq ref (substring in-reply-to (match-beginning 0)
5981                                            (match-end 0)))
5982                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5983                         (setq ref2 (substring in-reply-to (match-beginning 0)
5984                                               (match-end 0)))
5985                         (when (> (length ref2) (length ref))
5986                           (setq ref ref2)))
5987                       ref)
5988                   (setq ref nil))))
5989             ;; Chars.
5990             (progn
5991               (goto-char p)
5992               (if (search-forward "\nchars: " nil t)
5993                   (if (numberp (setq chars (ignore-errors (read cur))))
5994                       chars -1)
5995                 -1))
5996             ;; Lines.
5997             (progn
5998               (goto-char p)
5999               (if (search-forward "\nlines: " nil t)
6000                   (if (numberp (setq lines (ignore-errors (read cur))))
6001                       lines -1)
6002                 -1))
6003             ;; Xref.
6004             (progn
6005               (goto-char p)
6006               (and (search-forward "\nxref:" nil t)
6007                    (nnheader-header-value)))
6008             ;; Extra.
6009             (when gnus-extra-headers
6010               (let ((extra gnus-extra-headers)
6011                     out)
6012                 (while extra
6013                   (goto-char p)
6014                   (when (search-forward
6015                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6016                     (push (cons (car extra) (nnheader-header-value))
6017                           out))
6018                   (pop extra))
6019                 out))))
6020           (when (equal id ref)
6021             (setq ref nil))
6022
6023           (when gnus-alter-header-function
6024             (funcall gnus-alter-header-function header)
6025             (setq id (mail-header-id header)
6026                   ref (gnus-parent-id (mail-header-references header))))
6027
6028           (when (setq header
6029                       (gnus-dependencies-add-header
6030                        header dependencies force-new))
6031             (push header headers))
6032           (goto-char (point-max))
6033           (widen))
6034         (nreverse headers)))))
6035
6036 ;; Goes through the xover lines and returns a list of vectors
6037 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6038                                                   force-new dependencies
6039                                                   group also-fetch-heads)
6040   "Parse the news overview data in the server buffer.
6041 Return a list of headers that match SEQUENCE (see
6042 `nntp-retrieve-headers')."
6043   ;; Get the Xref when the users reads the articles since most/some
6044   ;; NNTP servers do not include Xrefs when using XOVER.
6045   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6046   (let ((mail-parse-charset gnus-newsgroup-charset)
6047         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6048         (cur nntp-server-buffer)
6049         (dependencies (or dependencies gnus-newsgroup-dependencies))
6050         (allp (cond
6051                ((eq gnus-read-all-available-headers t)
6052                 t)
6053                ((stringp gnus-read-all-available-headers)
6054                 (string-match gnus-read-all-available-headers group))
6055                (t
6056                 nil)))
6057         number headers header)
6058     (save-excursion
6059       (set-buffer nntp-server-buffer)
6060       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6061       ;; Allow the user to mangle the headers before parsing them.
6062       (gnus-run-hooks 'gnus-parse-headers-hook)
6063       (goto-char (point-min))
6064       (gnus-parse-without-error
6065         (while (and (or sequence allp)
6066                     (not (eobp)))
6067           (setq number (read cur))
6068           (when (not allp)
6069             (while (and sequence
6070                         (< (car sequence) number))
6071               (setq sequence (cdr sequence))))
6072           (when (and (or allp
6073                          (and sequence
6074                               (eq number (car sequence))))
6075                      (progn
6076                        (setq sequence (cdr sequence))
6077                        (setq header (inline
6078                                       (gnus-nov-parse-line
6079                                        number dependencies force-new)))))
6080             (push header headers))
6081           (forward-line 1)))
6082       ;; A common bug in inn is that if you have posted an article and
6083       ;; then retrieves the active file, it will answer correctly --
6084       ;; the new article is included.  However, a NOV entry for the
6085       ;; article may not have been generated yet, so this may fail.
6086       ;; We work around this problem by retrieving the last few
6087       ;; headers using HEAD.
6088       (if (or (not also-fetch-heads)
6089               (not sequence))
6090           ;; We (probably) got all the headers.
6091           (nreverse headers)
6092         (let ((gnus-nov-is-evil t))
6093           (nconc
6094            (nreverse headers)
6095            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6096              (gnus-get-newsgroup-headers))))))))
6097
6098 (defun gnus-article-get-xrefs ()
6099   "Fill in the Xref value in `gnus-current-headers', if necessary.
6100 This is meant to be called in `gnus-article-internal-prepare-hook'."
6101   (let ((headers (with-current-buffer gnus-summary-buffer
6102                    gnus-current-headers)))
6103     (or (not gnus-use-cross-reference)
6104         (not headers)
6105         (and (mail-header-xref headers)
6106              (not (string= (mail-header-xref headers) "")))
6107         (let ((case-fold-search t)
6108               xref)
6109           (save-restriction
6110             (nnheader-narrow-to-headers)
6111             (goto-char (point-min))
6112             (when (or (and (not (eobp))
6113                            (eq (downcase (char-after)) ?x)
6114                            (looking-at "Xref:"))
6115                       (search-forward "\nXref:" nil t))
6116               (goto-char (1+ (match-end 0)))
6117               (setq xref (buffer-substring (point) (point-at-eol)))
6118               (mail-header-set-xref headers xref)))))))
6119
6120 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6121   "Find article ID and insert the summary line for that article.
6122 OLD-HEADER can either be a header or a line number to insert
6123 the subject line on."
6124   (let* ((line (and (numberp old-header) old-header))
6125          (old-header (and (vectorp old-header) old-header))
6126          (header (cond ((and old-header use-old-header)
6127                         old-header)
6128                        ((and (numberp id)
6129                              (gnus-number-to-header id))
6130                         (gnus-number-to-header id))
6131                        (t
6132                         (gnus-read-header id))))
6133          (number (and (numberp id) id))
6134          d)
6135     (when header
6136       ;; Rebuild the thread that this article is part of and go to the
6137       ;; article we have fetched.
6138       (when (and (not gnus-show-threads)
6139                  old-header)
6140         (when (and number
6141                    (setq d (gnus-data-find (mail-header-number old-header))))
6142           (goto-char (gnus-data-pos d))
6143           (gnus-data-remove
6144            number
6145            (- (point-at-bol)
6146               (prog1
6147                   (1+ (point-at-eol))
6148                 (gnus-delete-line))))))
6149       ;; Remove list identifiers from subject.
6150       (when gnus-list-identifiers
6151         (let ((gnus-newsgroup-headers (list header)))
6152           (gnus-summary-remove-list-identifiers)))
6153       (when old-header
6154         (mail-header-set-number header (mail-header-number old-header)))
6155       (setq gnus-newsgroup-sparse
6156             (delq (setq number (mail-header-number header))
6157                   gnus-newsgroup-sparse))
6158       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6159       (push number gnus-newsgroup-limit)
6160       (gnus-rebuild-thread (mail-header-id header) line)
6161       (gnus-summary-goto-subject number nil t))
6162     (when (and (numberp number)
6163                (> number 0))
6164       ;; We have to update the boundaries even if we can't fetch the
6165       ;; article if ID is a number -- so that the next `P' or `N'
6166       ;; command will fetch the previous (or next) article even
6167       ;; if the one we tried to fetch this time has been canceled.
6168       (when (> number gnus-newsgroup-end)
6169         (setq gnus-newsgroup-end number))
6170       (when (< number gnus-newsgroup-begin)
6171         (setq gnus-newsgroup-begin number))
6172       (setq gnus-newsgroup-unselected
6173             (delq number gnus-newsgroup-unselected)))
6174     ;; Report back a success?
6175     (and header (mail-header-number header))))
6176
6177 ;;; Process/prefix in the summary buffer
6178
6179 (defun gnus-summary-work-articles (n)
6180   "Return a list of articles to be worked upon.
6181 The prefix argument, the list of process marked articles, and the
6182 current article will be taken into consideration."
6183   (save-excursion
6184     (set-buffer gnus-summary-buffer)
6185     (cond
6186      (n
6187       ;; A numerical prefix has been given.
6188       (setq n (prefix-numeric-value n))
6189       (let ((backward (< n 0))
6190             (n (abs (prefix-numeric-value n)))
6191             articles article)
6192         (save-excursion
6193           (while
6194               (and (> n 0)
6195                    (push (setq article (gnus-summary-article-number))
6196                          articles)
6197                    (if backward
6198                        (gnus-summary-find-prev nil article)
6199                      (gnus-summary-find-next nil article)))
6200             (decf n)))
6201         (nreverse articles)))
6202      ((and (gnus-region-active-p) (mark))
6203       (message "region active")
6204       ;; Work on the region between point and mark.
6205       (let ((max (max (point) (mark)))
6206             articles article)
6207         (save-excursion
6208           (goto-char (min (min (point) (mark))))
6209           (while
6210               (and
6211                (push (setq article (gnus-summary-article-number)) articles)
6212                (gnus-summary-find-next nil article)
6213                (< (point) max)))
6214           (nreverse articles))))
6215      (gnus-newsgroup-processable
6216       ;; There are process-marked articles present.
6217       ;; Save current state.
6218       (gnus-summary-save-process-mark)
6219       ;; Return the list.
6220       (reverse gnus-newsgroup-processable))
6221      (t
6222       ;; Just return the current article.
6223       (list (gnus-summary-article-number))))))
6224
6225 (defmacro gnus-summary-iterate (arg &rest forms)
6226   "Iterate over the process/prefixed articles and do FORMS.
6227 ARG is the interactive prefix given to the command.  FORMS will be
6228 executed with point over the summary line of the articles."
6229   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6230     `(let ((,articles (gnus-summary-work-articles ,arg)))
6231        (while ,articles
6232          (gnus-summary-goto-subject (car ,articles))
6233          ,@forms
6234          (pop ,articles)))))
6235
6236 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6237 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6238
6239 (defun gnus-summary-save-process-mark ()
6240   "Push the current set of process marked articles on the stack."
6241   (interactive)
6242   (push (copy-sequence gnus-newsgroup-processable)
6243         gnus-newsgroup-process-stack))
6244
6245 (defun gnus-summary-kill-process-mark ()
6246   "Push the current set of process marked articles on the stack and unmark."
6247   (interactive)
6248   (gnus-summary-save-process-mark)
6249   (gnus-summary-unmark-all-processable))
6250
6251 (defun gnus-summary-yank-process-mark ()
6252   "Pop the last process mark state off the stack and restore it."
6253   (interactive)
6254   (unless gnus-newsgroup-process-stack
6255     (error "Empty mark stack"))
6256   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6257
6258 (defun gnus-summary-process-mark-set (set)
6259   "Make SET into the current process marked articles."
6260   (gnus-summary-unmark-all-processable)
6261   (mapc 'gnus-summary-set-process-mark set))
6262
6263 ;;; Searching and stuff
6264
6265 (defun gnus-summary-search-group (&optional backward use-level)
6266   "Search for next unread newsgroup.
6267 If optional argument BACKWARD is non-nil, search backward instead."
6268   (save-excursion
6269     (set-buffer gnus-group-buffer)
6270     (when (gnus-group-search-forward
6271            backward nil (if use-level (gnus-group-group-level) nil))
6272       (gnus-group-group-name))))
6273
6274 (defun gnus-summary-best-group (&optional exclude-group)
6275   "Find the name of the best unread group.
6276 If EXCLUDE-GROUP, do not go to this group."
6277   (with-current-buffer gnus-group-buffer
6278     (save-excursion
6279       (gnus-group-best-unread-group exclude-group))))
6280
6281 (defun gnus-summary-find-next (&optional unread article backward)
6282   (if backward
6283       (gnus-summary-find-prev unread article)
6284     (let* ((dummy (gnus-summary-article-intangible-p))
6285            (article (or article (gnus-summary-article-number)))
6286            (data (gnus-data-find-list article))
6287            result)
6288       (when (and (not dummy)
6289                  (or (not gnus-summary-check-current)
6290                      (not unread)
6291                      (not (gnus-data-unread-p (car data)))))
6292         (setq data (cdr data)))
6293       (when (setq result
6294                   (if unread
6295                       (progn
6296                         (while data
6297                           (unless (memq (gnus-data-number (car data))
6298                                         (cond
6299                                          ((eq gnus-auto-goto-ignores
6300                                               'always-undownloaded)
6301                                           gnus-newsgroup-undownloaded)
6302                                          (gnus-plugged
6303                                           nil)
6304                                          ((eq gnus-auto-goto-ignores
6305                                               'unfetched)
6306                                           gnus-newsgroup-unfetched)
6307                                          ((eq gnus-auto-goto-ignores
6308                                               'undownloaded)
6309                                           gnus-newsgroup-undownloaded)))
6310                             (when (gnus-data-unread-p (car data))
6311                               (setq result (car data)
6312                                     data nil)))
6313                           (setq data (cdr data)))
6314                         result)
6315                     (car data)))
6316         (goto-char (gnus-data-pos result))
6317         (gnus-data-number result)))))
6318
6319 (defun gnus-summary-find-prev (&optional unread article)
6320   (let* ((eobp (eobp))
6321          (article (or article (gnus-summary-article-number)))
6322          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6323          result)
6324     (when (and (not eobp)
6325                (or (not gnus-summary-check-current)
6326                    (not unread)
6327                    (not (gnus-data-unread-p (car data)))))
6328       (setq data (cdr data)))
6329     (when (setq result
6330                 (if unread
6331                     (progn
6332                       (while data
6333                         (unless (memq (gnus-data-number (car data))
6334                                       (cond
6335                                        ((eq gnus-auto-goto-ignores
6336                                             'always-undownloaded)
6337                                         gnus-newsgroup-undownloaded)
6338                                        (gnus-plugged
6339                                         nil)
6340                                        ((eq gnus-auto-goto-ignores
6341                                             'unfetched)
6342                                         gnus-newsgroup-unfetched)
6343                                        ((eq gnus-auto-goto-ignores
6344                                             'undownloaded)
6345                                         gnus-newsgroup-undownloaded)))
6346                           (when (gnus-data-unread-p (car data))
6347                             (setq result (car data)
6348                                   data nil)))
6349                         (setq data (cdr data)))
6350                       result)
6351                   (car data)))
6352       (goto-char (gnus-data-pos result))
6353       (gnus-data-number result))))
6354
6355 (defun gnus-summary-find-subject (subject &optional unread backward article)
6356   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6357          (article (or article (gnus-summary-article-number)))
6358          (articles (gnus-data-list backward))
6359          (arts (gnus-data-find-list article articles))
6360          result)
6361     (when (or (not gnus-summary-check-current)
6362               (not unread)
6363               (not (gnus-data-unread-p (car arts))))
6364       (setq arts (cdr arts)))
6365     (while arts
6366       (and (or (not unread)
6367                (gnus-data-unread-p (car arts)))
6368            (vectorp (gnus-data-header (car arts)))
6369            (gnus-subject-equal
6370             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6371            (setq result (car arts)
6372                  arts nil))
6373       (setq arts (cdr arts)))
6374     (and result
6375          (goto-char (gnus-data-pos result))
6376          (gnus-data-number result))))
6377
6378 (defun gnus-summary-search-forward (&optional unread subject backward)
6379   "Search forward for an article.
6380 If UNREAD, look for unread articles.  If SUBJECT, look for
6381 articles with that subject.  If BACKWARD, search backward instead."
6382   (cond (subject (gnus-summary-find-subject subject unread backward))
6383         (backward (gnus-summary-find-prev unread))
6384         (t (gnus-summary-find-next unread))))
6385
6386 (defun gnus-recenter (&optional n)
6387   "Center point in window and redisplay frame.
6388 Also do horizontal recentering."
6389   (interactive "P")
6390   (when (and gnus-auto-center-summary
6391              (not (eq gnus-auto-center-summary 'vertical)))
6392     (gnus-horizontal-recenter))
6393   (recenter n))
6394
6395 (defun gnus-summary-recenter ()
6396   "Center point in the summary window.
6397 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6398 displayed, no centering will be performed."
6399   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6400   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6401   (interactive)
6402   ;; The user has to want it.
6403   (when gnus-auto-center-summary
6404     (let* ((top (cond ((< (window-height) 4) 0)
6405                       ((< (window-height) 7) 1)
6406                       (t (if (numberp gnus-auto-center-summary)
6407                              gnus-auto-center-summary
6408                            (/ (1- (window-height)) 2)))))
6409            (height (1- (window-height)))
6410            (bottom (save-excursion (goto-char (point-max))
6411                                    (forward-line (- height))
6412                                    (point)))
6413            (window (get-buffer-window (current-buffer))))
6414       (when (get-buffer-window gnus-article-buffer)
6415         ;; Only do recentering when the article buffer is displayed,
6416         ;; Set the window start to either `bottom', which is the biggest
6417         ;; possible valid number, or the second line from the top,
6418         ;; whichever is the least.
6419         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6420           (if (> bottom top-pos)
6421               ;; Keep the second line from the top visible
6422               (set-window-start window top-pos t)
6423             ;; Try to keep the bottom line visible; if it's partially
6424             ;; obscured, either scroll one more line to make it fully
6425             ;; visible, or revert to using TOP-POS.
6426             (save-excursion
6427               (goto-char (point-max))
6428               (forward-line -1)
6429               (let ((last-line-start (point)))
6430                 (goto-char bottom)
6431                 (set-window-start window (point) t)
6432                 (when (not (pos-visible-in-window-p last-line-start window))
6433                   (forward-line 1)
6434                   (set-window-start window (min (point) top-pos) t)))))))
6435       ;; Do horizontal recentering while we're at it.
6436       (when (and (get-buffer-window (current-buffer) t)
6437                  (not (eq gnus-auto-center-summary 'vertical)))
6438         (let ((selected (selected-window)))
6439           (select-window (get-buffer-window (current-buffer) t))
6440           (gnus-summary-position-point)
6441           (gnus-horizontal-recenter)
6442           (select-window selected))))))
6443
6444 (defun gnus-summary-jump-to-group (newsgroup)
6445   "Move point to NEWSGROUP in group mode buffer."
6446   ;; Keep update point of group mode buffer if visible.
6447   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6448       (save-window-excursion
6449         ;; Take care of tree window mode.
6450         (when (get-buffer-window gnus-group-buffer)
6451           (pop-to-buffer gnus-group-buffer))
6452         (gnus-group-jump-to-group newsgroup))
6453     (save-excursion
6454       ;; Take care of tree window mode.
6455       (if (get-buffer-window gnus-group-buffer)
6456           (pop-to-buffer gnus-group-buffer)
6457         (set-buffer gnus-group-buffer))
6458       (gnus-group-jump-to-group newsgroup))))
6459
6460 ;; This function returns a list of article numbers based on the
6461 ;; difference between the ranges of read articles in this group and
6462 ;; the range of active articles.
6463 (defun gnus-list-of-unread-articles (group)
6464   (let* ((read (gnus-info-read (gnus-get-info group)))
6465          (active (or (gnus-active group) (gnus-activate-group group)))
6466          (last (or (cdr active)
6467                    (error "Group %s couldn't be activated " group)))
6468          first nlast unread)
6469     ;; If none are read, then all are unread.
6470     (if (not read)
6471         (setq first (car active))
6472       ;; If the range of read articles is a single range, then the
6473       ;; first unread article is the article after the last read
6474       ;; article.  Sounds logical, doesn't it?
6475       (if (and (not (listp (cdr read)))
6476                (or (< (car read) (car active))
6477                    (progn (setq read (list read))
6478                           nil)))
6479           (setq first (max (car active) (1+ (cdr read))))
6480         ;; `read' is a list of ranges.
6481         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6482                                   (caar read)))
6483                   1)
6484           (setq first (car active)))
6485         (while read
6486           (when first
6487             (while (< first nlast)
6488               (setq unread (cons first unread)
6489                     first (1+ first))))
6490           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6491           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6492           (setq read (cdr read)))))
6493     ;; And add the last unread articles.
6494     (while (<= first last)
6495       (setq unread (cons first unread)
6496             first (1+ first)))
6497     ;; Return the list of unread articles.
6498     (delq 0 (nreverse unread))))
6499
6500 (defun gnus-list-of-read-articles (group)
6501   "Return a list of unread, unticked and non-dormant articles."
6502   (let* ((info (gnus-get-info group))
6503          (marked (gnus-info-marks info))
6504          (active (gnus-active group)))
6505     (and info active
6506          (gnus-list-range-difference
6507           (gnus-list-range-difference
6508            (gnus-sorted-complement
6509             (gnus-uncompress-range active)
6510             (gnus-list-of-unread-articles group))
6511            (cdr (assq 'dormant marked)))
6512           (cdr (assq 'tick marked))))))
6513
6514 ;; This function returns a sequence of article numbers based on the
6515 ;; difference between the ranges of read articles in this group and
6516 ;; the range of active articles.
6517 (defun gnus-sequence-of-unread-articles (group)
6518   (let* ((read (gnus-info-read (gnus-get-info group)))
6519          (active (or (gnus-active group) (gnus-activate-group group)))
6520          (last (cdr active))
6521          first nlast unread)
6522     ;; If none are read, then all are unread.
6523     (if (not read)
6524         (setq first (car active))
6525       ;; If the range of read articles is a single range, then the
6526       ;; first unread article is the article after the last read
6527       ;; article.  Sounds logical, doesn't it?
6528       (if (and (not (listp (cdr read)))
6529                (or (< (car read) (car active))
6530                    (progn (setq read (list read))
6531                           nil)))
6532           (setq first (max (car active) (1+ (cdr read))))
6533         ;; `read' is a list of ranges.
6534         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6535                                   (caar read)))
6536                   1)
6537           (setq first (car active)))
6538         (while read
6539           (when first
6540             (push (cons first nlast) unread))
6541           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6542           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6543           (setq read (cdr read)))))
6544     ;; And add the last unread articles.
6545     (cond ((< first last)
6546            (push (cons first last) unread))
6547           ((= first last)
6548            (push first unread)))
6549     ;; Return the sequence of unread articles.
6550     (delq 0 (nreverse unread))))
6551
6552 ;; Various summary commands
6553
6554 (defun gnus-summary-select-article-buffer ()
6555   "Reconfigure windows to show article buffer."
6556   (interactive)
6557   (if (not (gnus-buffer-live-p gnus-article-buffer))
6558       (error "There is no article buffer for this summary buffer")
6559     (gnus-configure-windows 'article)
6560     (select-window (get-buffer-window gnus-article-buffer))))
6561
6562 (defun gnus-summary-universal-argument (arg)
6563   "Perform any operation on all articles that are process/prefixed."
6564   (interactive "P")
6565   (let ((articles (gnus-summary-work-articles arg))
6566         func article)
6567     (if (eq
6568          (setq
6569           func
6570           (key-binding
6571            (read-key-sequence
6572             (substitute-command-keys
6573              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6574          'undefined)
6575         (gnus-error 1 "Undefined key")
6576       (save-excursion
6577         (while articles
6578           (gnus-summary-goto-subject (setq article (pop articles)))
6579           (let (gnus-newsgroup-processable)
6580             (command-execute func))
6581           (gnus-summary-remove-process-mark article)))))
6582   (gnus-summary-position-point))
6583
6584 (defun gnus-summary-toggle-truncation (&optional arg)
6585   "Toggle truncation of summary lines.
6586 With ARG, turn line truncation on if ARG is positive."
6587   (interactive "P")
6588   (setq truncate-lines
6589         (if (null arg) (not truncate-lines)
6590           (> (prefix-numeric-value arg) 0)))
6591   (redraw-display))
6592
6593 (defun gnus-summary-find-for-reselect ()
6594   "Return the number of an article to stay on across a reselect.
6595 The current article is considered, then following articles, then previous
6596 articles.  An article is sought which is not cancelled and isn't a temporary
6597 insertion from another group.  If there's no such then return a dummy 0."
6598   (let (found)
6599     (dolist (rev '(nil t))
6600       (unless found      ; don't demand the reverse list if we don't need it
6601         (let ((data (gnus-data-find-list
6602                      (gnus-summary-article-number) (gnus-data-list rev))))
6603           (while (and data (not found))
6604             (if (and (< 0 (gnus-data-number (car data)))
6605                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6606                 (setq found (gnus-data-number (car data))))
6607             (setq data (cdr data))))))
6608     (or found 0)))
6609
6610 (defun gnus-summary-reselect-current-group (&optional all rescan)
6611   "Exit and then reselect the current newsgroup.
6612 The prefix argument ALL means to select all articles."
6613   (interactive "P")
6614   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6615     (error "Ephemeral groups can't be reselected"))
6616   (let ((current-subject (gnus-summary-find-for-reselect))
6617         (group gnus-newsgroup-name))
6618     (setq gnus-newsgroup-begin nil)
6619     (gnus-summary-exit nil 'leave-hidden)
6620     ;; We have to adjust the point of group mode buffer because
6621     ;; point was moved to the next unread newsgroup by exiting.
6622     (gnus-summary-jump-to-group group)
6623     (when rescan
6624       (save-excursion
6625         (gnus-group-get-new-news-this-group 1)))
6626     (gnus-group-read-group all t)
6627     (gnus-summary-goto-subject current-subject nil t)))
6628
6629 (defun gnus-summary-rescan-group (&optional all)
6630   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6631   (interactive "P")
6632   (gnus-summary-reselect-current-group all t))
6633
6634 (defun gnus-summary-update-info (&optional non-destructive)
6635   (save-excursion
6636     (let ((group gnus-newsgroup-name))
6637       (when group
6638         (when gnus-newsgroup-kill-headers
6639           (setq gnus-newsgroup-killed
6640                 (gnus-compress-sequence
6641                  (gnus-sorted-union
6642                   (gnus-list-range-intersection
6643                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6644                   gnus-newsgroup-unreads)
6645                  t)))
6646         (unless (listp (cdr gnus-newsgroup-killed))
6647           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6648         (let ((headers gnus-newsgroup-headers))
6649           ;; Set the new ranges of read articles.
6650           (with-current-buffer gnus-group-buffer
6651             (gnus-undo-force-boundary))
6652           (gnus-update-read-articles
6653            group (gnus-sorted-union
6654                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6655           ;; Set the current article marks.
6656           (let ((gnus-newsgroup-scored
6657                  (if (and (not gnus-save-score)
6658                           (not non-destructive))
6659                      nil
6660                    gnus-newsgroup-scored)))
6661             (save-excursion
6662               (gnus-update-marks)))
6663           ;; Do the cross-ref thing.
6664           (when gnus-use-cross-reference
6665             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6666           ;; Do not switch windows but change the buffer to work.
6667           (set-buffer gnus-group-buffer)
6668           (unless (gnus-ephemeral-group-p group)
6669             (gnus-group-update-group group)))))))
6670
6671 (defun gnus-summary-save-newsrc (&optional force)
6672   "Save the current number of read/marked articles in the dribble buffer.
6673 The dribble buffer will then be saved.
6674 If FORCE (the prefix), also save the .newsrc file(s)."
6675   (interactive "P")
6676   (gnus-summary-update-info t)
6677   (if force
6678       (gnus-save-newsrc-file)
6679     (gnus-dribble-save)))
6680
6681 (defun gnus-summary-exit (&optional temporary leave-hidden)
6682   "Exit reading current newsgroup, and then return to group selection mode.
6683 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6684   (interactive)
6685   (gnus-set-global-variables)
6686   (when (gnus-buffer-live-p gnus-article-buffer)
6687     (save-excursion
6688       (set-buffer gnus-article-buffer)
6689       (mm-destroy-parts gnus-article-mime-handles)
6690       ;; Set it to nil for safety reason.
6691       (setq gnus-article-mime-handle-alist nil)
6692       (setq gnus-article-mime-handles nil)))
6693   (gnus-kill-save-kill-buffer)
6694   (gnus-async-halt-prefetch)
6695   (let* ((group gnus-newsgroup-name)
6696          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6697          (gnus-group-is-exiting-p t)
6698          (mode major-mode)
6699          (group-point nil)
6700          (buf (current-buffer)))
6701     (unless quit-config
6702       ;; Do adaptive scoring, and possibly save score files.
6703       (when gnus-newsgroup-adaptive
6704         (gnus-score-adaptive))
6705       (when gnus-use-scoring
6706         (gnus-score-save)))
6707     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6708     ;; If we have several article buffers, we kill them at exit.
6709     (unless gnus-single-article-buffer
6710       (gnus-kill-buffer gnus-original-article-buffer)
6711       (setq gnus-article-current nil))
6712     (when gnus-use-cache
6713       (gnus-cache-possibly-remove-articles)
6714       (gnus-cache-save-buffers))
6715     (gnus-async-prefetch-remove-group group)
6716     (when gnus-suppress-duplicates
6717       (gnus-dup-enter-articles))
6718     (when gnus-use-trees
6719       (gnus-tree-close group))
6720     (when gnus-use-cache
6721       (gnus-cache-write-active))
6722     ;; Remove entries for this group.
6723     (nnmail-purge-split-history (gnus-group-real-name group))
6724     ;; Make all changes in this group permanent.
6725     (unless quit-config
6726       (gnus-run-hooks 'gnus-exit-group-hook)
6727       (gnus-summary-update-info))
6728     (gnus-close-group group)
6729     ;; Make sure where we were, and go to next newsgroup.
6730     (set-buffer gnus-group-buffer)
6731     (unless quit-config
6732       (gnus-group-jump-to-group group))
6733     (gnus-run-hooks 'gnus-summary-exit-hook)
6734     (unless (or quit-config
6735                 ;; If this group has disappeared from the summary
6736                 ;; buffer, don't skip forwards.
6737                 (not (string= group (gnus-group-group-name))))
6738       (gnus-group-next-unread-group 1))
6739     (setq group-point (point))
6740     (if temporary
6741         nil                             ;Nothing to do.
6742       ;; If we have several article buffers, we kill them at exit.
6743       (unless gnus-single-article-buffer
6744         (gnus-kill-buffer gnus-article-buffer)
6745         (gnus-kill-buffer gnus-original-article-buffer)
6746         (setq gnus-article-current nil))
6747       (set-buffer buf)
6748       (if (not gnus-kill-summary-on-exit)
6749           (progn
6750             (gnus-deaden-summary)
6751             (setq mode nil))
6752         ;; We set all buffer-local variables to nil.  It is unclear why
6753         ;; this is needed, but if we don't, buffer-local variables are
6754         ;; not garbage-collected, it seems.  This would the lead to en
6755         ;; ever-growing Emacs.
6756         (gnus-summary-clear-local-variables)
6757         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6758           (gnus-summary-clear-local-variables))
6759         (when (get-buffer gnus-article-buffer)
6760           (bury-buffer gnus-article-buffer))
6761         ;; Return to group mode buffer.
6762         (when (eq mode 'gnus-summary-mode)
6763           (gnus-kill-buffer buf)))
6764       (setq gnus-current-select-method gnus-select-method)
6765       (set-buffer gnus-group-buffer)
6766       (if quit-config
6767           (gnus-handle-ephemeral-exit quit-config)
6768         (goto-char group-point)
6769         ;; If gnus-group-buffer is already displayed, make sure we also move
6770         ;; the cursor in the window that displays it.
6771         (let ((win (get-buffer-window (current-buffer) 0)))
6772           (if win (set-window-point win (point))))
6773         (unless leave-hidden
6774           (gnus-configure-windows 'group 'force)))
6775       ;; Clear the current group name.
6776       (unless quit-config
6777         (setq gnus-newsgroup-name nil)))))
6778
6779 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6780 (defun gnus-summary-exit-no-update (&optional no-questions)
6781   "Quit reading current newsgroup without updating read article info."
6782   (interactive)
6783   (let* ((group gnus-newsgroup-name)
6784          (gnus-group-is-exiting-p t)
6785          (gnus-group-is-exiting-without-update-p t)
6786          (quit-config (gnus-group-quit-config group)))
6787     (when (or no-questions
6788               gnus-expert-user
6789               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6790       (gnus-async-halt-prefetch)
6791       (run-hooks 'gnus-summary-prepare-exit-hook)
6792       (when (gnus-buffer-live-p gnus-article-buffer)
6793         (save-excursion
6794           (set-buffer gnus-article-buffer)
6795           (mm-destroy-parts gnus-article-mime-handles)
6796           ;; Set it to nil for safety reason.
6797           (setq gnus-article-mime-handle-alist nil)
6798           (setq gnus-article-mime-handles nil)))
6799       ;; If we have several article buffers, we kill them at exit.
6800       (unless gnus-single-article-buffer
6801         (gnus-kill-buffer gnus-article-buffer)
6802         (gnus-kill-buffer gnus-original-article-buffer)
6803         (setq gnus-article-current nil))
6804       (if (not gnus-kill-summary-on-exit)
6805           (gnus-deaden-summary)
6806         (gnus-close-group group)
6807         (gnus-summary-clear-local-variables)
6808         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6809           (gnus-summary-clear-local-variables))
6810         (gnus-kill-buffer gnus-summary-buffer))
6811       (unless gnus-single-article-buffer
6812         (setq gnus-article-current nil))
6813       (when gnus-use-trees
6814         (gnus-tree-close group))
6815       (gnus-async-prefetch-remove-group group)
6816       (when (get-buffer gnus-article-buffer)
6817         (bury-buffer gnus-article-buffer))
6818       ;; Return to the group buffer.
6819       (gnus-configure-windows 'group 'force)
6820       ;; Clear the current group name.
6821       (setq gnus-newsgroup-name nil)
6822       (unless (gnus-ephemeral-group-p group)
6823         (gnus-group-update-group group))
6824       (when (equal (gnus-group-group-name) group)
6825         (gnus-group-next-unread-group 1))
6826       (when quit-config
6827         (gnus-handle-ephemeral-exit quit-config)))))
6828
6829 (defun gnus-handle-ephemeral-exit (quit-config)
6830   "Handle movement when leaving an ephemeral group.
6831 The state which existed when entering the ephemeral is reset."
6832   (if (not (buffer-name (car quit-config)))
6833       (gnus-configure-windows 'group 'force)
6834     (set-buffer (car quit-config))
6835     (cond ((eq major-mode 'gnus-summary-mode)
6836            (gnus-set-global-variables))
6837           ((eq major-mode 'gnus-article-mode)
6838            (save-excursion
6839              ;; The `gnus-summary-buffer' variable may point
6840              ;; to the old summary buffer when using a single
6841              ;; article buffer.
6842              (unless (gnus-buffer-live-p gnus-summary-buffer)
6843                (set-buffer gnus-group-buffer))
6844              (set-buffer gnus-summary-buffer)
6845              (gnus-set-global-variables))))
6846     (if (or (eq (cdr quit-config) 'article)
6847             (eq (cdr quit-config) 'pick))
6848         (progn
6849           ;; The current article may be from the ephemeral group
6850           ;; thus it is best that we reload this article
6851           ;;
6852           ;; If we're exiting from a large digest, this can be
6853           ;; extremely slow.  So, it's better not to reload it. -- jh.
6854           ;;(gnus-summary-show-article)
6855           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6856               (gnus-configure-windows 'pick 'force)
6857             (gnus-configure-windows (cdr quit-config) 'force)))
6858       (gnus-configure-windows (cdr quit-config) 'force))
6859     (when (eq major-mode 'gnus-summary-mode)
6860       (gnus-summary-next-subject 1 nil t)
6861       (gnus-summary-recenter)
6862       (gnus-summary-position-point))))
6863
6864 ;;; Dead summaries.
6865
6866 (defvar gnus-dead-summary-mode-map nil)
6867
6868 (unless gnus-dead-summary-mode-map
6869   (setq gnus-dead-summary-mode-map (make-keymap))
6870   (suppress-keymap gnus-dead-summary-mode-map)
6871   (substitute-key-definition
6872    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6873   (dolist (key '("\C-d" "\r" "\177" [delete]))
6874     (define-key gnus-dead-summary-mode-map
6875       key 'gnus-summary-wake-up-the-dead))
6876   (dolist (key '("q" "Q"))
6877     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6878
6879 (defvar gnus-dead-summary-mode nil
6880   "Minor mode for Gnus summary buffers.")
6881
6882 (defun gnus-dead-summary-mode (&optional arg)
6883   "Minor mode for Gnus summary buffers."
6884   (interactive "P")
6885   (when (eq major-mode 'gnus-summary-mode)
6886     (make-local-variable 'gnus-dead-summary-mode)
6887     (setq gnus-dead-summary-mode
6888           (if (null arg) (not gnus-dead-summary-mode)
6889             (> (prefix-numeric-value arg) 0)))
6890     (when gnus-dead-summary-mode
6891       (add-minor-mode
6892        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6893
6894 (defun gnus-deaden-summary ()
6895   "Make the current summary buffer into a dead summary buffer."
6896   ;; Kill any previous dead summary buffer.
6897   (when (and gnus-dead-summary
6898              (buffer-name gnus-dead-summary))
6899     (with-current-buffer gnus-dead-summary
6900       (when gnus-dead-summary-mode
6901         (kill-buffer (current-buffer)))))
6902   ;; Make this the current dead summary.
6903   (setq gnus-dead-summary (current-buffer))
6904   (gnus-dead-summary-mode 1)
6905   (let ((name (buffer-name)))
6906     (when (string-match "Summary" name)
6907       (rename-buffer
6908        (concat (substring name 0 (match-beginning 0)) "Dead "
6909                (substring name (match-beginning 0)))
6910        t)
6911       (bury-buffer))))
6912
6913 (defun gnus-kill-or-deaden-summary (buffer)
6914   "Kill or deaden the summary BUFFER."
6915   (save-excursion
6916     (when (and (buffer-name buffer)
6917                (not gnus-single-article-buffer))
6918       (with-current-buffer buffer
6919         (gnus-kill-buffer gnus-article-buffer)
6920         (gnus-kill-buffer gnus-original-article-buffer)))
6921     (cond
6922      ;; Kill the buffer.
6923      (gnus-kill-summary-on-exit
6924       (when (and gnus-use-trees
6925                  (gnus-buffer-exists-p buffer))
6926         (save-excursion
6927           (set-buffer buffer)
6928           (gnus-tree-close gnus-newsgroup-name)))
6929       (gnus-kill-buffer buffer))
6930      ;; Deaden the buffer.
6931      ((gnus-buffer-exists-p buffer)
6932       (save-excursion
6933         (set-buffer buffer)
6934         (gnus-deaden-summary))))))
6935
6936 (defun gnus-summary-wake-up-the-dead (&rest args)
6937   "Wake up the dead summary buffer."
6938   (interactive)
6939   (gnus-dead-summary-mode -1)
6940   (let ((name (buffer-name)))
6941     (when (string-match "Dead " name)
6942       (rename-buffer
6943        (concat (substring name 0 (match-beginning 0))
6944                (substring name (match-end 0)))
6945        t)))
6946   (gnus-message 3 "This dead summary is now alive again"))
6947
6948 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6949 (defun gnus-summary-fetch-faq (&optional faq-dir)
6950   "Fetch the FAQ for the current group.
6951 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6952 in."
6953   (interactive
6954    (list
6955     (when current-prefix-arg
6956       (completing-read
6957        "FAQ dir: " (and (listp gnus-group-faq-directory)
6958                         (mapcar 'list
6959                                 gnus-group-faq-directory))))))
6960   (let (gnus-faq-buffer)
6961     (when (setq gnus-faq-buffer
6962                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6963       (gnus-configure-windows 'summary-faq))))
6964
6965 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6966 (defun gnus-summary-describe-group (&optional force)
6967   "Describe the current newsgroup."
6968   (interactive "P")
6969   (gnus-group-describe-group force gnus-newsgroup-name))
6970
6971 (defun gnus-summary-describe-briefly ()
6972   "Describe summary mode commands briefly."
6973   (interactive)
6974   (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")))
6975
6976 ;; Walking around group mode buffer from summary mode.
6977
6978 (defun gnus-summary-next-group (&optional no-article target-group backward)
6979   "Exit current newsgroup and then select next unread newsgroup.
6980 If prefix argument NO-ARTICLE is non-nil, no article is selected
6981 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6982 previous group instead."
6983   (interactive "P")
6984   ;; Stop pre-fetching.
6985   (gnus-async-halt-prefetch)
6986   (let ((current-group gnus-newsgroup-name)
6987         (current-buffer (current-buffer))
6988         entered)
6989     ;; First we semi-exit this group to update Xrefs and all variables.
6990     ;; We can't do a real exit, because the window conf must remain
6991     ;; the same in case the user is prompted for info, and we don't
6992     ;; want the window conf to change before that...
6993     (gnus-summary-exit t)
6994     (while (not entered)
6995       ;; Then we find what group we are supposed to enter.
6996       (set-buffer gnus-group-buffer)
6997       (gnus-group-jump-to-group current-group)
6998       (setq target-group
6999             (or target-group
7000                 (if (eq gnus-keep-same-level 'best)
7001                     (gnus-summary-best-group gnus-newsgroup-name)
7002                   (gnus-summary-search-group backward gnus-keep-same-level))))
7003       (if (not target-group)
7004           ;; There are no further groups, so we return to the group
7005           ;; buffer.
7006           (progn
7007             (gnus-message 5 "Returning to the group buffer")
7008             (setq entered t)
7009             (when (gnus-buffer-live-p current-buffer)
7010               (set-buffer current-buffer)
7011               (gnus-summary-exit))
7012             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7013         ;; We try to enter the target group.
7014         (gnus-group-jump-to-group target-group)
7015         (let ((unreads (gnus-group-group-unread)))
7016           (if (and (or (eq t unreads)
7017                        (and unreads (not (zerop unreads))))
7018                    (gnus-summary-read-group
7019                     target-group nil no-article
7020                     (and (buffer-name current-buffer) current-buffer)
7021                     nil backward))
7022               (setq entered t)
7023             (setq current-group target-group
7024                   target-group nil)))))))
7025
7026 (defun gnus-summary-prev-group (&optional no-article)
7027   "Exit current newsgroup and then select previous unread newsgroup.
7028 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7029   (interactive "P")
7030   (gnus-summary-next-group no-article nil t))
7031
7032 ;; Walking around summary lines.
7033
7034 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7035   "Go to the first subject satisfying any non-nil constraint.
7036 If UNREAD is non-nil, the article should be unread.
7037 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7038 If UNSEEN is non-nil, the article should be unseen.
7039 Returns the article selected or nil if there are no matching articles."
7040   (interactive "P")
7041   (cond
7042    ;; Empty summary.
7043    ((null gnus-newsgroup-data)
7044     (gnus-message 3 "No articles in the group")
7045     nil)
7046    ;; Pick the first article.
7047    ((not (or unread undownloaded unseen))
7048     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7049     (gnus-data-number (car gnus-newsgroup-data)))
7050    ;; Find the first unread article.
7051    (t
7052     (let ((data gnus-newsgroup-data))
7053       (while (and data
7054                   (let ((num (gnus-data-number (car data))))
7055                     (or (memq num gnus-newsgroup-unfetched)
7056                         (not (or (and unread
7057                                       (memq num gnus-newsgroup-unreads))
7058                                  (and undownloaded
7059                                       (memq num gnus-newsgroup-undownloaded))
7060                                  (and unseen
7061                                       (memq num gnus-newsgroup-unseen)))))))
7062         (setq data (cdr data)))
7063       (prog1
7064           (if data
7065               (progn
7066                 (goto-char (gnus-data-pos (car data)))
7067                 (gnus-data-number (car data)))
7068             (gnus-message 3 "No more%s articles"
7069                           (let* ((r (when unread " unread"))
7070                                  (d (when undownloaded " undownloaded"))
7071                                  (s (when unseen " unseen"))
7072                                  (l (delq nil (list r d s))))
7073                             (cond ((= 3 (length l))
7074                                    (concat r "," d ", or" s))
7075                                   ((= 2 (length l))
7076                                    (concat (car l) ", or" (cadr l)))
7077                                   ((= 1 (length l))
7078                                    (car l))
7079                                   (t
7080                                    ""))))
7081             nil
7082             )
7083         (gnus-summary-position-point))))))
7084
7085 (defun gnus-summary-next-subject (n &optional unread dont-display)
7086   "Go to next N'th summary line.
7087 If N is negative, go to the previous N'th subject line.
7088 If UNREAD is non-nil, only unread articles are selected.
7089 The difference between N and the actual number of steps taken is
7090 returned."
7091   (interactive "p")
7092   (let ((backward (< n 0))
7093         (n (abs n)))
7094     (while (and (> n 0)
7095                 (if backward
7096                     (gnus-summary-find-prev unread)
7097                   (gnus-summary-find-next unread)))
7098       (unless (zerop (setq n (1- n)))
7099         (gnus-summary-show-thread)))
7100     (when (/= 0 n)
7101       (gnus-message 7 "No more%s articles"
7102                     (if unread " unread" "")))
7103     (unless dont-display
7104       (gnus-summary-recenter)
7105       (gnus-summary-position-point))
7106     n))
7107
7108 (defun gnus-summary-next-unread-subject (n)
7109   "Go to next N'th unread summary line."
7110   (interactive "p")
7111   (gnus-summary-next-subject n t))
7112
7113 (defun gnus-summary-prev-subject (n &optional unread)
7114   "Go to previous N'th summary line.
7115 If optional argument UNREAD is non-nil, only unread article is selected."
7116   (interactive "p")
7117   (gnus-summary-next-subject (- n) unread))
7118
7119 (defun gnus-summary-prev-unread-subject (n)
7120   "Go to previous N'th unread summary line."
7121   (interactive "p")
7122   (gnus-summary-next-subject (- n) t))
7123
7124 (defun gnus-summary-goto-subjects (articles)
7125   "Insert the subject header for ARTICLES in the current buffer."
7126   (save-excursion
7127     (dolist (article articles)
7128       (gnus-summary-goto-subject article t)))
7129   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7130   (gnus-summary-position-point))
7131
7132 (defun gnus-summary-goto-subject (article &optional force silent)
7133   "Go the subject line of ARTICLE.
7134 If FORCE, also allow jumping to articles not currently shown."
7135   (interactive "nArticle number: ")
7136   (unless (numberp article)
7137     (error "Article %s is not a number" article))
7138   (let ((b (point))
7139         (data (gnus-data-find article)))
7140     ;; We read in the article if we have to.
7141     (and (not data)
7142          force
7143          (gnus-summary-insert-subject
7144           article
7145           (if (or (numberp force) (vectorp force)) force)
7146           t)
7147          (setq data (gnus-data-find article)))
7148     (goto-char b)
7149     (if (not data)
7150         (progn
7151           (unless silent
7152             (gnus-message 3 "Can't find article %d" article))
7153           nil)
7154       (let ((pt (gnus-data-pos data)))
7155         (goto-char pt)
7156         (gnus-summary-set-article-display-arrow pt))
7157       (gnus-summary-position-point)
7158       article)))
7159
7160 ;; Walking around summary lines with displaying articles.
7161
7162 (defun gnus-summary-expand-window (&optional arg)
7163   "Make the summary buffer take up the entire Emacs frame.
7164 Given a prefix, will force an `article' buffer configuration."
7165   (interactive "P")
7166   (if arg
7167       (gnus-configure-windows 'article 'force)
7168     (gnus-configure-windows 'summary 'force)))
7169
7170 (defun gnus-summary-display-article (article &optional all-header)
7171   "Display ARTICLE in article buffer."
7172   (when (gnus-buffer-live-p gnus-article-buffer)
7173     (with-current-buffer gnus-article-buffer
7174       (mm-enable-multibyte)))
7175   (gnus-set-global-variables)
7176   (when (gnus-buffer-live-p gnus-article-buffer)
7177     (with-current-buffer gnus-article-buffer
7178       (setq gnus-article-charset gnus-newsgroup-charset)
7179       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7180       (mm-enable-multibyte)))
7181   (if (null article)
7182       nil
7183     (prog1
7184         (if gnus-summary-display-article-function
7185             (funcall gnus-summary-display-article-function article all-header)
7186           (gnus-article-prepare article all-header))
7187       (gnus-run-hooks 'gnus-select-article-hook)
7188       (when (and gnus-current-article
7189                  (not (zerop gnus-current-article)))
7190         (gnus-summary-goto-subject gnus-current-article))
7191       (gnus-summary-recenter)
7192       (when (and gnus-use-trees gnus-show-threads)
7193         (gnus-possibly-generate-tree article)
7194         (gnus-highlight-selected-tree article))
7195       ;; Successfully display article.
7196       (gnus-article-set-window-start
7197        (cdr (assq article gnus-newsgroup-bookmarks))))))
7198
7199 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7200   "Select the current article.
7201 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7202 non-nil, the article will be re-fetched even if it already present in
7203 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7204 be displayed."
7205   ;; Make sure we are in the summary buffer to work around bbdb bug.
7206   (unless (eq major-mode 'gnus-summary-mode)
7207     (set-buffer gnus-summary-buffer))
7208   (let ((article (or article (gnus-summary-article-number)))
7209         (all-headers (not (not all-headers))) ;Must be t or nil.
7210         gnus-summary-display-article-function)
7211     (and (not pseudo)
7212          (gnus-summary-article-pseudo-p article)
7213          (error "This is a pseudo-article"))
7214     (save-excursion
7215       (set-buffer gnus-summary-buffer)
7216       (if (or (and gnus-single-article-buffer
7217                    (or (null gnus-current-article)
7218                        (null gnus-article-current)
7219                        (null (get-buffer gnus-article-buffer))
7220                        (not (eq article (cdr gnus-article-current)))
7221                        (not (equal (car gnus-article-current)
7222                                    gnus-newsgroup-name))))
7223               (and (not gnus-single-article-buffer)
7224                    (or (null gnus-current-article)
7225                        (not (eq gnus-current-article article))))
7226               force)
7227           ;; The requested article is different from the current article.
7228           (progn
7229             (gnus-summary-display-article article all-headers)
7230             (when (gnus-buffer-live-p gnus-article-buffer)
7231               (with-current-buffer gnus-article-buffer
7232                 (if (not gnus-article-decoded-p) ;; a local variable
7233                     (mm-disable-multibyte))))
7234             (gnus-article-set-window-start
7235              (cdr (assq article gnus-newsgroup-bookmarks)))
7236             article)
7237         'old))))
7238
7239 (defun gnus-summary-force-verify-and-decrypt ()
7240   "Display buttons for signed/encrypted parts and verify/decrypt them."
7241   (interactive)
7242   (let ((mm-verify-option 'known)
7243         (mm-decrypt-option 'known)
7244         (gnus-article-emulate-mime t)
7245         (gnus-buttonized-mime-types (append (list "multipart/signed"
7246                                                   "multipart/encrypted")
7247                                             gnus-buttonized-mime-types)))
7248     (gnus-summary-select-article nil 'force)))
7249
7250 (defun gnus-summary-set-current-mark (&optional current-mark)
7251   "Obsolete function."
7252   nil)
7253
7254 (defun gnus-summary-next-article (&optional unread subject backward push)
7255   "Select the next article.
7256 If UNREAD, only unread articles are selected.
7257 If SUBJECT, only articles with SUBJECT are selected.
7258 If BACKWARD, the previous article is selected instead of the next."
7259   (interactive "P")
7260   (cond
7261    ;; Is there such an article?
7262    ((and (gnus-summary-search-forward unread subject backward)
7263          (or (gnus-summary-display-article (gnus-summary-article-number))
7264              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7265     (gnus-summary-position-point))
7266    ;; If not, we try the first unread, if that is wanted.
7267    ((and subject
7268          gnus-auto-select-same
7269          (gnus-summary-first-unread-article))
7270     (gnus-summary-position-point)
7271     (gnus-message 6 "Wrapped"))
7272    ;; Try to get next/previous article not displayed in this group.
7273    ((and gnus-auto-extend-newsgroup
7274          (not unread) (not subject))
7275     (gnus-summary-goto-article
7276      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7277      nil (count-lines (point-min) (point))))
7278    ;; Go to next/previous group.
7279    (t
7280     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7281       (gnus-summary-jump-to-group gnus-newsgroup-name))
7282     (let ((cmd last-command-char)
7283           (point
7284            (with-current-buffer gnus-group-buffer
7285              (point)))
7286           (group
7287            (if (eq gnus-keep-same-level 'best)
7288                (gnus-summary-best-group gnus-newsgroup-name)
7289              (gnus-summary-search-group backward gnus-keep-same-level))))
7290       ;; For some reason, the group window gets selected.  We change
7291       ;; it back.
7292       (select-window (get-buffer-window (current-buffer)))
7293       ;; Select next unread newsgroup automagically.
7294       (cond
7295        ((or (not gnus-auto-select-next)
7296             (not cmd))
7297         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7298        ((or (eq gnus-auto-select-next 'quietly)
7299             (and (eq gnus-auto-select-next 'slightly-quietly)
7300                  push)
7301             (and (eq gnus-auto-select-next 'almost-quietly)
7302                  (gnus-summary-last-article-p)))
7303         ;; Select quietly.
7304         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7305             (gnus-summary-exit)
7306           (gnus-message 7 "No more%s articles (%s)..."
7307                         (if unread " unread" "")
7308                         (if group (concat "selecting " group)
7309                           "exiting"))
7310           (gnus-summary-next-group nil group backward)))
7311        (t
7312         (when (gnus-key-press-event-p last-input-event)
7313           (gnus-summary-walk-group-buffer
7314            gnus-newsgroup-name cmd unread backward point))))))))
7315
7316 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7317   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7318                       (?\C-p (gnus-group-prev-unread-group 1))))
7319         (cursor-in-echo-area t)
7320         keve key group ended prompt)
7321     (save-excursion
7322       (set-buffer gnus-group-buffer)
7323       (goto-char start)
7324       (setq group
7325             (if (eq gnus-keep-same-level 'best)
7326                 (gnus-summary-best-group gnus-newsgroup-name)
7327               (gnus-summary-search-group backward gnus-keep-same-level))))
7328     (while (not ended)
7329       (setq prompt
7330             (format
7331              "No more%s articles%s " (if unread " unread" "")
7332              (if (and group
7333                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7334                  (format " (Type %s for %s [%s])"
7335                          (single-key-description cmd)
7336                          (gnus-group-decoded-name group)
7337                          (gnus-group-unread group))
7338                (format " (Type %s to exit %s)"
7339                        (single-key-description cmd)
7340                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7341       ;; Confirm auto selection.
7342       (setq key (car (setq keve (gnus-read-event-char prompt)))
7343             ended t)
7344       (cond
7345        ((assq key keystrokes)
7346         (let ((obuf (current-buffer)))
7347           (switch-to-buffer gnus-group-buffer)
7348           (when group
7349             (gnus-group-jump-to-group group))
7350           (eval (cadr (assq key keystrokes)))
7351           (setq group (gnus-group-group-name))
7352           (switch-to-buffer obuf))
7353         (setq ended nil))
7354        ((equal key cmd)
7355         (if (or (not group)
7356                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7357             (gnus-summary-exit)
7358           (gnus-summary-next-group nil group backward)))
7359        (t
7360         (push (cdr keve) unread-command-events))))))
7361
7362 (defun gnus-summary-next-unread-article ()
7363   "Select unread article after current one."
7364   (interactive)
7365   (gnus-summary-next-article
7366    (or (not (eq gnus-summary-goto-unread 'never))
7367        (gnus-summary-last-article-p (gnus-summary-article-number)))
7368    (and gnus-auto-select-same
7369         (gnus-summary-article-subject))))
7370
7371 (defun gnus-summary-prev-article (&optional unread subject)
7372   "Select the article after the current one.
7373 If UNREAD is non-nil, only unread articles are selected."
7374   (interactive "P")
7375   (gnus-summary-next-article unread subject t))
7376
7377 (defun gnus-summary-prev-unread-article ()
7378   "Select unread article before current one."
7379   (interactive)
7380   (gnus-summary-prev-article
7381    (or (not (eq gnus-summary-goto-unread 'never))
7382        (gnus-summary-first-article-p (gnus-summary-article-number)))
7383    (and gnus-auto-select-same
7384         (gnus-summary-article-subject))))
7385
7386 (defun gnus-summary-next-page (&optional lines circular stop)
7387   "Show next page of the selected article.
7388 If at the end of the current article, select the next article.
7389 LINES says how many lines should be scrolled up.
7390
7391 If CIRCULAR is non-nil, go to the start of the article instead of
7392 selecting the next article when reaching the end of the current
7393 article.
7394
7395 If STOP is non-nil, just stop when reaching the end of the message.
7396
7397 Also see the variable `gnus-article-skip-boring'."
7398   (interactive "P")
7399   (setq gnus-summary-buffer (current-buffer))
7400   (gnus-set-global-variables)
7401   (let ((article (gnus-summary-article-number))
7402         (article-window (get-buffer-window gnus-article-buffer t))
7403         endp)
7404     ;; If the buffer is empty, we have no article.
7405     (unless article
7406       (error "No article to select"))
7407     (gnus-configure-windows 'article)
7408     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7409         (if (and (eq gnus-summary-goto-unread 'never)
7410                  (not (gnus-summary-last-article-p article)))
7411             (gnus-summary-next-article)
7412           (gnus-summary-next-unread-article))
7413       (if (or (null gnus-current-article)
7414               (null gnus-article-current)
7415               (/= article (cdr gnus-article-current))
7416               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7417           ;; Selected subject is different from current article's.
7418           (gnus-summary-display-article article)
7419         (when article-window
7420           (gnus-eval-in-buffer-window gnus-article-buffer
7421             (setq endp (or (gnus-article-next-page lines)
7422                            (gnus-article-only-boring-p))))
7423           (when endp
7424             (cond (stop
7425                    (gnus-message 3 "End of message"))
7426                   (circular
7427                    (gnus-summary-beginning-of-article))
7428                   (lines
7429                    (gnus-message 3 "End of message"))
7430                   ((null lines)
7431                    (if (and (eq gnus-summary-goto-unread 'never)
7432                             (not (gnus-summary-last-article-p article)))
7433                        (gnus-summary-next-article)
7434                      (gnus-summary-next-unread-article))))))))
7435     (gnus-summary-recenter)
7436     (gnus-summary-position-point)))
7437
7438 (defun gnus-summary-prev-page (&optional lines move)
7439   "Show previous page of selected article.
7440 Argument LINES specifies lines to be scrolled down.
7441 If MOVE, move to the previous unread article if point is at
7442 the beginning of the buffer."
7443   (interactive "P")
7444   (let ((article (gnus-summary-article-number))
7445         (article-window (get-buffer-window gnus-article-buffer t))
7446         endp)
7447     (gnus-configure-windows 'article)
7448     (if (or (null gnus-current-article)
7449             (null gnus-article-current)
7450             (/= article (cdr gnus-article-current))
7451             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7452         ;; Selected subject is different from current article's.
7453         (gnus-summary-display-article article)
7454       (gnus-summary-recenter)
7455       (when article-window
7456         (gnus-eval-in-buffer-window gnus-article-buffer
7457           (setq endp (gnus-article-prev-page lines)))
7458         (when (and move endp)
7459           (cond (lines
7460                  (gnus-message 3 "Beginning of message"))
7461                 ((null lines)
7462                  (if (and (eq gnus-summary-goto-unread 'never)
7463                           (not (gnus-summary-first-article-p article)))
7464                      (gnus-summary-prev-article)
7465                    (gnus-summary-prev-unread-article))))))))
7466   (gnus-summary-position-point))
7467
7468 (defun gnus-summary-prev-page-or-article (&optional lines)
7469   "Show previous page of selected article.
7470 Argument LINES specifies lines to be scrolled down.
7471 If at the beginning of the article, go to the next article."
7472   (interactive "P")
7473   (gnus-summary-prev-page lines t))
7474
7475 (defun gnus-summary-scroll-up (lines)
7476   "Scroll up (or down) one line current article.
7477 Argument LINES specifies lines to be scrolled up (or down if negative)."
7478   (interactive "p")
7479   (gnus-configure-windows 'article)
7480   (gnus-summary-show-thread)
7481   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7482     (gnus-eval-in-buffer-window gnus-article-buffer
7483       (cond ((> lines 0)
7484              (when (gnus-article-next-page lines)
7485                (gnus-message 3 "End of message")))
7486             ((< lines 0)
7487              (gnus-article-prev-page (- lines))))))
7488   (gnus-summary-recenter)
7489   (gnus-summary-position-point))
7490
7491 (defun gnus-summary-scroll-down (lines)
7492   "Scroll down (or up) one line current article.
7493 Argument LINES specifies lines to be scrolled down (or up if negative)."
7494   (interactive "p")
7495   (gnus-summary-scroll-up (- lines)))
7496
7497 (defun gnus-summary-next-same-subject ()
7498   "Select next article which has the same subject as current one."
7499   (interactive)
7500   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7501
7502 (defun gnus-summary-prev-same-subject ()
7503   "Select previous article which has the same subject as current one."
7504   (interactive)
7505   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7506
7507 (defun gnus-summary-next-unread-same-subject ()
7508   "Select next unread article which has the same subject as current one."
7509   (interactive)
7510   (gnus-summary-next-article t (gnus-summary-article-subject)))
7511
7512 (defun gnus-summary-prev-unread-same-subject ()
7513   "Select previous unread article which has the same subject as current one."
7514   (interactive)
7515   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7516
7517 (defun gnus-summary-first-unread-article ()
7518   "Select the first unread article.
7519 Return nil if there are no unread articles."
7520   (interactive)
7521   (prog1
7522       (when (gnus-summary-first-subject t)
7523         (gnus-summary-show-thread)
7524         (gnus-summary-first-subject t)
7525         (gnus-summary-display-article (gnus-summary-article-number)))
7526     (gnus-summary-position-point)))
7527
7528 (defun gnus-summary-first-unread-subject ()
7529   "Place the point on the subject line of the first unread article.
7530 Return nil if there are no unread articles."
7531   (interactive)
7532   (prog1
7533       (when (gnus-summary-first-subject t)
7534         (gnus-summary-show-thread)
7535         (gnus-summary-first-subject t))
7536     (gnus-summary-position-point)))
7537
7538 (defun gnus-summary-first-unseen-subject ()
7539   "Place the point on the subject line of the first unseen article.
7540 Return nil if there are no unseen articles."
7541   (interactive)
7542   (prog1
7543       (when (gnus-summary-first-subject nil nil t)
7544         (gnus-summary-show-thread)
7545         (gnus-summary-first-subject nil nil t))
7546     (gnus-summary-position-point)))
7547
7548 (defun gnus-summary-first-unseen-or-unread-subject ()
7549   "Place the point on the subject line of the first unseen article or,
7550 if all article have been seen, on the subject line of the first unread
7551 article."
7552   (interactive)
7553   (prog1
7554       (unless (when (gnus-summary-first-subject nil nil t)
7555                 (gnus-summary-show-thread)
7556                 (gnus-summary-first-subject nil nil t))
7557         (when (gnus-summary-first-subject t)
7558           (gnus-summary-show-thread)
7559           (gnus-summary-first-subject t)))
7560     (gnus-summary-position-point)))
7561
7562 (defun gnus-summary-first-article ()
7563   "Select the first article.
7564 Return nil if there are no articles."
7565   (interactive)
7566   (prog1
7567       (when (gnus-summary-first-subject)
7568         (gnus-summary-show-thread)
7569         (gnus-summary-first-subject)
7570         (gnus-summary-display-article (gnus-summary-article-number)))
7571     (gnus-summary-position-point)))
7572
7573 (defun gnus-summary-best-unread-article (&optional arg)
7574   "Select the unread article with the highest score.
7575 If given a prefix argument, select the next unread article that has a
7576 score higher than the default score."
7577   (interactive "P")
7578   (let ((article (if arg
7579                      (gnus-summary-better-unread-subject)
7580                    (gnus-summary-best-unread-subject))))
7581     (if article
7582         (gnus-summary-goto-article article)
7583       (error "No unread articles"))))
7584
7585 (defun gnus-summary-best-unread-subject ()
7586   "Select the unread subject with the highest score."
7587   (interactive)
7588   (let ((best -1000000)
7589         (data gnus-newsgroup-data)
7590         article score)
7591     (while data
7592       (and (gnus-data-unread-p (car data))
7593            (> (setq score
7594                     (gnus-summary-article-score (gnus-data-number (car data))))
7595               best)
7596            (setq best score
7597                  article (gnus-data-number (car data))))
7598       (setq data (cdr data)))
7599     (when article
7600       (gnus-summary-goto-subject article))
7601     (gnus-summary-position-point)
7602     article))
7603
7604 (defun gnus-summary-better-unread-subject ()
7605   "Select the first unread subject that has a score over the default score."
7606   (interactive)
7607   (let ((data gnus-newsgroup-data)
7608         article score)
7609     (while (and (setq article (gnus-data-number (car data)))
7610                 (or (gnus-data-read-p (car data))
7611                     (not (> (gnus-summary-article-score article)
7612                             gnus-summary-default-score))))
7613       (setq data (cdr data)))
7614     (when article
7615       (gnus-summary-goto-subject article))
7616     (gnus-summary-position-point)
7617     article))
7618
7619 (defun gnus-summary-last-subject ()
7620   "Go to the last displayed subject line in the group."
7621   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7622     (when article
7623       (gnus-summary-goto-subject article))))
7624
7625 (defun gnus-summary-goto-article (article &optional all-headers force)
7626   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7627 If ALL-HEADERS is non-nil, no header lines are hidden.
7628 If FORCE, go to the article even if it isn't displayed.  If FORCE
7629 is a number, it is the line the article is to be displayed on."
7630   (interactive
7631    (list
7632     (completing-read
7633      "Article number or Message-ID: "
7634      (mapcar (lambda (number) (list (int-to-string number)))
7635              gnus-newsgroup-limit))
7636     current-prefix-arg
7637     t))
7638   (prog1
7639       (if (and (stringp article)
7640                (string-match "@\\|%40" article))
7641           (gnus-summary-refer-article article)
7642         (when (stringp article)
7643           (setq article (string-to-number article)))
7644         (if (gnus-summary-goto-subject article force)
7645             (gnus-summary-display-article article all-headers)
7646           (gnus-message 4 "Couldn't go to article %s" article) nil))
7647     (gnus-summary-position-point)))
7648
7649 (defun gnus-summary-goto-last-article ()
7650   "Go to the previously read article."
7651   (interactive)
7652   (prog1
7653       (when gnus-last-article
7654         (gnus-summary-goto-article gnus-last-article nil t))
7655     (gnus-summary-position-point)))
7656
7657 (defun gnus-summary-pop-article (number)
7658   "Pop one article off the history and go to the previous.
7659 NUMBER articles will be popped off."
7660   (interactive "p")
7661   (let (to)
7662     (setq gnus-newsgroup-history
7663           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7664     (if to
7665         (gnus-summary-goto-article (car to) nil t)
7666       (error "Article history empty")))
7667   (gnus-summary-position-point))
7668
7669 ;; Summary commands and functions for limiting the summary buffer.
7670
7671 (defun gnus-summary-limit-to-articles (n)
7672   "Limit the summary buffer to the next N articles.
7673 If not given a prefix, use the process marked articles instead."
7674   (interactive "P")
7675   (prog1
7676       (let ((articles (gnus-summary-work-articles n)))
7677         (setq gnus-newsgroup-processable nil)
7678         (gnus-summary-limit articles))
7679     (gnus-summary-position-point)))
7680
7681 (defun gnus-summary-pop-limit (&optional total)
7682   "Restore the previous limit.
7683 If given a prefix, remove all limits."
7684   (interactive "P")
7685   (when total
7686     (setq gnus-newsgroup-limits
7687           (list (mapcar (lambda (h) (mail-header-number h))
7688                         gnus-newsgroup-headers))))
7689   (unless gnus-newsgroup-limits
7690     (error "No limit to pop"))
7691   (prog1
7692       (gnus-summary-limit nil 'pop)
7693     (gnus-summary-position-point)))
7694
7695 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7696   "Limit the summary buffer to articles that have subjects that match a regexp.
7697 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7698   (interactive
7699    (list (read-string (if current-prefix-arg
7700                           "Exclude subject (regexp): "
7701                         "Limit to subject (regexp): "))
7702          nil current-prefix-arg))
7703   (unless header
7704     (setq header "subject"))
7705   (when (not (equal "" subject))
7706     (prog1
7707         (let ((articles (gnus-summary-find-matching
7708                          (or header "subject") subject 'all nil nil
7709                          not-matching)))
7710           (unless articles
7711             (error "Found no matches for \"%s\"" subject))
7712           (gnus-summary-limit articles))
7713       (gnus-summary-position-point))))
7714
7715 (defun gnus-summary-limit-to-author (from &optional not-matching)
7716   "Limit the summary buffer to articles that have authors that match a regexp.
7717 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7718   (interactive
7719    (list (read-string (if current-prefix-arg
7720                           "Exclude author (regexp): "
7721                         "Limit to author (regexp): "))
7722          current-prefix-arg))
7723   (gnus-summary-limit-to-subject from "from" not-matching))
7724
7725 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7726   "Limit the summary buffer to articles with the given RECIPIENT.
7727
7728 If NOT-MATCHING, exclude RECIPIENT.
7729
7730 To and Cc headers are checked.  You need to include them in
7731 `nnmail-extra-headers'."
7732   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7733   (interactive
7734    (list (read-string (format "%s recipient (regexp): "
7735                               (if current-prefix-arg "Exclude" "Limit to")))
7736          current-prefix-arg))
7737   (when (not (equal "" recipient))
7738     (prog1 (let* ((to
7739                    (if (memq 'To nnmail-extra-headers)
7740                        (gnus-summary-find-matching
7741                         (cons 'extra 'To) recipient 'all nil nil
7742                         not-matching)
7743                      (gnus-message
7744                       1 "`To' isn't present in `nnmail-extra-headers'")
7745                      (sit-for 1)
7746                      nil))
7747                   (cc
7748                    (if (memq 'Cc nnmail-extra-headers)
7749                        (gnus-summary-find-matching
7750                         (cons 'extra 'Cc) recipient 'all nil nil
7751                         not-matching)
7752                      (gnus-message
7753                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7754                      (sit-for 1)
7755                      nil))
7756                   (articles
7757                    (if not-matching
7758                        ;; We need the numbers that are in both lists:
7759                        (mapcar (lambda (a)
7760                                  (and (memq a to) a))
7761                                cc)
7762                      (nconc to cc))))
7763              (unless articles
7764                (error "Found no matches for \"%s\"" recipient))
7765              (gnus-summary-limit articles))
7766       (gnus-summary-position-point))))
7767
7768 (defun gnus-summary-limit-to-age (age &optional younger-p)
7769   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7770 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7771 articles that are younger than AGE days."
7772   (interactive
7773    (let ((younger current-prefix-arg)
7774          (days-got nil)
7775          days)
7776      (while (not days-got)
7777        (setq days (if younger
7778                       (read-string "Limit to articles younger than (in days, older when negative): ")
7779                     (read-string
7780                      "Limit to articles older than (in days, younger when negative): ")))
7781        (when (> (length days) 0)
7782          (setq days (read days)))
7783        (if (numberp days)
7784            (progn
7785              (setq days-got t)
7786              (when (< days 0)
7787                (setq younger (not younger))
7788                (setq days (* days -1))))
7789          (message "Please enter a number.")
7790          (sleep-for 1)))
7791      (list days younger)))
7792   (prog1
7793       (let ((data gnus-newsgroup-data)
7794             (cutoff (days-to-time age))
7795             articles d date is-younger)
7796         (while (setq d (pop data))
7797           (when (and (vectorp (gnus-data-header d))
7798                      (setq date (mail-header-date (gnus-data-header d))))
7799             (setq is-younger (time-less-p
7800                               (time-since (condition-case ()
7801                                               (date-to-time date)
7802                                             (error '(0 0))))
7803                               cutoff))
7804             (when (if younger-p
7805                       is-younger
7806                     (not is-younger))
7807               (push (gnus-data-number d) articles))))
7808         (gnus-summary-limit (nreverse articles)))
7809     (gnus-summary-position-point)))
7810
7811 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7812   "Limit the summary buffer to articles that match an 'extra' header."
7813   (interactive
7814    (let ((header
7815           (intern
7816            (gnus-completing-read-with-default
7817             (symbol-name (car gnus-extra-headers))
7818             (if current-prefix-arg
7819                 "Exclude extra header"
7820               "Limit extra header")
7821             (mapcar (lambda (x)
7822                       (cons (symbol-name x) x))
7823                     gnus-extra-headers)
7824             nil
7825             t))))
7826      (list header
7827            (read-string (format "%s header %s (regexp): "
7828                                 (if current-prefix-arg "Exclude" "Limit to")
7829                                 header))
7830            current-prefix-arg)))
7831   (when (not (equal "" regexp))
7832     (prog1
7833         (let ((articles (gnus-summary-find-matching
7834                          (cons 'extra header) regexp 'all nil nil
7835                          not-matching)))
7836           (unless articles
7837             (error "Found no matches for \"%s\"" regexp))
7838           (gnus-summary-limit articles))
7839       (gnus-summary-position-point))))
7840
7841 (defun gnus-summary-limit-to-display-predicate ()
7842   "Limit the summary buffer to the predicated in the `display' group parameter."
7843   (interactive)
7844   (unless gnus-newsgroup-display
7845     (error "There is no `display' group parameter"))
7846   (let (articles)
7847     (dolist (number gnus-newsgroup-articles)
7848       (when (funcall gnus-newsgroup-display)
7849         (push number articles)))
7850     (gnus-summary-limit articles))
7851   (gnus-summary-position-point))
7852
7853 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7854 (make-obsolete
7855  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7856
7857 (defun gnus-summary-limit-to-unread (&optional all)
7858   "Limit the summary buffer to articles that are not marked as read.
7859 If ALL is non-nil, limit strictly to unread articles."
7860   (interactive "P")
7861   (if all
7862       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7863     (gnus-summary-limit-to-marks
7864      ;; Concat all the marks that say that an article is read and have
7865      ;; those removed.
7866      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7867            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7868            gnus-low-score-mark gnus-expirable-mark
7869            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7870            gnus-duplicate-mark gnus-souped-mark)
7871      'reverse)))
7872
7873 (defun gnus-summary-limit-to-replied (&optional unreplied)
7874   "Limit the summary buffer to replied articles.
7875 If UNREPLIED (the prefix), limit to unreplied articles."
7876   (interactive "P")
7877   (if unreplied
7878       (gnus-summary-limit
7879        (gnus-set-difference gnus-newsgroup-articles
7880         gnus-newsgroup-replied))
7881     (gnus-summary-limit gnus-newsgroup-replied))
7882   (gnus-summary-position-point))
7883
7884 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7885 (make-obsolete 'gnus-summary-delete-marked-with
7886                'gnus-summary-limit-exclude-marks)
7887
7888 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7889   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7890 If REVERSE, limit the summary buffer to articles that are marked
7891 with MARKS.  MARKS can either be a string of marks or a list of marks.
7892 Returns how many articles were removed."
7893   (interactive "sMarks: ")
7894   (gnus-summary-limit-to-marks marks t))
7895
7896 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7897   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7898 If REVERSE (the prefix), limit the summary buffer to articles that are
7899 not marked with MARKS.  MARKS can either be a string of marks or a
7900 list of marks.
7901 Returns how many articles were removed."
7902   (interactive "sMarks: \nP")
7903   (prog1
7904       (let ((data gnus-newsgroup-data)
7905             (marks (if (listp marks) marks
7906                      (append marks nil))) ; Transform to list.
7907             articles)
7908         (while data
7909           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7910                   (memq (gnus-data-mark (car data)) marks))
7911             (push (gnus-data-number (car data)) articles))
7912           (setq data (cdr data)))
7913         (gnus-summary-limit articles))
7914     (gnus-summary-position-point)))
7915
7916 (defun gnus-summary-limit-to-score (score)
7917   "Limit to articles with score at or above SCORE."
7918   (interactive "NLimit to articles with score of at least: ")
7919   (let ((data gnus-newsgroup-data)
7920         articles)
7921     (while data
7922       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7923                 score)
7924         (push (gnus-data-number (car data)) articles))
7925       (setq data (cdr data)))
7926     (prog1
7927         (gnus-summary-limit articles)
7928       (gnus-summary-position-point))))
7929
7930 (defun gnus-summary-limit-to-unseen ()
7931   "Limit to unseen articles."
7932   (interactive)
7933   (prog1
7934       (gnus-summary-limit gnus-newsgroup-unseen)
7935     (gnus-summary-position-point)))
7936
7937 (defun gnus-summary-limit-include-thread (id)
7938   "Display all the hidden articles that is in the thread with ID in it.
7939 When called interactively, ID is the Message-ID of the current
7940 article."
7941   (interactive (list (mail-header-id (gnus-summary-article-header))))
7942   (let ((articles (gnus-articles-in-thread
7943                    (gnus-id-to-thread (gnus-root-id id)))))
7944     (prog1
7945         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7946       (gnus-summary-limit-include-matching-articles
7947        "subject"
7948        (regexp-quote (gnus-simplify-subject-re
7949                       (mail-header-subject (gnus-id-to-header id)))))
7950       (gnus-summary-position-point))))
7951
7952 (defun gnus-summary-limit-include-matching-articles (header regexp)
7953   "Display all the hidden articles that have HEADERs that match REGEXP."
7954   (interactive (list (read-string "Match on header: ")
7955                      (read-string "Regexp: ")))
7956   (let ((articles (gnus-find-matching-articles header regexp)))
7957     (prog1
7958         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7959       (gnus-summary-position-point))))
7960
7961 (defun gnus-summary-insert-dormant-articles ()
7962   "Insert all the dormant articles for this group into the current buffer."
7963   (interactive)
7964   (let ((gnus-verbose (max 6 gnus-verbose)))
7965     (if (not gnus-newsgroup-dormant)
7966         (gnus-message 3 "No cached articles for this group")
7967       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7968
7969 (defun gnus-summary-limit-include-dormant ()
7970   "Display all the hidden articles that are marked as dormant.
7971 Note that this command only works on a subset of the articles currently
7972 fetched for this group."
7973   (interactive)
7974   (unless gnus-newsgroup-dormant
7975     (error "There are no dormant articles in this group"))
7976   (prog1
7977       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7978     (gnus-summary-position-point)))
7979
7980 (defun gnus-summary-limit-exclude-dormant ()
7981   "Hide all dormant articles."
7982   (interactive)
7983   (prog1
7984       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7985     (gnus-summary-position-point)))
7986
7987 (defun gnus-summary-limit-exclude-childless-dormant ()
7988   "Hide all dormant articles that have no children."
7989   (interactive)
7990   (let ((data (gnus-data-list t))
7991         articles d children)
7992     ;; Find all articles that are either not dormant or have
7993     ;; children.
7994     (while (setq d (pop data))
7995       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7996                 (and (setq children
7997                            (gnus-article-children (gnus-data-number d)))
7998                      (let (found)
7999                        (while children
8000                          (when (memq (car children) articles)
8001                            (setq children nil
8002                                  found t))
8003                          (pop children))
8004                        found)))
8005         (push (gnus-data-number d) articles)))
8006     ;; Do the limiting.
8007     (prog1
8008         (gnus-summary-limit articles)
8009       (gnus-summary-position-point))))
8010
8011 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8012   "Mark all unread excluded articles as read.
8013 If ALL, mark even excluded ticked and dormants as read."
8014   (interactive "P")
8015   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8016   (let ((articles (gnus-sorted-ndifference
8017                    (sort
8018                     (mapcar (lambda (h) (mail-header-number h))
8019                             gnus-newsgroup-headers)
8020                     '<)
8021                    gnus-newsgroup-limit))
8022         article)
8023     (setq gnus-newsgroup-unreads
8024           (gnus-sorted-intersection gnus-newsgroup-unreads
8025                                     gnus-newsgroup-limit))
8026     (if all
8027         (setq gnus-newsgroup-dormant nil
8028               gnus-newsgroup-marked nil
8029               gnus-newsgroup-reads
8030               (nconc
8031                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8032                gnus-newsgroup-reads))
8033       (while (setq article (pop articles))
8034         (unless (or (memq article gnus-newsgroup-dormant)
8035                     (memq article gnus-newsgroup-marked))
8036           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8037
8038 (defun gnus-summary-limit (articles &optional pop)
8039   (if pop
8040       ;; We pop the previous limit off the stack and use that.
8041       (setq articles (car gnus-newsgroup-limits)
8042             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8043     ;; We use the new limit, so we push the old limit on the stack.
8044     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8045   ;; Set the limit.
8046   (setq gnus-newsgroup-limit articles)
8047   (let ((total (length gnus-newsgroup-data))
8048         (data (gnus-data-find-list (gnus-summary-article-number)))
8049         (gnus-summary-mark-below nil)   ; Inhibit this.
8050         found)
8051     ;; This will do all the work of generating the new summary buffer
8052     ;; according to the new limit.
8053     (gnus-summary-prepare)
8054     ;; Hide any threads, possibly.
8055     (gnus-summary-maybe-hide-threads)
8056     ;; Try to return to the article you were at, or one in the
8057     ;; neighborhood.
8058     (when data
8059       ;; We try to find some article after the current one.
8060       (while data
8061         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8062           (setq data nil
8063                 found t))
8064         (setq data (cdr data))))
8065     (unless found
8066       ;; If there is no data, that means that we were after the last
8067       ;; article.  The same goes when we can't find any articles
8068       ;; after the current one.
8069       (goto-char (point-max))
8070       (gnus-summary-find-prev))
8071     (gnus-set-mode-line 'summary)
8072     ;; We return how many articles were removed from the summary
8073     ;; buffer as a result of the new limit.
8074     (- total (length gnus-newsgroup-data))))
8075
8076 (defsubst gnus-invisible-cut-children (threads)
8077   (let ((num 0))
8078     (while threads
8079       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8080         (incf num))
8081       (pop threads))
8082     (< num 2)))
8083
8084 (defsubst gnus-cut-thread (thread)
8085   "Go forwards in the thread until we find an article that we want to display."
8086   (when (or (eq gnus-fetch-old-headers 'some)
8087             (eq gnus-fetch-old-headers 'invisible)
8088             (numberp gnus-fetch-old-headers)
8089             (eq gnus-build-sparse-threads 'some)
8090             (eq gnus-build-sparse-threads 'more))
8091     ;; Deal with old-fetched headers and sparse threads.
8092     (while (and
8093             thread
8094             (or
8095              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8096              (gnus-summary-article-ancient-p
8097               (mail-header-number (car thread))))
8098             (if (or (<= (length (cdr thread)) 1)
8099                     (eq gnus-fetch-old-headers 'invisible))
8100                 (setq gnus-newsgroup-limit
8101                       (delq (mail-header-number (car thread))
8102                             gnus-newsgroup-limit)
8103                       thread (cadr thread))
8104               (when (gnus-invisible-cut-children (cdr thread))
8105                 (let ((th (cdr thread)))
8106                   (while th
8107                     (if (memq (mail-header-number (caar th))
8108                               gnus-newsgroup-limit)
8109                         (setq thread (car th)
8110                               th nil)
8111                       (setq th (cdr th))))))))))
8112   thread)
8113
8114 (defun gnus-cut-threads (threads)
8115   "Cut off all uninteresting articles from the beginning of THREADS."
8116   (when (or (eq gnus-fetch-old-headers 'some)
8117             (eq gnus-fetch-old-headers 'invisible)
8118             (numberp gnus-fetch-old-headers)
8119             (eq gnus-build-sparse-threads 'some)
8120             (eq gnus-build-sparse-threads 'more))
8121     (let ((th threads))
8122       (while th
8123         (setcar th (gnus-cut-thread (car th)))
8124         (setq th (cdr th)))))
8125   ;; Remove nixed out threads.
8126   (delq nil threads))
8127
8128 (defun gnus-summary-initial-limit (&optional show-if-empty)
8129   "Figure out what the initial limit is supposed to be on group entry.
8130 This entails weeding out unwanted dormants, low-scored articles,
8131 fetch-old-headers verbiage, and so on."
8132   ;; Most groups have nothing to remove.
8133   (if (or gnus-inhibit-limiting
8134           (and (null gnus-newsgroup-dormant)
8135                (eq gnus-newsgroup-display 'gnus-not-ignore)
8136                (not (eq gnus-fetch-old-headers 'some))
8137                (not (numberp gnus-fetch-old-headers))
8138                (not (eq gnus-fetch-old-headers 'invisible))
8139                (null gnus-summary-expunge-below)
8140                (not (eq gnus-build-sparse-threads 'some))
8141                (not (eq gnus-build-sparse-threads 'more))
8142                (null gnus-thread-expunge-below)
8143                (not gnus-use-nocem)))
8144       ()                                ; Do nothing.
8145     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8146     (setq gnus-newsgroup-limit nil)
8147     (mapatoms
8148      (lambda (node)
8149        (unless (car (symbol-value node))
8150          ;; These threads have no parents -- they are roots.
8151          (let ((nodes (cdr (symbol-value node)))
8152                thread)
8153            (while nodes
8154              (if (and gnus-thread-expunge-below
8155                       (< (gnus-thread-total-score (car nodes))
8156                          gnus-thread-expunge-below))
8157                  (gnus-expunge-thread (pop nodes))
8158                (setq thread (pop nodes))
8159                (gnus-summary-limit-children thread))))))
8160      gnus-newsgroup-dependencies)
8161     ;; If this limitation resulted in an empty group, we might
8162     ;; pop the previous limit and use it instead.
8163     (when (and (not gnus-newsgroup-limit)
8164                show-if-empty)
8165       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8166     gnus-newsgroup-limit))
8167
8168 (defun gnus-summary-limit-children (thread)
8169   "Return 1 if this subthread is visible and 0 if it is not."
8170   ;; First we get the number of visible children to this thread.  This
8171   ;; is done by recursing down the thread using this function, so this
8172   ;; will really go down to a leaf article first, before slowly
8173   ;; working its way up towards the root.
8174   (when thread
8175     (let* ((max-lisp-eval-depth 5000)
8176            (children
8177            (if (cdr thread)
8178                (apply '+ (mapcar 'gnus-summary-limit-children
8179                                  (cdr thread)))
8180              0))
8181           (number (mail-header-number (car thread)))
8182           score)
8183       (if (and
8184            (not (memq number gnus-newsgroup-marked))
8185            (or
8186             ;; If this article is dormant and has absolutely no visible
8187             ;; children, then this article isn't visible.
8188             (and (memq number gnus-newsgroup-dormant)
8189                  (zerop children))
8190             ;; If this is "fetch-old-headered" and there is no
8191             ;; visible children, then we don't want this article.
8192             (and (or (eq gnus-fetch-old-headers 'some)
8193                      (numberp gnus-fetch-old-headers))
8194                  (gnus-summary-article-ancient-p number)
8195                  (zerop children))
8196             ;; If this is "fetch-old-headered" and `invisible', then
8197             ;; we don't want this article.
8198             (and (eq gnus-fetch-old-headers 'invisible)
8199                  (gnus-summary-article-ancient-p number))
8200             ;; If this is a sparsely inserted article with no children,
8201             ;; we don't want it.
8202             (and (eq gnus-build-sparse-threads 'some)
8203                  (gnus-summary-article-sparse-p number)
8204                  (zerop children))
8205             ;; If we use expunging, and this article is really
8206             ;; low-scored, then we don't want this article.
8207             (when (and gnus-summary-expunge-below
8208                        (< (setq score
8209                                 (or (cdr (assq number gnus-newsgroup-scored))
8210                                     gnus-summary-default-score))
8211                           gnus-summary-expunge-below))
8212               ;; We increase the expunge-tally here, but that has
8213               ;; nothing to do with the limits, really.
8214               (incf gnus-newsgroup-expunged-tally)
8215               ;; We also mark as read here, if that's wanted.
8216               (when (and gnus-summary-mark-below
8217                          (< score gnus-summary-mark-below))
8218                 (setq gnus-newsgroup-unreads
8219                       (delq number gnus-newsgroup-unreads))
8220                 (if gnus-newsgroup-auto-expire
8221                     (push number gnus-newsgroup-expirable)
8222                   (push (cons number gnus-low-score-mark)
8223                         gnus-newsgroup-reads)))
8224               t)
8225             ;; Do the `display' group parameter.
8226             (and gnus-newsgroup-display
8227                  (not (funcall gnus-newsgroup-display)))
8228             ;; Check NoCeM things.
8229             (when (and gnus-use-nocem
8230                        (gnus-nocem-unwanted-article-p
8231                         (mail-header-id (car thread))))
8232               (setq gnus-newsgroup-unreads
8233                     (delq number gnus-newsgroup-unreads))
8234               t)))
8235           ;; Nope, invisible article.
8236           0
8237         ;; Ok, this article is to be visible, so we add it to the limit
8238         ;; and return 1.
8239         (push number gnus-newsgroup-limit)
8240         1))))
8241
8242 (defun gnus-expunge-thread (thread)
8243   "Mark all articles in THREAD as read."
8244   (let* ((number (mail-header-number (car thread))))
8245     (incf gnus-newsgroup-expunged-tally)
8246     ;; We also mark as read here, if that's wanted.
8247     (setq gnus-newsgroup-unreads
8248           (delq number gnus-newsgroup-unreads))
8249     (if gnus-newsgroup-auto-expire
8250         (push number gnus-newsgroup-expirable)
8251       (push (cons number gnus-low-score-mark)
8252             gnus-newsgroup-reads)))
8253   ;; Go recursively through all subthreads.
8254   (mapcar 'gnus-expunge-thread (cdr thread)))
8255
8256 ;; Summary article oriented commands
8257
8258 (defun gnus-summary-refer-parent-article (n)
8259   "Refer parent article N times.
8260 If N is negative, go to ancestor -N instead.
8261 The difference between N and the number of articles fetched is returned."
8262   (interactive "p")
8263   (let ((skip 1)
8264         error header ref)
8265     (when (not (natnump n))
8266       (setq skip (abs n)
8267             n 1))
8268     (while (and (> n 0)
8269                 (not error))
8270       (setq header (gnus-summary-article-header))
8271       (if (and (eq (mail-header-number header)
8272                    (cdr gnus-article-current))
8273                (equal gnus-newsgroup-name
8274                       (car gnus-article-current)))
8275           ;; If we try to find the parent of the currently
8276           ;; displayed article, then we take a look at the actual
8277           ;; References header, since this is slightly more
8278           ;; reliable than the References field we got from the
8279           ;; server.
8280           (save-excursion
8281             (set-buffer gnus-original-article-buffer)
8282             (nnheader-narrow-to-headers)
8283             (unless (setq ref (message-fetch-field "references"))
8284               (when (setq ref (message-fetch-field "in-reply-to"))
8285                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8286             (widen))
8287         (setq ref
8288               ;; It's not the current article, so we take a bet on
8289               ;; the value we got from the server.
8290               (mail-header-references header)))
8291       (if (and ref
8292                (not (equal ref "")))
8293           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8294             (gnus-message 1 "Couldn't find parent"))
8295         (gnus-message 1 "No references in article %d"
8296                       (gnus-summary-article-number))
8297         (setq error t))
8298       (decf n))
8299     (gnus-summary-position-point)
8300     n))
8301
8302 (defun gnus-summary-refer-references ()
8303   "Fetch all articles mentioned in the References header.
8304 Return the number of articles fetched."
8305   (interactive)
8306   (let ((ref (mail-header-references (gnus-summary-article-header)))
8307         (current (gnus-summary-article-number))
8308         (n 0))
8309     (if (or (not ref)
8310             (equal ref ""))
8311         (error "No References in the current article")
8312       ;; For each Message-ID in the References header...
8313       (while (string-match "<[^>]*>" ref)
8314         (incf n)
8315         ;; ... fetch that article.
8316         (gnus-summary-refer-article
8317          (prog1 (match-string 0 ref)
8318            (setq ref (substring ref (match-end 0))))))
8319       (gnus-summary-goto-subject current)
8320       (gnus-summary-position-point)
8321       n)))
8322
8323 (defun gnus-summary-refer-thread (&optional limit)
8324   "Fetch all articles in the current thread.
8325 If LIMIT (the numerical prefix), fetch that many old headers instead
8326 of what's specified by the `gnus-refer-thread-limit' variable."
8327   (interactive "P")
8328   (let ((id (mail-header-id (gnus-summary-article-header)))
8329         (limit (if limit (prefix-numeric-value limit)
8330                  gnus-refer-thread-limit)))
8331     (unless (eq gnus-fetch-old-headers 'invisible)
8332       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8333       ;; Retrieve the headers and read them in.
8334       (if (eq (if (numberp limit)
8335                   (gnus-retrieve-headers
8336                    (list (min
8337                           (+ (mail-header-number
8338                               (gnus-summary-article-header))
8339                              limit)
8340                           gnus-newsgroup-end))
8341                    gnus-newsgroup-name (* limit 2))
8342                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8343                 ;; headers.
8344                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8345                                        gnus-newsgroup-name limit))
8346               'nov)
8347           (gnus-build-all-threads)
8348         (error "Can't fetch thread from back ends that don't support NOV"))
8349       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8350     (gnus-summary-limit-include-thread id)))
8351
8352 (defun gnus-summary-refer-article (message-id)
8353   "Fetch an article specified by MESSAGE-ID."
8354   (interactive "sMessage-ID: ")
8355   (when (and (stringp message-id)
8356              (not (zerop (length message-id))))
8357     (setq message-id (gnus-replace-in-string message-id " " ""))
8358     ;; Construct the correct Message-ID if necessary.
8359     ;; Suggested by tale@pawl.rpi.edu.
8360     (unless (string-match "^<" message-id)
8361       (setq message-id (concat "<" message-id)))
8362     (unless (string-match ">$" message-id)
8363       (setq message-id (concat message-id ">")))
8364     ;; People often post MIDs from URLs, so unhex it:
8365     (unless (string-match "@" message-id)
8366       (setq message-id (gnus-url-unhex-string message-id)))
8367     (let* ((header (gnus-id-to-header message-id))
8368            (sparse (and header
8369                         (gnus-summary-article-sparse-p
8370                          (mail-header-number header))
8371                         (memq (mail-header-number header)
8372                               gnus-newsgroup-limit)))
8373            number)
8374       (cond
8375        ;; If the article is present in the buffer we just go to it.
8376        ((and header
8377              (or (not (gnus-summary-article-sparse-p
8378                        (mail-header-number header)))
8379                  sparse))
8380         (prog1
8381             (gnus-summary-goto-article
8382              (mail-header-number header) nil t)
8383           (when sparse
8384             (gnus-summary-update-article (mail-header-number header)))))
8385        (t
8386         ;; We fetch the article.
8387         (catch 'found
8388           (dolist (gnus-override-method (gnus-refer-article-methods))
8389             (when (and (gnus-check-server gnus-override-method)
8390                        ;; Fetch the header,
8391                        (setq number (gnus-summary-insert-subject message-id)))
8392               ;; and display the article.
8393               (gnus-summary-select-article nil nil nil number)
8394               (throw 'found t)))
8395           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8396
8397 (defun gnus-refer-article-methods ()
8398   "Return a list of referable methods."
8399   (cond
8400    ;; No method, so we default to current and native.
8401    ((null gnus-refer-article-method)
8402     (list gnus-current-select-method gnus-select-method))
8403    ;; Current.
8404    ((eq 'current gnus-refer-article-method)
8405     (list gnus-current-select-method))
8406    ;; List of select methods.
8407    ((not (and (symbolp (car gnus-refer-article-method))
8408               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8409     (let (out)
8410       (dolist (method gnus-refer-article-method)
8411         (push (if (eq 'current method)
8412                   gnus-current-select-method
8413                 method)
8414               out))
8415       (nreverse out)))
8416    ;; One single select method.
8417    (t
8418     (list gnus-refer-article-method))))
8419
8420 (defun gnus-summary-edit-parameters ()
8421   "Edit the group parameters of the current group."
8422   (interactive)
8423   (gnus-group-edit-group gnus-newsgroup-name 'params))
8424
8425 (defun gnus-summary-customize-parameters ()
8426   "Customize the group parameters of the current group."
8427   (interactive)
8428   (gnus-group-customize gnus-newsgroup-name))
8429
8430 (defun gnus-summary-enter-digest-group (&optional force)
8431   "Enter an nndoc group based on the current article.
8432 If FORCE, force a digest interpretation.  If not, try
8433 to guess what the document format is."
8434   (interactive "P")
8435   (let ((conf gnus-current-window-configuration))
8436     (save-window-excursion
8437       (save-excursion
8438         (let (gnus-article-prepare-hook
8439               gnus-display-mime-function
8440               gnus-break-pages)
8441           (gnus-summary-select-article))))
8442     (setq gnus-current-window-configuration conf)
8443     (let* ((name (format "%s-%d"
8444                          (gnus-group-prefixed-name
8445                           gnus-newsgroup-name (list 'nndoc ""))
8446                          (with-current-buffer gnus-summary-buffer
8447                            gnus-current-article)))
8448            (ogroup gnus-newsgroup-name)
8449            (params (append (gnus-info-params (gnus-get-info ogroup))
8450                            (list (cons 'to-group ogroup))
8451                            (list (cons 'parent-group ogroup))
8452                            (list (cons 'save-article-group ogroup))))
8453            (case-fold-search t)
8454            (buf (current-buffer))
8455            dig to-address)
8456       (save-excursion
8457         (set-buffer gnus-original-article-buffer)
8458         ;; Have the digest group inherit the main mail address of
8459         ;; the parent article.
8460         (when (setq to-address (or (gnus-fetch-field "reply-to")
8461                                    (gnus-fetch-field "from")))
8462           (setq params (append
8463                         (list (cons 'to-address
8464                                     (funcall gnus-decode-encoded-word-function
8465                                              to-address))))))
8466         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8467         (insert-buffer-substring gnus-original-article-buffer)
8468         ;; Remove lines that may lead nndoc to misinterpret the
8469         ;; document type.
8470         (narrow-to-region
8471          (goto-char (point-min))
8472          (or (search-forward "\n\n" nil t) (point)))
8473         (goto-char (point-min))
8474         (delete-matching-lines "^Path:\\|^From ")
8475         (widen))
8476       (unwind-protect
8477           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8478                     (gnus-newsgroup-ephemeral-ignored-charsets
8479                      gnus-newsgroup-ignored-charsets))
8480                 (gnus-group-read-ephemeral-group
8481                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8482                               (nndoc-article-type
8483                                ,(if force 'mbox 'guess)))
8484                  t nil nil nil
8485                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8486                                                         "ADAPT")))))
8487               ;; Make all postings to this group go to the parent group.
8488               (nconc (gnus-info-params (gnus-get-info name))
8489                      params)
8490             ;; Couldn't select this doc group.
8491             (switch-to-buffer buf)
8492             (gnus-set-global-variables)
8493             (gnus-configure-windows 'summary)
8494             (gnus-message 3 "Article couldn't be entered?"))
8495         (kill-buffer dig)))))
8496
8497 (defun gnus-summary-read-document (n)
8498   "Open a new group based on the current article(s).
8499 This will allow you to read digests and other similar
8500 documents as newsgroups.
8501 Obeys the standard process/prefix convention."
8502   (interactive "P")
8503   (let* ((ogroup gnus-newsgroup-name)
8504          (params (append (gnus-info-params (gnus-get-info ogroup))
8505                          (list (cons 'to-group ogroup))))
8506          group egroup groups vgroup)
8507     (dolist (article (gnus-summary-work-articles n))
8508       (setq group (format "%s-%d" gnus-newsgroup-name article))
8509       (gnus-summary-remove-process-mark article)
8510       (when (gnus-summary-display-article article)
8511         (save-excursion
8512           (with-temp-buffer
8513             (insert-buffer-substring gnus-original-article-buffer)
8514             ;; Remove some headers that may lead nndoc to make
8515             ;; the wrong guess.
8516             (message-narrow-to-head)
8517             (goto-char (point-min))
8518             (delete-matching-lines "^Path:\\|^From ")
8519             (widen)
8520             (if (setq egroup
8521                       (gnus-group-read-ephemeral-group
8522                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8523                                      (nndoc-article-type guess))
8524                        t nil t))
8525                 (progn
8526             ;; Make all postings to this group go to the parent group.
8527                   (nconc (gnus-info-params (gnus-get-info egroup))
8528                          params)
8529                   (push egroup groups))
8530               ;; Couldn't select this doc group.
8531               (gnus-error 3 "Article couldn't be entered"))))))
8532     ;; Now we have selected all the documents.
8533     (cond
8534      ((not groups)
8535       (error "None of the articles could be interpreted as documents"))
8536      ((gnus-group-read-ephemeral-group
8537        (setq vgroup (format
8538                      "nnvirtual:%s-%s" gnus-newsgroup-name
8539                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8540        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8541        t
8542        (cons (current-buffer) 'summary)))
8543      (t
8544       (error "Couldn't select virtual nndoc group")))))
8545
8546 (defun gnus-summary-isearch-article (&optional regexp-p)
8547   "Do incremental search forward on the current article.
8548 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8549   (interactive "P")
8550   (gnus-summary-select-article)
8551   (gnus-configure-windows 'article)
8552   (gnus-eval-in-buffer-window gnus-article-buffer
8553     (save-restriction
8554       (widen)
8555       (isearch-forward regexp-p))))
8556
8557 (defun gnus-summary-search-article-forward (regexp &optional backward)
8558   "Search for an article containing REGEXP forward.
8559 If BACKWARD, search backward instead."
8560   (interactive
8561    (list (read-string
8562           (format "Search article %s (regexp%s): "
8563                   (if current-prefix-arg "backward" "forward")
8564                   (if gnus-last-search-regexp
8565                       (concat ", default " gnus-last-search-regexp)
8566                     "")))
8567          current-prefix-arg))
8568   (if (string-equal regexp "")
8569       (setq regexp (or gnus-last-search-regexp ""))
8570     (setq gnus-last-search-regexp regexp)
8571     (setq gnus-article-before-search gnus-current-article))
8572   ;; Intentionally set gnus-last-article.
8573   (setq gnus-last-article gnus-article-before-search)
8574   (let ((gnus-last-article gnus-last-article))
8575     (if (gnus-summary-search-article regexp backward)
8576         (gnus-summary-show-thread)
8577       (signal 'search-failed (list regexp)))))
8578
8579 (defun gnus-summary-search-article-backward (regexp)
8580   "Search for an article containing REGEXP backward."
8581   (interactive
8582    (list (read-string
8583           (format "Search article backward (regexp%s): "
8584                   (if gnus-last-search-regexp
8585                       (concat ", default " gnus-last-search-regexp)
8586                     "")))))
8587   (gnus-summary-search-article-forward regexp 'backward))
8588
8589 (defun gnus-summary-search-article (regexp &optional backward)
8590   "Search for an article containing REGEXP.
8591 Optional argument BACKWARD means do search for backward.
8592 `gnus-select-article-hook' is not called during the search."
8593   ;; We have to require this here to make sure that the following
8594   ;; dynamic binding isn't shadowed by autoloading.
8595   (require 'gnus-async)
8596   (require 'gnus-art)
8597   (let ((gnus-select-article-hook nil)  ;Disable hook.
8598         (gnus-article-prepare-hook nil)
8599         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8600         (gnus-use-article-prefetch nil)
8601         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8602         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8603         (gnus-visual nil)
8604         (gnus-keep-backlog nil)
8605         (gnus-break-pages nil)
8606         (gnus-summary-display-arrow nil)
8607         (gnus-updated-mode-lines nil)
8608         (gnus-auto-center-summary nil)
8609         (sum (current-buffer))
8610         (gnus-display-mime-function nil)
8611         (found nil)
8612         point)
8613     (gnus-save-hidden-threads
8614       (gnus-summary-select-article)
8615       (set-buffer gnus-article-buffer)
8616       (goto-char (window-point (get-buffer-window (current-buffer))))
8617       (when backward
8618         (forward-line -1))
8619       (while (not found)
8620         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8621         (if (if backward
8622                 (re-search-backward regexp nil t)
8623               (re-search-forward regexp nil t))
8624             ;; We found the regexp.
8625             (progn
8626               (setq found 'found)
8627               (beginning-of-line)
8628               (set-window-start
8629                (get-buffer-window (current-buffer))
8630                (point))
8631               (forward-line 1)
8632               (set-window-point
8633                (get-buffer-window (current-buffer))
8634                (point))
8635               (set-buffer sum)
8636               (setq point (point)))
8637           ;; We didn't find it, so we go to the next article.
8638           (set-buffer sum)
8639           (setq found 'not)
8640           (while (eq found 'not)
8641             (if (not (if backward (gnus-summary-find-prev)
8642                        (gnus-summary-find-next)))
8643                 ;; No more articles.
8644                 (setq found t)
8645               ;; Select the next article and adjust point.
8646               (unless (gnus-summary-article-sparse-p
8647                        (gnus-summary-article-number))
8648                 (setq found nil)
8649                 (gnus-summary-select-article)
8650                 (set-buffer gnus-article-buffer)
8651                 (widen)
8652                 (goto-char (if backward (point-max) (point-min))))))))
8653       (gnus-message 7 ""))
8654     ;; Return whether we found the regexp.
8655     (when (eq found 'found)
8656       (goto-char point)
8657       (gnus-summary-show-thread)
8658       (gnus-summary-goto-subject gnus-current-article)
8659       (gnus-summary-position-point)
8660       t)))
8661
8662 (defun gnus-find-matching-articles (header regexp)
8663   "Return a list of all articles that match REGEXP on HEADER.
8664 This search includes all articles in the current group that Gnus has
8665 fetched headers for, whether they are displayed or not."
8666   (let ((articles nil)
8667         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8668         (case-fold-search t))
8669     (dolist (header gnus-newsgroup-headers)
8670       (when (string-match regexp (funcall func header))
8671         (push (mail-header-number header) articles)))
8672     (nreverse articles)))
8673
8674 (defun gnus-summary-find-matching (header regexp &optional backward unread
8675                                           not-case-fold not-matching)
8676   "Return a list of all articles that match REGEXP on HEADER.
8677 The search stars on the current article and goes forwards unless
8678 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8679 If UNREAD is non-nil, only unread articles will
8680 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8681 in the comparisons. If NOT-MATCHING, return a list of all articles that
8682 not match REGEXP on HEADER."
8683   (let ((case-fold-search (not not-case-fold))
8684         articles d func)
8685     (if (consp header)
8686         (if (eq (car header) 'extra)
8687             (setq func
8688                   `(lambda (h)
8689                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8690                          "")))
8691           (error "%s is an invalid header" header))
8692       (unless (fboundp (intern (concat "mail-header-" header)))
8693         (error "%s is not a valid header" header))
8694       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8695     (dolist (d (if (eq backward 'all)
8696                    gnus-newsgroup-data
8697                  (gnus-data-find-list
8698                   (gnus-summary-article-number)
8699                   (gnus-data-list backward))))
8700       (when (and (or (not unread)       ; We want all articles...
8701                      (gnus-data-unread-p d)) ; Or just unreads.
8702                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8703                  (if not-matching
8704                      (not (string-match
8705                            regexp
8706                            (funcall func (gnus-data-header d))))
8707                    (string-match regexp
8708                                  (funcall func (gnus-data-header d)))))
8709         (push (gnus-data-number d) articles))) ; Success!
8710     (nreverse articles)))
8711
8712 (defun gnus-summary-execute-command (header regexp command &optional backward)
8713   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8714 If HEADER is an empty string (or nil), the match is done on the entire
8715 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8716   (interactive
8717    (list (let ((completion-ignore-case t))
8718            (completing-read
8719             "Header name: "
8720             (mapcar (lambda (header) (list (format "%s" header)))
8721                     (append
8722                      '("Number" "Subject" "From" "Lines" "Date"
8723                        "Message-ID" "Xref" "References" "Body")
8724                      gnus-extra-headers))
8725             nil 'require-match))
8726          (read-string "Regexp: ")
8727          (read-key-sequence "Command: ")
8728          current-prefix-arg))
8729   (when (equal header "Body")
8730     (setq header ""))
8731   ;; Hidden thread subtrees must be searched as well.
8732   (gnus-summary-show-all-threads)
8733   ;; We don't want to change current point nor window configuration.
8734   (save-excursion
8735     (save-window-excursion
8736       (let (gnus-visual
8737             gnus-treat-strip-trailing-blank-lines
8738             gnus-treat-strip-leading-blank-lines
8739             gnus-treat-strip-multiple-blank-lines
8740             gnus-treat-hide-boring-headers
8741             gnus-treat-fold-newsgroups
8742             gnus-article-prepare-hook)
8743         (gnus-message 6 "Executing %s..." (key-description command))
8744         ;; We'd like to execute COMMAND interactively so as to give arguments.
8745         (gnus-execute header regexp
8746                       `(call-interactively ',(key-binding command))
8747                       backward)
8748         (gnus-message 6 "Executing %s...done" (key-description command))))))
8749
8750 (defun gnus-summary-beginning-of-article ()
8751   "Scroll the article back to the beginning."
8752   (interactive)
8753   (gnus-summary-select-article)
8754   (gnus-configure-windows 'article)
8755   (gnus-eval-in-buffer-window gnus-article-buffer
8756     (widen)
8757     (goto-char (point-min))
8758     (when gnus-break-pages
8759       (gnus-narrow-to-page))))
8760
8761 (defun gnus-summary-end-of-article ()
8762   "Scroll to the end of the article."
8763   (interactive)
8764   (gnus-summary-select-article)
8765   (gnus-configure-windows 'article)
8766   (gnus-eval-in-buffer-window gnus-article-buffer
8767     (widen)
8768     (goto-char (point-max))
8769     (recenter -3)
8770     (when gnus-break-pages
8771       (when (re-search-backward page-delimiter nil t)
8772         (narrow-to-region (match-end 0) (point-max)))
8773       (gnus-narrow-to-page))))
8774
8775 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8776   "Truncate to LEN and quote all \"(\"'s in STRING."
8777   (gnus-replace-in-string (if (and len (> (length string) len))
8778                               (substring string 0 len)
8779                             string)
8780                           "[()]" "\\\\\\&"))
8781
8782 (defun gnus-summary-print-article (&optional filename n)
8783   "Generate and print a PostScript image of the process-marked (mail) articles.
8784
8785 If used interactively, print the current article if none are
8786 process-marked.  With prefix arg, prompt the user for the name of the
8787 file to save in.
8788
8789 When used from Lisp, accept two optional args FILENAME and N.  N means
8790 to print the next N articles.  If N is negative, print the N previous
8791 articles.  If N is nil and articles have been marked with the process
8792 mark, print these instead.
8793
8794 If the optional first argument FILENAME is nil, send the image to the
8795 printer.  If FILENAME is a string, save the PostScript image in a file with
8796 that name.  If FILENAME is a number, prompt the user for the name of the file
8797 to save in."
8798   (interactive (list (ps-print-preprint current-prefix-arg)))
8799   (dolist (article (gnus-summary-work-articles n))
8800     (gnus-summary-select-article nil nil 'pseudo article)
8801     (gnus-eval-in-buffer-window gnus-article-buffer
8802       (gnus-print-buffer))
8803     (gnus-summary-remove-process-mark article))
8804   (ps-despool filename))
8805
8806 (defun gnus-print-buffer ()
8807   (let ((buffer (generate-new-buffer " *print*")))
8808     (unwind-protect
8809         (progn
8810           (copy-to-buffer buffer (point-min) (point-max))
8811           (set-buffer buffer)
8812           (gnus-remove-text-with-property 'gnus-decoration)
8813           (when (gnus-visual-p 'article-highlight 'highlight)
8814             ;; Copy-to-buffer doesn't copy overlay.  So redo
8815             ;; highlight.
8816             (let ((gnus-article-buffer buffer))
8817               (gnus-article-highlight-citation t)
8818               (gnus-article-highlight-signature)
8819               (gnus-article-emphasize)
8820               (gnus-article-delete-invisible-text)))
8821           (let ((ps-left-header
8822                  (list
8823                   (concat "("
8824                           (gnus-summary-print-truncate-and-quote
8825                            (mail-header-subject gnus-current-headers)
8826                            66) ")")
8827                   (concat "("
8828                           (gnus-summary-print-truncate-and-quote
8829                            (mail-header-from gnus-current-headers)
8830                            45) ")")))
8831                 (ps-right-header
8832                  (list
8833                   "/pagenumberstring load"
8834                   (concat "("
8835                           (mail-header-date gnus-current-headers) ")"))))
8836             (gnus-run-hooks 'gnus-ps-print-hook)
8837             (save-excursion
8838               (if window-system
8839                   (ps-spool-buffer-with-faces)
8840                 (ps-spool-buffer)))))
8841       (kill-buffer buffer))))
8842
8843 (defun gnus-summary-show-article (&optional arg)
8844   "Force redisplaying of the current article.
8845 If ARG (the prefix) is a number, show the article with the charset
8846 defined in `gnus-summary-show-article-charset-alist', or the charset
8847 input.
8848 If ARG (the prefix) is non-nil and not a number, show the raw article
8849 without any article massaging functions being run.  Normally, the key
8850 strokes are `C-u g'."
8851   (interactive "P")
8852   (cond
8853    ((numberp arg)
8854     (gnus-summary-show-article t)
8855     (let ((gnus-newsgroup-charset
8856            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8857                (mm-read-coding-system
8858                 "View as charset: " ;; actually it is coding system.
8859                 (with-current-buffer gnus-article-buffer
8860                   (mm-detect-coding-region (point) (point-max))))))
8861           (gnus-newsgroup-ignored-charsets 'gnus-all))
8862       (gnus-summary-select-article nil 'force)
8863       (let ((deps gnus-newsgroup-dependencies)
8864             head header lines)
8865         (save-excursion
8866           (set-buffer gnus-original-article-buffer)
8867           (save-restriction
8868             (message-narrow-to-head)
8869             (setq head (buffer-string))
8870             (goto-char (point-min))
8871             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8872               (goto-char (point-max))
8873               (widen)
8874               (setq lines (1- (count-lines (point) (point-max))))))
8875           (with-temp-buffer
8876             (insert (format "211 %d Article retrieved.\n"
8877                             (cdr gnus-article-current)))
8878             (insert head)
8879             (if lines (insert (format "Lines: %d\n" lines)))
8880             (insert ".\n")
8881             (let ((nntp-server-buffer (current-buffer)))
8882               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8883         (gnus-data-set-header
8884          (gnus-data-find (cdr gnus-article-current))
8885          header)
8886         (gnus-summary-update-article-line
8887          (cdr gnus-article-current) header)
8888         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8889           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8890    ((not arg)
8891     ;; Select the article the normal way.
8892     (gnus-summary-select-article nil 'force))
8893    (t
8894     ;; We have to require this here to make sure that the following
8895     ;; dynamic binding isn't shadowed by autoloading.
8896     (require 'gnus-async)
8897     (require 'gnus-art)
8898     ;; Bind the article treatment functions to nil.
8899     (let ((gnus-have-all-headers t)
8900           gnus-article-prepare-hook
8901           gnus-article-decode-hook
8902           gnus-display-mime-function
8903           gnus-break-pages)
8904       ;; Destroy any MIME parts.
8905       (when (gnus-buffer-live-p gnus-article-buffer)
8906         (save-excursion
8907           (set-buffer gnus-article-buffer)
8908           (mm-destroy-parts gnus-article-mime-handles)
8909           ;; Set it to nil for safety reason.
8910           (setq gnus-article-mime-handle-alist nil)
8911           (setq gnus-article-mime-handles nil)))
8912       (gnus-summary-select-article nil 'force))))
8913   (gnus-summary-goto-subject gnus-current-article)
8914   (gnus-summary-position-point))
8915
8916 (defun gnus-summary-show-raw-article ()
8917   "Show the raw article without any article massaging functions being run."
8918   (interactive)
8919   (gnus-summary-show-article t))
8920
8921 (defun gnus-summary-verbose-headers (&optional arg)
8922   "Toggle permanent full header display.
8923 If ARG is a positive number, turn header display on.
8924 If ARG is a negative number, turn header display off."
8925   (interactive "P")
8926   (setq gnus-show-all-headers
8927         (cond ((or (not (numberp arg))
8928                    (zerop arg))
8929                (not gnus-show-all-headers))
8930               ((natnump arg)
8931                t)))
8932   (gnus-summary-show-article))
8933
8934 (defun gnus-summary-toggle-header (&optional arg)
8935   "Show the headers if they are hidden, or hide them if they are shown.
8936 If ARG is a positive number, show the entire header.
8937 If ARG is a negative number, hide the unwanted header lines."
8938   (interactive "P")
8939   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8940                      (get-buffer-window gnus-article-buffer t))))
8941     (with-current-buffer gnus-article-buffer
8942       (widen)
8943       (article-narrow-to-head)
8944       (let* ((buffer-read-only nil)
8945              (inhibit-point-motion-hooks t)
8946              (hidden (if (numberp arg)
8947                          (>= arg 0)
8948                        (or (not (looking-at "[^ \t\n]+:"))
8949                            (gnus-article-hidden-text-p 'headers))))
8950              s e)
8951         (delete-region (point-min) (point-max))
8952         (with-current-buffer gnus-original-article-buffer
8953           (goto-char (setq s (point-min)))
8954           (setq e (if (search-forward "\n\n" nil t)
8955                       (1- (point))
8956                     (point-max))))
8957         (insert-buffer-substring gnus-original-article-buffer s e)
8958         (run-hooks 'gnus-article-decode-hook)
8959         (if hidden
8960             (let ((gnus-treat-hide-headers nil)
8961                   (gnus-treat-hide-boring-headers nil))
8962               (gnus-delete-wash-type 'headers)
8963               (gnus-treat-article 'head))
8964           (gnus-treat-article 'head))
8965         (widen)
8966         (if window
8967             (set-window-start window (goto-char (point-min))))
8968         (if gnus-break-pages
8969             (gnus-narrow-to-page)
8970           (when (gnus-visual-p 'page-marker)
8971             (let ((buffer-read-only nil))
8972               (gnus-remove-text-with-property 'gnus-prev)
8973               (gnus-remove-text-with-property 'gnus-next))))
8974         (gnus-set-mode-line 'article)))))
8975
8976 (defun gnus-summary-show-all-headers ()
8977   "Make all header lines visible."
8978   (interactive)
8979   (gnus-summary-toggle-header 1))
8980
8981 (defun gnus-summary-caesar-message (&optional arg)
8982   "Caesar rotate the current article by 13.
8983 The numerical prefix specifies how many places to rotate each letter
8984 forward."
8985   (interactive "P")
8986   (gnus-summary-select-article)
8987   (let ((mail-header-separator ""))
8988     (gnus-eval-in-buffer-window gnus-article-buffer
8989       (save-restriction
8990         (widen)
8991         (let ((start (window-start))
8992               buffer-read-only)
8993           (message-caesar-buffer-body arg)
8994           (set-window-start (get-buffer-window (current-buffer)) start)))))
8995   ;; Create buttons and stuff...
8996   (gnus-treat-article nil))
8997
8998 (defun gnus-summary-idna-message (&optional arg)
8999   "Decode IDNA encoded domain names in the current articles.
9000 IDNA encoded domain names looks like `xn--bar'.  If a string
9001 remain unencoded after running this function, it is likely an
9002 invalid IDNA string (`xn--bar' is invalid).
9003
9004 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9005 installed for this command to work."
9006   (interactive "P")
9007   (if (not (and (condition-case nil (require 'idna)
9008                   (file-error))
9009                 (mm-coding-system-p 'utf-8)
9010                 (executable-find (symbol-value 'idna-program))))
9011       (gnus-message
9012        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9013     (gnus-summary-select-article)
9014     (let ((mail-header-separator ""))
9015       (gnus-eval-in-buffer-window gnus-article-buffer
9016         (save-restriction
9017           (widen)
9018           (let ((start (window-start))
9019                 buffer-read-only)
9020             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9021               (replace-match (idna-to-unicode (match-string 1))))
9022             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9023
9024 (autoload 'unmorse-region "morse"
9025   "Convert morse coded text in region to ordinary ASCII text."
9026   t)
9027
9028 (defun gnus-summary-morse-message (&optional arg)
9029   "Morse decode the current article."
9030   (interactive "P")
9031   (gnus-summary-select-article)
9032   (let ((mail-header-separator ""))
9033     (gnus-eval-in-buffer-window gnus-article-buffer
9034       (save-excursion
9035         (save-restriction
9036           (widen)
9037           (let ((pos (window-start))
9038                 buffer-read-only)
9039             (goto-char (point-min))
9040             (when (message-goto-body)
9041               (gnus-narrow-to-body))
9042             (goto-char (point-min))
9043             (while (search-forward "·" (point-max) t)
9044               (replace-match "."))
9045             (unmorse-region (point-min) (point-max))
9046             (widen)
9047             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9048
9049 (defun gnus-summary-stop-page-breaking ()
9050   "Stop page breaking in the current article."
9051   (interactive)
9052   (gnus-summary-select-article)
9053   (gnus-eval-in-buffer-window gnus-article-buffer
9054     (widen)
9055     (when (gnus-visual-p 'page-marker)
9056       (let ((buffer-read-only nil))
9057         (gnus-remove-text-with-property 'gnus-prev)
9058         (gnus-remove-text-with-property 'gnus-next))
9059       (setq gnus-page-broken nil))))
9060
9061 (defun gnus-summary-move-article (&optional n to-newsgroup
9062                                             select-method action)
9063   "Move the current article to a different newsgroup.
9064 If N is a positive number, move the N next articles.
9065 If N is a negative number, move the N previous articles.
9066 If N is nil and any articles have been marked with the process mark,
9067 move those articles instead.
9068 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9069 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9070 re-spool using this method.
9071
9072 When called interactively with TO-NEWSGROUP being nil, the value of
9073 the variable `gnus-move-split-methods' is used for finding a default
9074 for the target newsgroup.
9075
9076 For this function to work, both the current newsgroup and the
9077 newsgroup that you want to move to have to support the `request-move'
9078 and `request-accept' functions.
9079
9080 ACTION can be either `move' (the default), `crosspost' or `copy'."
9081   (interactive "P")
9082   (unless action
9083     (setq action 'move))
9084   ;; Check whether the source group supports the required functions.
9085   (cond ((and (eq action 'move)
9086               (not (gnus-check-backend-function
9087                     'request-move-article gnus-newsgroup-name)))
9088          (error "The current group does not support article moving"))
9089         ((and (eq action 'crosspost)
9090               (not (gnus-check-backend-function
9091                     'request-replace-article gnus-newsgroup-name)))
9092          (error "The current group does not support article editing")))
9093   (let ((articles (gnus-summary-work-articles n))
9094         (prefix (if (gnus-check-backend-function
9095                      'request-move-article gnus-newsgroup-name)
9096                     (funcall gnus-move-group-prefix-function
9097                              gnus-newsgroup-name)
9098                   ""))
9099         (names '((move "Move" "Moving")
9100                  (copy "Copy" "Copying")
9101                  (crosspost "Crosspost" "Crossposting")))
9102         (copy-buf (save-excursion
9103                     (nnheader-set-temp-buffer " *copy article*")))
9104         art-group to-method new-xref article to-groups articles-to-update-marks)
9105     (unless (assq action names)
9106       (error "Unknown action %s" action))
9107     ;; Read the newsgroup name.
9108     (when (and (not to-newsgroup)
9109                (not select-method))
9110       (if (and gnus-move-split-methods
9111                (not
9112                 (and (memq gnus-current-article articles)
9113                      (gnus-buffer-live-p gnus-original-article-buffer))))
9114           ;; When `gnus-move-split-methods' is non-nil, we have to
9115           ;; select an article to give `gnus-read-move-group-name' an
9116           ;; opportunity to suggest an appropriate default.  However,
9117           ;; we needn't render or mark the article.
9118           (let ((gnus-display-mime-function nil)
9119                 (gnus-article-prepare-hook nil)
9120                 (gnus-mark-article-hook nil))
9121             (gnus-summary-select-article nil nil nil (car articles))))
9122       (setq to-newsgroup
9123             (gnus-read-move-group-name
9124              (cadr (assq action names))
9125              (symbol-value (intern (format "gnus-current-%s-group" action)))
9126              articles prefix))
9127       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9128     (setq to-method (or select-method
9129                         (gnus-server-to-method
9130                          (gnus-group-method to-newsgroup))))
9131     ;; Check the method we are to move this article to...
9132     (unless (gnus-check-backend-function
9133              'request-accept-article (car to-method))
9134       (error "%s does not support article copying" (car to-method)))
9135     (unless (gnus-check-server to-method)
9136       (error "Can't open server %s" (car to-method)))
9137     (gnus-message 6 "%s to %s: %s..."
9138                   (caddr (assq action names))
9139                   (or (car select-method) to-newsgroup) articles)
9140     (while articles
9141       (setq article (pop articles))
9142       (setq
9143        art-group
9144        (cond
9145         ;; Move the article.
9146         ((eq action 'move)
9147          ;; Remove this article from future suppression.
9148          (gnus-dup-unsuppress-article article)
9149          (let* ((from-method (gnus-find-method-for-group
9150                               gnus-newsgroup-name))
9151                 (to-method (gnus-find-method-for-group
9152                             to-newsgroup))
9153                 (move-is-internal (gnus-method-equal from-method to-method)))
9154          (gnus-request-move-article
9155           article                       ; Article to move
9156           gnus-newsgroup-name           ; From newsgroup
9157           (nth 1 (gnus-find-method-for-group
9158                   gnus-newsgroup-name)) ; Server
9159           (list 'gnus-request-accept-article
9160                 to-newsgroup (list 'quote select-method)
9161                 (not articles) t)       ; Accept form
9162           (not articles)                ; Only save nov last time
9163           move-is-internal)))           ; is this move internal?
9164         ;; Copy the article.
9165         ((eq action 'copy)
9166          (save-excursion
9167            (set-buffer copy-buf)
9168            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9169              (save-restriction
9170                (nnheader-narrow-to-headers)
9171                (dolist (hdr gnus-copy-article-ignored-headers)
9172                  (message-remove-header hdr t)))
9173              (gnus-request-accept-article
9174               to-newsgroup select-method (not articles) t))))
9175         ;; Crosspost the article.
9176         ((eq action 'crosspost)
9177          (let ((xref (message-tokenize-header
9178                       (mail-header-xref (gnus-summary-article-header article))
9179                       " ")))
9180            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9181                                   ":" (number-to-string article)))
9182            (unless xref
9183              (setq xref (list (system-name))))
9184            (setq new-xref
9185                  (concat
9186                   (mapconcat 'identity
9187                              (delete "Xref:" (delete new-xref xref))
9188                              " ")
9189                   " " new-xref))
9190            (save-excursion
9191              (set-buffer copy-buf)
9192              ;; First put the article in the destination group.
9193              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9194              (when (consp (setq art-group
9195                                 (gnus-request-accept-article
9196                                  to-newsgroup select-method (not articles))))
9197                (setq new-xref (concat new-xref " " (car art-group)
9198                                       ":"
9199                                       (number-to-string (cdr art-group))))
9200                ;; Now we have the new Xrefs header, so we insert
9201                ;; it and replace the new article.
9202                (nnheader-replace-header "Xref" new-xref)
9203                (gnus-request-replace-article
9204                 (cdr art-group) to-newsgroup (current-buffer))
9205                art-group))))))
9206       (cond
9207        ((not art-group)
9208         (gnus-message 1 "Couldn't %s article %s: %s"
9209                       (cadr (assq action names)) article
9210                       (nnheader-get-report (car to-method))))
9211        ((eq art-group 'junk)
9212         (when (eq action 'move)
9213           (gnus-summary-mark-article article gnus-canceled-mark)
9214           (gnus-message 4 "Deleted article %s" article)
9215           ;; run the delete hook
9216           (run-hook-with-args 'gnus-summary-article-delete-hook
9217                               action
9218                               (gnus-data-header
9219                                (assoc article (gnus-data-list nil)))
9220                               gnus-newsgroup-name nil
9221                               select-method)))
9222        (t
9223         (let* ((pto-group (gnus-group-prefixed-name
9224                            (car art-group) to-method))
9225                (info (gnus-get-info pto-group))
9226                (to-group (gnus-info-group info))
9227                to-marks)
9228           ;; Update the group that has been moved to.
9229           (when (and info
9230                      (memq action '(move copy)))
9231             (unless (member to-group to-groups)
9232               (push to-group to-groups))
9233
9234             (unless (memq article gnus-newsgroup-unreads)
9235               (push 'read to-marks)
9236               (gnus-info-set-read
9237                info (gnus-add-to-range (gnus-info-read info)
9238                                        (list (cdr art-group)))))
9239
9240             ;; See whether the article is to be put in the cache.
9241             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9242                              gnus-article-mark-lists
9243                            (delete '(expirable . expire)
9244                                    (copy-sequence gnus-article-mark-lists))))
9245                   (to-article (cdr art-group)))
9246
9247               ;; Enter the article into the cache in the new group,
9248               ;; if that is required.
9249               (when gnus-use-cache
9250                 (gnus-cache-possibly-enter-article
9251                  to-group to-article
9252                  (memq article gnus-newsgroup-marked)
9253                  (memq article gnus-newsgroup-dormant)
9254                  (memq article gnus-newsgroup-unreads)))
9255
9256               (when gnus-preserve-marks
9257                 ;; Copy any marks over to the new group.
9258                 (when (and (equal to-group gnus-newsgroup-name)
9259                            (not (memq article gnus-newsgroup-unreads)))
9260                   ;; Mark this article as read in this group.
9261                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9262                   (setcdr (gnus-active to-group) to-article)
9263                   (setcdr gnus-newsgroup-active to-article))
9264
9265                 (while marks
9266                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9267                     (when (memq article (symbol-value
9268                                          (intern (format "gnus-newsgroup-%s"
9269                                                          (caar marks)))))
9270                       (push (cdar marks) to-marks)
9271                       ;; If the other group is the same as this group,
9272                       ;; then we have to add the mark to the list.
9273                       (when (equal to-group gnus-newsgroup-name)
9274                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9275                              (cons to-article
9276                                    (symbol-value
9277                                     (intern (format "gnus-newsgroup-%s"
9278                                                     (caar marks)))))))
9279                       ;; Copy the marks to other group.
9280                       (gnus-add-marked-articles
9281                        to-group (cdar marks) (list to-article) info)))
9282                   (setq marks (cdr marks)))
9283
9284                 (gnus-request-set-mark
9285                  to-group (list (list (list to-article) 'add to-marks))))
9286
9287               (gnus-dribble-enter
9288                (concat "(gnus-group-set-info '"
9289                        (gnus-prin1-to-string (gnus-get-info to-group))
9290                        ")"))))
9291
9292           ;; Update the Xref header in this article to point to
9293           ;; the new crossposted article we have just created.
9294           (when (eq action 'crosspost)
9295             (save-excursion
9296               (set-buffer copy-buf)
9297               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9298               (nnheader-replace-header "Xref" new-xref)
9299               (gnus-request-replace-article
9300                article gnus-newsgroup-name (current-buffer))))
9301
9302           ;; run the move/copy/crosspost/respool hook
9303           (run-hook-with-args 'gnus-summary-article-move-hook
9304                               action
9305                               (gnus-data-header
9306                                (assoc article (gnus-data-list nil)))
9307                               gnus-newsgroup-name
9308                               to-newsgroup
9309                               select-method))
9310
9311         ;;;!!!Why is this necessary?
9312         (set-buffer gnus-summary-buffer)
9313         
9314         (gnus-summary-goto-subject article)
9315         (when (eq action 'move)
9316           (gnus-summary-mark-article article gnus-canceled-mark))))
9317       (push article articles-to-update-marks))
9318
9319     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9320     ;; Re-activate all groups that have been moved to.
9321     (save-excursion
9322       (set-buffer gnus-group-buffer)
9323       (let ((gnus-group-marked to-groups))
9324         (gnus-group-get-new-news-this-group nil t)))
9325     
9326     (gnus-kill-buffer copy-buf)
9327     (gnus-summary-position-point)
9328     (gnus-set-mode-line 'summary)))
9329
9330 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9331   "Copy the current article to some other group.
9332 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9333 When called interactively, if TO-NEWSGROUP is nil, use the value of
9334 the variable `gnus-move-split-methods' for finding a default target
9335 newsgroup.
9336 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9337 re-spool using this method."
9338   (interactive "P")
9339   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9340
9341 (defun gnus-summary-crosspost-article (&optional n)
9342   "Crosspost the current article to some other group."
9343   (interactive "P")
9344   (gnus-summary-move-article n nil nil 'crosspost))
9345
9346 (defcustom gnus-summary-respool-default-method nil
9347   "Default method type for respooling an article.
9348 If nil, use to the current newsgroup method."
9349   :type 'symbol
9350   :group 'gnus-summary-mail)
9351
9352 (defcustom gnus-summary-display-while-building nil
9353   "If non-nil, show and update the summary buffer as it's being built.
9354 If the value is t, update the buffer after every line is inserted.  If
9355 the value is an integer (N), update the display every N lines."
9356   :version "22.1"
9357   :group 'gnus-thread
9358   :type '(choice (const :tag "off" nil)
9359                  number
9360                  (const :tag "frequently" t)))
9361
9362 (defun gnus-summary-respool-article (&optional n method)
9363   "Respool the current article.
9364 The article will be squeezed through the mail spooling process again,
9365 which means that it will be put in some mail newsgroup or other
9366 depending on `nnmail-split-methods'.
9367 If N is a positive number, respool the N next articles.
9368 If N is a negative number, respool the N previous articles.
9369 If N is nil and any articles have been marked with the process mark,
9370 respool those articles instead.
9371
9372 Respooling can be done both from mail groups and \"real\" newsgroups.
9373 In the former case, the articles in question will be moved from the
9374 current group into whatever groups they are destined to.  In the
9375 latter case, they will be copied into the relevant groups."
9376   (interactive
9377    (list current-prefix-arg
9378          (let* ((methods (gnus-methods-using 'respool))
9379                 (methname
9380                  (symbol-name (or gnus-summary-respool-default-method
9381                                   (car (gnus-find-method-for-group
9382                                         gnus-newsgroup-name)))))
9383                 (method
9384                  (gnus-completing-read-with-default
9385                   methname "Backend to use when respooling"
9386                   methods nil t nil 'gnus-mail-method-history))
9387                 ms)
9388            (cond
9389             ((zerop (length (setq ms (gnus-servers-using-backend
9390                                       (intern method)))))
9391              (list (intern method) ""))
9392             ((= 1 (length ms))
9393              (car ms))
9394             (t
9395              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9396                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9397                            ms-alist))))))))
9398   (unless method
9399     (error "No method given for respooling"))
9400   (if (assoc (symbol-name
9401               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9402              (gnus-methods-using 'respool))
9403       (gnus-summary-move-article n nil method)
9404     (gnus-summary-copy-article n nil method)))
9405
9406 (defun gnus-summary-import-article (file &optional edit)
9407   "Import an arbitrary file into a mail newsgroup."
9408   (interactive "fImport file: \nP")
9409   (let ((group gnus-newsgroup-name)
9410         (now (current-time))
9411         atts lines group-art)
9412     (unless (gnus-check-backend-function 'request-accept-article group)
9413       (error "%s does not support article importing" group))
9414     (or (file-readable-p file)
9415         (not (file-regular-p file))
9416         (error "Can't read %s" file))
9417     (save-excursion
9418       (set-buffer (gnus-get-buffer-create " *import file*"))
9419       (erase-buffer)
9420       (nnheader-insert-file-contents file)
9421       (goto-char (point-min))
9422       (if (nnheader-article-p)
9423           (save-restriction
9424             (goto-char (point-min))
9425             (search-forward "\n\n" nil t)
9426             (narrow-to-region (point-min) (1- (point)))
9427             (goto-char (point-min))
9428             (unless (re-search-forward "^date:" nil t)
9429               (goto-char (point-max))
9430               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9431        ;; This doesn't look like an article, so we fudge some headers.
9432         (setq atts (file-attributes file)
9433               lines (count-lines (point-min) (point-max)))
9434         (insert "From: " (read-string "From: ") "\n"
9435                 "Subject: " (read-string "Subject: ") "\n"
9436                 "Date: " (message-make-date (nth 5 atts)) "\n"
9437                 "Message-ID: " (message-make-message-id) "\n"
9438                 "Lines: " (int-to-string lines) "\n"
9439                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9440       (setq group-art (gnus-request-accept-article group nil t))
9441       (kill-buffer (current-buffer)))
9442     (setq gnus-newsgroup-active (gnus-activate-group group))
9443     (forward-line 1)
9444     (gnus-summary-goto-article (cdr group-art) nil t)
9445     (when edit
9446       (gnus-summary-edit-article))))
9447
9448 (defun gnus-summary-create-article ()
9449   "Create an article in a mail newsgroup."
9450   (interactive)
9451   (let ((group gnus-newsgroup-name)
9452         (now (current-time))
9453         group-art)
9454     (unless (gnus-check-backend-function 'request-accept-article group)
9455       (error "%s does not support article importing" group))
9456     (save-excursion
9457       (set-buffer (gnus-get-buffer-create " *import file*"))
9458       (erase-buffer)
9459       (goto-char (point-min))
9460       ;; This doesn't look like an article, so we fudge some headers.
9461       (insert "From: " (read-string "From: ") "\n"
9462               "Subject: " (read-string "Subject: ") "\n"
9463               "Date: " (message-make-date now) "\n"
9464               "Message-ID: " (message-make-message-id) "\n")
9465       (setq group-art (gnus-request-accept-article group nil t))
9466       (kill-buffer (current-buffer)))
9467     (setq gnus-newsgroup-active (gnus-activate-group group))
9468     (forward-line 1)
9469     (gnus-summary-goto-article (cdr group-art) nil t)
9470     (gnus-summary-edit-article)))
9471
9472 (defun gnus-summary-article-posted-p ()
9473   "Say whether the current (mail) article is available from news as well.
9474 This will be the case if the article has both been mailed and posted."
9475   (interactive)
9476   (let ((id (mail-header-references (gnus-summary-article-header)))
9477         (gnus-override-method (car (gnus-refer-article-methods))))
9478     (if (gnus-request-head id "")
9479         (gnus-message 2 "The current message was found on %s"
9480                       gnus-override-method)
9481       (gnus-message 2 "The current message couldn't be found on %s"
9482                     gnus-override-method)
9483       nil)))
9484
9485 (defun gnus-summary-expire-articles (&optional now)
9486   "Expire all articles that are marked as expirable in the current group."
9487   (interactive)
9488   (when (and (not gnus-group-is-exiting-without-update-p)
9489              (gnus-check-backend-function
9490               'request-expire-articles gnus-newsgroup-name))
9491     ;; This backend supports expiry.
9492     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9493            (expirable (if total
9494                           (progn
9495                             ;; We need to update the info for
9496                             ;; this group for `gnus-list-of-read-articles'
9497                             ;; to give us the right answer.
9498                             (gnus-run-hooks 'gnus-exit-group-hook)
9499                             (gnus-summary-update-info)
9500                             (gnus-list-of-read-articles gnus-newsgroup-name))
9501                         (setq gnus-newsgroup-expirable
9502                               (sort gnus-newsgroup-expirable '<))))
9503            (expiry-wait (if now 'immediate
9504                           (gnus-group-find-parameter
9505                            gnus-newsgroup-name 'expiry-wait)))
9506            (nnmail-expiry-target
9507             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9508                 nnmail-expiry-target))
9509            es)
9510       (when expirable
9511         ;; There are expirable articles in this group, so we run them
9512         ;; through the expiry process.
9513         (gnus-message 6 "Expiring articles...")
9514         (unless (gnus-check-group gnus-newsgroup-name)
9515           (error "Can't open server for %s" gnus-newsgroup-name))
9516         ;; The list of articles that weren't expired is returned.
9517         (save-excursion
9518           (if expiry-wait
9519               (let ((nnmail-expiry-wait-function nil)
9520                     (nnmail-expiry-wait expiry-wait))
9521                 (setq es (gnus-request-expire-articles
9522                           expirable gnus-newsgroup-name)))
9523             (setq es (gnus-request-expire-articles
9524                       expirable gnus-newsgroup-name)))
9525           (unless total
9526             (setq gnus-newsgroup-expirable es))
9527           ;; We go through the old list of expirable, and mark all
9528           ;; really expired articles as nonexistent.
9529           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9530             (let ((gnus-use-cache nil))
9531               (dolist (article expirable)
9532                 (when (and (not (memq article es))
9533                            (gnus-data-find article))
9534                   (gnus-summary-mark-article article gnus-canceled-mark)
9535                   (run-hook-with-args 'gnus-summary-article-expire-hook
9536                                       'delete
9537                                       (gnus-data-header
9538                                        (assoc article (gnus-data-list nil)))
9539                                       gnus-newsgroup-name
9540                                       nil
9541                                       nil))))))
9542         (gnus-message 6 "Expiring articles...done")))))
9543
9544 (defun gnus-summary-expire-articles-now ()
9545   "Expunge all expirable articles in the current group.
9546 This means that *all* articles that are marked as expirable will be
9547 deleted forever, right now."
9548   (interactive)
9549   (or gnus-expert-user
9550       (gnus-yes-or-no-p
9551        "Are you really, really, really sure you want to delete all these messages? ")
9552       (error "Phew!"))
9553   (gnus-summary-expire-articles t))
9554
9555 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9556 (defun gnus-summary-delete-article (&optional n)
9557   "Delete the N next (mail) articles.
9558 This command actually deletes articles.  This is not a marking
9559 command.  The article will disappear forever from your life, never to
9560 return.
9561
9562 If N is negative, delete backwards.
9563 If N is nil and articles have been marked with the process mark,
9564 delete these instead.
9565
9566 If `gnus-novice-user' is non-nil you will be asked for
9567 confirmation before the articles are deleted."
9568   (interactive "P")
9569   (unless (gnus-check-backend-function 'request-expire-articles
9570                                        gnus-newsgroup-name)
9571     (error "The current newsgroup does not support article deletion"))
9572   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9573     (error "Couldn't open server"))
9574   ;; Compute the list of articles to delete.
9575   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9576         (nnmail-expiry-target 'delete)
9577         not-deleted)
9578     (if (and gnus-novice-user
9579              (not (gnus-yes-or-no-p
9580                    (format "Do you really want to delete %s forever? "
9581                            (if (> (length articles) 1)
9582                                (format "these %s articles" (length articles))
9583                              "this article")))))
9584         ()
9585       ;; Delete the articles.
9586       (setq not-deleted (gnus-request-expire-articles
9587                          articles gnus-newsgroup-name 'force))
9588       (while articles
9589         (gnus-summary-remove-process-mark (car articles))
9590         ;; The backend might not have been able to delete the article
9591         ;; after all.
9592         (unless (memq (car articles) not-deleted)
9593           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9594         (let* ((article (car articles))
9595                (ghead  (gnus-data-header
9596                                     (assoc article (gnus-data-list nil)))))
9597           (run-hook-with-args 'gnus-summary-article-delete-hook
9598                               'delete ghead gnus-newsgroup-name nil
9599                               nil))
9600         (setq articles (cdr articles)))
9601       (when not-deleted
9602         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9603     (gnus-summary-position-point)
9604     (gnus-set-mode-line 'summary)
9605     not-deleted))
9606
9607 (defun gnus-summary-edit-article (&optional arg)
9608   "Edit the current article.
9609 This will have permanent effect only in mail groups.
9610 If ARG is nil, edit the decoded articles.
9611 If ARG is 1, edit the raw articles.
9612 If ARG is 2, edit the raw articles even in read-only groups.
9613 If ARG is 3, edit the articles with the current handles.
9614 Otherwise, allow editing of articles even in read-only
9615 groups."
9616   (interactive "P")
9617   (let (force raw current-handles)
9618     (cond
9619      ((null arg))
9620      ((eq arg 1)
9621       (setq raw t))
9622      ((eq arg 2)
9623       (setq raw t
9624             force t))
9625      ((eq arg 3)
9626       (setq current-handles
9627             (and (gnus-buffer-live-p gnus-article-buffer)
9628                  (with-current-buffer gnus-article-buffer
9629                    (prog1
9630                        gnus-article-mime-handles
9631                      (setq gnus-article-mime-handles nil))))))
9632      (t
9633       (setq force t)))
9634     (when (and raw (not force)
9635                (member gnus-newsgroup-name '("nndraft:delayed"
9636                                              "nndraft:drafts"
9637                                              "nndraft:queue")))
9638       (error "Can't edit the raw article in group %s"
9639              gnus-newsgroup-name))
9640     (save-excursion
9641       (set-buffer gnus-summary-buffer)
9642       (let ((mail-parse-charset gnus-newsgroup-charset)
9643             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9644         (gnus-set-global-variables)
9645         (when (and (not force)
9646                    (gnus-group-read-only-p))
9647           (error "The current newsgroup does not support article editing"))
9648         (gnus-summary-show-article t)
9649         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9650           (with-current-buffer gnus-article-buffer
9651             (mm-enable-multibyte)))
9652         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9653             (setq raw t))
9654         (gnus-article-edit-article
9655          (if raw 'ignore
9656            `(lambda ()
9657               (let ((mbl mml-buffer-list))
9658                 (setq mml-buffer-list nil)
9659                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9660                   (mime-to-mml ,'current-handles))
9661                 (let ((mbl1 mml-buffer-list))
9662                   (setq mml-buffer-list mbl)
9663                   (set (make-local-variable 'mml-buffer-list) mbl1))
9664                 (gnus-make-local-hook 'kill-buffer-hook)
9665                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9666          `(lambda (no-highlight)
9667             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9668                   (message-options message-options)
9669                   (message-options-set-recipient)
9670                   (mail-parse-ignored-charsets
9671                    ',gnus-newsgroup-ignored-charsets))
9672               ,(if (not raw) '(progn
9673                                 (mml-to-mime)
9674                                 (mml-destroy-buffers)
9675                                 (remove-hook 'kill-buffer-hook
9676                                              'mml-destroy-buffers t)
9677                                 (kill-local-variable 'mml-buffer-list)))
9678               (gnus-summary-edit-article-done
9679                ,(or (mail-header-references gnus-current-headers) "")
9680                ,(gnus-group-read-only-p)
9681                ,gnus-summary-buffer no-highlight))))))))
9682
9683 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9684
9685 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9686                                                  no-highlight)
9687   "Make edits to the current article permanent."
9688   (interactive)
9689   (save-excursion
9690    ;; The buffer restriction contains the entire article if it exists.
9691     (when (article-goto-body)
9692       (let ((lines (count-lines (point) (point-max)))
9693             (length (- (point-max) (point)))
9694             (case-fold-search t)
9695             (body (copy-marker (point))))
9696         (goto-char (point-min))
9697         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9698           (delete-region (match-beginning 1) (match-end 1))
9699           (insert (number-to-string length)))
9700         (goto-char (point-min))
9701         (when (re-search-forward
9702                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9703           (delete-region (match-beginning 1) (match-end 1))
9704           (insert (number-to-string length)))
9705         (goto-char (point-min))
9706         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9707           (delete-region (match-beginning 1) (match-end 1))
9708           (insert (number-to-string lines))))))
9709   ;; Replace the article.
9710   (let ((buf (current-buffer)))
9711     (with-temp-buffer
9712       (insert-buffer-substring buf)
9713
9714       (if (and (not read-only)
9715                (not (gnus-request-replace-article
9716                      (cdr gnus-article-current) (car gnus-article-current)
9717                      (current-buffer) t)))
9718           (error "Couldn't replace article")
9719         ;; Update the summary buffer.
9720         (if (and references
9721                  (equal (message-tokenize-header references " ")
9722                         (message-tokenize-header
9723                          (or (message-fetch-field "references") "") " ")))
9724             ;; We only have to update this line.
9725             (save-excursion
9726               (save-restriction
9727                 (message-narrow-to-head)
9728                 (let ((head (buffer-substring-no-properties
9729                              (point-min) (point-max)))
9730                       header)
9731                   (with-temp-buffer
9732                     (insert (format "211 %d Article retrieved.\n"
9733                                     (cdr gnus-article-current)))
9734                     (insert head)
9735                     (insert ".\n")
9736                     (let ((nntp-server-buffer (current-buffer)))
9737                       (setq header (car (gnus-get-newsgroup-headers
9738                                          nil t))))
9739                     (save-excursion
9740                       (set-buffer gnus-summary-buffer)
9741                       (gnus-data-set-header
9742                        (gnus-data-find (cdr gnus-article-current))
9743                        header)
9744                       (gnus-summary-update-article-line
9745                        (cdr gnus-article-current) header)
9746                       (if (gnus-summary-goto-subject
9747                            (cdr gnus-article-current) nil t)
9748                           (gnus-summary-update-secondary-mark
9749                            (cdr gnus-article-current))))))))
9750           ;; Update threads.
9751           (set-buffer (or buffer gnus-summary-buffer))
9752           (gnus-summary-update-article (cdr gnus-article-current))
9753           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9754               (gnus-summary-update-secondary-mark
9755                (cdr gnus-article-current))))
9756         ;; Prettify the article buffer again.
9757         (unless no-highlight
9758           (save-excursion
9759             (set-buffer gnus-article-buffer)
9760             ;;;!!! Fix this -- article should be rehighlighted.
9761             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9762             (set-buffer gnus-original-article-buffer)
9763             (gnus-request-article
9764              (cdr gnus-article-current)
9765              (car gnus-article-current) (current-buffer))))
9766         ;; Prettify the summary buffer line.
9767         (when (gnus-visual-p 'summary-highlight 'highlight)
9768           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9769
9770 (defun gnus-summary-edit-wash (key)
9771   "Perform editing command KEY in the article buffer."
9772   (interactive
9773    (list
9774     (progn
9775       (message "%s" (concat (this-command-keys) "- "))
9776       (read-char))))
9777   (message "")
9778   (gnus-summary-edit-article)
9779   (execute-kbd-macro (concat (this-command-keys) key))
9780   (gnus-article-edit-done))
9781
9782 ;;; Respooling
9783
9784 (defun gnus-summary-respool-query (&optional silent trace)
9785   "Query where the respool algorithm would put this article."
9786   (interactive)
9787   (let (gnus-mark-article-hook)
9788     (gnus-summary-select-article)
9789     (save-excursion
9790       (set-buffer gnus-original-article-buffer)
9791       (let ((groups (nnmail-article-group 'identity trace)))
9792         (unless silent
9793           (if groups
9794               (message "This message would go to %s"
9795                        (mapconcat 'car groups ", "))
9796             (message "This message would go to no groups"))
9797           groups)))))
9798
9799 (defun gnus-summary-respool-trace ()
9800   "Trace where the respool algorithm would put this article.
9801 Display a buffer showing all fancy splitting patterns which matched."
9802   (interactive)
9803   (gnus-summary-respool-query nil t))
9804
9805 ;; Summary marking commands.
9806
9807 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9808   "Mark articles which has the same subject as read, and then select the next.
9809 If UNMARK is positive, remove any kind of mark.
9810 If UNMARK is negative, tick articles."
9811   (interactive "P")
9812   (when unmark
9813     (setq unmark (prefix-numeric-value unmark)))
9814   (let ((count
9815          (gnus-summary-mark-same-subject
9816           (gnus-summary-article-subject) unmark)))
9817     ;; Select next unread article.  If auto-select-same mode, should
9818     ;; select the first unread article.
9819     (gnus-summary-next-article t (and gnus-auto-select-same
9820                                       (gnus-summary-article-subject)))
9821     (gnus-message 7 "%d article%s marked as %s"
9822                   count (if (= count 1) " is" "s are")
9823                   (if unmark "unread" "read"))))
9824
9825 (defun gnus-summary-kill-same-subject (&optional unmark)
9826   "Mark articles which has the same subject as read.
9827 If UNMARK is positive, remove any kind of mark.
9828 If UNMARK is negative, tick articles."
9829   (interactive "P")
9830   (when unmark
9831     (setq unmark (prefix-numeric-value unmark)))
9832   (let ((count
9833          (gnus-summary-mark-same-subject
9834           (gnus-summary-article-subject) unmark)))
9835     ;; If marked as read, go to next unread subject.
9836     (when (null unmark)
9837       ;; Go to next unread subject.
9838       (gnus-summary-next-subject 1 t))
9839     (gnus-message 7 "%d articles are marked as %s"
9840                   count (if unmark "unread" "read"))))
9841
9842 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9843   "Mark articles with same SUBJECT as read, and return marked number.
9844 If optional argument UNMARK is positive, remove any kinds of marks.
9845 If optional argument UNMARK is negative, mark articles as unread instead."
9846   (let ((count 1))
9847     (save-excursion
9848       (cond
9849        ((null unmark)                   ; Mark as read.
9850         (while (and
9851                 (progn
9852                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9853                   (gnus-summary-show-thread) t)
9854                 (gnus-summary-find-subject subject))
9855           (setq count (1+ count))))
9856        ((> unmark 0)                    ; Tick.
9857         (while (and
9858                 (progn
9859                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9860                   (gnus-summary-show-thread) t)
9861                 (gnus-summary-find-subject subject))
9862           (setq count (1+ count))))
9863        (t                               ; Mark as unread.
9864         (while (and
9865                 (progn
9866                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9867                   (gnus-summary-show-thread) t)
9868                 (gnus-summary-find-subject subject))
9869           (setq count (1+ count)))))
9870       (gnus-set-mode-line 'summary)
9871       ;; Return the number of marked articles.
9872       count)))
9873
9874 (defun gnus-summary-mark-as-processable (n &optional unmark)
9875   "Set the process mark on the next N articles.
9876 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9877 the process mark instead.  The difference between N and the actual
9878 number of articles marked is returned."
9879   (interactive "P")
9880   (if (and (null n) (gnus-region-active-p))
9881       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9882     (setq n (prefix-numeric-value n))
9883     (let ((backward (< n 0))
9884           (n (abs n)))
9885       (while (and
9886               (> n 0)
9887               (if unmark
9888                   (gnus-summary-remove-process-mark
9889                    (gnus-summary-article-number))
9890                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9891               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9892         (setq n (1- n)))
9893       (when (/= 0 n)
9894         (gnus-message 7 "No more articles"))
9895       (gnus-summary-recenter)
9896       (gnus-summary-position-point)
9897       n)))
9898
9899 (defun gnus-summary-unmark-as-processable (n)
9900   "Remove the process mark from the next N articles.
9901 If N is negative, unmark backward instead.  The difference between N and
9902 the actual number of articles unmarked is returned."
9903   (interactive "P")
9904   (gnus-summary-mark-as-processable n t))
9905
9906 (defun gnus-summary-unmark-all-processable ()
9907   "Remove the process mark from all articles."
9908   (interactive)
9909   (save-excursion
9910     (while gnus-newsgroup-processable
9911       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9912   (gnus-summary-position-point))
9913
9914 (defun gnus-summary-add-mark (article type)
9915   "Mark ARTICLE with a mark of TYPE."
9916   (let ((vtype (car (assq type gnus-article-mark-lists)))
9917         var)
9918     (if (not vtype)
9919         (error "No such mark type: %s" type)
9920       (setq var (intern (format "gnus-newsgroup-%s" type)))
9921       (set var (cons article (symbol-value var)))
9922       (if (memq type '(processable cached replied forwarded recent saved))
9923           (gnus-summary-update-secondary-mark article)
9924         ;;; !!! This is bogus.  We should find out what primary
9925         ;;; !!! mark we want to set.
9926         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9927
9928 (defun gnus-summary-mark-as-expirable (n)
9929   "Mark N articles forward as expirable.
9930 If N is negative, mark backward instead.  The difference between N and
9931 the actual number of articles marked is returned."
9932   (interactive "p")
9933   (gnus-summary-mark-forward n gnus-expirable-mark))
9934
9935 (defun gnus-summary-mark-as-spam (n)
9936   "Mark N articles forward as spam.
9937 If N is negative, mark backward instead.  The difference between N and
9938 the actual number of articles marked is returned."
9939   (interactive "p")
9940   (gnus-summary-mark-forward n gnus-spam-mark))
9941
9942 (defun gnus-summary-mark-article-as-replied (article)
9943   "Mark ARTICLE as replied to and update the summary line.
9944 ARTICLE can also be a list of articles."
9945   (interactive (list (gnus-summary-article-number)))
9946   (let ((articles (if (listp article) article (list article))))
9947     (dolist (article articles)
9948       (unless (numberp article)
9949         (error "%s is not a number" article))
9950       (push article gnus-newsgroup-replied)
9951       (let ((buffer-read-only nil))
9952         (when (gnus-summary-goto-subject article nil t)
9953           (gnus-summary-update-secondary-mark article))))))
9954
9955 (defun gnus-summary-mark-article-as-forwarded (article)
9956   "Mark ARTICLE as forwarded and update the summary line.
9957 ARTICLE can also be a list of articles."
9958   (let ((articles (if (listp article) article (list article))))
9959     (dolist (article articles)
9960       (push article gnus-newsgroup-forwarded)
9961       (let ((buffer-read-only nil))
9962         (when (gnus-summary-goto-subject article nil t)
9963           (gnus-summary-update-secondary-mark article))))))
9964
9965 (defun gnus-summary-set-bookmark (article)
9966   "Set a bookmark in current article."
9967   (interactive (list (gnus-summary-article-number)))
9968   (when (or (not (get-buffer gnus-article-buffer))
9969             (not gnus-current-article)
9970             (not gnus-article-current)
9971             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9972     (error "No current article selected"))
9973   ;; Remove old bookmark, if one exists.
9974   (gnus-pull article gnus-newsgroup-bookmarks)
9975   ;; Set the new bookmark, which is on the form
9976   ;; (article-number . line-number-in-body).
9977   (push
9978    (cons article
9979          (with-current-buffer gnus-article-buffer
9980            (count-lines
9981             (min (point)
9982                  (save-excursion
9983                    (article-goto-body)
9984                    (point)))
9985             (point))))
9986    gnus-newsgroup-bookmarks)
9987   (gnus-message 6 "A bookmark has been added to the current article."))
9988
9989 (defun gnus-summary-remove-bookmark (article)
9990   "Remove the bookmark from the current article."
9991   (interactive (list (gnus-summary-article-number)))
9992   ;; Remove old bookmark, if one exists.
9993   (if (not (assq article gnus-newsgroup-bookmarks))
9994       (gnus-message 6 "No bookmark in current article.")
9995     (gnus-pull article gnus-newsgroup-bookmarks)
9996     (gnus-message 6 "Removed bookmark.")))
9997
9998 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9999 (defun gnus-summary-mark-as-dormant (n)
10000   "Mark N articles forward as dormant.
10001 If N is negative, mark backward instead.  The difference between N and
10002 the actual number of articles marked is returned."
10003   (interactive "p")
10004   (gnus-summary-mark-forward n gnus-dormant-mark))
10005
10006 (defun gnus-summary-set-process-mark (article)
10007   "Set the process mark on ARTICLE and update the summary line."
10008   (setq gnus-newsgroup-processable
10009         (cons article
10010               (delq article gnus-newsgroup-processable)))
10011   (when (gnus-summary-goto-subject article)
10012     (gnus-summary-show-thread)
10013     (gnus-summary-goto-subject article)
10014     (gnus-summary-update-secondary-mark article)))
10015
10016 (defun gnus-summary-remove-process-mark (&rest articles)
10017   "Remove the process mark from ARTICLES and update the summary line."
10018   (dolist (article articles)
10019     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10020     (when (gnus-summary-goto-subject article)
10021       (gnus-summary-show-thread)
10022       (gnus-summary-goto-subject article)
10023       (gnus-summary-update-secondary-mark article)))
10024   t)
10025
10026 (defun gnus-summary-set-saved-mark (article)
10027   "Set the process mark on ARTICLE and update the summary line."
10028   (push article gnus-newsgroup-saved)
10029   (when (gnus-summary-goto-subject article)
10030     (gnus-summary-update-secondary-mark article)))
10031
10032 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10033   "Mark N articles as read forwards.
10034 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10035 The difference between N and the actual number of articles marked is
10036 returned.
10037 If NO-EXPIRE, auto-expiry will be inhibited."
10038   (interactive "p")
10039   (gnus-summary-show-thread)
10040   (let ((backward (< n 0))
10041         (gnus-summary-goto-unread
10042          (and gnus-summary-goto-unread
10043               (not (eq gnus-summary-goto-unread 'never))
10044               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10045                                     gnus-ticked-mark gnus-dormant-mark)))))
10046         (n (abs n))
10047         (mark (or mark gnus-del-mark)))
10048     (while (and (> n 0)
10049                 (gnus-summary-mark-article nil mark no-expire)
10050                 (zerop (gnus-summary-next-subject
10051                         (if backward -1 1)
10052                         (and gnus-summary-goto-unread
10053                              (not (eq gnus-summary-goto-unread 'never)))
10054                         t)))
10055       (setq n (1- n)))
10056     (when (/= 0 n)
10057       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10058     (gnus-summary-recenter)
10059     (gnus-summary-position-point)
10060     (gnus-set-mode-line 'summary)
10061     n))
10062
10063 (defun gnus-summary-mark-article-as-read (mark)
10064   "Mark the current article quickly as read with MARK."
10065   (let ((article (gnus-summary-article-number)))
10066     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10067     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10068     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10069     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10070     (push (cons article mark) gnus-newsgroup-reads)
10071     ;; Possibly remove from cache, if that is used.
10072     (when gnus-use-cache
10073       (gnus-cache-enter-remove-article article))
10074     ;; Allow the backend to change the mark.
10075     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10076     ;; Check for auto-expiry.
10077     (when (and gnus-newsgroup-auto-expire
10078                (memq mark gnus-auto-expirable-marks))
10079       (setq mark gnus-expirable-mark)
10080       ;; Let the backend know about the mark change.
10081       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10082       (push article gnus-newsgroup-expirable))
10083     ;; Set the mark in the buffer.
10084     (gnus-summary-update-mark mark 'unread)
10085     t))
10086
10087 (defun gnus-summary-mark-article-as-unread (mark)
10088   "Mark the current article quickly as unread with MARK."
10089   (let* ((article (gnus-summary-article-number))
10090          (old-mark (gnus-summary-article-mark article)))
10091     ;; Allow the backend to change the mark.
10092     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10093     (if (eq mark old-mark)
10094         t
10095       (if (<= article 0)
10096           (progn
10097             (gnus-error 1 "Can't mark negative article numbers")
10098             nil)
10099         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10100         (setq gnus-newsgroup-spam-marked
10101               (delq article gnus-newsgroup-spam-marked))
10102         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10103         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10104         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10105         (cond ((= mark gnus-ticked-mark)
10106                (setq gnus-newsgroup-marked
10107                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10108                                               article)))
10109               ((= mark gnus-spam-mark)
10110                (setq gnus-newsgroup-spam-marked
10111                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10112                                               article)))
10113               ((= mark gnus-dormant-mark)
10114                (setq gnus-newsgroup-dormant
10115                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10116                                               article)))
10117               (t
10118                (setq gnus-newsgroup-unreads
10119                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10120                                               article))))
10121         (gnus-pull article gnus-newsgroup-reads)
10122
10123         ;; See whether the article is to be put in the cache.
10124         (and gnus-use-cache
10125              (vectorp (gnus-summary-article-header article))
10126              (save-excursion
10127                (gnus-cache-possibly-enter-article
10128                 gnus-newsgroup-name article
10129                 (= mark gnus-ticked-mark)
10130                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10131
10132         ;; Fix the mark.
10133         (gnus-summary-update-mark mark 'unread)
10134         t))))
10135
10136 (defun gnus-summary-mark-article (&optional article mark no-expire)
10137   "Mark ARTICLE with MARK.  MARK can be any character.
10138 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10139 `??' (dormant) and `?E' (expirable).
10140 If MARK is nil, then the default character `?r' is used.
10141 If ARTICLE is nil, then the article on the current line will be
10142 marked.
10143 If NO-EXPIRE, auto-expiry will be inhibited."
10144   ;; The mark might be a string.
10145   (when (stringp mark)
10146     (setq mark (aref mark 0)))
10147   ;; If no mark is given, then we check auto-expiring.
10148   (when (null mark)
10149     (setq mark gnus-del-mark))
10150   (when (and (not no-expire)
10151              gnus-newsgroup-auto-expire
10152              (memq mark gnus-auto-expirable-marks))
10153     (setq mark gnus-expirable-mark))
10154   (let ((article (or article (gnus-summary-article-number)))
10155         (old-mark (gnus-summary-article-mark article)))
10156     ;; Allow the backend to change the mark.
10157     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10158     (if (eq mark old-mark)
10159         t
10160       (unless article
10161         (error "No article on current line"))
10162       (if (not (if (or (= mark gnus-unread-mark)
10163                        (= mark gnus-ticked-mark)
10164                        (= mark gnus-spam-mark)
10165                        (= mark gnus-dormant-mark))
10166                    (gnus-mark-article-as-unread article mark)
10167                  (gnus-mark-article-as-read article mark)))
10168           t
10169         ;; See whether the article is to be put in the cache.
10170         (and gnus-use-cache
10171              (not (= mark gnus-canceled-mark))
10172              (vectorp (gnus-summary-article-header article))
10173              (save-excursion
10174                (gnus-cache-possibly-enter-article
10175                 gnus-newsgroup-name article
10176                 (= mark gnus-ticked-mark)
10177                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10178
10179         (when (gnus-summary-goto-subject article nil t)
10180           (let ((buffer-read-only nil))
10181             (gnus-summary-show-thread)
10182             ;; Fix the mark.
10183             (gnus-summary-update-mark mark 'unread)
10184             t))))))
10185
10186 (defun gnus-summary-update-secondary-mark (article)
10187   "Update the secondary (read, process, cache) mark."
10188   (gnus-summary-update-mark
10189    (cond ((memq article gnus-newsgroup-processable)
10190           gnus-process-mark)
10191          ((memq article gnus-newsgroup-cached)
10192           gnus-cached-mark)
10193          ((memq article gnus-newsgroup-replied)
10194           gnus-replied-mark)
10195          ((memq article gnus-newsgroup-forwarded)
10196           gnus-forwarded-mark)
10197          ((memq article gnus-newsgroup-saved)
10198           gnus-saved-mark)
10199          ((memq article gnus-newsgroup-recent)
10200           gnus-recent-mark)
10201          ((memq article gnus-newsgroup-unseen)
10202           gnus-unseen-mark)
10203          (t gnus-no-mark))
10204    'replied)
10205   (when (gnus-visual-p 'summary-highlight 'highlight)
10206     (gnus-run-hooks 'gnus-summary-update-hook))
10207   t)
10208
10209 (defun gnus-summary-update-download-mark (article)
10210   "Update the download mark."
10211   (gnus-summary-update-mark
10212    (cond ((memq article gnus-newsgroup-undownloaded)
10213           gnus-undownloaded-mark)
10214          (gnus-newsgroup-agentized
10215           gnus-downloaded-mark)
10216          (t
10217           gnus-no-mark))
10218    'download)
10219   (gnus-summary-update-line t)
10220   t)
10221
10222 (defun gnus-summary-update-mark (mark type)
10223   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10224         (buffer-read-only nil))
10225     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10226     (when forward
10227       (when (looking-at "\r")
10228         (incf forward))
10229       (when (<= (+ forward (point)) (point-max))
10230         ;; Go to the right position on the line.
10231         (goto-char (+ forward (point)))
10232         ;; Replace the old mark with the new mark.
10233         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10234         ;; Optionally update the marks by some user rule.
10235         (when (eq type 'unread)
10236           (gnus-data-set-mark
10237            (gnus-data-find (gnus-summary-article-number)) mark)
10238           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10239
10240 (defun gnus-mark-article-as-read (article &optional mark)
10241   "Enter ARTICLE in the pertinent lists and remove it from others."
10242   ;; Make the article expirable.
10243   (let ((mark (or mark gnus-del-mark)))
10244     (setq gnus-newsgroup-expirable
10245           (if (= mark gnus-expirable-mark)
10246               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10247             (delq article gnus-newsgroup-expirable)))
10248     ;; Remove from unread and marked lists.
10249     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10250     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10251     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10252     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10253     (push (cons article mark) gnus-newsgroup-reads)
10254     ;; Possibly remove from cache, if that is used.
10255     (when gnus-use-cache
10256       (gnus-cache-enter-remove-article article))
10257     t))
10258
10259 (defun gnus-mark-article-as-unread (article &optional mark)
10260   "Enter ARTICLE in the pertinent lists and remove it from others."
10261   (let ((mark (or mark gnus-ticked-mark)))
10262     (if (<= article 0)
10263         (progn
10264           (gnus-error 1 "Can't mark negative article numbers")
10265           nil)
10266       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10267             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10268             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10269             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10270             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10271
10272       ;; Unsuppress duplicates?
10273       (when gnus-suppress-duplicates
10274         (gnus-dup-unsuppress-article article))
10275
10276       (cond ((= mark gnus-ticked-mark)
10277              (setq gnus-newsgroup-marked
10278                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10279             ((= mark gnus-spam-mark)
10280              (setq gnus-newsgroup-spam-marked
10281                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10282                                             article)))
10283             ((= mark gnus-dormant-mark)
10284              (setq gnus-newsgroup-dormant
10285                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10286             (t
10287              (setq gnus-newsgroup-unreads
10288                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10289       (gnus-pull article gnus-newsgroup-reads)
10290       t)))
10291
10292 (defalias 'gnus-summary-mark-as-unread-forward
10293   'gnus-summary-tick-article-forward)
10294 (make-obsolete 'gnus-summary-mark-as-unread-forward
10295                'gnus-summary-tick-article-forward)
10296 (defun gnus-summary-tick-article-forward (n)
10297   "Tick N articles forwards.
10298 If N is negative, tick backwards instead.
10299 The difference between N and the number of articles ticked is returned."
10300   (interactive "p")
10301   (gnus-summary-mark-forward n gnus-ticked-mark))
10302
10303 (defalias 'gnus-summary-mark-as-unread-backward
10304   'gnus-summary-tick-article-backward)
10305 (make-obsolete 'gnus-summary-mark-as-unread-backward
10306                'gnus-summary-tick-article-backward)
10307 (defun gnus-summary-tick-article-backward (n)
10308   "Tick N articles backwards.
10309 The difference between N and the number of articles ticked is returned."
10310   (interactive "p")
10311   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10312
10313 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10314 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10315 (defun gnus-summary-tick-article (&optional article clear-mark)
10316   "Mark current article as unread.
10317 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10318 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10319   (interactive)
10320   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10321                                        gnus-ticked-mark)))
10322
10323 (defun gnus-summary-mark-as-read-forward (n)
10324   "Mark N articles as read forwards.
10325 If N is negative, mark backwards instead.
10326 The difference between N and the actual number of articles marked is
10327 returned."
10328   (interactive "p")
10329   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10330
10331 (defun gnus-summary-mark-as-read-backward (n)
10332   "Mark the N articles as read backwards.
10333 The difference between N and the actual number of articles marked is
10334 returned."
10335   (interactive "p")
10336   (gnus-summary-mark-forward
10337    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10338
10339 (defun gnus-summary-mark-as-read (&optional article mark)
10340   "Mark current article as read.
10341 ARTICLE specifies the article to be marked as read.
10342 MARK specifies a string to be inserted at the beginning of the line."
10343   (gnus-summary-mark-article article mark))
10344
10345 (defun gnus-summary-clear-mark-forward (n)
10346   "Clear marks from N articles forward.
10347 If N is negative, clear backward instead.
10348 The difference between N and the number of marks cleared is returned."
10349   (interactive "p")
10350   (gnus-summary-mark-forward n gnus-unread-mark))
10351
10352 (defun gnus-summary-clear-mark-backward (n)
10353   "Clear marks from N articles backward.
10354 The difference between N and the number of marks cleared is returned."
10355   (interactive "p")
10356   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10357
10358 (defun gnus-summary-mark-unread-as-read ()
10359   "Intended to be used by `gnus-summary-mark-article-hook'."
10360   (when (memq gnus-current-article gnus-newsgroup-unreads)
10361     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10362
10363 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10364   "Intended to be used by `gnus-summary-mark-article-hook'."
10365   (let ((mark (gnus-summary-article-mark)))
10366     (when (or (gnus-unread-mark-p mark)
10367               (gnus-read-mark-p mark))
10368       (gnus-summary-mark-article gnus-current-article
10369                                  (or new-mark gnus-read-mark)))))
10370
10371 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10372   "Intended to be used by `gnus-summary-mark-article-hook'."
10373   (let ((mark (gnus-summary-article-mark)))
10374     (when (or (gnus-unread-mark-p mark)
10375               (gnus-read-mark-p mark))
10376       (gnus-summary-mark-article (gnus-summary-article-number)
10377                                  (or new-mark gnus-read-mark)))))
10378
10379 (defun gnus-summary-mark-unread-as-ticked ()
10380   "Intended to be used by `gnus-summary-mark-article-hook'."
10381   (when (memq gnus-current-article gnus-newsgroup-unreads)
10382     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10383
10384 (defun gnus-summary-mark-region-as-read (point mark all)
10385   "Mark all unread articles between point and mark as read.
10386 If given a prefix, mark all articles between point and mark as read,
10387 even ticked and dormant ones."
10388   (interactive "r\nP")
10389   (save-excursion
10390     (let (article)
10391       (goto-char point)
10392       (beginning-of-line)
10393       (while (and
10394               (< (point) mark)
10395               (progn
10396                 (when (or all
10397                           (memq (setq article (gnus-summary-article-number))
10398                                 gnus-newsgroup-unreads))
10399                   (gnus-summary-mark-article article gnus-del-mark))
10400                 t)
10401               (gnus-summary-find-next))))))
10402
10403 (defun gnus-summary-mark-below (score mark)
10404   "Mark articles with score less than SCORE with MARK."
10405   (interactive "P\ncMark: ")
10406   (setq score (if score
10407                   (prefix-numeric-value score)
10408                 (or gnus-summary-default-score 0)))
10409   (save-excursion
10410     (set-buffer gnus-summary-buffer)
10411     (goto-char (point-min))
10412     (while
10413         (progn
10414           (and (< (gnus-summary-article-score) score)
10415                (gnus-summary-mark-article nil mark))
10416           (gnus-summary-find-next)))))
10417
10418 (defun gnus-summary-kill-below (&optional score)
10419   "Mark articles with score below SCORE as read."
10420   (interactive "P")
10421   (gnus-summary-mark-below score gnus-killed-mark))
10422
10423 (defun gnus-summary-clear-above (&optional score)
10424   "Clear all marks from articles with score above SCORE."
10425   (interactive "P")
10426   (gnus-summary-mark-above score gnus-unread-mark))
10427
10428 (defun gnus-summary-tick-above (&optional score)
10429   "Tick all articles with score above SCORE."
10430   (interactive "P")
10431   (gnus-summary-mark-above score gnus-ticked-mark))
10432
10433 (defun gnus-summary-mark-above (score mark)
10434   "Mark articles with score over SCORE with MARK."
10435   (interactive "P\ncMark: ")
10436   (setq score (if score
10437                   (prefix-numeric-value score)
10438                 (or gnus-summary-default-score 0)))
10439   (save-excursion
10440     (set-buffer gnus-summary-buffer)
10441     (goto-char (point-min))
10442     (while (and (progn
10443                   (when (> (gnus-summary-article-score) score)
10444                     (gnus-summary-mark-article nil mark))
10445                   t)
10446                 (gnus-summary-find-next)))))
10447
10448 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10449 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10450 (defun gnus-summary-limit-include-expunged (&optional no-error)
10451   "Display all the hidden articles that were expunged for low scores."
10452   (interactive)
10453   (let ((buffer-read-only nil))
10454     (let ((scored gnus-newsgroup-scored)
10455           headers h)
10456       (while scored
10457         (unless (gnus-summary-article-header (caar scored))
10458           (and (setq h (gnus-number-to-header (caar scored)))
10459                (< (cdar scored) gnus-summary-expunge-below)
10460                (push h headers)))
10461         (setq scored (cdr scored)))
10462       (if (not headers)
10463           (when (not no-error)
10464             (error "No expunged articles hidden"))
10465         (goto-char (point-min))
10466         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10467         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10468         (mapcar (lambda (x) (push (mail-header-number x)
10469                                   gnus-newsgroup-limit))
10470                 headers)
10471         (gnus-summary-prepare-unthreaded (nreverse headers))
10472         (goto-char (point-min))
10473         (gnus-summary-position-point)
10474         t))))
10475
10476 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10477   "Mark all unread articles in this newsgroup as read.
10478 If prefix argument ALL is non-nil, ticked and dormant articles will
10479 also be marked as read.
10480 If QUIETLY is non-nil, no questions will be asked.
10481
10482 If TO-HERE is non-nil, it should be a point in the buffer.  All
10483 articles before (after, if REVERSE is set) this point will be marked
10484 as read.
10485
10486 Note that this function will only catch up the unread article
10487 in the current summary buffer limitation.
10488
10489 The number of articles marked as read is returned."
10490   (interactive "P")
10491   (prog1
10492       (save-excursion
10493         (when (or quietly
10494                   (not gnus-interactive-catchup) ;Without confirmation?
10495                   gnus-expert-user
10496                   (gnus-y-or-n-p
10497                    (if all
10498                        "Mark absolutely all articles as read? "
10499                      "Mark all unread articles as read? ")))
10500           (if (and not-mark
10501                    (not gnus-newsgroup-adaptive)
10502                    (not gnus-newsgroup-auto-expire)
10503                    (not gnus-suppress-duplicates)
10504                    (or (not gnus-use-cache)
10505                        (eq gnus-use-cache 'passive)))
10506               (progn
10507                 (when all
10508                   (setq gnus-newsgroup-marked nil
10509                         gnus-newsgroup-spam-marked nil
10510                         gnus-newsgroup-dormant nil))
10511                 (setq gnus-newsgroup-unreads
10512                       (gnus-sorted-nunion
10513                        (gnus-intersection gnus-newsgroup-unreads
10514                                           gnus-newsgroup-downloadable)
10515                        gnus-newsgroup-unfetched)))
10516             ;; We actually mark all articles as canceled, which we
10517             ;; have to do when using auto-expiry or adaptive scoring.
10518             (gnus-summary-show-all-threads)
10519             (if (and to-here reverse)
10520                 (progn
10521                   (goto-char to-here)
10522                   (gnus-summary-mark-current-read-and-unread-as-read
10523                    gnus-catchup-mark)
10524                   (while (gnus-summary-find-next (not all))
10525                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10526               (when (gnus-summary-first-subject (not all))
10527                 (while (and
10528                         (if to-here (< (point) to-here) t)
10529                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10530                         (gnus-summary-find-next (not all))))))
10531             (gnus-set-mode-line 'summary))
10532           t))
10533     (gnus-summary-position-point)))
10534
10535 (defun gnus-summary-catchup-to-here (&optional all)
10536   "Mark all unticked articles before the current one as read.
10537 If ALL is non-nil, also mark ticked and dormant articles as read."
10538   (interactive "P")
10539   (save-excursion
10540     (gnus-save-hidden-threads
10541       (let ((beg (point)))
10542         ;; We check that there are unread articles.
10543         (when (or all (gnus-summary-find-prev))
10544           (gnus-summary-catchup all t beg)))))
10545   (gnus-summary-position-point))
10546
10547 (defun gnus-summary-catchup-from-here (&optional all)
10548   "Mark all unticked articles after (and including) the current one as read.
10549 If ALL is non-nil, also mark ticked and dormant articles as read."
10550   (interactive "P")
10551   (save-excursion
10552     (gnus-save-hidden-threads
10553       (let ((beg (point)))
10554         ;; We check that there are unread articles.
10555         (when (or all (gnus-summary-find-next))
10556           (gnus-summary-catchup all t beg nil t)))))
10557   (gnus-summary-position-point))
10558
10559 (defun gnus-summary-catchup-all (&optional quietly)
10560   "Mark all articles in this newsgroup as read.
10561 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10562 instead, which marks only unread articles as read."
10563   (interactive "P")
10564   (gnus-summary-catchup t quietly))
10565
10566 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10567   "Mark all unread articles in this group as read, then exit.
10568 If prefix argument ALL is non-nil, all articles are marked as read.
10569 If QUIETLY is non-nil, no questions will be asked."
10570   (interactive "P")
10571   (when (gnus-summary-catchup all quietly nil 'fast)
10572     ;; Select next newsgroup or exit.
10573     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10574              (eq gnus-auto-select-next 'quietly))
10575         (gnus-summary-next-group nil)
10576       (gnus-summary-exit))))
10577
10578 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10579   "Mark all articles in this newsgroup as read, and then exit.
10580 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10581 instead, which marks only unread articles as read."
10582   (interactive "P")
10583   (gnus-summary-catchup-and-exit t quietly))
10584
10585 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10586   "Mark all articles in this group as read and select the next group.
10587 If given a prefix, mark all articles, unread as well as ticked, as
10588 read."
10589   (interactive "P")
10590   (save-excursion
10591     (gnus-summary-catchup all))
10592   (gnus-summary-next-group))
10593
10594 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
10595   "Mark all articles in this group as read and select the previous group.
10596 If given a prefix, mark all articles, unread as well as ticked, as
10597 read."
10598   (interactive "P")
10599   (save-excursion
10600     (gnus-summary-catchup all))
10601   (gnus-summary-next-group nil nil t))
10602
10603 ;;;
10604 ;;; with article
10605 ;;;
10606
10607 (defmacro gnus-with-article (article &rest forms)
10608   "Select ARTICLE and perform FORMS in the original article buffer.
10609 Then replace the article with the result."
10610   `(progn
10611      ;; We don't want the article to be marked as read.
10612      (let (gnus-mark-article-hook)
10613        (gnus-summary-select-article t t nil ,article))
10614      (set-buffer gnus-original-article-buffer)
10615      ,@forms
10616      (if (not (gnus-check-backend-function
10617                'request-replace-article (car gnus-article-current)))
10618          (gnus-message 5 "Read-only group; not replacing")
10619        (unless (gnus-request-replace-article
10620                 ,article (car gnus-article-current)
10621                 (current-buffer) t)
10622          (error "Couldn't replace article")))
10623      ;; The cache and backlog have to be flushed somewhat.
10624      (when gnus-keep-backlog
10625        (gnus-backlog-remove-article
10626         (car gnus-article-current) (cdr gnus-article-current)))
10627      (when gnus-use-cache
10628        (gnus-cache-update-article
10629         (car gnus-article-current) (cdr gnus-article-current)))))
10630
10631 (put 'gnus-with-article 'lisp-indent-function 1)
10632 (put 'gnus-with-article 'edebug-form-spec '(form body))
10633
10634 ;; Thread-based commands.
10635
10636 (defun gnus-summary-articles-in-thread (&optional article)
10637   "Return a list of all articles in the current thread.
10638 If ARTICLE is non-nil, return all articles in the thread that starts
10639 with that article."
10640   (let* ((article (or article (gnus-summary-article-number)))
10641          (data (gnus-data-find-list article))
10642          (top-level (gnus-data-level (car data)))
10643          (top-subject
10644           (cond ((null gnus-thread-operation-ignore-subject)
10645                  (gnus-simplify-subject-re
10646                   (mail-header-subject (gnus-data-header (car data)))))
10647                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10648                  (gnus-simplify-subject-fuzzy
10649                   (mail-header-subject (gnus-data-header (car data)))))
10650                 (t nil)))
10651          (end-point (save-excursion
10652                       (if (gnus-summary-go-to-next-thread)
10653                           (point) (point-max))))
10654          articles)
10655     (while (and data
10656                 (< (gnus-data-pos (car data)) end-point))
10657       (when (or (not top-subject)
10658                 (string= top-subject
10659                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10660                              (gnus-simplify-subject-fuzzy
10661                               (mail-header-subject
10662                                (gnus-data-header (car data))))
10663                            (gnus-simplify-subject-re
10664                             (mail-header-subject
10665                              (gnus-data-header (car data)))))))
10666         (push (gnus-data-number (car data)) articles))
10667       (unless (and (setq data (cdr data))
10668                    (> (gnus-data-level (car data)) top-level))
10669         (setq data nil)))
10670     ;; Return the list of articles.
10671     (nreverse articles)))
10672
10673 (defun gnus-summary-rethread-current ()
10674   "Rethread the thread the current article is part of."
10675   (interactive)
10676   (let* ((gnus-show-threads t)
10677          (article (gnus-summary-article-number))
10678          (id (mail-header-id (gnus-summary-article-header)))
10679          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10680     (unless id
10681       (error "No article on the current line"))
10682     (gnus-rebuild-thread id)
10683     (gnus-summary-goto-subject article)))
10684
10685 (defun gnus-summary-reparent-thread ()
10686   "Make the current article child of the marked (or previous) article.
10687
10688 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10689 is non-nil or the Subject: of both articles are the same."
10690   (interactive)
10691   (unless (not (gnus-group-read-only-p))
10692     (error "The current newsgroup does not support article editing"))
10693   (unless (<= (length gnus-newsgroup-processable) 1)
10694     (error "No more than one article may be marked"))
10695   (save-window-excursion
10696     (let ((gnus-article-buffer " *reparent*")
10697           (current-article (gnus-summary-article-number))
10698           ;; First grab the marked article, otherwise one line up.
10699           (parent-article (if (not (null gnus-newsgroup-processable))
10700                               (car gnus-newsgroup-processable)
10701                             (save-excursion
10702                               (if (eq (forward-line -1) 0)
10703                                   (gnus-summary-article-number)
10704                                 (error "Beginning of summary buffer"))))))
10705       (unless (not (eq current-article parent-article))
10706         (error "An article may not be self-referential"))
10707       (let ((message-id (mail-header-id
10708                          (gnus-summary-article-header parent-article))))
10709         (unless (and message-id (not (equal message-id "")))
10710           (error "No message-id in desired parent"))
10711         (gnus-with-article current-article
10712           (save-restriction
10713             (goto-char (point-min))
10714             (message-narrow-to-head)
10715             (if (re-search-forward "^References: " nil t)
10716                 (progn
10717                   (re-search-forward "^[^ \t]" nil t)
10718                   (forward-line -1)
10719                   (end-of-line)
10720                   (insert " " message-id))
10721               (insert "References: " message-id "\n"))))
10722         (set-buffer gnus-summary-buffer)
10723         (gnus-summary-unmark-all-processable)
10724         (gnus-summary-update-article current-article)
10725         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10726             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10727         (gnus-summary-rethread-current)
10728         (gnus-message 3 "Article %d is now the child of article %d"
10729                       current-article parent-article)))))
10730
10731 (defun gnus-summary-toggle-threads (&optional arg)
10732   "Toggle showing conversation threads.
10733 If ARG is positive number, turn showing conversation threads on."
10734   (interactive "P")
10735   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10736     (setq gnus-show-threads
10737           (if (null arg) (not gnus-show-threads)
10738             (> (prefix-numeric-value arg) 0)))
10739     (gnus-summary-prepare)
10740     (gnus-summary-goto-subject current)
10741     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10742     (gnus-summary-position-point)))
10743
10744 (defun gnus-summary-show-all-threads ()
10745   "Show all threads."
10746   (interactive)
10747   (save-excursion
10748     (let ((buffer-read-only nil))
10749       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10750   (gnus-summary-position-point))
10751
10752 (defun gnus-summary-show-thread ()
10753   "Show thread subtrees.
10754 Returns nil if no thread was there to be shown."
10755   (interactive)
10756   (let ((buffer-read-only nil)
10757         (orig (point))
10758         (end (point-at-eol))
10759         ;; Leave point at bol
10760         (beg (progn (beginning-of-line) (point))))
10761     (prog1
10762         ;; Any hidden lines here?
10763         (search-forward "\r" end t)
10764       (subst-char-in-region beg end ?\^M ?\n t)
10765       (goto-char orig)
10766       (gnus-summary-position-point))))
10767
10768 (defun gnus-summary-maybe-hide-threads ()
10769   "If requested, hide the threads that should be hidden."
10770   (when (and gnus-show-threads
10771              gnus-thread-hide-subtree)
10772     (gnus-summary-hide-all-threads
10773      (if (or (consp gnus-thread-hide-subtree)
10774              (functionp gnus-thread-hide-subtree))
10775          (gnus-make-predicate gnus-thread-hide-subtree)
10776        nil))))
10777
10778 ;;; Hiding predicates.
10779
10780 (defun gnus-article-unread-p (header)
10781   (memq (mail-header-number header) gnus-newsgroup-unreads))
10782
10783 (defun gnus-article-unseen-p (header)
10784   (memq (mail-header-number header) gnus-newsgroup-unseen))
10785
10786 (defun gnus-map-articles (predicate articles)
10787   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10788   (apply 'gnus-or (mapcar predicate
10789                           (mapcar 'gnus-summary-article-header articles))))
10790
10791 (defun gnus-summary-hide-all-threads (&optional predicate)
10792   "Hide all thread subtrees.
10793 If PREDICATE is supplied, threads that satisfy this predicate
10794 will not be hidden."
10795   (interactive)
10796   (save-excursion
10797     (goto-char (point-min))
10798     (let ((end nil))
10799       (while (not end)
10800         (when (or (not predicate)
10801                   (gnus-map-articles
10802                    predicate (gnus-summary-article-children)))
10803             (gnus-summary-hide-thread))
10804         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10805   (gnus-summary-position-point))
10806
10807 (defun gnus-summary-hide-thread ()
10808   "Hide thread subtrees.
10809 If PREDICATE is supplied, threads that satisfy this predicate
10810 will not be hidden.
10811 Returns nil if no threads were there to be hidden."
10812   (interactive)
10813   (let ((buffer-read-only nil)
10814         (start (point))
10815         (article (gnus-summary-article-number)))
10816     (goto-char start)
10817     ;; Go forward until either the buffer ends or the subthread
10818     ;; ends.
10819     (when (and (not (eobp))
10820                (or (zerop (gnus-summary-next-thread 1 t))
10821                    (goto-char (point-max))))
10822       (prog1
10823           (if (and (> (point) start)
10824                    (search-backward "\n" start t))
10825               (progn
10826                 (subst-char-in-region start (point) ?\n ?\^M)
10827                 (gnus-summary-goto-subject article))
10828             (goto-char start)
10829             nil)))))
10830
10831 (defun gnus-summary-go-to-next-thread (&optional previous)
10832   "Go to the same level (or less) next thread.
10833 If PREVIOUS is non-nil, go to previous thread instead.
10834 Return the article number moved to, or nil if moving was impossible."
10835   (let ((level (gnus-summary-thread-level))
10836         (way (if previous -1 1))
10837         (beg (point)))
10838     (forward-line way)
10839     (while (and (not (eobp))
10840                 (< level (gnus-summary-thread-level)))
10841       (forward-line way))
10842     (if (eobp)
10843         (progn
10844           (goto-char beg)
10845           nil)
10846       (setq beg (point))
10847       (prog1
10848           (gnus-summary-article-number)
10849         (goto-char beg)))))
10850
10851 (defun gnus-summary-next-thread (n &optional silent)
10852   "Go to the same level next N'th thread.
10853 If N is negative, search backward instead.
10854 Returns the difference between N and the number of skips actually
10855 done.
10856
10857 If SILENT, don't output messages."
10858   (interactive "p")
10859   (let ((backward (< n 0))
10860         (n (abs n)))
10861     (while (and (> n 0)
10862                 (gnus-summary-go-to-next-thread backward))
10863       (decf n))
10864     (unless silent
10865       (gnus-summary-position-point))
10866     (when (and (not silent) (/= 0 n))
10867       (gnus-message 7 "No more threads"))
10868     n))
10869
10870 (defun gnus-summary-prev-thread (n)
10871   "Go to the same level previous N'th thread.
10872 Returns the difference between N and the number of skips actually
10873 done."
10874   (interactive "p")
10875   (gnus-summary-next-thread (- n)))
10876
10877 (defun gnus-summary-go-down-thread ()
10878   "Go down one level in the current thread."
10879   (let ((children (gnus-summary-article-children)))
10880     (when children
10881       (gnus-summary-goto-subject (car children)))))
10882
10883 (defun gnus-summary-go-up-thread ()
10884   "Go up one level in the current thread."
10885   (let ((parent (gnus-summary-article-parent)))
10886     (when parent
10887       (gnus-summary-goto-subject parent))))
10888
10889 (defun gnus-summary-down-thread (n)
10890   "Go down thread N steps.
10891 If N is negative, go up instead.
10892 Returns the difference between N and how many steps down that were
10893 taken."
10894   (interactive "p")
10895   (let ((up (< n 0))
10896         (n (abs n)))
10897     (while (and (> n 0)
10898                 (if up (gnus-summary-go-up-thread)
10899                   (gnus-summary-go-down-thread)))
10900       (setq n (1- n)))
10901     (gnus-summary-position-point)
10902     (when (/= 0 n)
10903       (gnus-message 7 "Can't go further"))
10904     n))
10905
10906 (defun gnus-summary-up-thread (n)
10907   "Go up thread N steps.
10908 If N is negative, go down instead.
10909 Returns the difference between N and how many steps down that were
10910 taken."
10911   (interactive "p")
10912   (gnus-summary-down-thread (- n)))
10913
10914 (defun gnus-summary-top-thread ()
10915   "Go to the top of the thread."
10916   (interactive)
10917   (while (gnus-summary-go-up-thread))
10918   (gnus-summary-article-number))
10919
10920 (defun gnus-summary-kill-thread (&optional unmark)
10921   "Mark articles under current thread as read.
10922 If the prefix argument is positive, remove any kinds of marks.
10923 If the prefix argument is negative, tick articles instead."
10924   (interactive "P")
10925   (when unmark
10926     (setq unmark (prefix-numeric-value unmark)))
10927   (let ((articles (gnus-summary-articles-in-thread)))
10928     (save-excursion
10929       ;; Expand the thread.
10930       (gnus-summary-show-thread)
10931       ;; Mark all the articles.
10932       (while articles
10933         (gnus-summary-goto-subject (car articles))
10934         (cond ((null unmark)
10935                (gnus-summary-mark-article-as-read gnus-killed-mark))
10936               ((> unmark 0)
10937                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10938               (t
10939                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10940         (setq articles (cdr articles))))
10941     ;; Hide killed subtrees.
10942     (and (null unmark)
10943          gnus-thread-hide-killed
10944          (gnus-summary-hide-thread))
10945     ;; If marked as read, go to next unread subject.
10946     (when (null unmark)
10947       ;; Go to next unread subject.
10948       (gnus-summary-next-subject 1 t)))
10949   (gnus-set-mode-line 'summary))
10950
10951 ;; Summary sorting commands
10952
10953 (defun gnus-summary-sort-by-number (&optional reverse)
10954   "Sort the summary buffer by article number.
10955 Argument REVERSE means reverse order."
10956   (interactive "P")
10957   (gnus-summary-sort 'number reverse))
10958
10959 (defun gnus-summary-sort-by-random (&optional reverse)
10960   "Randomize the order in the summary buffer.
10961 Argument REVERSE means to randomize in reverse order."
10962   (interactive "P")
10963   (gnus-summary-sort 'random reverse))
10964
10965 (defun gnus-summary-sort-by-author (&optional reverse)
10966   "Sort the summary buffer by author name alphabetically.
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 'author reverse))
10971
10972 (defun gnus-summary-sort-by-recipient (&optional reverse)
10973   "Sort the summary buffer by recipient name alphabetically.
10974 If `case-fold-search' is non-nil, case of letters is ignored.
10975 Argument REVERSE means reverse order."
10976   (interactive "P")
10977   (gnus-summary-sort 'recipient reverse))
10978
10979 (defun gnus-summary-sort-by-subject (&optional reverse)
10980   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10981 If `case-fold-search' is non-nil, case of letters is ignored.
10982 Argument REVERSE means reverse order."
10983   (interactive "P")
10984   (gnus-summary-sort 'subject reverse))
10985
10986 (defun gnus-summary-sort-by-date (&optional reverse)
10987   "Sort the summary buffer by date.
10988 Argument REVERSE means reverse order."
10989   (interactive "P")
10990   (gnus-summary-sort 'date reverse))
10991
10992 (defun gnus-summary-sort-by-score (&optional reverse)
10993   "Sort the summary buffer by score.
10994 Argument REVERSE means reverse order."
10995   (interactive "P")
10996   (gnus-summary-sort 'score reverse))
10997
10998 (defun gnus-summary-sort-by-lines (&optional reverse)
10999   "Sort the summary buffer by the number of lines.
11000 Argument REVERSE means reverse order."
11001   (interactive "P")
11002   (gnus-summary-sort 'lines reverse))
11003
11004 (defun gnus-summary-sort-by-chars (&optional reverse)
11005   "Sort the summary buffer by article length.
11006 Argument REVERSE means reverse order."
11007   (interactive "P")
11008   (gnus-summary-sort 'chars reverse))
11009
11010 (defun gnus-summary-sort-by-original (&optional reverse)
11011   "Sort the summary buffer using the default sorting method.
11012 Argument REVERSE means reverse order."
11013   (interactive "P")
11014   (let* ((buffer-read-only)
11015          (gnus-summary-prepare-hook nil))
11016     ;; We do the sorting by regenerating the threads.
11017     (gnus-summary-prepare)
11018     ;; Hide subthreads if needed.
11019     (gnus-summary-maybe-hide-threads)))
11020
11021 (defun gnus-summary-sort (predicate reverse)
11022   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11023   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11024          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11025          (gnus-thread-sort-functions
11026           (if (not reverse)
11027               thread
11028             `(lambda (t1 t2)
11029                (,thread t2 t1))))
11030          (gnus-sort-gathered-threads-function
11031           gnus-thread-sort-functions)
11032          (gnus-article-sort-functions
11033           (if (not reverse)
11034               article
11035             `(lambda (t1 t2)
11036                (,article t2 t1))))
11037          (buffer-read-only)
11038          (gnus-summary-prepare-hook nil))
11039     ;; We do the sorting by regenerating the threads.
11040     (gnus-summary-prepare)
11041     ;; Hide subthreads if needed.
11042     (gnus-summary-maybe-hide-threads)))
11043
11044 ;; Summary saving commands.
11045
11046 (defun gnus-summary-save-article (&optional n not-saved)
11047   "Save the current article using the default saver function.
11048 If N is a positive number, save the N next articles.
11049 If N is a negative number, save the N previous articles.
11050 If N is nil and any articles have been marked with the process mark,
11051 save those articles instead.
11052 The variable `gnus-default-article-saver' specifies the saver function."
11053   (interactive "P")
11054   (let* ((articles (gnus-summary-work-articles n))
11055          (save-buffer (save-excursion
11056                         (nnheader-set-temp-buffer " *Gnus Save*")))
11057          (num (length articles))
11058          header file)
11059     (dolist (article articles)
11060       (setq header (gnus-summary-article-header article))
11061       (if (not (vectorp header))
11062           ;; This is a pseudo-article.
11063           (if (assq 'name header)
11064               (gnus-copy-file (cdr (assq 'name header)))
11065             (gnus-message 1 "Article %d is unsaveable" article))
11066         ;; This is a real article.
11067         (save-window-excursion
11068           (let ((gnus-display-mime-function nil)
11069                 (gnus-article-prepare-hook nil))
11070             (gnus-summary-select-article t nil nil article)))
11071         (save-excursion
11072           (set-buffer save-buffer)
11073           (erase-buffer)
11074           (insert-buffer-substring gnus-original-article-buffer))
11075         (setq file (gnus-article-save save-buffer file num))
11076         (gnus-summary-remove-process-mark article)
11077         (unless not-saved
11078           (gnus-summary-set-saved-mark article))))
11079     (gnus-kill-buffer save-buffer)
11080     (gnus-summary-position-point)
11081     (gnus-set-mode-line 'summary)
11082     n))
11083
11084 (defun gnus-summary-pipe-output (&optional arg headers)
11085   "Pipe the current article to a subprocess.
11086 If N is a positive number, pipe the N next articles.
11087 If N is a negative number, pipe the N previous articles.
11088 If N is nil and any articles have been marked with the process mark,
11089 pipe those articles instead.
11090 If HEADERS (the symbolic prefix), include the headers, too."
11091   (interactive (gnus-interactive "P\ny"))
11092   (require 'gnus-art)
11093   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11094         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11095     (gnus-summary-save-article arg t))
11096   (let ((buffer (get-buffer "*Shell Command Output*")))
11097     (when (and buffer
11098                (not (zerop (buffer-size buffer))))
11099       (gnus-configure-windows 'pipe))))
11100
11101 (defun gnus-summary-save-article-mail (&optional arg)
11102   "Append the current article to an mail file.
11103 If N is a positive number, save the N next articles.
11104 If N is a negative number, save the N previous articles.
11105 If N is nil and any articles have been marked with the process mark,
11106 save those articles instead."
11107   (interactive "P")
11108   (require 'gnus-art)
11109   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11110     (gnus-summary-save-article arg)))
11111
11112 (defun gnus-summary-save-article-rmail (&optional arg)
11113   "Append the current article to an rmail file.
11114 If N is a positive number, save the N next articles.
11115 If N is a negative number, save the N previous articles.
11116 If N is nil and any articles have been marked with the process mark,
11117 save those articles instead."
11118   (interactive "P")
11119   (require 'gnus-art)
11120   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11121     (gnus-summary-save-article arg)))
11122
11123 (defun gnus-summary-save-article-file (&optional arg)
11124   "Append the current article to a file.
11125 If N is a positive number, save the N next articles.
11126 If N is a negative number, save the N previous articles.
11127 If N is nil and any articles have been marked with the process mark,
11128 save those articles instead."
11129   (interactive "P")
11130   (require 'gnus-art)
11131   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11132     (gnus-summary-save-article arg)))
11133
11134 (defun gnus-summary-write-article-file (&optional arg)
11135   "Write the current article to a file, deleting the previous file.
11136 If N is a positive number, save the N next articles.
11137 If N is a negative number, save the N previous articles.
11138 If N is nil and any articles have been marked with the process mark,
11139 save those articles instead."
11140   (interactive "P")
11141   (require 'gnus-art)
11142   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11143     (gnus-summary-save-article arg)))
11144
11145 (defun gnus-summary-save-article-body-file (&optional arg)
11146   "Append the current article body to a file.
11147 If N is a positive number, save the N next articles.
11148 If N is a negative number, save the N previous articles.
11149 If N is nil and any articles have been marked with the process mark,
11150 save those articles instead."
11151   (interactive "P")
11152   (require 'gnus-art)
11153   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11154     (gnus-summary-save-article arg)))
11155
11156 (defun gnus-summary-muttprint (&optional arg)
11157   "Print the current article using Muttprint.
11158 If N is a positive number, save the N next articles.
11159 If N is a negative number, save the N previous articles.
11160 If N is nil and any articles have been marked with the process mark,
11161 save those articles instead."
11162   (interactive "P")
11163   (require 'gnus-art)
11164   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11165     (gnus-summary-save-article arg t)))
11166
11167 (defun gnus-summary-pipe-message (program)
11168   "Pipe the current article through PROGRAM."
11169   (interactive "sProgram: ")
11170   (gnus-summary-select-article)
11171   (let ((mail-header-separator ""))
11172     (gnus-eval-in-buffer-window gnus-article-buffer
11173       (save-restriction
11174         (widen)
11175         (let ((start (window-start))
11176               buffer-read-only)
11177           (message-pipe-buffer-body program)
11178           (set-window-start (get-buffer-window (current-buffer)) start))))))
11179
11180 (defun gnus-get-split-value (methods)
11181   "Return a value based on the split METHODS."
11182   (let (split-name method result match)
11183     (when methods
11184       (save-excursion
11185         (set-buffer gnus-original-article-buffer)
11186         (save-restriction
11187           (nnheader-narrow-to-headers)
11188           (while (and methods (not split-name))
11189             (goto-char (point-min))
11190             (setq method (pop methods))
11191             (setq match (car method))
11192             (when (cond
11193                    ((stringp match)
11194                     ;; Regular expression.
11195                     (ignore-errors
11196                       (re-search-forward match nil t)))
11197                    ((functionp match)
11198                     ;; Function.
11199                     (save-restriction
11200                       (widen)
11201                       (setq result (funcall match gnus-newsgroup-name))))
11202                    ((consp match)
11203                     ;; Form.
11204                     (save-restriction
11205                       (widen)
11206                       (setq result (eval match)))))
11207               (setq split-name (cdr method))
11208               (cond ((stringp result)
11209                      (push (expand-file-name
11210                             result gnus-article-save-directory)
11211                            split-name))
11212                     ((consp result)
11213                      (setq split-name (append result split-name)))))))))
11214     (nreverse split-name)))
11215
11216 (defun gnus-valid-move-group-p (group)
11217   (and (boundp group)
11218        (symbol-name group)
11219        (symbol-value group)
11220        (gnus-get-function (gnus-find-method-for-group
11221                            (symbol-name group)) 'request-accept-article t)))
11222
11223 (defun gnus-read-move-group-name (prompt default articles prefix)
11224   "Read a group name."
11225   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11226          (minibuffer-confirm-incomplete nil) ; XEmacs
11227          (prom
11228           (format "%s %s to"
11229                   prompt
11230                   (if (> (length articles) 1)
11231                       (format "these %d articles" (length articles))
11232                     "this article")))
11233          (to-newsgroup
11234           (cond
11235            ((null split-name)
11236             (gnus-completing-read-with-default
11237              default prom
11238              gnus-active-hashtb
11239              'gnus-valid-move-group-p
11240              nil prefix
11241              'gnus-group-history))
11242            ((= 1 (length split-name))
11243             (gnus-completing-read-with-default
11244              (car split-name) prom
11245              gnus-active-hashtb
11246              'gnus-valid-move-group-p
11247              nil nil
11248              'gnus-group-history))
11249            (t
11250             (gnus-completing-read-with-default
11251              nil prom
11252              (mapcar 'list (nreverse split-name))
11253              nil nil nil
11254              'gnus-group-history))))
11255          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11256     (when to-newsgroup
11257       (if (or (string= to-newsgroup "")
11258               (string= to-newsgroup prefix))
11259           (setq to-newsgroup default))
11260       (unless to-newsgroup
11261         (error "No group name entered"))
11262       (or (gnus-active to-newsgroup)
11263           (gnus-activate-group to-newsgroup nil nil to-method)
11264           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11265                                      to-newsgroup))
11266               (or (and (gnus-request-create-group to-newsgroup to-method)
11267                        (gnus-activate-group
11268                         to-newsgroup nil nil to-method)
11269                        (gnus-subscribe-group to-newsgroup))
11270                   (error "Couldn't create group %s" to-newsgroup)))
11271           (error "No such group: %s" to-newsgroup)))
11272     to-newsgroup))
11273
11274 (defun gnus-summary-save-parts (type dir n &optional reverse)
11275   "Save parts matching TYPE to DIR.
11276 If REVERSE, save parts that do not match TYPE."
11277   (interactive
11278    (list (read-string "Save parts of type: "
11279                       (or (car gnus-summary-save-parts-type-history)
11280                           gnus-summary-save-parts-default-mime)
11281                       'gnus-summary-save-parts-type-history)
11282          (setq gnus-summary-save-parts-last-directory
11283                (read-file-name "Save to directory: "
11284                                gnus-summary-save-parts-last-directory
11285                                nil t))
11286          current-prefix-arg))
11287   (gnus-summary-iterate n
11288     (let ((gnus-display-mime-function nil)
11289           (gnus-inhibit-treatment t))
11290       (gnus-summary-select-article))
11291     (save-excursion
11292       (set-buffer gnus-article-buffer)
11293       (let ((handles (or gnus-article-mime-handles
11294                          (mm-dissect-buffer nil gnus-article-loose-mime)
11295                          (and gnus-article-emulate-mime
11296                               (mm-uu-dissect)))))
11297         (when handles
11298           (gnus-summary-save-parts-1 type dir handles reverse)
11299           (unless gnus-article-mime-handles ;; Don't destroy this case.
11300             (mm-destroy-parts handles)))))))
11301
11302 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11303   (if (stringp (car handle))
11304       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11305               (cdr handle))
11306     (when (if reverse
11307               (not (string-match type (mm-handle-media-type handle)))
11308             (string-match type (mm-handle-media-type handle)))
11309       (let ((file (expand-file-name
11310                    (gnus-map-function
11311                     mm-file-name-rewrite-functions
11312                     (file-name-nondirectory
11313                      (or
11314                       (mail-content-type-get
11315                        (mm-handle-disposition handle) 'filename)
11316                       (mail-content-type-get
11317                        (mm-handle-type handle) 'name)
11318                       (concat gnus-newsgroup-name
11319                               "." (number-to-string
11320                                    (cdr gnus-article-current))))))
11321                    dir)))
11322         (unless (file-exists-p file)
11323           (mm-save-part-to-file handle file))))))
11324
11325 ;; Summary extract commands
11326
11327 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11328   (let ((buffer-read-only nil)
11329         (article (gnus-summary-article-number))
11330         after-article b e)
11331     (unless (gnus-summary-goto-subject article)
11332       (error "No such article: %d" article))
11333     (gnus-summary-position-point)
11334     ;; If all commands are to be bunched up on one line, we collect
11335     ;; them here.
11336     (unless gnus-view-pseudos-separately
11337       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11338             files action)
11339         (while ps
11340           (setq action (cdr (assq 'action (car ps))))
11341           (setq files (list (cdr (assq 'name (car ps)))))
11342           (while (and ps (cdr ps)
11343                       (string= (or action "1")
11344                                (or (cdr (assq 'action (cadr ps))) "2")))
11345             (push (cdr (assq 'name (cadr ps))) files)
11346             (setcdr ps (cddr ps)))
11347           (when files
11348             (when (not (string-match "%s" action))
11349               (push " " files))
11350             (push " " files)
11351             (when (assq 'execute (car ps))
11352               (setcdr (assq 'execute (car ps))
11353                       (funcall (if (string-match "%s" action)
11354                                    'format 'concat)
11355                                action
11356                                (mapconcat
11357                                 (lambda (f)
11358                                   (if (equal f " ")
11359                                       f
11360                                     (shell-quote-argument f)))
11361                                 files " ")))))
11362           (setq ps (cdr ps)))))
11363     (if (and gnus-view-pseudos (not not-view))
11364         (while pslist
11365           (when (assq 'execute (car pslist))
11366             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11367                                   (eq gnus-view-pseudos 'not-confirm)))
11368           (setq pslist (cdr pslist)))
11369       (save-excursion
11370         (while pslist
11371           (setq after-article (or (cdr (assq 'article (car pslist)))
11372                                   (gnus-summary-article-number)))
11373           (gnus-summary-goto-subject after-article)
11374           (forward-line 1)
11375           (setq b (point))
11376           (insert "    " (file-name-nondirectory
11377                           (cdr (assq 'name (car pslist))))
11378                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11379           (setq e (point))
11380           (forward-line -1)             ; back to `b'
11381           (gnus-add-text-properties
11382            b (1- e) (list 'gnus-number gnus-reffed-article-number
11383                           gnus-mouse-face-prop gnus-mouse-face))
11384           (gnus-data-enter
11385            after-article gnus-reffed-article-number
11386            gnus-unread-mark b (car pslist) 0 (- e b))
11387           (setq gnus-newsgroup-unreads
11388                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11389                                          gnus-reffed-article-number))
11390           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11391           (setq pslist (cdr pslist)))))))
11392
11393 (defun gnus-pseudos< (p1 p2)
11394   (let ((c1 (cdr (assq 'action p1)))
11395         (c2 (cdr (assq 'action p2))))
11396     (and c1 c2 (string< c1 c2))))
11397
11398 (defun gnus-request-pseudo-article (props)
11399   (cond ((assq 'execute props)
11400          (gnus-execute-command (cdr (assq 'execute props)))))
11401   (let ((gnus-current-article (gnus-summary-article-number)))
11402     (gnus-run-hooks 'gnus-mark-article-hook)))
11403
11404 (defun gnus-execute-command (command &optional automatic)
11405   (save-excursion
11406     (gnus-article-setup-buffer)
11407     (set-buffer gnus-article-buffer)
11408     (setq buffer-read-only nil)
11409     (let ((command (if automatic command
11410                      (read-string "Command: " (cons command 0)))))
11411       (erase-buffer)
11412       (insert "$ " command "\n\n")
11413       (if gnus-view-pseudo-asynchronously
11414           (start-process "gnus-execute" (current-buffer) shell-file-name
11415                          shell-command-switch command)
11416         (call-process shell-file-name nil t nil
11417                       shell-command-switch command)))))
11418
11419 ;; Summary kill commands.
11420
11421 (defun gnus-summary-edit-global-kill (article)
11422   "Edit the \"global\" kill file."
11423   (interactive (list (gnus-summary-article-number)))
11424   (gnus-group-edit-global-kill article))
11425
11426 (defun gnus-summary-edit-local-kill ()
11427   "Edit a local kill file applied to the current newsgroup."
11428   (interactive)
11429   (setq gnus-current-headers (gnus-summary-article-header))
11430   (gnus-group-edit-local-kill
11431    (gnus-summary-article-number) gnus-newsgroup-name))
11432
11433 ;;; Header reading.
11434
11435 (defun gnus-read-header (id &optional header)
11436   "Read the headers of article ID and enter them into the Gnus system."
11437   (let ((group gnus-newsgroup-name)
11438         (gnus-override-method
11439          (or
11440           gnus-override-method
11441           (and (gnus-news-group-p gnus-newsgroup-name)
11442                (car (gnus-refer-article-methods)))))
11443         where)
11444     ;; First we check to see whether the header in question is already
11445     ;; fetched.
11446     (if (stringp id)
11447         ;; This is a Message-ID.
11448         (setq header (or header (gnus-id-to-header id)))
11449       ;; This is an article number.
11450       (setq header (or header (gnus-summary-article-header id))))
11451     (if (and header
11452              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11453         ;; We have found the header.
11454         header
11455       ;; We have to really fetch the header to this article.
11456       (save-excursion
11457         (set-buffer nntp-server-buffer)
11458         (when (setq where (gnus-request-head id group))
11459           (nnheader-fold-continuation-lines)
11460           (goto-char (point-max))
11461           (insert ".\n")
11462           (goto-char (point-min))
11463           (insert "211 ")
11464           (princ (cond
11465                   ((numberp id) id)
11466                   ((cdr where) (cdr where))
11467                   (header (mail-header-number header))
11468                   (t gnus-reffed-article-number))
11469                  (current-buffer))
11470           (insert " Article retrieved.\n"))
11471         (if (or (not where)
11472                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11473             ()                          ; Malformed head.
11474           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11475             (when (and (stringp id)
11476                        (not (string= (gnus-group-real-name group)
11477                                      (car where))))
11478               ;; If we fetched by Message-ID and the article came
11479               ;; from a different group, we fudge some bogus article
11480               ;; numbers for this article.
11481               (mail-header-set-number header gnus-reffed-article-number))
11482             (save-excursion
11483               (set-buffer gnus-summary-buffer)
11484               (decf gnus-reffed-article-number)
11485               (gnus-remove-header (mail-header-number header))
11486               (push header gnus-newsgroup-headers)
11487               (setq gnus-current-headers header)
11488               (push (mail-header-number header) gnus-newsgroup-limit)))
11489           header)))))
11490
11491 (defun gnus-remove-header (number)
11492   "Remove header NUMBER from `gnus-newsgroup-headers'."
11493   (if (and gnus-newsgroup-headers
11494            (= number (mail-header-number (car gnus-newsgroup-headers))))
11495       (pop gnus-newsgroup-headers)
11496     (let ((headers gnus-newsgroup-headers))
11497       (while (and (cdr headers)
11498                   (not (= number (mail-header-number (cadr headers)))))
11499         (pop headers))
11500       (when (cdr headers)
11501         (setcdr headers (cddr headers))))))
11502
11503 ;;;
11504 ;;; summary highlights
11505 ;;;
11506
11507 (defun gnus-highlight-selected-summary ()
11508   "Highlight selected article in summary buffer."
11509   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11510   (when gnus-summary-selected-face
11511     (save-excursion
11512       (let* ((beg (point-at-bol))
11513              (end (point-at-eol))
11514              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11515              (from (if (get-text-property beg gnus-mouse-face-prop)
11516                        beg
11517                      (or (next-single-property-change
11518                           beg gnus-mouse-face-prop nil end)
11519                          beg)))
11520              (to
11521               (if (= from end)
11522                   (- from 2)
11523                 (or (next-single-property-change
11524                      from gnus-mouse-face-prop nil end)
11525                     end))))
11526         ;; If no mouse-face prop on line we will have to = from = end,
11527         ;; so we highlight the entire line instead.
11528         (when (= (+ to 2) from)
11529           (setq from beg)
11530           (setq to end))
11531         (if gnus-newsgroup-selected-overlay
11532             ;; Move old overlay.
11533             (gnus-move-overlay
11534              gnus-newsgroup-selected-overlay from to (current-buffer))
11535           ;; Create new overlay.
11536           (gnus-overlay-put
11537            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11538            'face gnus-summary-selected-face))))))
11539
11540 (defvar gnus-summary-highlight-line-cached nil)
11541 (defvar gnus-summary-highlight-line-trigger nil)
11542
11543 (defun gnus-summary-highlight-line-0 ()
11544   (if (and (eq gnus-summary-highlight-line-trigger
11545                gnus-summary-highlight)
11546            gnus-summary-highlight-line-cached)
11547       gnus-summary-highlight-line-cached
11548     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11549           gnus-summary-highlight-line-cached
11550           (let* ((cond (list 'cond))
11551                  (c cond)
11552                  (list gnus-summary-highlight))
11553             (while list
11554               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11555                               nil))
11556               (setq c (cdr c)
11557                     list (cdr list)))
11558             (gnus-byte-compile (list 'lambda nil cond))))))
11559
11560 (defun gnus-summary-highlight-line ()
11561   "Highlight current line according to `gnus-summary-highlight'."
11562   (let* ((beg (point-at-bol))
11563          (article (or (gnus-summary-article-number) gnus-current-article))
11564          (score (or (cdr (assq article
11565                                gnus-newsgroup-scored))
11566                     gnus-summary-default-score 0))
11567          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11568          (inhibit-read-only t)
11569          (default gnus-summary-default-score)
11570          (default-high gnus-summary-default-high-score)
11571          (default-low gnus-summary-default-low-score)
11572          (uncached (and gnus-summary-use-undownloaded-faces
11573                         (memq article gnus-newsgroup-undownloaded)
11574                         (not (memq article gnus-newsgroup-cached)))))
11575     (let ((face (funcall (gnus-summary-highlight-line-0))))
11576       (unless (eq face (get-text-property beg 'face))
11577         (gnus-put-text-property-excluding-characters-with-faces
11578          beg (point-at-eol) 'face
11579          (setq face (if (boundp face) (symbol-value face) face)))
11580         (when gnus-summary-highlight-line-function
11581           (funcall gnus-summary-highlight-line-function article face))))))
11582
11583 (defun gnus-update-read-articles (group unread &optional compute)
11584   "Update the list of read articles in GROUP.
11585 UNREAD is a sorted list."
11586   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11587         (info (gnus-get-info group))
11588         (prev 1)
11589         read)
11590     (if (or (not info) (not active))
11591         ;; There is no info on this group if it was, in fact,
11592         ;; killed.  Gnus stores no information on killed groups, so
11593         ;; there's nothing to be done.
11594         ;; One could store the information somewhere temporarily,
11595         ;; perhaps...  Hmmm...
11596         ()
11597       ;; Remove any negative articles numbers.
11598       (while (and unread (< (car unread) 0))
11599         (setq unread (cdr unread)))
11600       ;; Remove any expired article numbers
11601       (while (and unread (< (car unread) (car active)))
11602         (setq unread (cdr unread)))
11603       ;; Compute the ranges of read articles by looking at the list of
11604       ;; unread articles.
11605       (while unread
11606         (when (/= (car unread) prev)
11607           (push (if (= prev (1- (car unread))) prev
11608                   (cons prev (1- (car unread))))
11609                 read))
11610         (setq prev (1+ (car unread)))
11611         (setq unread (cdr unread)))
11612       (when (<= prev (cdr active))
11613         (push (cons prev (cdr active)) read))
11614       (setq read (if (> (length read) 1) (nreverse read) read))
11615       (if compute
11616           read
11617         (save-excursion
11618           (let (setmarkundo)
11619             ;; Propagate the read marks to the backend.
11620             (when (gnus-check-backend-function 'request-set-mark group)
11621               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11622                     (add (gnus-remove-from-range read (gnus-info-read info))))
11623                 (when (or add del)
11624                   (unless (gnus-check-group group)
11625                     (error "Can't open server for %s" group))
11626                   (gnus-request-set-mark
11627                    group (delq nil (list (if add (list add 'add '(read)))
11628                                          (if del (list del 'del '(read))))))
11629                   (setq setmarkundo
11630                         `(gnus-request-set-mark
11631                           ,group
11632                           ',(delq nil (list
11633                                        (if del (list del 'add '(read)))
11634                                        (if add (list add 'del '(read))))))))))
11635             (set-buffer gnus-group-buffer)
11636             (gnus-undo-register
11637               `(progn
11638                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11639                  (gnus-info-set-read ',info ',(gnus-info-read info))
11640                  (gnus-get-unread-articles-in-group ',info
11641                                                     (gnus-active ,group))
11642                  (gnus-group-update-group ,group t)
11643                  ,setmarkundo))))
11644         ;; Enter this list into the group info.
11645         (gnus-info-set-read info read)
11646         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11647         (gnus-get-unread-articles-in-group info (gnus-active group))
11648         t))))
11649
11650 (defun gnus-offer-save-summaries ()
11651   "Offer to save all active summary buffers."
11652   (let (buffers)
11653     ;; Go through all buffers and find all summaries.
11654     (dolist (buffer (buffer-list))
11655       (when (and (setq buffer (buffer-name buffer))
11656                  (string-match "Summary" buffer)
11657                  (with-current-buffer buffer
11658                    ;; We check that this is, indeed, a summary buffer.
11659                    (and (eq major-mode 'gnus-summary-mode)
11660                         ;; Also make sure this isn't bogus.
11661                         gnus-newsgroup-prepared
11662                         ;; Also make sure that this isn't a
11663                         ;; dead summary buffer.
11664                         (not gnus-dead-summary-mode))))
11665         (push buffer buffers)))
11666     ;; Go through all these summary buffers and offer to save them.
11667     (when buffers
11668       (save-excursion
11669         (map-y-or-n-p
11670          "Update summary buffer %s? "
11671          (lambda (buf)
11672            (switch-to-buffer buf)
11673            (gnus-summary-exit))
11674          buffers)))))
11675
11676 (defun gnus-summary-setup-default-charset ()
11677   "Setup newsgroup default charset."
11678   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11679       (setq gnus-newsgroup-charset nil)
11680     (let* ((ignored-charsets
11681             (or gnus-newsgroup-ephemeral-ignored-charsets
11682                 (append
11683                  (and gnus-newsgroup-name
11684                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11685                  gnus-newsgroup-ignored-charsets))))
11686       (setq gnus-newsgroup-charset
11687             (or gnus-newsgroup-ephemeral-charset
11688                 (and gnus-newsgroup-name
11689                      (gnus-parameter-charset gnus-newsgroup-name))
11690                 gnus-default-charset))
11691       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11692            ignored-charsets))))
11693
11694 ;;;
11695 ;;; Mime Commands
11696 ;;;
11697
11698 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11699   "Display the current article buffer fully MIME-buttonized.
11700 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11701 treated as multipart/mixed."
11702   (interactive "P")
11703   (require 'gnus-art)
11704   (let ((gnus-unbuttonized-mime-types nil)
11705         (gnus-mime-display-multipart-as-mixed show-all-parts))
11706     (gnus-summary-show-article)))
11707
11708 (defun gnus-summary-repair-multipart (article)
11709   "Add a Content-Type header to a multipart article without one."
11710   (interactive (list (gnus-summary-article-number)))
11711   (gnus-with-article article
11712     (message-narrow-to-head)
11713     (message-remove-header "Mime-Version")
11714     (goto-char (point-max))
11715     (insert "Mime-Version: 1.0\n")
11716     (widen)
11717     (when (search-forward "\n--" nil t)
11718       (let ((separator (buffer-substring (point) (point-at-eol))))
11719         (message-narrow-to-head)
11720         (message-remove-header "Content-Type")
11721         (goto-char (point-max))
11722         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11723                         separator))
11724         (widen))))
11725   (let (gnus-mark-article-hook)
11726     (gnus-summary-select-article t t nil article)))
11727
11728 (defun gnus-summary-toggle-display-buttonized ()
11729   "Toggle the buttonizing of the article buffer."
11730   (interactive)
11731   (require 'gnus-art)
11732   (if (setq gnus-inhibit-mime-unbuttonizing
11733             (not gnus-inhibit-mime-unbuttonizing))
11734       (let ((gnus-unbuttonized-mime-types nil))
11735         (gnus-summary-show-article))
11736     (gnus-summary-show-article)))
11737
11738 ;;;
11739 ;;; Generic summary marking commands
11740 ;;;
11741
11742 (defvar gnus-summary-marking-alist
11743   '((read gnus-del-mark "d")
11744     (unread gnus-unread-mark "u")
11745     (ticked gnus-ticked-mark "!")
11746     (dormant gnus-dormant-mark "?")
11747     (expirable gnus-expirable-mark "e"))
11748   "An alist of names/marks/keystrokes.")
11749
11750 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11751 (defvar gnus-summary-mark-map)
11752
11753 (defun gnus-summary-make-all-marking-commands ()
11754   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11755   (dolist (elem gnus-summary-marking-alist)
11756     (apply 'gnus-summary-make-marking-command elem)))
11757
11758 (defun gnus-summary-make-marking-command (name mark keystroke)
11759   (let ((map (make-sparse-keymap)))
11760     (define-key gnus-summary-generic-mark-map keystroke map)
11761     (dolist (lway `((next "next" next nil "n")
11762                     (next-unread "next unread" next t "N")
11763                     (prev "previous" prev nil "p")
11764                     (prev-unread "previous unread" prev t "P")
11765                     (nomove "" nil nil ,keystroke)))
11766       (let ((func (gnus-summary-make-marking-command-1
11767                    mark (car lway) lway name)))
11768         (setq func (eval func))
11769         (define-key map (nth 4 lway) func)))))
11770
11771 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11772   `(defun ,(intern
11773             (format "gnus-summary-put-mark-as-%s%s"
11774                     name (if (eq way 'nomove)
11775                              ""
11776                            (concat "-" (symbol-name way)))))
11777      (n)
11778      ,(format
11779        "Mark the current article as %s%s.
11780 If N, the prefix, then repeat N times.
11781 If N is negative, move in reverse order.
11782 The difference between N and the actual number of articles marked is
11783 returned."
11784        name (cadr lway))
11785      (interactive "p")
11786      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11787
11788 (defun gnus-summary-generic-mark (n mark move unread)
11789   "Mark N articles with MARK."
11790   (unless (eq major-mode 'gnus-summary-mode)
11791     (error "This command can only be used in the summary buffer"))
11792   (gnus-summary-show-thread)
11793   (let ((nummove
11794          (cond
11795           ((eq move 'next) 1)
11796           ((eq move 'prev) -1)
11797           (t 0))))
11798     (if (zerop nummove)
11799         (setq n 1)
11800       (when (< n 0)
11801         (setq n (abs n)
11802               nummove (* -1 nummove))))
11803     (while (and (> n 0)
11804                 (gnus-summary-mark-article nil mark)
11805                 (zerop (gnus-summary-next-subject nummove unread t)))
11806       (setq n (1- n)))
11807     (when (/= 0 n)
11808       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11809     (gnus-summary-recenter)
11810     (gnus-summary-position-point)
11811     (gnus-set-mode-line 'summary)
11812     n))
11813
11814 (defun gnus-summary-insert-articles (articles)
11815   (when (setq articles
11816               (gnus-sorted-difference articles
11817                                       (mapcar (lambda (h)
11818                                                 (mail-header-number h))
11819                                               gnus-newsgroup-headers)))
11820     (setq gnus-newsgroup-headers
11821           (gnus-merge 'list
11822                       gnus-newsgroup-headers
11823                       (gnus-fetch-headers articles)
11824                       'gnus-article-sort-by-number))
11825     ;; Suppress duplicates?
11826     (when gnus-suppress-duplicates
11827       (gnus-dup-suppress-articles))
11828
11829     ;; We might want to build some more threads first.
11830     (when (and gnus-fetch-old-headers
11831                (eq gnus-headers-retrieved-by 'nov))
11832       (if (eq gnus-fetch-old-headers 'invisible)
11833           (gnus-build-all-threads)
11834         (gnus-build-old-threads)))
11835     ;; Let the Gnus agent mark articles as read.
11836     (when gnus-agent
11837       (gnus-agent-get-undownloaded-list))
11838     ;; Remove list identifiers from subject
11839     (when gnus-list-identifiers
11840       (gnus-summary-remove-list-identifiers))
11841     ;; First and last article in this newsgroup.
11842     (when gnus-newsgroup-headers
11843       (setq gnus-newsgroup-begin
11844             (mail-header-number (car gnus-newsgroup-headers))
11845             gnus-newsgroup-end
11846             (mail-header-number
11847              (gnus-last-element gnus-newsgroup-headers))))
11848     (when gnus-use-scoring
11849       (gnus-possibly-score-headers))))
11850
11851 (defun gnus-summary-insert-old-articles (&optional all)
11852   "Insert all old articles in this group.
11853 If ALL is non-nil, already read articles become readable.
11854 If ALL is a number, fetch this number of articles."
11855   (interactive "P")
11856   (prog1
11857       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11858             older len)
11859         (setq older
11860               ;; Some nntp servers lie about their active range.  When
11861               ;; this happens, the active range can be in the millions.
11862               ;; Use a compressed range to avoid creating a huge list.
11863               (gnus-range-difference (list gnus-newsgroup-active) old))
11864         (setq len (gnus-range-length older))
11865         (cond
11866          ((null older) nil)
11867          ((numberp all)
11868           (if (< all len)
11869               (let ((older-range (nreverse older)))
11870                 (setq older nil)
11871
11872                 (while (> all 0)
11873                   (let* ((r (pop older-range))
11874                          (min (if (numberp r) r (car r)))
11875                          (max (if (numberp r) r (cdr r))))
11876                     (while (and (<= min max)
11877                                 (> all 0))
11878                       (push max older)
11879                       (setq all (1- all)
11880                             max (1- max))))))
11881             (setq older (gnus-uncompress-range older))))
11882          (all
11883           (setq older (gnus-uncompress-range older)))
11884          (t
11885           (when (and (numberp gnus-large-newsgroup)
11886                    (> len gnus-large-newsgroup))
11887               (let* ((cursor-in-echo-area nil)
11888                      (initial (gnus-parameter-large-newsgroup-initial
11889                                gnus-newsgroup-name))
11890                      (input
11891                       (read-string
11892                        (format
11893                         "How many articles from %s (%s %d): "
11894                         (gnus-limit-string
11895                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11896                         (if initial "max" "default")
11897                         len)
11898                        (if initial
11899                            (cons (number-to-string initial)
11900                                  0)))))
11901                 (unless (string-match "^[ \t]*$" input)
11902                   (setq all (string-to-number input))
11903                   (if (< all len)
11904                       (let ((older-range (nreverse older)))
11905                         (setq older nil)
11906
11907                         (while (> all 0)
11908                           (let* ((r (pop older-range))
11909                                  (min (if (numberp r) r (car r)))
11910                                  (max (if (numberp r) r (cdr r))))
11911                             (while (and (<= min max)
11912                                         (> all 0))
11913                               (push max older)
11914                               (setq all (1- all)
11915                                     max (1- max))))))))))
11916           (setq older (gnus-uncompress-range older))))
11917         (if (not older)
11918             (message "No old news.")
11919           (gnus-summary-insert-articles older)
11920           (gnus-summary-limit (gnus-sorted-nunion old older))))
11921     (gnus-summary-position-point)))
11922
11923 (defun gnus-summary-insert-new-articles ()
11924   "Insert all new articles in this group."
11925   (interactive)
11926   (prog1
11927       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11928             (old-active gnus-newsgroup-active)
11929             (nnmail-fetched-sources (list t))
11930             i new)
11931         (setq gnus-newsgroup-active
11932               (gnus-activate-group gnus-newsgroup-name 'scan))
11933         (setq i (cdr gnus-newsgroup-active))
11934         (while (> i (cdr old-active))
11935           (push i new)
11936           (decf i))
11937         (if (not new)
11938             (message "No gnus is bad news")
11939           (gnus-summary-insert-articles new)
11940           (setq gnus-newsgroup-unreads
11941                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11942           (gnus-summary-limit (gnus-sorted-nunion old new))))
11943     (gnus-summary-position-point)))
11944
11945 (gnus-summary-make-all-marking-commands)
11946
11947 (gnus-ems-redefine)
11948
11949 (provide 'gnus-sum)
11950
11951 (run-hooks 'gnus-sum-load-hook)
11952
11953 ;; Local Variables:
11954 ;; coding: iso-8859-1
11955 ;; End:
11956
11957 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11958 ;;; gnus-sum.el ends here