(gnus-summary-edit-article-done): Remove text props before parsing header.
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-map))
33
34 (require 'gnus)
35 (require 'gnus-group)
36 (require 'gnus-spec)
37 (require 'gnus-range)
38 (require 'gnus-int)
39 (require 'gnus-undo)
40 (require 'gnus-util)
41 (require 'mm-decode)
42 (require 'nnoo)
43
44 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
45 (autoload 'gnus-cache-write-active "gnus-cache")
46 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
47 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
48 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
49 (autoload 'mm-uu-dissect "mm-uu")
50 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
51   "Deuglify broken Outlook (Express) articles and redisplay."
52   t)
53 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
54 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
55 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
56
57 (defcustom gnus-kill-summary-on-exit t
58   "*If non-nil, kill the summary buffer when you exit from it.
59 If nil, the summary will become a \"*Dead Summary*\" buffer, and
60 it will be killed sometime later."
61   :group 'gnus-summary-exit
62   :type 'boolean)
63
64 (defcustom gnus-fetch-old-headers nil
65   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
66 If an unread article in the group refers to an older, already read (or
67 just marked as read) article, the old article will not normally be
68 displayed in the Summary buffer.  If this variable is t, Gnus
69 will attempt to grab the headers to the old articles, and thereby
70 build complete threads.  If it has the value `some', only enough
71 headers to connect otherwise loose threads will be displayed.  This
72 variable can also be a number.  In that case, no more than that number
73 of old headers will be fetched.  If it has the value `invisible', all
74 old headers will be fetched, but none will be displayed.
75
76 The server has to support NOV for any of this to work."
77   :group 'gnus-thread
78   :type '(choice (const :tag "off" nil)
79                  (const :tag "on" t)
80                  (const some)
81                  (const invisible)
82                  number
83                  (sexp :menu-tag "other" t)))
84
85 (defcustom gnus-refer-thread-limit 200
86   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
87 If t, fetch all the available old headers."
88   :group 'gnus-thread
89   :type '(choice number
90                  (sexp :menu-tag "other" t)))
91
92 (defcustom gnus-summary-make-false-root 'adopt
93   "*nil means that Gnus won't gather loose threads.
94 If the root of a thread has expired or been read in a previous
95 session, the information necessary to build a complete thread has been
96 lost.  Instead of having many small sub-threads from this original thread
97 scattered all over the summary buffer, Gnus can gather them.
98
99 If non-nil, Gnus will try to gather all loose sub-threads from an
100 original thread into one large thread.
101
102 If this variable is non-nil, it should be one of `none', `adopt',
103 `dummy' or `empty'.
104
105 If this variable is `none', Gnus will not make a false root, but just
106 present the sub-threads after another.
107 If this variable is `dummy', Gnus will create a dummy root that will
108 have all the sub-threads as children.
109 If this variable is `adopt', Gnus will make one of the \"children\"
110 the parent and mark all the step-children as such.
111 If this variable is `empty', the \"children\" are printed with empty
112 subject fields.  (Or rather, they will be printed with a string
113 given by the `gnus-summary-same-subject' variable.)"
114   :group 'gnus-thread
115   :type '(choice (const :tag "off" nil)
116                  (const none)
117                  (const dummy)
118                  (const adopt)
119                  (const empty)))
120
121 (defcustom gnus-summary-make-false-root-always nil
122   "Always make a false dummy root."
123   :version "22.1"
124   :group 'gnus-thread
125   :type 'boolean)
126
127 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
128   "*A regexp to match subjects to be excluded from loose thread gathering.
129 As loose thread gathering is done on subjects only, that means that
130 there can be many false gatherings performed.  By rooting out certain
131 common subjects, gathering might become saner."
132   :group 'gnus-thread
133   :type 'regexp)
134
135 (defcustom gnus-summary-gather-subject-limit nil
136   "*Maximum length of subject comparisons when gathering loose threads.
137 Use nil to compare full subjects.  Setting this variable to a low
138 number will help gather threads that have been corrupted by
139 newsreaders chopping off subject lines, but it might also mean that
140 unrelated articles that have subject that happen to begin with the
141 same few characters will be incorrectly gathered.
142
143 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
144 comparing subjects."
145   :group 'gnus-thread
146   :type '(choice (const :tag "off" nil)
147                  (const fuzzy)
148                  (sexp :menu-tag "on" t)))
149
150 (defcustom gnus-simplify-subject-functions nil
151   "List of functions taking a string argument that simplify subjects.
152 The functions are applied recursively.
153
154 Useful functions to put in this list include:
155 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
156 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
157   :group 'gnus-thread
158   :type '(repeat function))
159
160 (defcustom gnus-simplify-ignored-prefixes nil
161   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
162   :group 'gnus-thread
163   :type '(choice (const :tag "off" nil)
164                  regexp))
165
166 (defcustom gnus-build-sparse-threads nil
167   "*If non-nil, fill in the gaps in threads.
168 If `some', only fill in the gaps that are needed to tie loose threads
169 together.  If `more', fill in all leaf nodes that Gnus can find.  If
170 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
171   :group 'gnus-thread
172   :type '(choice (const :tag "off" nil)
173                  (const some)
174                  (const more)
175                  (sexp :menu-tag "all" t)))
176
177 (defcustom gnus-summary-thread-gathering-function
178   'gnus-gather-threads-by-subject
179   "*Function used for gathering loose threads.
180 There are two pre-defined functions: `gnus-gather-threads-by-subject',
181 which only takes Subjects into consideration; and
182 `gnus-gather-threads-by-references', which compared the References
183 headers of the articles to find matches."
184   :group 'gnus-thread
185   :type '(radio (function-item gnus-gather-threads-by-subject)
186                 (function-item gnus-gather-threads-by-references)
187                 (function :tag "other")))
188
189 (defcustom gnus-summary-same-subject ""
190   "*String indicating that the current article has the same subject as the previous.
191 This variable will only be used if the value of
192 `gnus-summary-make-false-root' is `empty'."
193   :group 'gnus-summary-format
194   :type 'string)
195
196 (defcustom gnus-summary-goto-unread t
197   "*If t, many commands will go to the next unread article.
198 This applies to marking commands as well as other commands that
199 \"naturally\" select the next article, like, for instance, `SPC' at
200 the end of an article.
201
202 If nil, the marking commands do NOT go to the next unread article
203 \(they go to the next article instead).  If `never', commands that
204 usually go to the next unread article, will go to the next article,
205 whether it is read or not."
206   :group 'gnus-summary-marks
207   :link '(custom-manual "(gnus)Setting Marks")
208   :type '(choice (const :tag "off" nil)
209                  (const never)
210                  (sexp :menu-tag "on" t)))
211
212 (defcustom gnus-summary-default-score 0
213   "*Default article score level.
214 All scores generated by the score files will be added to this score.
215 If this variable is nil, scoring will be disabled."
216   :group 'gnus-score-default
217   :type '(choice (const :tag "disable")
218                  integer))
219
220 (defcustom gnus-summary-default-high-score 0
221   "*Default threshold for a high scored article.
222 An article will be highlighted as high scored if its score is greater
223 than this score."
224   :version "22.1"
225   :group 'gnus-score-default
226   :type 'integer)
227
228 (defcustom gnus-summary-default-low-score 0
229   "*Default threshold for a low scored article.
230 An article will be highlighted as low scored if its score is smaller
231 than this score."
232   :version "22.1"
233   :group 'gnus-score-default
234   :type 'integer)
235
236 (defcustom gnus-summary-zcore-fuzz 0
237   "*Fuzziness factor for the zcore in the summary buffer.
238 Articles with scores closer than this to `gnus-summary-default-score'
239 will not be marked."
240   :group 'gnus-summary-format
241   :type 'integer)
242
243 (defcustom gnus-simplify-subject-fuzzy-regexp nil
244   "*Strings to be removed when doing fuzzy matches.
245 This can either be a regular expression or list of regular expressions
246 that will be removed from subject strings if fuzzy subject
247 simplification is selected."
248   :group 'gnus-thread
249   :type '(repeat regexp))
250
251 (defcustom gnus-show-threads t
252   "*If non-nil, display threads in summary mode."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-hide-subtree nil
257   "*If non-nil, hide all threads initially.
258 This can be a predicate specifier which says which threads to hide.
259 If threads are hidden, you have to run the command
260 `gnus-summary-show-thread' by hand or select an article."
261   :group 'gnus-thread
262   :type '(radio (sexp :format "Non-nil\n"
263                       :match (lambda (widget value)
264                                (not (or (consp value) (functionp value))))
265                       :value t)
266                 (const nil)
267                 (sexp :tag "Predicate specifier")))
268
269 (defcustom gnus-thread-hide-killed t
270   "*If non-nil, hide killed threads automatically."
271   :group 'gnus-thread
272   :type 'boolean)
273
274 (defcustom gnus-thread-ignore-subject t
275   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
276 If nil, articles that have different subjects from their parents will
277 start separate threads."
278   :group 'gnus-thread
279   :type 'boolean)
280
281 (defcustom gnus-thread-operation-ignore-subject t
282   "*If non-nil, subjects will be ignored when doing thread commands.
283 This affects commands like `gnus-summary-kill-thread' and
284 `gnus-summary-lower-thread'.
285
286 If this variable is nil, articles in the same thread with different
287 subjects will not be included in the operation in question.  If this
288 variable is `fuzzy', only articles that have subjects that are fuzzily
289 equal will be included."
290   :group 'gnus-thread
291   :type '(choice (const :tag "off" nil)
292                  (const fuzzy)
293                  (sexp :tag "on" t)))
294
295 (defcustom gnus-thread-indent-level 4
296   "*Number that says how much each sub-thread should be indented."
297   :group 'gnus-thread
298   :type 'integer)
299
300 (defcustom gnus-auto-extend-newsgroup t
301   "*If non-nil, extend newsgroup forward and backward when requested."
302   :group 'gnus-summary-choose
303   :type 'boolean)
304
305 (defcustom gnus-auto-select-first t
306   "*If non-nil, select the article under point.
307 Which article this is is controlled by the `gnus-auto-select-subject'
308 variable.
309
310 If you want to prevent automatic selection of articles in some
311 newsgroups, set the variable to nil in `gnus-select-group-hook'."
312   :group 'gnus-group-select
313   :type '(choice (const :tag "none" nil)
314                  (sexp :menu-tag "first" t)))
315
316 (defcustom gnus-auto-select-subject 'unread
317   "*Says what subject to place under point when entering a group.
318
319 This variable can either be the symbols `first' (place point on the
320 first subject), `unread' (place point on the subject line of the first
321 unread article), `best' (place point on the subject line of the
322 higest-scored article), `unseen' (place point on the subject line of
323 the first unseen article), `unseen-or-unread' (place point on the subject
324 line of the first unseen article or, if all article have been seen, on the
325 subject line of the first unread article), or a function to be called to
326 place point on some subject line."
327   :version "22.1"
328   :group 'gnus-group-select
329   :type '(choice (const best)
330                  (const unread)
331                  (const first)
332                  (const unseen)
333                  (const unseen-or-unread)))
334
335 (defcustom gnus-auto-select-next t
336   "*If non-nil, offer to go to the next group from the end of the previous.
337 If the value is t and the next newsgroup is empty, Gnus will exit
338 summary mode and go back to group mode.  If the value is neither nil
339 nor t, Gnus will select the following unread newsgroup.  In
340 particular, if the value is the symbol `quietly', the next unread
341 newsgroup will be selected without any confirmation, and if it is
342 `almost-quietly', the next group will be selected without any
343 confirmation if you are located on the last article in the group.
344 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
345 will go to the next group without confirmation."
346   :group 'gnus-summary-maneuvering
347   :type '(choice (const :tag "off" nil)
348                  (const quietly)
349                  (const almost-quietly)
350                  (const slightly-quietly)
351                  (sexp :menu-tag "on" t)))
352
353 (defcustom gnus-auto-select-same nil
354   "*If non-nil, select the next article with the same subject.
355 If there are no more articles with the same subject, go to
356 the first unread article."
357   :group 'gnus-summary-maneuvering
358   :type 'boolean)
359
360 (defcustom gnus-auto-goto-ignores 'unfetched
361   "*Says how to handle unfetched articles when maneuvering.
362
363 This variable can either be the symbols nil (maneuver to any
364 article), `undownloaded' (maneuvering while unplugged ignores articles
365 that have not been fetched), `always-undownloaded' (maneuvering always
366 ignores articles that have not been fetched), `unfetched' (maneuvering
367 ignores articles whose headers have not been fetched).
368
369 NOTE: The list of unfetched articles will always be nil when plugged
370 and, when unplugged, a subset of the undownloaded article list."
371   :version "22.1"
372   :group 'gnus-summary-maneuvering
373   :type '(choice (const :tag "None" nil)
374                  (const :tag "Undownloaded when unplugged" undownloaded)
375                  (const :tag "Undownloaded" always-undownloaded)
376                  (const :tag "Unfetched" unfetched)))
377
378 (defcustom gnus-summary-check-current nil
379   "*If non-nil, consider the current article when moving.
380 The \"unread\" movement commands will stay on the same line if the
381 current article is unread."
382   :group 'gnus-summary-maneuvering
383   :type 'boolean)
384
385 (defcustom gnus-auto-center-summary 2
386   "*If non-nil, always center the current summary buffer.
387 In particular, if `vertical' do only vertical recentering.  If non-nil
388 and non-`vertical', do both horizontal and vertical recentering."
389   :group 'gnus-summary-maneuvering
390   :type '(choice (const :tag "none" nil)
391                  (const vertical)
392                  (integer :tag "height")
393                  (sexp :menu-tag "both" t)))
394
395 (defvar gnus-auto-center-group t
396   "*If non-nil, always center the group buffer.")
397
398 (defcustom gnus-show-all-headers nil
399   "*If non-nil, don't hide any headers."
400   :group 'gnus-article-hiding
401   :group 'gnus-article-headers
402   :type 'boolean)
403
404 (defcustom gnus-summary-ignore-duplicates nil
405   "*If non-nil, ignore articles with identical Message-ID headers."
406   :group 'gnus-summary
407   :type 'boolean)
408
409 (defcustom gnus-single-article-buffer t
410   "*If non-nil, display all articles in the same buffer.
411 If nil, each group will get its own article buffer."
412   :group 'gnus-article-various
413   :type 'boolean)
414
415 (defcustom gnus-break-pages t
416   "*If non-nil, do page breaking on articles.
417 The page delimiter is specified by the `gnus-page-delimiter'
418 variable."
419   :group 'gnus-article-various
420   :type 'boolean)
421
422 (defcustom gnus-move-split-methods nil
423   "*Variable used to suggest where articles are to be moved to.
424 It uses the same syntax as the `gnus-split-methods' variable.
425 However, whereas `gnus-split-methods' specifies file names as targets,
426 this variable specifies group names."
427   :group 'gnus-summary-mail
428   :type '(repeat (choice (list :value (fun) function)
429                          (cons :value ("" "") regexp (repeat string))
430                          (sexp :value nil))))
431
432 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
433   "Function used to compute default prefix for article move/copy/etc prompts.
434 The function should take one argument, a group name, and return a
435 string with the suggested prefix."
436   :group 'gnus-summary-mail
437   :type 'function)
438
439 ;; FIXME: Although the custom type is `character' for the following variables,
440 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
441
442 (defcustom gnus-unread-mark ?           ;Whitespace
443   "*Mark used for unread articles."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-ticked-mark ?!
448   "*Mark used for ticked articles."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-dormant-mark ??
453   "*Mark used for dormant articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-del-mark ?r
458   "*Mark used for del'd articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-read-mark ?R
463   "*Mark used for read articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-expirable-mark ?E
468   "*Mark used for expirable articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-killed-mark ?K
473   "*Mark used for killed articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-spam-mark ?$
478   "*Mark used for spam articles."
479   :version "22.1"
480   :group 'gnus-summary-marks
481   :type 'character)
482
483 (defcustom gnus-souped-mark ?F
484   "*Mark used for souped articles."
485   :group 'gnus-summary-marks
486   :type 'character)
487
488 (defcustom gnus-kill-file-mark ?X
489   "*Mark used for articles killed by kill files."
490   :group 'gnus-summary-marks
491   :type 'character)
492
493 (defcustom gnus-low-score-mark ?Y
494   "*Mark used for articles with a low score."
495   :group 'gnus-summary-marks
496   :type 'character)
497
498 (defcustom gnus-catchup-mark ?C
499   "*Mark used for articles that are caught up."
500   :group 'gnus-summary-marks
501   :type 'character)
502
503 (defcustom gnus-replied-mark ?A
504   "*Mark used for articles that have been replied to."
505   :group 'gnus-summary-marks
506   :type 'character)
507
508 (defcustom gnus-forwarded-mark ?F
509   "*Mark used for articles that have been forwarded."
510   :version "22.1"
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-recent-mark ?N
515   "*Mark used for articles that are recent."
516   :version "22.1"
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-cached-mark ?*
521   "*Mark used for articles that are in the cache."
522   :group 'gnus-summary-marks
523   :type 'character)
524
525 (defcustom gnus-saved-mark ?S
526   "*Mark used for articles that have been saved."
527   :group 'gnus-summary-marks
528   :type 'character)
529
530 (defcustom gnus-unseen-mark ?.
531   "*Mark used for articles that haven't been seen."
532   :version "22.1"
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-no-mark ?               ;Whitespace
537   "*Mark used for articles that have no other secondary mark."
538   :version "22.1"
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-ancient-mark ?O
543   "*Mark used for ancient articles."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-sparse-mark ?Q
548   "*Mark used for sparsely reffed articles."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-canceled-mark ?G
553   "*Mark used for canceled articles."
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-duplicate-mark ?M
558   "*Mark used for duplicate articles."
559   :group 'gnus-summary-marks
560   :type 'character)
561
562 (defcustom gnus-undownloaded-mark ?-
563   "*Mark used for articles that weren't downloaded."
564   :version "22.1"
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-downloaded-mark ?+
569   "*Mark used for articles that were downloaded."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-downloadable-mark ?%
574   "*Mark used for articles that are to be downloaded."
575   :group 'gnus-summary-marks
576   :type 'character)
577
578 (defcustom gnus-unsendable-mark ?=
579   "*Mark used for articles that won't be sent."
580   :group 'gnus-summary-marks
581   :type 'character)
582
583 (defcustom gnus-score-over-mark ?+
584   "*Score mark used for articles with high scores."
585   :group 'gnus-summary-marks
586   :type 'character)
587
588 (defcustom gnus-score-below-mark ?-
589   "*Score mark used for articles with low scores."
590   :group 'gnus-summary-marks
591   :type 'character)
592
593 (defcustom gnus-empty-thread-mark ?     ;Whitespace
594   "*There is no thread under the article."
595   :group 'gnus-summary-marks
596   :type 'character)
597
598 (defcustom gnus-not-empty-thread-mark ?=
599   "*There is a thread under the article."
600   :group 'gnus-summary-marks
601   :type 'character)
602
603 (defcustom gnus-view-pseudo-asynchronously nil
604   "*If non-nil, Gnus will view pseudo-articles asynchronously."
605   :group 'gnus-extract-view
606   :type 'boolean)
607
608 (defcustom gnus-auto-expirable-marks
609   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
610         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
611         gnus-souped-mark gnus-duplicate-mark)
612   "*The list of marks converted into expiration if a group is auto-expirable."
613   :version "21.1"
614   :group 'gnus-summary
615   :type '(repeat character))
616
617 (defcustom gnus-inhibit-user-auto-expire t
618   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
619   :version "21.1"
620   :group 'gnus-summary
621   :type 'boolean)
622
623 (defcustom gnus-view-pseudos nil
624   "*If `automatic', pseudo-articles will be viewed automatically.
625 If `not-confirm', pseudos will be viewed automatically, and the user
626 will not be asked to confirm the command."
627   :group 'gnus-extract-view
628   :type '(choice (const :tag "off" nil)
629                  (const automatic)
630                  (const not-confirm)))
631
632 (defcustom gnus-view-pseudos-separately t
633   "*If non-nil, one pseudo-article will be created for each file to be viewed.
634 If nil, all files that use the same viewing command will be given as a
635 list of parameters to that command."
636   :group 'gnus-extract-view
637   :type 'boolean)
638
639 (defcustom gnus-insert-pseudo-articles t
640   "*If non-nil, insert pseudo-articles when decoding articles."
641   :group 'gnus-extract-view
642   :type 'boolean)
643
644 (defcustom gnus-summary-dummy-line-format
645   "   %(:                             :%) %S\n"
646   "*The format specification for the dummy roots in the summary buffer.
647 It works along the same lines as a normal formatting string,
648 with some simple extensions.
649
650 %S  The subject
651
652 General format specifiers can also be used.
653 See `(gnus)Formatting Variables'."
654   :link '(custom-manual "(gnus)Formatting Variables")
655   :group 'gnus-threading
656   :type 'string)
657
658 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
659   "*The format specification for the summary mode line.
660 It works along the same lines as a normal formatting string,
661 with some simple extensions:
662
663 %G  Group name
664 %p  Unprefixed group name
665 %A  Current article number
666 %z  Current article score
667 %V  Gnus version
668 %U  Number of unread articles in the group
669 %e  Number of unselected articles in the group
670 %Z  A string with unread/unselected article counts
671 %g  Shortish group name
672 %S  Subject of the current article
673 %u  User-defined spec
674 %s  Current score file name
675 %d  Number of dormant articles
676 %r  Number of articles that have been marked as read in this session
677 %E  Number of articles expunged by the score files"
678   :group 'gnus-summary-format
679   :type 'string)
680
681 (defcustom gnus-list-identifiers nil
682   "Regexp that matches list identifiers to be removed from subject.
683 This can also be a list of regexps."
684   :version "21.1"
685   :group 'gnus-summary-format
686   :group 'gnus-article-hiding
687   :type '(choice (const :tag "none" nil)
688                  (regexp :value ".*")
689                  (repeat :value (".*") regexp)))
690
691 (defcustom gnus-summary-mark-below 0
692   "*Mark all articles with a score below this variable as read.
693 This variable is local to each summary buffer and usually set by the
694 score file."
695   :group 'gnus-score-default
696   :type 'integer)
697
698 (defun gnus-widget-reversible-match (widget value)
699   "Ignoring WIDGET, convert VALUE to internal form.
700 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
701   ;; (debug value)
702   (or (symbolp value)
703       (and (listp value)
704            (eq (length value) 2)
705            (eq (nth 0 value) 'not)
706            (symbolp (nth 1 value)))))
707
708 (defun gnus-widget-reversible-to-internal (widget value)
709   "Ignoring WIDGET, convert VALUE to internal form.
710 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
711 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
712   ;; (debug value)
713   (if (atom value)
714       (list value nil)
715     (list (nth 1 value) t)))
716
717 (defun gnus-widget-reversible-to-external (widget value)
718   "Ignoring WIDGET, convert VALUE to external form.
719 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
720 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
721   ;; (debug value)
722   (if (nth 1 value)
723       (list 'not (nth 0 value))
724     (nth 0 value)))
725
726 (define-widget 'gnus-widget-reversible 'group
727   "A `group' that convert values."
728   :match 'gnus-widget-reversible-match
729   :value-to-internal 'gnus-widget-reversible-to-internal
730   :value-to-external 'gnus-widget-reversible-to-external)
731
732 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
733   "*List of functions used for sorting articles in the summary buffer.
734
735 Each function takes two articles and returns non-nil if the first
736 article should be sorted before the other.  If you use more than one
737 function, the primary sort function should be the last.  You should
738 probably always include `gnus-article-sort-by-number' in the list of
739 sorting functions -- preferably first.  Also note that sorting by date
740 is often much slower than sorting by number, and the sorting order is
741 very similar.  (Sorting by date means sorting by the time the message
742 was sent, sorting by number means sorting by arrival time.)
743
744 Each item can also be a list `(not F)' where F is a function;
745 this reverses the sort order.
746
747 Ready-made functions include `gnus-article-sort-by-number',
748 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
749 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
750 and `gnus-article-sort-by-score'.
751
752 When threading is turned on, the variable `gnus-thread-sort-functions'
753 controls how articles are sorted."
754   :group 'gnus-summary-sort
755   :type '(repeat (gnus-widget-reversible
756                   (choice (function-item gnus-article-sort-by-number)
757                           (function-item gnus-article-sort-by-author)
758                           (function-item gnus-article-sort-by-subject)
759                           (function-item gnus-article-sort-by-date)
760                           (function-item gnus-article-sort-by-score)
761                           (function-item gnus-article-sort-by-random)
762                           (function :tag "other"))
763                   (boolean :tag "Reverse order"))))
764
765
766 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
767   "*List of functions used for sorting threads in the summary buffer.
768 By default, threads are sorted by article number.
769
770 Each function takes two threads and returns non-nil if the first
771 thread should be sorted before the other.  If you use more than one
772 function, the primary sort function should be the last.  You should
773 probably always include `gnus-thread-sort-by-number' in the list of
774 sorting functions -- preferably first.  Also note that sorting by date
775 is often much slower than sorting by number, and the sorting order is
776 very similar.  (Sorting by date means sorting by the time the message
777 was sent, sorting by number means sorting by arrival time.)
778
779 Each list item can also be a list `(not F)' where F is a
780 function; this specifies reversed sort order.
781
782 Ready-made functions include `gnus-thread-sort-by-number',
783 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
784 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
785 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
786 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
787 and `gnus-thread-sort-by-total-score' (see
788 `gnus-thread-score-function').
789
790 When threading is turned off, the variable
791 `gnus-article-sort-functions' controls how articles are sorted."
792   :group 'gnus-summary-sort
793   :type '(repeat
794           (gnus-widget-reversible
795            (choice (function-item gnus-thread-sort-by-number)
796                    (function-item gnus-thread-sort-by-author)
797                    (function-item gnus-thread-sort-by-recipient)
798                    (function-item gnus-thread-sort-by-subject)
799                    (function-item gnus-thread-sort-by-date)
800                    (function-item gnus-thread-sort-by-score)
801                    (function-item gnus-thread-sort-by-most-recent-number)
802                    (function-item gnus-thread-sort-by-most-recent-date)
803                    (function-item gnus-thread-sort-by-random)
804                    (function-item gnus-thread-sort-by-total-score)
805                    (function :tag "other"))
806            (boolean :tag "Reverse order"))))
807
808 (defcustom gnus-thread-score-function '+
809   "*Function used for calculating the total score of a thread.
810
811 The function is called with the scores of the article and each
812 subthread and should then return the score of the thread.
813
814 Some functions you can use are `+', `max', or `min'."
815   :group 'gnus-summary-sort
816   :type 'function)
817
818 (defcustom gnus-summary-expunge-below nil
819   "All articles that have a score less than this variable will be expunged.
820 This variable is local to the summary buffers."
821   :group 'gnus-score-default
822   :type '(choice (const :tag "off" nil)
823                  integer))
824
825 (defcustom gnus-thread-expunge-below nil
826   "All threads that have a total score less than this variable will be expunged.
827 See `gnus-thread-score-function' for en explanation of what a
828 \"thread score\" is.
829
830 This variable is local to the summary buffers."
831   :group 'gnus-threading
832   :group 'gnus-score-default
833   :type '(choice (const :tag "off" nil)
834                  integer))
835
836 (defcustom gnus-summary-mode-hook nil
837   "*A hook for Gnus summary mode.
838 This hook is run before any variables are set in the summary buffer."
839   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
840   :group 'gnus-summary-various
841   :type 'hook)
842
843 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
844 (when (featurep 'xemacs)
845   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
846   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
847   (add-hook 'gnus-summary-mode-hook
848             'gnus-xmas-switch-horizontal-scrollbar-off))
849
850 (defcustom gnus-summary-menu-hook nil
851   "*Hook run after the creation of the summary mode menu."
852   :group 'gnus-summary-visual
853   :type 'hook)
854
855 (defcustom gnus-summary-exit-hook nil
856   "*A hook called on exit from the summary buffer.
857 It will be called with point in the group buffer."
858   :group 'gnus-summary-exit
859   :type 'hook)
860
861 (defcustom gnus-summary-prepare-hook nil
862   "*A hook called after the summary buffer has been generated.
863 If you want to modify the summary buffer, you can use this hook."
864   :group 'gnus-summary-various
865   :type 'hook)
866
867 (defcustom gnus-summary-prepared-hook nil
868   "*A hook called as the last thing after the summary buffer has been generated."
869   :group 'gnus-summary-various
870   :type 'hook)
871
872 (defcustom gnus-summary-generate-hook nil
873   "*A hook run just before generating the summary buffer.
874 This hook is commonly used to customize threading variables and the
875 like."
876   :group 'gnus-summary-various
877   :type 'hook)
878
879 (defcustom gnus-select-group-hook nil
880   "*A hook called when a newsgroup is selected.
881
882 If you'd like to simplify subjects like the
883 `gnus-summary-next-same-subject' command does, you can use the
884 following hook:
885
886  (add-hook gnus-select-group-hook
887            (lambda ()
888              (mapcar (lambda (header)
889                        (mail-header-set-subject
890                         header
891                         (gnus-simplify-subject
892                          (mail-header-subject header) 're-only)))
893                      gnus-newsgroup-headers)))"
894   :group 'gnus-group-select
895   :type 'hook)
896
897 (defcustom gnus-select-article-hook nil
898   "*A hook called when an article is selected."
899   :group 'gnus-summary-choose
900   :options '(gnus-agent-fetch-selected-article)
901   :type 'hook)
902
903 (defcustom gnus-visual-mark-article-hook
904   (list 'gnus-highlight-selected-summary)
905   "*Hook run after selecting an article in the summary buffer.
906 It is meant to be used for highlighting the article in some way.  It
907 is not run if `gnus-visual' is nil."
908   :group 'gnus-summary-visual
909   :type 'hook)
910
911 (defcustom gnus-parse-headers-hook nil
912   "*A hook called before parsing the headers."
913   :group 'gnus-various
914   :type 'hook)
915
916 (defcustom gnus-exit-group-hook nil
917   "*A hook called when exiting summary mode.
918 This hook is not called from the non-updating exit commands like `Q'."
919   :group 'gnus-various
920   :type 'hook)
921
922 (defcustom gnus-summary-update-hook
923   (list 'gnus-summary-highlight-line)
924   "*A hook called when a summary line is changed.
925 The hook will not be called if `gnus-visual' is nil.
926
927 The default function `gnus-summary-highlight-line' will
928 highlight the line according to the `gnus-summary-highlight'
929 variable."
930   :group 'gnus-summary-visual
931   :type 'hook)
932
933 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
934   "*A hook called when an article is selected for the first time.
935 The hook is intended to mark an article as read (or unread)
936 automatically when it is selected."
937   :group 'gnus-summary-choose
938   :type 'hook)
939
940 (defcustom gnus-group-no-more-groups-hook nil
941   "*A hook run when returning to group mode having no more (unread) groups."
942   :group 'gnus-group-select
943   :type 'hook)
944
945 (defcustom gnus-ps-print-hook nil
946   "*A hook run before ps-printing something from Gnus."
947   :group 'gnus-summary
948   :type 'hook)
949
950 (defcustom gnus-summary-article-move-hook nil
951   "*A hook called after an article is moved, copied, respooled, or crossposted."
952   :version "22.1"
953   :group 'gnus-summary
954   :type 'hook)
955
956 (defcustom gnus-summary-article-delete-hook nil
957   "*A hook called after an article is deleted."
958   :version "22.1"
959   :group 'gnus-summary
960   :type 'hook)
961
962 (defcustom gnus-summary-article-expire-hook nil
963   "*A hook called after an article is expired."
964   :version "22.1"
965   :group 'gnus-summary
966   :type 'hook)
967
968 (defcustom gnus-summary-display-arrow
969   (and (fboundp 'display-graphic-p)
970        (display-graphic-p))
971   "*If non-nil, display an arrow highlighting the current article."
972   :version "22.1"
973   :group 'gnus-summary
974   :type 'boolean)
975
976 (defcustom gnus-summary-selected-face 'gnus-summary-selected
977   "Face used for highlighting the current article in the summary buffer."
978   :group 'gnus-summary-visual
979   :type 'face)
980
981 (defvar gnus-tmp-downloaded nil)
982
983 (defcustom gnus-summary-highlight
984   '(((eq mark gnus-canceled-mark)
985      . gnus-summary-cancelled)
986     ((and uncached (> score default-high))
987      . gnus-summary-high-undownloaded)
988     ((and uncached (< score default-low))
989      . gnus-summary-low-undownloaded)
990     (uncached
991      . gnus-summary-normal-undownloaded)
992     ((and (> score default-high)
993           (or (eq mark gnus-dormant-mark)
994               (eq mark gnus-ticked-mark)))
995      . gnus-summary-high-ticked)
996     ((and (< score default-low)
997           (or (eq mark gnus-dormant-mark)
998               (eq mark gnus-ticked-mark)))
999      . gnus-summary-low-ticked)
1000     ((or (eq mark gnus-dormant-mark)
1001          (eq mark gnus-ticked-mark))
1002      . gnus-summary-normal-ticked)
1003     ((and (> score default-high) (eq mark gnus-ancient-mark))
1004      . gnus-summary-high-ancient)
1005     ((and (< score default-low) (eq mark gnus-ancient-mark))
1006      . gnus-summary-low-ancient)
1007     ((eq mark gnus-ancient-mark)
1008      . gnus-summary-normal-ancient)
1009     ((and (> score default-high) (eq mark gnus-unread-mark))
1010      . gnus-summary-high-unread)
1011     ((and (< score default-low) (eq mark gnus-unread-mark))
1012      . gnus-summary-low-unread)
1013     ((eq mark gnus-unread-mark)
1014      . gnus-summary-normal-unread)
1015     ((> score default-high)
1016      . gnus-summary-high-read)
1017     ((< score default-low)
1018      . gnus-summary-low-read)
1019     (t
1020      . gnus-summary-normal-read))
1021   "*Controls the highlighting of summary buffer lines.
1022
1023 A list of (FORM . FACE) pairs.  When deciding how a a particular
1024 summary line should be displayed, each form is evaluated.  The content
1025 of the face field after the first true form is used.  You can change
1026 how those summary lines are displayed, by editing the face field.
1027
1028 You can use the following variables in the FORM field.
1029
1030 score:        The article's score
1031 default:      The default article score.
1032 default-high: The default score for high scored articles.
1033 default-low:  The default score for low scored articles.
1034 below:        The score below which articles are automatically marked as read.
1035 mark:         The article's mark.
1036 uncached:     Non-nil if the article is uncached."
1037   :group 'gnus-summary-visual
1038   :type '(repeat (cons (sexp :tag "Form" nil)
1039                        face)))
1040
1041 (defcustom gnus-alter-header-function nil
1042   "Function called to allow alteration of article header structures.
1043 The function is called with one parameter, the article header vector,
1044 which it may alter in any way."
1045   :type '(choice (const :tag "None" nil)
1046                  function)
1047   :group 'gnus-summary)
1048
1049 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1050   "Variable that says which function should be used to decode a string with encoded words.")
1051
1052 (defcustom gnus-extra-headers '(To Newsgroups)
1053   "*Extra headers to parse."
1054   :version "21.1"
1055   :group 'gnus-summary
1056   :type '(repeat symbol))
1057
1058 (defcustom gnus-ignored-from-addresses
1059   (and user-mail-address (regexp-quote user-mail-address))
1060   "*Regexp of From headers that may be suppressed in favor of To headers."
1061   :version "21.1"
1062   :group 'gnus-summary
1063   :type 'regexp)
1064
1065 (defcustom gnus-summary-to-prefix "-> "
1066   "*String prefixed to the To field in the summary line when
1067 using `gnus-ignored-from-addresses'."
1068   :version "22.1"
1069   :group 'gnus-summary
1070   :type 'string)
1071
1072 (defcustom gnus-summary-newsgroup-prefix "=> "
1073   "*String prefixed to the Newsgroup field in the summary
1074 line when using `gnus-ignored-from-addresses'."
1075   :version "22.1"
1076   :group 'gnus-summary
1077   :type 'string)
1078
1079 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1080   "List of charsets that should be ignored.
1081 When these charsets are used in the \"charset\" parameter, the
1082 default charset will be used instead."
1083   :version "21.1"
1084   :type '(repeat symbol)
1085   :group 'gnus-charset)
1086
1087 (gnus-define-group-parameter
1088  ignored-charsets
1089  :type list
1090  :function-document
1091  "Return the ignored charsets of GROUP."
1092  :variable gnus-group-ignored-charsets-alist
1093  :variable-default
1094  '(("alt\\.chinese\\.text" iso-8859-1))
1095  :variable-document
1096  "Alist of regexps (to match group names) and charsets that should be ignored.
1097 When these charsets are used in the \"charset\" parameter, the
1098 default charset will be used instead."
1099  :variable-group gnus-charset
1100  :variable-type '(repeat (cons (regexp :tag "Group")
1101                                (repeat symbol)))
1102  :parameter-type '(choice :tag "Ignored charsets"
1103                           :value nil
1104                           (repeat (symbol)))
1105  :parameter-document       "\
1106 List of charsets that should be ignored.
1107
1108 When these charsets are used in the \"charset\" parameter, the
1109 default charset will be used instead.")
1110
1111 (defcustom gnus-group-highlight-words-alist nil
1112   "Alist of group regexps and highlight regexps.
1113 This variable uses the same syntax as `gnus-emphasis-alist'."
1114   :version "21.1"
1115   :type '(repeat (cons (regexp :tag "Group")
1116                        (repeat (list (regexp :tag "Highlight regexp")
1117                                      (number :tag "Group for entire word" 0)
1118                                      (number :tag "Group for displayed part" 0)
1119                                      (symbol :tag "Face"
1120                                              gnus-emphasis-highlight-words)))))
1121   :group 'gnus-summary-visual)
1122
1123 (defcustom gnus-summary-show-article-charset-alist
1124   nil
1125   "Alist of number and charset.
1126 The article will be shown with the charset corresponding to the
1127 numbered argument.
1128 For example: ((1 . cn-gb-2312) (2 . big5))."
1129   :version "21.1"
1130   :type '(repeat (cons (number :tag "Argument" 1)
1131                        (symbol :tag "Charset")))
1132   :group 'gnus-charset)
1133
1134 (defcustom gnus-preserve-marks t
1135   "Whether marks are preserved when moving, copying and respooling messages."
1136   :version "21.1"
1137   :type 'boolean
1138   :group 'gnus-summary-marks)
1139
1140 (defcustom gnus-alter-articles-to-read-function nil
1141   "Function to be called to alter the list of articles to be selected."
1142   :type '(choice (const nil) function)
1143   :group 'gnus-summary)
1144
1145 (defcustom gnus-orphan-score nil
1146   "*All orphans get this score added.  Set in the score file."
1147   :group 'gnus-score-default
1148   :type '(choice (const nil)
1149                  integer))
1150
1151 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1152   "*A regexp to match MIME parts when saving multiple parts of a
1153 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1154 This regexp will be used by default when prompting the user for which
1155 type of files to save."
1156   :group 'gnus-summary
1157   :type 'regexp)
1158
1159 (defcustom gnus-read-all-available-headers nil
1160   "Whether Gnus should parse all headers made available to it.
1161 This is mostly relevant for slow back ends where the user may
1162 wish to widen the summary buffer to include all headers
1163 that were fetched.  Say, for nnultimate groups."
1164   :version "22.1"
1165   :group 'gnus-summary
1166   :type '(choice boolean regexp))
1167
1168 (defcustom gnus-summary-muttprint-program "muttprint"
1169   "Command (and optional arguments) used to run Muttprint."
1170   :version "22.1"
1171   :group 'gnus-summary
1172   :type 'string)
1173
1174 (defcustom gnus-article-loose-mime t
1175   "If non-nil, don't require MIME-Version header.
1176 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1177 supply the MIME-Version header or deliberately strip it from the mail.
1178 If non-nil (the default), Gnus will treat some articles as MIME
1179 even if the MIME-Version header is missing."
1180   :version "22.1"
1181   :type 'boolean
1182   :group 'gnus-article-mime)
1183
1184 (defcustom gnus-article-emulate-mime t
1185   "If non-nil, use MIME emulation for uuencode and the like.
1186 This means that Gnus will search message bodies for text that look
1187 like uuencoded bits, yEncoded bits, and so on, and present that using
1188 the normal Gnus MIME machinery."
1189   :version "22.1"
1190   :type 'boolean
1191   :group 'gnus-article-mime)
1192
1193 ;;; Internal variables
1194
1195 (defvar gnus-summary-display-cache nil)
1196 (defvar gnus-article-mime-handles nil)
1197 (defvar gnus-article-decoded-p nil)
1198 (defvar gnus-article-charset nil)
1199 (defvar gnus-article-ignored-charsets nil)
1200 (defvar gnus-scores-exclude-files nil)
1201 (defvar gnus-page-broken nil)
1202
1203 (defvar gnus-original-article nil)
1204 (defvar gnus-article-internal-prepare-hook nil)
1205 (defvar gnus-newsgroup-process-stack nil)
1206
1207 (defvar gnus-thread-indent-array nil)
1208 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1209 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1210   "Function called to sort the articles within a thread after it has been gathered together.")
1211
1212 (defvar gnus-summary-save-parts-type-history nil)
1213 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1214
1215 ;; Avoid highlighting in kill files.
1216 (defvar gnus-summary-inhibit-highlight nil)
1217 (defvar gnus-newsgroup-selected-overlay nil)
1218 (defvar gnus-inhibit-limiting nil)
1219 (defvar gnus-newsgroup-adaptive-score-file nil)
1220 (defvar gnus-current-score-file nil)
1221 (defvar gnus-current-move-group nil)
1222 (defvar gnus-current-copy-group nil)
1223 (defvar gnus-current-crosspost-group nil)
1224 (defvar gnus-newsgroup-display nil)
1225
1226 (defvar gnus-newsgroup-dependencies nil)
1227 (defvar gnus-newsgroup-adaptive nil)
1228 (defvar gnus-summary-display-article-function nil)
1229 (defvar gnus-summary-highlight-line-function nil
1230   "Function called after highlighting a summary line.")
1231
1232 (defvar gnus-summary-line-format-alist
1233   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1234     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1235     (?s gnus-tmp-subject-or-nil ?s)
1236     (?n gnus-tmp-name ?s)
1237     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1238         ?s)
1239     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1240             gnus-tmp-from) ?s)
1241     (?F gnus-tmp-from ?s)
1242     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1243     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1244     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1245     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1246     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1247     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1248     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1249     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1250     (?L gnus-tmp-lines ?s)
1251     (?O gnus-tmp-downloaded ?c)
1252     (?I gnus-tmp-indentation ?s)
1253     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1254     (?R gnus-tmp-replied ?c)
1255     (?\[ gnus-tmp-opening-bracket ?c)
1256     (?\] gnus-tmp-closing-bracket ?c)
1257     (?\> (make-string gnus-tmp-level ? ) ?s)
1258     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1259     (?i gnus-tmp-score ?d)
1260     (?z gnus-tmp-score-char ?c)
1261     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1262     (?U gnus-tmp-unread ?c)
1263     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1264         ?s)
1265     (?t (gnus-summary-number-of-articles-in-thread
1266          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1267         ?d)
1268     (?e (gnus-summary-number-of-articles-in-thread
1269          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1270         ?c)
1271     (?u gnus-tmp-user-defined ?s)
1272     (?P (gnus-pick-line-number) ?d)
1273     (?B gnus-tmp-thread-tree-header-string ?s)
1274     (user-date (gnus-user-date
1275                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1276   "An alist of format specifications that can appear in summary lines.
1277 These are paired with what variables they correspond with, along with
1278 the type of the variable (string, integer, character, etc).")
1279
1280 (defvar gnus-summary-dummy-line-format-alist
1281   `((?S gnus-tmp-subject ?s)
1282     (?N gnus-tmp-number ?d)
1283     (?u gnus-tmp-user-defined ?s)))
1284
1285 (defvar gnus-summary-mode-line-format-alist
1286   `((?G gnus-tmp-group-name ?s)
1287     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1288     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1289     (?A gnus-tmp-article-number ?d)
1290     (?Z gnus-tmp-unread-and-unselected ?s)
1291     (?V gnus-version ?s)
1292     (?U gnus-tmp-unread-and-unticked ?d)
1293     (?S gnus-tmp-subject ?s)
1294     (?e gnus-tmp-unselected ?d)
1295     (?u gnus-tmp-user-defined ?s)
1296     (?d (length gnus-newsgroup-dormant) ?d)
1297     (?t (length gnus-newsgroup-marked) ?d)
1298     (?h (length gnus-newsgroup-spam-marked) ?d)
1299     (?r (length gnus-newsgroup-reads) ?d)
1300     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1301     (?E gnus-newsgroup-expunged-tally ?d)
1302     (?s (gnus-current-score-file-nondirectory) ?s)))
1303
1304 (defvar gnus-last-search-regexp nil
1305   "Default regexp for article search command.")
1306
1307 (defvar gnus-last-shell-command nil
1308   "Default shell command on article.")
1309
1310 (defvar gnus-newsgroup-agentized nil
1311   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1312 (defvar gnus-newsgroup-begin nil)
1313 (defvar gnus-newsgroup-end nil)
1314 (defvar gnus-newsgroup-last-rmail nil)
1315 (defvar gnus-newsgroup-last-mail nil)
1316 (defvar gnus-newsgroup-last-folder nil)
1317 (defvar gnus-newsgroup-last-file nil)
1318 (defvar gnus-newsgroup-auto-expire nil)
1319 (defvar gnus-newsgroup-active nil)
1320
1321 (defvar gnus-newsgroup-data nil)
1322 (defvar gnus-newsgroup-data-reverse nil)
1323 (defvar gnus-newsgroup-limit nil)
1324 (defvar gnus-newsgroup-limits nil)
1325 (defvar gnus-summary-use-undownloaded-faces nil)
1326
1327 (defvar gnus-newsgroup-unreads nil
1328   "Sorted list of unread articles in the current newsgroup.")
1329
1330 (defvar gnus-newsgroup-unselected nil
1331   "Sorted list of unselected unread articles in the current newsgroup.")
1332
1333 (defvar gnus-newsgroup-reads nil
1334   "Alist of read articles and article marks in the current newsgroup.")
1335
1336 (defvar gnus-newsgroup-expunged-tally nil)
1337
1338 (defvar gnus-newsgroup-marked nil
1339   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1340
1341 (defvar gnus-newsgroup-spam-marked nil
1342   "List of ranges of articles that have been marked as spam.")
1343
1344 (defvar gnus-newsgroup-killed nil
1345   "List of ranges of articles that have been through the scoring process.")
1346
1347 (defvar gnus-newsgroup-cached nil
1348   "Sorted list of articles that come from the article cache.")
1349
1350 (defvar gnus-newsgroup-saved nil
1351   "List of articles that have been saved.")
1352
1353 (defvar gnus-newsgroup-kill-headers nil)
1354
1355 (defvar gnus-newsgroup-replied nil
1356   "List of articles that have been replied to in the current newsgroup.")
1357
1358 (defvar gnus-newsgroup-forwarded nil
1359   "List of articles that have been forwarded in the current newsgroup.")
1360
1361 (defvar gnus-newsgroup-recent nil
1362   "List of articles that have are recent in the current newsgroup.")
1363
1364 (defvar gnus-newsgroup-expirable nil
1365   "Sorted list of articles in the current newsgroup that can be expired.")
1366
1367 (defvar gnus-newsgroup-processable nil
1368   "List of articles in the current newsgroup that can be processed.")
1369
1370 (defvar gnus-newsgroup-downloadable nil
1371   "Sorted list of articles in the current newsgroup that can be processed.")
1372
1373 (defvar gnus-newsgroup-unfetched nil
1374   "Sorted list of articles in the current newsgroup whose headers have
1375 not been fetched into the agent.
1376
1377 This list will always be a subset of gnus-newsgroup-undownloaded.")
1378
1379 (defvar gnus-newsgroup-undownloaded nil
1380   "List of articles in the current newsgroup that haven't been downloaded.")
1381
1382 (defvar gnus-newsgroup-unsendable nil
1383   "List of articles in the current newsgroup that won't be sent.")
1384
1385 (defvar gnus-newsgroup-bookmarks nil
1386   "List of articles in the current newsgroup that have bookmarks.")
1387
1388 (defvar gnus-newsgroup-dormant nil
1389   "Sorted list of dormant articles in the current newsgroup.")
1390
1391 (defvar gnus-newsgroup-unseen nil
1392   "List of unseen articles in the current newsgroup.")
1393
1394 (defvar gnus-newsgroup-seen nil
1395   "Range of seen articles in the current newsgroup.")
1396
1397 (defvar gnus-newsgroup-articles nil
1398   "List of articles in the current newsgroup.")
1399
1400 (defvar gnus-newsgroup-scored nil
1401   "List of scored articles in the current newsgroup.")
1402
1403 (defvar gnus-newsgroup-headers nil
1404   "List of article headers in the current newsgroup.")
1405
1406 (defvar gnus-newsgroup-threads nil)
1407
1408 (defvar gnus-newsgroup-prepared nil
1409   "Whether the current group has been prepared properly.")
1410
1411 (defvar gnus-newsgroup-ancient nil
1412   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1413
1414 (defvar gnus-newsgroup-sparse nil)
1415
1416 (defvar gnus-current-article nil)
1417 (defvar gnus-article-current nil)
1418 (defvar gnus-current-headers nil)
1419 (defvar gnus-have-all-headers nil)
1420 (defvar gnus-last-article nil)
1421 (defvar gnus-newsgroup-history nil)
1422 (defvar gnus-newsgroup-charset nil)
1423 (defvar gnus-newsgroup-ephemeral-charset nil)
1424 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1425
1426 (defvar gnus-article-before-search nil)
1427
1428 (defvar gnus-summary-local-variables
1429   '(gnus-newsgroup-name
1430     gnus-newsgroup-begin gnus-newsgroup-end
1431     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1432     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1433     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1434     gnus-newsgroup-unselected gnus-newsgroup-marked
1435     gnus-newsgroup-spam-marked
1436     gnus-newsgroup-reads gnus-newsgroup-saved
1437     gnus-newsgroup-replied gnus-newsgroup-forwarded
1438     gnus-newsgroup-recent
1439     gnus-newsgroup-expirable
1440     gnus-newsgroup-processable gnus-newsgroup-killed
1441     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1442     gnus-newsgroup-unfetched
1443     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1444     gnus-newsgroup-seen gnus-newsgroup-articles
1445     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1446     gnus-newsgroup-headers gnus-newsgroup-threads
1447     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1448     gnus-current-article gnus-current-headers gnus-have-all-headers
1449     gnus-last-article gnus-article-internal-prepare-hook
1450     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1451     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1452     gnus-thread-expunge-below
1453     gnus-score-alist gnus-current-score-file
1454     (gnus-summary-expunge-below . global)
1455     (gnus-summary-mark-below . global)
1456     (gnus-orphan-score . global)
1457     gnus-newsgroup-active gnus-scores-exclude-files
1458     gnus-newsgroup-history gnus-newsgroup-ancient
1459     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1460     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1461     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1462     (gnus-newsgroup-expunged-tally . 0)
1463     gnus-cache-removable-articles gnus-newsgroup-cached
1464     gnus-newsgroup-data gnus-newsgroup-data-reverse
1465     gnus-newsgroup-limit gnus-newsgroup-limits
1466     gnus-newsgroup-charset gnus-newsgroup-display
1467     gnus-summary-use-undownloaded-faces)
1468   "Variables that are buffer-local to the summary buffers.")
1469
1470 (defvar gnus-newsgroup-variables nil
1471   "A list of variables that have separate values in different newsgroups.
1472 A list of newsgroup (summary buffer) local variables, or cons of
1473 variables and their default expressions to be evalled (when the default
1474 values are not nil), that should be made global while the summary buffer
1475 is active.
1476
1477 Note: The default expressions will be evaluated (using function `eval')
1478 before assignment to the local variable rather than just assigned to it.
1479 If the default expression is the symbol `global', that symbol will not
1480 be evaluated but the global value of the local variable will be used
1481 instead.
1482
1483 These variables can be used to set variables in the group parameters
1484 while still allowing them to affect operations done in other buffers.
1485 For example:
1486
1487 \(setq gnus-newsgroup-variables
1488      '(message-use-followup-to
1489        (gnus-visible-headers .
1490          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1491 ")
1492
1493 ;; Byte-compiler warning.
1494 (eval-when-compile
1495   ;; Bind features so that require will believe that gnus-sum has
1496   ;; already been loaded (avoids infinite recursion)
1497   (let ((features (cons 'gnus-sum features)))
1498     ;; Several of the declarations in gnus-sum are needed to load the
1499     ;; following files. Right now, these definitions have been
1500     ;; compiled but not defined (evaluated).  We could either do a
1501     ;; eval-and-compile about all of the declarations or evaluate the
1502     ;; source file.
1503     (if (boundp 'gnus-newsgroup-variables)
1504         nil
1505       (load "gnus-sum.el" t t t))
1506     (require 'gnus)
1507     (require 'gnus-art)))
1508
1509 ;; MIME stuff.
1510
1511 (defvar gnus-decode-encoded-word-methods
1512   '(mail-decode-encoded-word-string)
1513   "List of methods used to decode encoded words.
1514
1515 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1516 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1517 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1518 whose names match REGEXP.
1519
1520 For example:
1521 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1522  mail-decode-encoded-word-string
1523  (\"chinese\" . rfc1843-decode-string))")
1524
1525 (defvar gnus-decode-encoded-word-methods-cache nil)
1526
1527 (defun gnus-multi-decode-encoded-word-string (string)
1528   "Apply the functions from `gnus-encoded-word-methods' that match."
1529   (unless (and gnus-decode-encoded-word-methods-cache
1530                (eq gnus-newsgroup-name
1531                    (car gnus-decode-encoded-word-methods-cache)))
1532     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1533     (mapcar (lambda (x)
1534               (if (symbolp x)
1535                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1536                 (if (and gnus-newsgroup-name
1537                          (string-match (car x) gnus-newsgroup-name))
1538                     (nconc gnus-decode-encoded-word-methods-cache
1539                            (list (cdr x))))))
1540             gnus-decode-encoded-word-methods))
1541   (let ((xlist gnus-decode-encoded-word-methods-cache))
1542     (pop xlist)
1543     (while xlist
1544       (setq string (funcall (pop xlist) string))))
1545   string)
1546
1547 ;; Subject simplification.
1548
1549 (defun gnus-simplify-whitespace (str)
1550   "Remove excessive whitespace from STR."
1551   ;; Multiple spaces.
1552   (while (string-match "[ \t][ \t]+" str)
1553     (setq str (concat (substring str 0 (match-beginning 0))
1554                         " "
1555                         (substring str (match-end 0)))))
1556   ;; Leading spaces.
1557   (when (string-match "^[ \t]+" str)
1558     (setq str (substring str (match-end 0))))
1559   ;; Trailing spaces.
1560   (when (string-match "[ \t]+$" str)
1561     (setq str (substring str 0 (match-beginning 0))))
1562   str)
1563
1564 (defun gnus-simplify-all-whitespace (str)
1565   "Remove all whitespace from STR."
1566   (while (string-match "[ \t\n]+" str)
1567     (setq str (replace-match "" nil nil str)))
1568   str)
1569
1570 (defsubst gnus-simplify-subject-re (subject)
1571   "Remove \"Re:\" from subject lines."
1572   (if (string-match message-subject-re-regexp subject)
1573       (substring subject (match-end 0))
1574     subject))
1575
1576 (defun gnus-simplify-subject (subject &optional re-only)
1577   "Remove `Re:' and words in parentheses.
1578 If RE-ONLY is non-nil, strip leading `Re:'s only."
1579   (let ((case-fold-search t))           ;Ignore case.
1580     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1581     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1582       (setq subject (substring subject (match-end 0))))
1583     ;; Remove uninteresting prefixes.
1584     (when (and (not re-only)
1585                gnus-simplify-ignored-prefixes
1586                (string-match gnus-simplify-ignored-prefixes subject))
1587       (setq subject (substring subject (match-end 0))))
1588     ;; Remove words in parentheses from end.
1589     (unless re-only
1590       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1591         (setq subject (substring subject 0 (match-beginning 0)))))
1592     ;; Return subject string.
1593     subject))
1594
1595 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1596 ;; all whitespace.
1597 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1598   (goto-char (point-min))
1599   (while (re-search-forward regexp nil t)
1600     (replace-match (or newtext ""))))
1601
1602 (defun gnus-simplify-buffer-fuzzy ()
1603   "Simplify string in the buffer fuzzily.
1604 The string in the accessible portion of the current buffer is simplified.
1605 It is assumed to be a single-line subject.
1606 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1607 matter is removed.  Additional things can be deleted by setting
1608 `gnus-simplify-subject-fuzzy-regexp'."
1609   (let ((case-fold-search t)
1610         (modified-tick))
1611     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1612
1613     (while (not (eq modified-tick (buffer-modified-tick)))
1614       (setq modified-tick (buffer-modified-tick))
1615       (cond
1616        ((listp gnus-simplify-subject-fuzzy-regexp)
1617         (mapcar 'gnus-simplify-buffer-fuzzy-step
1618                 gnus-simplify-subject-fuzzy-regexp))
1619        (gnus-simplify-subject-fuzzy-regexp
1620         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1621       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1622       (gnus-simplify-buffer-fuzzy-step
1623        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1624       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1625
1626     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1627     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1628     (gnus-simplify-buffer-fuzzy-step " $")
1629     (gnus-simplify-buffer-fuzzy-step "^ +")))
1630
1631 (defun gnus-simplify-subject-fuzzy (subject)
1632   "Simplify a subject string fuzzily.
1633 See `gnus-simplify-buffer-fuzzy' for details."
1634   (save-excursion
1635     (gnus-set-work-buffer)
1636     (let ((case-fold-search t))
1637       ;; Remove uninteresting prefixes.
1638       (when (and gnus-simplify-ignored-prefixes
1639                  (string-match gnus-simplify-ignored-prefixes subject))
1640         (setq subject (substring subject (match-end 0))))
1641       (insert subject)
1642       (inline (gnus-simplify-buffer-fuzzy))
1643       (buffer-string))))
1644
1645 (defsubst gnus-simplify-subject-fully (subject)
1646   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1647   (cond
1648    (gnus-simplify-subject-functions
1649     (gnus-map-function gnus-simplify-subject-functions subject))
1650    ((null gnus-summary-gather-subject-limit)
1651     (gnus-simplify-subject-re subject))
1652    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1653     (gnus-simplify-subject-fuzzy subject))
1654    ((numberp gnus-summary-gather-subject-limit)
1655     (gnus-limit-string (gnus-simplify-subject-re subject)
1656                        gnus-summary-gather-subject-limit))
1657    (t
1658     subject)))
1659
1660 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1661   "Check whether two subjects are equal.
1662 If optional argument SIMPLE-FIRST is t, first argument is already
1663 simplified."
1664   (cond
1665    ((null simple-first)
1666     (equal (gnus-simplify-subject-fully s1)
1667            (gnus-simplify-subject-fully s2)))
1668    (t
1669     (equal s1
1670            (gnus-simplify-subject-fully s2)))))
1671
1672 (defun gnus-summary-bubble-group ()
1673   "Increase the score of the current group.
1674 This is a handy function to add to `gnus-summary-exit-hook' to
1675 increase the score of each group you read."
1676   (gnus-group-add-score gnus-newsgroup-name))
1677
1678 \f
1679 ;;;
1680 ;;; Gnus summary mode
1681 ;;;
1682
1683 (put 'gnus-summary-mode 'mode-class 'special)
1684
1685 (defvar gnus-article-commands-menu)
1686
1687 ;; Non-orthogonal keys
1688
1689 (gnus-define-keys gnus-summary-mode-map
1690   " " gnus-summary-next-page
1691   "\177" gnus-summary-prev-page
1692   [delete] gnus-summary-prev-page
1693   [backspace] gnus-summary-prev-page
1694   "\r" gnus-summary-scroll-up
1695   "\M-\r" gnus-summary-scroll-down
1696   "n" gnus-summary-next-unread-article
1697   "p" gnus-summary-prev-unread-article
1698   "N" gnus-summary-next-article
1699   "P" gnus-summary-prev-article
1700   "\M-\C-n" gnus-summary-next-same-subject
1701   "\M-\C-p" gnus-summary-prev-same-subject
1702   "\M-n" gnus-summary-next-unread-subject
1703   "\M-p" gnus-summary-prev-unread-subject
1704   "." gnus-summary-first-unread-article
1705   "," gnus-summary-best-unread-article
1706   "\M-s" gnus-summary-search-article-forward
1707   "\M-r" gnus-summary-search-article-backward
1708   "<" gnus-summary-beginning-of-article
1709   ">" gnus-summary-end-of-article
1710   "j" gnus-summary-goto-article
1711   "^" gnus-summary-refer-parent-article
1712   "\M-^" gnus-summary-refer-article
1713   "u" gnus-summary-tick-article-forward
1714   "!" gnus-summary-tick-article-forward
1715   "U" gnus-summary-tick-article-backward
1716   "d" gnus-summary-mark-as-read-forward
1717   "D" gnus-summary-mark-as-read-backward
1718   "E" gnus-summary-mark-as-expirable
1719   "\M-u" gnus-summary-clear-mark-forward
1720   "\M-U" gnus-summary-clear-mark-backward
1721   "k" gnus-summary-kill-same-subject-and-select
1722   "\C-k" gnus-summary-kill-same-subject
1723   "\M-\C-k" gnus-summary-kill-thread
1724   "\M-\C-l" gnus-summary-lower-thread
1725   "e" gnus-summary-edit-article
1726   "#" gnus-summary-mark-as-processable
1727   "\M-#" gnus-summary-unmark-as-processable
1728   "\M-\C-t" gnus-summary-toggle-threads
1729   "\M-\C-s" gnus-summary-show-thread
1730   "\M-\C-h" gnus-summary-hide-thread
1731   "\M-\C-f" gnus-summary-next-thread
1732   "\M-\C-b" gnus-summary-prev-thread
1733   [(meta down)] gnus-summary-next-thread
1734   [(meta up)] gnus-summary-prev-thread
1735   "\M-\C-u" gnus-summary-up-thread
1736   "\M-\C-d" gnus-summary-down-thread
1737   "&" gnus-summary-execute-command
1738   "c" gnus-summary-catchup-and-exit
1739   "\C-w" gnus-summary-mark-region-as-read
1740   "\C-t" gnus-summary-toggle-truncation
1741   "?" gnus-summary-mark-as-dormant
1742   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1743   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1744   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1745   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1746   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1747   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1748   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1749   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1750   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1751   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1752   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1753   "=" gnus-summary-expand-window
1754   "\C-x\C-s" gnus-summary-reselect-current-group
1755   "\M-g" gnus-summary-rescan-group
1756   "w" gnus-summary-stop-page-breaking
1757   "\C-c\C-r" gnus-summary-caesar-message
1758   "f" gnus-summary-followup
1759   "F" gnus-summary-followup-with-original
1760   "C" gnus-summary-cancel-article
1761   "r" gnus-summary-reply
1762   "R" gnus-summary-reply-with-original
1763   "\C-c\C-f" gnus-summary-mail-forward
1764   "o" gnus-summary-save-article
1765   "\C-o" gnus-summary-save-article-mail
1766   "|" gnus-summary-pipe-output
1767   "\M-k" gnus-summary-edit-local-kill
1768   "\M-K" gnus-summary-edit-global-kill
1769   ;; "V" gnus-version
1770   "\C-c\C-d" gnus-summary-describe-group
1771   "q" gnus-summary-exit
1772   "Q" gnus-summary-exit-no-update
1773   "\C-c\C-i" gnus-info-find-node
1774   gnus-mouse-2 gnus-mouse-pick-article
1775   [follow-link] mouse-face
1776   "m" gnus-summary-mail-other-window
1777   "a" gnus-summary-post-news
1778   "i" gnus-summary-news-other-window
1779   "x" gnus-summary-limit-to-unread
1780   "s" gnus-summary-isearch-article
1781   "t" gnus-summary-toggle-header
1782   "g" gnus-summary-show-article
1783   "l" gnus-summary-goto-last-article
1784   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1785   "\C-d" gnus-summary-enter-digest-group
1786   "\M-\C-d" gnus-summary-read-document
1787   "\M-\C-e" gnus-summary-edit-parameters
1788   "\M-\C-a" gnus-summary-customize-parameters
1789   "\C-c\C-b" gnus-bug
1790   "*" gnus-cache-enter-article
1791   "\M-*" gnus-cache-remove-article
1792   "\M-&" gnus-summary-universal-argument
1793   "\C-l" gnus-recenter
1794   "I" gnus-summary-increase-score
1795   "L" gnus-summary-lower-score
1796   "\M-i" gnus-symbolic-argument
1797   "h" gnus-summary-select-article-buffer
1798
1799   "b" gnus-article-view-part
1800   "\M-t" gnus-summary-toggle-display-buttonized
1801
1802   "V" gnus-summary-score-map
1803   "X" gnus-uu-extract-map
1804   "S" gnus-summary-send-map)
1805
1806 ;; Sort of orthogonal keymap
1807 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1808   "t" gnus-summary-tick-article-forward
1809   "!" gnus-summary-tick-article-forward
1810   "d" gnus-summary-mark-as-read-forward
1811   "r" gnus-summary-mark-as-read-forward
1812   "c" gnus-summary-clear-mark-forward
1813   " " gnus-summary-clear-mark-forward
1814   "e" gnus-summary-mark-as-expirable
1815   "x" gnus-summary-mark-as-expirable
1816   "?" gnus-summary-mark-as-dormant
1817   "b" gnus-summary-set-bookmark
1818   "B" gnus-summary-remove-bookmark
1819   "#" gnus-summary-mark-as-processable
1820   "\M-#" gnus-summary-unmark-as-processable
1821   "S" gnus-summary-limit-include-expunged
1822   "C" gnus-summary-catchup
1823   "H" gnus-summary-catchup-to-here
1824   "h" gnus-summary-catchup-from-here
1825   "\C-c" gnus-summary-catchup-all
1826   "k" gnus-summary-kill-same-subject-and-select
1827   "K" gnus-summary-kill-same-subject
1828   "P" gnus-uu-mark-map)
1829
1830 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1831   "c" gnus-summary-clear-above
1832   "u" gnus-summary-tick-above
1833   "m" gnus-summary-mark-above
1834   "k" gnus-summary-kill-below)
1835
1836 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1837   "/" gnus-summary-limit-to-subject
1838   "n" gnus-summary-limit-to-articles
1839   "w" gnus-summary-pop-limit
1840   "s" gnus-summary-limit-to-subject
1841   "a" gnus-summary-limit-to-author
1842   "u" gnus-summary-limit-to-unread
1843   "m" gnus-summary-limit-to-marks
1844   "M" gnus-summary-limit-exclude-marks
1845   "v" gnus-summary-limit-to-score
1846   "*" gnus-summary-limit-include-cached
1847   "D" gnus-summary-limit-include-dormant
1848   "T" gnus-summary-limit-include-thread
1849   "d" gnus-summary-limit-exclude-dormant
1850   "t" gnus-summary-limit-to-age
1851   "." gnus-summary-limit-to-unseen
1852   "x" gnus-summary-limit-to-extra
1853   "p" gnus-summary-limit-to-display-predicate
1854   "E" gnus-summary-limit-include-expunged
1855   "c" gnus-summary-limit-exclude-childless-dormant
1856   "C" gnus-summary-limit-mark-excluded-as-read
1857   "o" gnus-summary-insert-old-articles
1858   "N" gnus-summary-insert-new-articles
1859   "r" gnus-summary-limit-to-replied
1860   "R" gnus-summary-limit-to-recipient)
1861
1862 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1863   "n" gnus-summary-next-unread-article
1864   "p" gnus-summary-prev-unread-article
1865   "N" gnus-summary-next-article
1866   "P" gnus-summary-prev-article
1867   "\C-n" gnus-summary-next-same-subject
1868   "\C-p" gnus-summary-prev-same-subject
1869   "\M-n" gnus-summary-next-unread-subject
1870   "\M-p" gnus-summary-prev-unread-subject
1871   "f" gnus-summary-first-unread-article
1872   "b" gnus-summary-best-unread-article
1873   "j" gnus-summary-goto-article
1874   "g" gnus-summary-goto-subject
1875   "l" gnus-summary-goto-last-article
1876   "o" gnus-summary-pop-article)
1877
1878 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1879   "k" gnus-summary-kill-thread
1880   "l" gnus-summary-lower-thread
1881   "i" gnus-summary-raise-thread
1882   "T" gnus-summary-toggle-threads
1883   "t" gnus-summary-rethread-current
1884   "^" gnus-summary-reparent-thread
1885   "s" gnus-summary-show-thread
1886   "S" gnus-summary-show-all-threads
1887   "h" gnus-summary-hide-thread
1888   "H" gnus-summary-hide-all-threads
1889   "n" gnus-summary-next-thread
1890   "p" gnus-summary-prev-thread
1891   "u" gnus-summary-up-thread
1892   "o" gnus-summary-top-thread
1893   "d" gnus-summary-down-thread
1894   "#" gnus-uu-mark-thread
1895   "\M-#" gnus-uu-unmark-thread)
1896
1897 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1898   "g" gnus-summary-prepare
1899   "c" gnus-summary-insert-cached-articles
1900   "d" gnus-summary-insert-dormant-articles)
1901
1902 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1903   "c" gnus-summary-catchup-and-exit
1904   "C" gnus-summary-catchup-all-and-exit
1905   "E" gnus-summary-exit-no-update
1906   "Q" gnus-summary-exit
1907   "Z" gnus-summary-exit
1908   "n" gnus-summary-catchup-and-goto-next-group
1909   "R" gnus-summary-reselect-current-group
1910   "G" gnus-summary-rescan-group
1911   "N" gnus-summary-next-group
1912   "s" gnus-summary-save-newsrc
1913   "P" gnus-summary-prev-group)
1914
1915 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1916   " " gnus-summary-next-page
1917   "n" gnus-summary-next-page
1918   "\177" gnus-summary-prev-page
1919   [delete] gnus-summary-prev-page
1920   "p" gnus-summary-prev-page
1921   "\r" gnus-summary-scroll-up
1922   "\M-\r" gnus-summary-scroll-down
1923   "<" gnus-summary-beginning-of-article
1924   ">" gnus-summary-end-of-article
1925   "b" gnus-summary-beginning-of-article
1926   "e" gnus-summary-end-of-article
1927   "^" gnus-summary-refer-parent-article
1928   "r" gnus-summary-refer-parent-article
1929   "D" gnus-summary-enter-digest-group
1930   "R" gnus-summary-refer-references
1931   "T" gnus-summary-refer-thread
1932   "g" gnus-summary-show-article
1933   "s" gnus-summary-isearch-article
1934   "P" gnus-summary-print-article
1935   "M" gnus-mailing-list-insinuate
1936   "t" gnus-article-babel)
1937
1938 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1939   "b" gnus-article-add-buttons
1940   "B" gnus-article-add-buttons-to-head
1941   "o" gnus-article-treat-overstrike
1942   "e" gnus-article-emphasize
1943   "w" gnus-article-fill-cited-article
1944   "Q" gnus-article-fill-long-lines
1945   "C" gnus-article-capitalize-sentences
1946   "c" gnus-article-remove-cr
1947   "q" gnus-article-de-quoted-unreadable
1948   "6" gnus-article-de-base64-unreadable
1949   "Z" gnus-article-decode-HZ
1950   "A" gnus-article-treat-ansi-sequences
1951   "h" gnus-article-wash-html
1952   "u" gnus-article-unsplit-urls
1953   "s" gnus-summary-force-verify-and-decrypt
1954   "f" gnus-article-display-x-face
1955   "l" gnus-summary-stop-page-breaking
1956   "r" gnus-summary-caesar-message
1957   "m" gnus-summary-morse-message
1958   "t" gnus-summary-toggle-header
1959   "g" gnus-treat-smiley
1960   "v" gnus-summary-verbose-headers
1961   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1962   "p" gnus-article-verify-x-pgp-sig
1963   "d" gnus-article-treat-dumbquotes
1964   "i" gnus-summary-idna-message)
1965
1966 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1967   ;; mnemonic: deuglif*Y*
1968   "u" gnus-article-outlook-unwrap-lines
1969   "a" gnus-article-outlook-repair-attribution
1970   "c" gnus-article-outlook-rearrange-citation
1971   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1972
1973 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1974   "a" gnus-article-hide
1975   "h" gnus-article-hide-headers
1976   "b" gnus-article-hide-boring-headers
1977   "s" gnus-article-hide-signature
1978   "c" gnus-article-hide-citation
1979   "C" gnus-article-hide-citation-in-followups
1980   "l" gnus-article-hide-list-identifiers
1981   "B" gnus-article-strip-banner
1982   "P" gnus-article-hide-pem
1983   "\C-c" gnus-article-hide-citation-maybe)
1984
1985 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1986   "a" gnus-article-highlight
1987   "h" gnus-article-highlight-headers
1988   "c" gnus-article-highlight-citation
1989   "s" gnus-article-highlight-signature)
1990
1991 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1992   "f" gnus-article-treat-fold-headers
1993   "u" gnus-article-treat-unfold-headers
1994   "n" gnus-article-treat-fold-newsgroups)
1995
1996 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1997   "x" gnus-article-display-x-face
1998   "d" gnus-article-display-face
1999   "s" gnus-treat-smiley
2000   "D" gnus-article-remove-images
2001   "f" gnus-treat-from-picon
2002   "m" gnus-treat-mail-picon
2003   "n" gnus-treat-newsgroups-picon)
2004
2005 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2006   "w" gnus-article-decode-mime-words
2007   "c" gnus-article-decode-charset
2008   "v" gnus-mime-view-all-parts
2009   "b" gnus-article-view-part)
2010
2011 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2012   "z" gnus-article-date-ut
2013   "u" gnus-article-date-ut
2014   "l" gnus-article-date-local
2015   "p" gnus-article-date-english
2016   "e" gnus-article-date-lapsed
2017   "o" gnus-article-date-original
2018   "i" gnus-article-date-iso8601
2019   "s" gnus-article-date-user)
2020
2021 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2022   "t" gnus-article-remove-trailing-blank-lines
2023   "l" gnus-article-strip-leading-blank-lines
2024   "m" gnus-article-strip-multiple-blank-lines
2025   "a" gnus-article-strip-blank-lines
2026   "A" gnus-article-strip-all-blank-lines
2027   "s" gnus-article-strip-leading-space
2028   "e" gnus-article-strip-trailing-space
2029   "w" gnus-article-remove-leading-whitespace)
2030
2031 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2032   "v" gnus-version
2033   "f" gnus-summary-fetch-faq
2034   "d" gnus-summary-describe-group
2035   "h" gnus-summary-describe-briefly
2036   "i" gnus-info-find-node
2037   "c" gnus-group-fetch-charter
2038   "C" gnus-group-fetch-control)
2039
2040 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2041   "e" gnus-summary-expire-articles
2042   "\M-\C-e" gnus-summary-expire-articles-now
2043   "\177" gnus-summary-delete-article
2044   [delete] gnus-summary-delete-article
2045   [backspace] gnus-summary-delete-article
2046   "m" gnus-summary-move-article
2047   "r" gnus-summary-respool-article
2048   "w" gnus-summary-edit-article
2049   "c" gnus-summary-copy-article
2050   "B" gnus-summary-crosspost-article
2051   "q" gnus-summary-respool-query
2052   "t" gnus-summary-respool-trace
2053   "i" gnus-summary-import-article
2054   "I" gnus-summary-create-article
2055   "p" gnus-summary-article-posted-p)
2056
2057 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2058   "o" gnus-summary-save-article
2059   "m" gnus-summary-save-article-mail
2060   "F" gnus-summary-write-article-file
2061   "r" gnus-summary-save-article-rmail
2062   "f" gnus-summary-save-article-file
2063   "b" gnus-summary-save-article-body-file
2064   "h" gnus-summary-save-article-folder
2065   "v" gnus-summary-save-article-vm
2066   "p" gnus-summary-pipe-output
2067   "P" gnus-summary-muttprint
2068   "s" gnus-soup-add-article)
2069
2070 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2071   "b" gnus-summary-display-buttonized
2072   "m" gnus-summary-repair-multipart
2073   "v" gnus-article-view-part
2074   "o" gnus-article-save-part
2075   "O" gnus-article-save-part-and-strip
2076   "d" gnus-article-delete-part
2077   "j" gnus-article-jump-to-part
2078   "c" gnus-article-copy-part
2079   "C" gnus-article-view-part-as-charset
2080   "e" gnus-article-view-part-externally
2081   "E" gnus-article-encrypt-body
2082   "i" gnus-article-inline-part
2083   "|" gnus-article-pipe-part)
2084
2085 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2086   "p" gnus-summary-mark-as-processable
2087   "u" gnus-summary-unmark-as-processable
2088   "U" gnus-summary-unmark-all-processable
2089   "v" gnus-uu-mark-over
2090   "s" gnus-uu-mark-series
2091   "r" gnus-uu-mark-region
2092   "g" gnus-uu-unmark-region
2093   "R" gnus-uu-mark-by-regexp
2094   "G" gnus-uu-unmark-by-regexp
2095   "t" gnus-uu-mark-thread
2096   "T" gnus-uu-unmark-thread
2097   "a" gnus-uu-mark-all
2098   "b" gnus-uu-mark-buffer
2099   "S" gnus-uu-mark-sparse
2100   "k" gnus-summary-kill-process-mark
2101   "y" gnus-summary-yank-process-mark
2102   "w" gnus-summary-save-process-mark
2103   "i" gnus-uu-invert-processable)
2104
2105 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2106   ;;"x" gnus-uu-extract-any
2107   "m" gnus-summary-save-parts
2108   "u" gnus-uu-decode-uu
2109   "U" gnus-uu-decode-uu-and-save
2110   "s" gnus-uu-decode-unshar
2111   "S" gnus-uu-decode-unshar-and-save
2112   "o" gnus-uu-decode-save
2113   "O" gnus-uu-decode-save
2114   "b" gnus-uu-decode-binhex
2115   "B" gnus-uu-decode-binhex
2116   "p" gnus-uu-decode-postscript
2117   "P" gnus-uu-decode-postscript-and-save)
2118
2119 (gnus-define-keys
2120     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2121   "u" gnus-uu-decode-uu-view
2122   "U" gnus-uu-decode-uu-and-save-view
2123   "s" gnus-uu-decode-unshar-view
2124   "S" gnus-uu-decode-unshar-and-save-view
2125   "o" gnus-uu-decode-save-view
2126   "O" gnus-uu-decode-save-view
2127   "b" gnus-uu-decode-binhex-view
2128   "B" gnus-uu-decode-binhex-view
2129   "p" gnus-uu-decode-postscript-view
2130   "P" gnus-uu-decode-postscript-and-save-view)
2131
2132 (defvar gnus-article-post-menu nil)
2133
2134 (defconst gnus-summary-menu-maxlen 20)
2135
2136 (defun gnus-summary-menu-split (menu)
2137   ;; If we have lots of elements, divide them into groups of 20
2138   ;; and make a pane (or submenu) for each one.
2139   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2140       (let ((menu menu) sublists next
2141             (i 1))
2142         (while menu
2143           ;; Pull off the next gnus-summary-menu-maxlen elements
2144           ;; and make them the next element of sublist.
2145           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2146           (if next
2147               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2148                       nil))
2149           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2150                                              (aref (car (last menu)) 0)) menu)
2151                                sublists))
2152           (setq i (1+ i))
2153           (setq menu next))
2154         (nreverse sublists))
2155     ;; Few elements--put them all in one pane.
2156     menu))
2157
2158 (defun gnus-summary-make-menu-bar ()
2159   (gnus-turn-off-edit-menu 'summary)
2160
2161   (unless (boundp 'gnus-summary-misc-menu)
2162
2163     (easy-menu-define
2164       gnus-summary-kill-menu gnus-summary-mode-map ""
2165       (cons
2166        "Score"
2167        (nconc
2168         (list
2169          ["Customize" gnus-score-customize t])
2170         (gnus-make-score-map 'increase)
2171         (gnus-make-score-map 'lower)
2172         '(("Mark"
2173            ["Kill below" gnus-summary-kill-below t]
2174            ["Mark above" gnus-summary-mark-above t]
2175            ["Tick above" gnus-summary-tick-above t]
2176            ["Clear above" gnus-summary-clear-above t])
2177           ["Current score" gnus-summary-current-score t]
2178           ["Set score" gnus-summary-set-score t]
2179           ["Switch current score file..." gnus-score-change-score-file t]
2180           ["Set mark below..." gnus-score-set-mark-below t]
2181           ["Set expunge below..." gnus-score-set-expunge-below t]
2182           ["Edit current score file" gnus-score-edit-current-scores t]
2183           ["Edit score file" gnus-score-edit-file t]
2184           ["Trace score" gnus-score-find-trace t]
2185           ["Find words" gnus-score-find-favourite-words t]
2186           ["Rescore buffer" gnus-summary-rescore t]
2187           ["Increase score..." gnus-summary-increase-score t]
2188           ["Lower score..." gnus-summary-lower-score t]))))
2189
2190     ;; Define both the Article menu in the summary buffer and the
2191     ;; equivalent Commands menu in the article buffer here for
2192     ;; consistency.
2193     (let ((innards
2194            `(("Hide"
2195               ["All" gnus-article-hide t]
2196               ["Headers" gnus-article-hide-headers t]
2197               ["Signature" gnus-article-hide-signature t]
2198               ["Citation" gnus-article-hide-citation t]
2199               ["List identifiers" gnus-article-hide-list-identifiers t]
2200               ["Banner" gnus-article-strip-banner t]
2201               ["Boring headers" gnus-article-hide-boring-headers t])
2202              ("Highlight"
2203               ["All" gnus-article-highlight t]
2204               ["Headers" gnus-article-highlight-headers t]
2205               ["Signature" gnus-article-highlight-signature t]
2206               ["Citation" gnus-article-highlight-citation t])
2207              ("MIME"
2208               ["Words" gnus-article-decode-mime-words t]
2209               ["Charset" gnus-article-decode-charset t]
2210               ["QP" gnus-article-de-quoted-unreadable t]
2211               ["Base64" gnus-article-de-base64-unreadable t]
2212               ["View MIME buttons" gnus-summary-display-buttonized t]
2213               ["View all" gnus-mime-view-all-parts t]
2214               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2215               ["Encrypt body" gnus-article-encrypt-body
2216                :active (not (gnus-group-read-only-p))
2217                ,@(if (featurep 'xemacs) nil
2218                    '(:help "Encrypt the message body on disk"))]
2219               ["Extract all parts..." gnus-summary-save-parts t]
2220               ("Multipart"
2221                ["Repair multipart" gnus-summary-repair-multipart t]
2222                ["Pipe part..." gnus-article-pipe-part t]
2223                ["Inline part" gnus-article-inline-part t]
2224                ["Encrypt body" gnus-article-encrypt-body
2225                 :active (not (gnus-group-read-only-p))
2226                ,@(if (featurep 'xemacs) nil
2227                    '(:help "Encrypt the message body on disk"))]
2228                ["View part externally" gnus-article-view-part-externally t]
2229                ["View part with charset..." gnus-article-view-part-as-charset t]
2230                ["Copy part" gnus-article-copy-part t]
2231                ["Save part..." gnus-article-save-part t]
2232                ["View part" gnus-article-view-part t]))
2233              ("Date"
2234               ["Local" gnus-article-date-local t]
2235               ["ISO8601" gnus-article-date-iso8601 t]
2236               ["UT" gnus-article-date-ut t]
2237               ["Original" gnus-article-date-original t]
2238               ["Lapsed" gnus-article-date-lapsed t]
2239               ["User-defined" gnus-article-date-user t])
2240              ("Display"
2241               ["Remove images" gnus-article-remove-images t]
2242               ["Toggle smiley" gnus-treat-smiley t]
2243               ["Show X-Face" gnus-article-display-x-face t]
2244               ["Show picons in From" gnus-treat-from-picon t]
2245               ["Show picons in mail headers" gnus-treat-mail-picon t]
2246               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2247               ("View as different encoding"
2248                ,@(gnus-summary-menu-split
2249                   (mapcar
2250                    (lambda (cs)
2251                      ;; Since easymenu under Emacs doesn't allow
2252                      ;; lambda forms for menu commands, we should
2253                      ;; provide intern'ed function symbols.
2254                      (let ((command (intern (format "\
2255 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2256                        (fset command
2257                              `(lambda ()
2258                                 (interactive)
2259                                 (let ((gnus-summary-show-article-charset-alist
2260                                        '((1 . ,cs))))
2261                                   (gnus-summary-show-article 1))))
2262                        `[,(symbol-name cs) ,command t]))
2263                    (sort (if (fboundp 'coding-system-list)
2264                              (coding-system-list)
2265                            (mapcar 'car mm-mime-mule-charset-alist))
2266                          'string<)))))
2267              ("Washing"
2268               ("Remove Blanks"
2269                ["Leading" gnus-article-strip-leading-blank-lines t]
2270                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2271                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2272                ["All of the above" gnus-article-strip-blank-lines t]
2273                ["All" gnus-article-strip-all-blank-lines t]
2274                ["Leading space" gnus-article-strip-leading-space t]
2275                ["Trailing space" gnus-article-strip-trailing-space t]
2276                ["Leading space in headers"
2277                 gnus-article-remove-leading-whitespace t])
2278               ["Overstrike" gnus-article-treat-overstrike t]
2279               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2280               ["Emphasis" gnus-article-emphasize t]
2281               ["Word wrap" gnus-article-fill-cited-article t]
2282               ["Fill long lines" gnus-article-fill-long-lines t]
2283               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2284               ["Remove CR" gnus-article-remove-cr t]
2285               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2286               ["Base64" gnus-article-de-base64-unreadable t]
2287               ["Rot 13" gnus-summary-caesar-message
2288                ,@(if (featurep 'xemacs) '(t)
2289                    '(:help "\"Caesar rotate\" article by 13"))]
2290               ["De-IDNA" gnus-summary-idna-message t]
2291               ["Morse decode" gnus-summary-morse-message t]
2292               ["Unix pipe..." gnus-summary-pipe-message t]
2293               ["Add buttons" gnus-article-add-buttons t]
2294               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2295               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2296               ["Verbose header" gnus-summary-verbose-headers t]
2297               ["Toggle header" gnus-summary-toggle-header t]
2298               ["Unfold headers" gnus-article-treat-unfold-headers t]
2299               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2300               ["Html" gnus-article-wash-html t]
2301               ["Unsplit URLs" gnus-article-unsplit-urls t]
2302               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2303               ["Decode HZ" gnus-article-decode-HZ t]
2304               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2305               ("(Outlook) Deuglify"
2306                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2307                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2308                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2309                ["Full (Outlook) deuglify"
2310                 gnus-article-outlook-deuglify-article t])
2311               )
2312              ("Output"
2313               ["Save in default format..." gnus-summary-save-article
2314                ,@(if (featurep 'xemacs) '(t)
2315                    '(:help "Save article using default method"))]
2316               ["Save in file..." gnus-summary-save-article-file
2317                ,@(if (featurep 'xemacs) '(t)
2318                    '(:help "Save article in file"))]
2319               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2320               ["Save in MH folder..." gnus-summary-save-article-folder t]
2321               ["Save in VM folder..." gnus-summary-save-article-vm t]
2322               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2323               ["Save body in file..." gnus-summary-save-article-body-file t]
2324               ["Pipe through a filter..." gnus-summary-pipe-output t]
2325               ["Add to SOUP packet" gnus-soup-add-article t]
2326               ["Print with Muttprint..." gnus-summary-muttprint t]
2327               ["Print" gnus-summary-print-article
2328                ,@(if (featurep 'xemacs) '(t)
2329                    '(:help "Generate and print a PostScript image"))])
2330              ("Copy, move,... (Backend)"
2331               ,@(if (featurep 'xemacs) nil
2332                   '(:help "Copying, moving, expiring articles..."))
2333               ["Respool article..." gnus-summary-respool-article t]
2334               ["Move article..." gnus-summary-move-article
2335                (gnus-check-backend-function
2336                 'request-move-article gnus-newsgroup-name)]
2337               ["Copy article..." gnus-summary-copy-article t]
2338               ["Crosspost article..." gnus-summary-crosspost-article
2339                (gnus-check-backend-function
2340                 'request-replace-article gnus-newsgroup-name)]
2341               ["Import file..." gnus-summary-import-article
2342                (gnus-check-backend-function
2343                 'request-accept-article gnus-newsgroup-name)]
2344               ["Create article..." gnus-summary-create-article
2345                (gnus-check-backend-function
2346                 'request-accept-article gnus-newsgroup-name)]
2347               ["Check if posted" gnus-summary-article-posted-p t]
2348               ["Edit article" gnus-summary-edit-article
2349                (not (gnus-group-read-only-p))]
2350               ["Delete article" gnus-summary-delete-article
2351                (gnus-check-backend-function
2352                 'request-expire-articles gnus-newsgroup-name)]
2353               ["Query respool" gnus-summary-respool-query t]
2354               ["Trace respool" gnus-summary-respool-trace t]
2355               ["Delete expirable articles" gnus-summary-expire-articles-now
2356                (gnus-check-backend-function
2357                 'request-expire-articles gnus-newsgroup-name)])
2358              ("Extract"
2359               ["Uudecode" gnus-uu-decode-uu
2360                ,@(if (featurep 'xemacs) '(t)
2361                    '(:help "Decode uuencoded article(s)"))]
2362               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2363               ["Unshar" gnus-uu-decode-unshar t]
2364               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2365               ["Save" gnus-uu-decode-save t]
2366               ["Binhex" gnus-uu-decode-binhex t]
2367               ["Postscript" gnus-uu-decode-postscript t]
2368               ["All MIME parts" gnus-summary-save-parts t])
2369              ("Cache"
2370               ["Enter article" gnus-cache-enter-article t]
2371               ["Remove article" gnus-cache-remove-article t])
2372              ["Translate" gnus-article-babel t]
2373              ["Select article buffer" gnus-summary-select-article-buffer t]
2374              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2375              ["Isearch article..." gnus-summary-isearch-article t]
2376              ["Beginning of the article" gnus-summary-beginning-of-article t]
2377              ["End of the article" gnus-summary-end-of-article t]
2378              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2379              ["Fetch referenced articles" gnus-summary-refer-references t]
2380              ["Fetch current thread" gnus-summary-refer-thread t]
2381              ["Fetch article with id..." gnus-summary-refer-article t]
2382              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2383              ["Redisplay" gnus-summary-show-article t]
2384              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2385       (easy-menu-define
2386         gnus-summary-article-menu gnus-summary-mode-map ""
2387         (cons "Article" innards))
2388
2389       (if (not (keymapp gnus-summary-article-menu))
2390           (easy-menu-define
2391             gnus-article-commands-menu gnus-article-mode-map ""
2392             (cons "Commands" innards))
2393         ;; in Emacs, don't share menu.
2394         (setq gnus-article-commands-menu
2395               (copy-keymap gnus-summary-article-menu))
2396         (define-key gnus-article-mode-map [menu-bar commands]
2397           (cons "Commands" gnus-article-commands-menu))))
2398
2399     (easy-menu-define
2400       gnus-summary-thread-menu gnus-summary-mode-map ""
2401       '("Threads"
2402         ["Find all messages in thread" gnus-summary-refer-thread t]
2403         ["Toggle threading" gnus-summary-toggle-threads t]
2404         ["Hide threads" gnus-summary-hide-all-threads t]
2405         ["Show threads" gnus-summary-show-all-threads t]
2406         ["Hide thread" gnus-summary-hide-thread t]
2407         ["Show thread" gnus-summary-show-thread t]
2408         ["Go to next thread" gnus-summary-next-thread t]
2409         ["Go to previous thread" gnus-summary-prev-thread t]
2410         ["Go down thread" gnus-summary-down-thread t]
2411         ["Go up thread" gnus-summary-up-thread t]
2412         ["Top of thread" gnus-summary-top-thread t]
2413         ["Mark thread as read" gnus-summary-kill-thread t]
2414         ["Lower thread score" gnus-summary-lower-thread t]
2415         ["Raise thread score" gnus-summary-raise-thread t]
2416         ["Rethread current" gnus-summary-rethread-current t]))
2417
2418     (easy-menu-define
2419       gnus-summary-post-menu gnus-summary-mode-map ""
2420       `("Post"
2421         ["Send a message (mail or news)" gnus-summary-post-news
2422          ,@(if (featurep 'xemacs) '(t)
2423              '(:help "Compose a new message (mail or news)"))]
2424         ["Followup" gnus-summary-followup
2425          ,@(if (featurep 'xemacs) '(t)
2426              '(:help "Post followup to this article"))]
2427         ["Followup and yank" gnus-summary-followup-with-original
2428          ,@(if (featurep 'xemacs) '(t)
2429              '(:help "Post followup to this article, quoting its contents"))]
2430         ["Supersede article" gnus-summary-supersede-article t]
2431         ["Cancel article" gnus-summary-cancel-article
2432          ,@(if (featurep 'xemacs) '(t)
2433              '(:help "Cancel an article you posted"))]
2434         ["Reply" gnus-summary-reply t]
2435         ["Reply and yank" gnus-summary-reply-with-original t]
2436         ["Wide reply" gnus-summary-wide-reply t]
2437         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2438          ,@(if (featurep 'xemacs) '(t)
2439              '(:help "Mail a reply, quoting this article"))]
2440         ["Very wide reply" gnus-summary-very-wide-reply t]
2441         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2442          ,@(if (featurep 'xemacs) '(t)
2443              '(:help "Mail a very wide reply, quoting this article"))]
2444         ["Mail forward" gnus-summary-mail-forward t]
2445         ["Post forward" gnus-summary-post-forward t]
2446         ["Digest and mail" gnus-uu-digest-mail-forward t]
2447         ["Digest and post" gnus-uu-digest-post-forward t]
2448         ["Resend message" gnus-summary-resend-message t]
2449         ["Resend message edit" gnus-summary-resend-message-edit t]
2450         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2451         ["Send a mail" gnus-summary-mail-other-window t]
2452         ["Create a local message" gnus-summary-news-other-window t]
2453         ["Uuencode and post" gnus-uu-post-news
2454          ,@(if (featurep 'xemacs) '(t)
2455              '(:help "Post a uuencoded article"))]
2456         ["Followup via news" gnus-summary-followup-to-mail t]
2457         ["Followup via news and yank"
2458          gnus-summary-followup-to-mail-with-original t]
2459         ;;("Draft"
2460         ;;["Send" gnus-summary-send-draft t]
2461         ;;["Send bounced" gnus-resend-bounced-mail t])
2462         ))
2463
2464     (cond
2465      ((not (keymapp gnus-summary-post-menu))
2466       (setq gnus-article-post-menu gnus-summary-post-menu))
2467      ((not gnus-article-post-menu)
2468       ;; Don't share post menu.
2469       (setq gnus-article-post-menu
2470             (copy-keymap gnus-summary-post-menu))))
2471     (define-key gnus-article-mode-map [menu-bar post]
2472       (cons "Post" gnus-article-post-menu))
2473
2474     (easy-menu-define
2475       gnus-summary-misc-menu gnus-summary-mode-map ""
2476       `("Gnus"
2477         ("Mark Read"
2478          ["Mark as read" gnus-summary-mark-as-read-forward t]
2479          ["Mark same subject and select"
2480           gnus-summary-kill-same-subject-and-select t]
2481          ["Mark same subject" gnus-summary-kill-same-subject t]
2482          ["Catchup" gnus-summary-catchup
2483           ,@(if (featurep 'xemacs) '(t)
2484               '(:help "Mark unread articles in this group as read"))]
2485          ["Catchup all" gnus-summary-catchup-all t]
2486          ["Catchup to here" gnus-summary-catchup-to-here t]
2487          ["Catchup from here" gnus-summary-catchup-from-here t]
2488          ["Catchup region" gnus-summary-mark-region-as-read
2489           (gnus-mark-active-p)]
2490          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2491         ("Mark Various"
2492          ["Tick" gnus-summary-tick-article-forward t]
2493          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2494          ["Remove marks" gnus-summary-clear-mark-forward t]
2495          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2496          ["Set bookmark" gnus-summary-set-bookmark t]
2497          ["Remove bookmark" gnus-summary-remove-bookmark t])
2498         ("Limit to"
2499          ["Marks..." gnus-summary-limit-to-marks t]
2500          ["Subject..." gnus-summary-limit-to-subject t]
2501          ["Author..." gnus-summary-limit-to-author t]
2502          ["Recipient..." gnus-summary-limit-to-recipient t]
2503          ["Age..." gnus-summary-limit-to-age t]
2504          ["Extra..." gnus-summary-limit-to-extra t]
2505          ["Score..." gnus-summary-limit-to-score t]
2506          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2507          ["Unread" gnus-summary-limit-to-unread t]
2508          ["Unseen" gnus-summary-limit-to-unseen t]
2509          ["Replied" gnus-summary-limit-to-replied t]
2510          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2511          ["Next articles" gnus-summary-limit-to-articles t]
2512          ["Pop limit" gnus-summary-pop-limit t]
2513          ["Show dormant" gnus-summary-limit-include-dormant t]
2514          ["Hide childless dormant"
2515           gnus-summary-limit-exclude-childless-dormant t]
2516          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2517          ["Hide marked" gnus-summary-limit-exclude-marks t]
2518          ["Show expunged" gnus-summary-limit-include-expunged t])
2519         ("Process Mark"
2520          ["Set mark" gnus-summary-mark-as-processable t]
2521          ["Remove mark" gnus-summary-unmark-as-processable t]
2522          ["Remove all marks" gnus-summary-unmark-all-processable t]
2523          ["Invert marks" gnus-uu-invert-processable t]
2524          ["Mark above" gnus-uu-mark-over t]
2525          ["Mark series" gnus-uu-mark-series t]
2526          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2527          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2528          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2529          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2530          ["Mark all" gnus-uu-mark-all t]
2531          ["Mark buffer" gnus-uu-mark-buffer t]
2532          ["Mark sparse" gnus-uu-mark-sparse t]
2533          ["Mark thread" gnus-uu-mark-thread t]
2534          ["Unmark thread" gnus-uu-unmark-thread t]
2535          ("Process Mark Sets"
2536           ["Kill" gnus-summary-kill-process-mark t]
2537           ["Yank" gnus-summary-yank-process-mark
2538            gnus-newsgroup-process-stack]
2539           ["Save" gnus-summary-save-process-mark t]
2540           ["Run command on marked..." gnus-summary-universal-argument t]))
2541         ("Scroll article"
2542          ["Page forward" gnus-summary-next-page
2543           ,@(if (featurep 'xemacs) '(t)
2544               '(:help "Show next page of article"))]
2545          ["Page backward" gnus-summary-prev-page
2546           ,@(if (featurep 'xemacs) '(t)
2547               '(:help "Show previous page of article"))]
2548          ["Line forward" gnus-summary-scroll-up t])
2549         ("Move"
2550          ["Next unread article" gnus-summary-next-unread-article t]
2551          ["Previous unread article" gnus-summary-prev-unread-article t]
2552          ["Next article" gnus-summary-next-article t]
2553          ["Previous article" gnus-summary-prev-article t]
2554          ["Next unread subject" gnus-summary-next-unread-subject t]
2555          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2556          ["Next article same subject" gnus-summary-next-same-subject t]
2557          ["Previous article same subject" gnus-summary-prev-same-subject t]
2558          ["First unread article" gnus-summary-first-unread-article t]
2559          ["Best unread article" gnus-summary-best-unread-article t]
2560          ["Go to subject number..." gnus-summary-goto-subject t]
2561          ["Go to article number..." gnus-summary-goto-article t]
2562          ["Go to the last article" gnus-summary-goto-last-article t]
2563          ["Pop article off history" gnus-summary-pop-article t])
2564         ("Sort"
2565          ["Sort by number" gnus-summary-sort-by-number t]
2566          ["Sort by author" gnus-summary-sort-by-author t]
2567          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2568          ["Sort by subject" gnus-summary-sort-by-subject t]
2569          ["Sort by date" gnus-summary-sort-by-date t]
2570          ["Sort by score" gnus-summary-sort-by-score t]
2571          ["Sort by lines" gnus-summary-sort-by-lines t]
2572          ["Sort by characters" gnus-summary-sort-by-chars t]
2573          ["Randomize" gnus-summary-sort-by-random t]
2574          ["Original sort" gnus-summary-sort-by-original t])
2575         ("Help"
2576          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2577          ["Describe group" gnus-summary-describe-group t]
2578          ["Fetch charter" gnus-group-fetch-charter
2579           ,@(if (featurep 'xemacs) nil
2580               '(:help "Display the charter of the current group"))]
2581          ["Fetch control message" gnus-group-fetch-control
2582           ,@(if (featurep 'xemacs) nil
2583               '(:help "Display the archived control message for the current group"))]
2584          ["Read manual" gnus-info-find-node t])
2585         ("Modes"
2586          ["Pick and read" gnus-pick-mode t]
2587          ["Binary" gnus-binary-mode t])
2588         ("Regeneration"
2589          ["Regenerate" gnus-summary-prepare t]
2590          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2591          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2592          ["Toggle threading" gnus-summary-toggle-threads t])
2593         ["See old articles" gnus-summary-insert-old-articles t]
2594         ["See new articles" gnus-summary-insert-new-articles t]
2595         ["Filter articles..." gnus-summary-execute-command t]
2596         ["Run command on articles..." gnus-summary-universal-argument t]
2597         ["Search articles forward..." gnus-summary-search-article-forward t]
2598         ["Search articles backward..." gnus-summary-search-article-backward t]
2599         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2600         ["Expand window" gnus-summary-expand-window t]
2601         ["Expire expirable articles" gnus-summary-expire-articles
2602          (gnus-check-backend-function
2603           'request-expire-articles gnus-newsgroup-name)]
2604         ["Edit local kill file" gnus-summary-edit-local-kill t]
2605         ["Edit main kill file" gnus-summary-edit-global-kill t]
2606         ["Edit group parameters" gnus-summary-edit-parameters t]
2607         ["Customize group parameters" gnus-summary-customize-parameters t]
2608         ["Send a bug report" gnus-bug t]
2609         ("Exit"
2610          ["Catchup and exit" gnus-summary-catchup-and-exit
2611           ,@(if (featurep 'xemacs) '(t)
2612               '(:help "Mark unread articles in this group as read, then exit"))]
2613          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2614          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2615          ["Exit group" gnus-summary-exit
2616           ,@(if (featurep 'xemacs) '(t)
2617               '(:help "Exit current group, return to group selection mode"))]
2618          ["Exit group without updating" gnus-summary-exit-no-update t]
2619          ["Exit and goto next group" gnus-summary-next-group t]
2620          ["Exit and goto prev group" gnus-summary-prev-group t]
2621          ["Reselect group" gnus-summary-reselect-current-group t]
2622          ["Rescan group" gnus-summary-rescan-group t]
2623          ["Update dribble" gnus-summary-save-newsrc t])))
2624
2625     (gnus-run-hooks 'gnus-summary-menu-hook)))
2626
2627 (defvar gnus-summary-tool-bar-map nil)
2628
2629 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2630 (defun gnus-summary-make-tool-bar ()
2631   (if (and (fboundp 'tool-bar-add-item-from-menu)
2632            (default-value 'tool-bar-mode)
2633            (not gnus-summary-tool-bar-map))
2634       (setq gnus-summary-tool-bar-map
2635             (let ((tool-bar-map (make-sparse-keymap))
2636                   (load-path (mm-image-load-path)))
2637               (tool-bar-add-item-from-menu
2638                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2639               (tool-bar-add-item-from-menu
2640                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2641               (tool-bar-add-item-from-menu
2642                'gnus-summary-post-news "post" gnus-summary-mode-map)
2643               (tool-bar-add-item-from-menu
2644                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2645               (tool-bar-add-item-from-menu
2646                'gnus-summary-followup "followup" gnus-summary-mode-map)
2647               (tool-bar-add-item-from-menu
2648                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2649               (tool-bar-add-item-from-menu
2650                'gnus-summary-reply "reply" gnus-summary-mode-map)
2651               (tool-bar-add-item-from-menu
2652                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2653               (tool-bar-add-item-from-menu
2654                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2655               (tool-bar-add-item-from-menu
2656                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2657               (tool-bar-add-item-from-menu
2658                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2659               (tool-bar-add-item-from-menu
2660                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2661               (tool-bar-add-item-from-menu
2662                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2663               (tool-bar-add-item-from-menu
2664                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2665               (tool-bar-add-item-from-menu
2666                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2667               (tool-bar-add-item-from-menu
2668                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2669               tool-bar-map)))
2670   (if gnus-summary-tool-bar-map
2671       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2672
2673 (defun gnus-score-set-default (var value)
2674   "A version of set that updates the GNU Emacs menu-bar."
2675   (set var value)
2676   ;; It is the message that forces the active status to be updated.
2677   (message ""))
2678
2679 (defun gnus-make-score-map (type)
2680   "Make a summary score map of type TYPE."
2681   (if t
2682       nil
2683     (let ((headers '(("author" "from" string)
2684                      ("subject" "subject" string)
2685                      ("article body" "body" string)
2686                      ("article head" "head" string)
2687                      ("xref" "xref" string)
2688                      ("extra header" "extra" string)
2689                      ("lines" "lines" number)
2690                      ("followups to author" "followup" string)))
2691           (types '((number ("less than" <)
2692                            ("greater than" >)
2693                            ("equal" =))
2694                    (string ("substring" s)
2695                            ("exact string" e)
2696                            ("fuzzy string" f)
2697                            ("regexp" r))))
2698           (perms '(("temporary" (current-time-string))
2699                    ("permanent" nil)
2700                    ("immediate" now)))
2701           header)
2702       (list
2703        (apply
2704         'nconc
2705         (list
2706          (if (eq type 'lower)
2707              "Lower score"
2708            "Increase score"))
2709         (let (outh)
2710           (while headers
2711             (setq header (car headers))
2712             (setq outh
2713                   (cons
2714                    (apply
2715                     'nconc
2716                     (list (car header))
2717                     (let ((ts (cdr (assoc (nth 2 header) types)))
2718                           outt)
2719                       (while ts
2720                         (setq outt
2721                               (cons
2722                                (apply
2723                                 'nconc
2724                                 (list (caar ts))
2725                                 (let ((ps perms)
2726                                       outp)
2727                                   (while ps
2728                                     (setq outp
2729                                           (cons
2730                                            (vector
2731                                             (caar ps)
2732                                             (list
2733                                              'gnus-summary-score-entry
2734                                              (nth 1 header)
2735                                              (if (or (string= (nth 1 header)
2736                                                               "head")
2737                                                      (string= (nth 1 header)
2738                                                               "body"))
2739                                                  ""
2740                                                (list 'gnus-summary-header
2741                                                      (nth 1 header)))
2742                                              (list 'quote (nth 1 (car ts)))
2743                                              (list 'gnus-score-delta-default
2744                                                    nil)
2745                                              (nth 1 (car ps))
2746                                              t)
2747                                             t)
2748                                            outp))
2749                                     (setq ps (cdr ps)))
2750                                   (list (nreverse outp))))
2751                                outt))
2752                         (setq ts (cdr ts)))
2753                       (list (nreverse outt))))
2754                    outh))
2755             (setq headers (cdr headers)))
2756           (list (nreverse outh))))))))
2757
2758 \f
2759
2760 (defun gnus-summary-mode (&optional group)
2761   "Major mode for reading articles.
2762
2763 All normal editing commands are switched off.
2764 \\<gnus-summary-mode-map>
2765 Each line in this buffer represents one article.  To read an
2766 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2767 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2768 respectively.
2769
2770 You can also post articles and send mail from this buffer.  To
2771 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2772 of an article, type `\\[gnus-summary-reply]'.
2773
2774 There are approx. one gazillion commands you can execute in this
2775 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2776
2777 The following commands are available:
2778
2779 \\{gnus-summary-mode-map}"
2780   (interactive)
2781   (kill-all-local-variables)
2782   (when (gnus-visual-p 'summary-menu 'menu)
2783     (gnus-summary-make-menu-bar)
2784     (gnus-summary-make-tool-bar))
2785   (gnus-summary-make-local-variables)
2786   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2787     (gnus-summary-make-local-variables))
2788   (gnus-make-thread-indent-array)
2789   (gnus-simplify-mode-line)
2790   (setq major-mode 'gnus-summary-mode)
2791   (setq mode-name "Summary")
2792   (make-local-variable 'minor-mode-alist)
2793   (use-local-map gnus-summary-mode-map)
2794   (buffer-disable-undo)
2795   (setq buffer-read-only t              ;Disable modification
2796         show-trailing-whitespace nil)
2797   (setq truncate-lines t)
2798   (setq selective-display t)
2799   (setq selective-display-ellipses t)   ;Display `...'
2800   (gnus-summary-set-display-table)
2801   (gnus-set-default-directory)
2802   (setq gnus-newsgroup-name group)
2803   (make-local-variable 'gnus-summary-line-format)
2804   (make-local-variable 'gnus-summary-line-format-spec)
2805   (make-local-variable 'gnus-summary-dummy-line-format)
2806   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2807   (make-local-variable 'gnus-summary-mark-positions)
2808   (gnus-make-local-hook 'pre-command-hook)
2809   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2810   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
2811   (turn-on-gnus-mailing-list-mode)
2812   (mm-enable-multibyte)
2813   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2814   (gnus-update-summary-mark-positions))
2815
2816 (defun gnus-summary-make-local-variables ()
2817   "Make all the local summary buffer variables."
2818   (let (global)
2819     (dolist (local gnus-summary-local-variables)
2820       (if (consp local)
2821           (progn
2822             (if (eq (cdr local) 'global)
2823                 ;; Copy the global value of the variable.
2824                 (setq global (symbol-value (car local)))
2825               ;; Use the value from the list.
2826               (setq global (eval (cdr local))))
2827             (set (make-local-variable (car local)) global))
2828         ;; Simple nil-valued local variable.
2829         (set (make-local-variable local) nil)))))
2830
2831 (defun gnus-summary-clear-local-variables ()
2832   (let ((locals gnus-summary-local-variables))
2833     (while locals
2834       (if (consp (car locals))
2835           (and (symbolp (caar locals))
2836                (set (caar locals) nil))
2837         (and (symbolp (car locals))
2838              (set (car locals) nil)))
2839       (setq locals (cdr locals)))))
2840
2841 ;; Summary data functions.
2842
2843 (defmacro gnus-data-number (data)
2844   `(car ,data))
2845
2846 (defmacro gnus-data-set-number (data number)
2847   `(setcar ,data ,number))
2848
2849 (defmacro gnus-data-mark (data)
2850   `(nth 1 ,data))
2851
2852 (defmacro gnus-data-set-mark (data mark)
2853   `(setcar (nthcdr 1 ,data) ,mark))
2854
2855 (defmacro gnus-data-pos (data)
2856   `(nth 2 ,data))
2857
2858 (defmacro gnus-data-set-pos (data pos)
2859   `(setcar (nthcdr 2 ,data) ,pos))
2860
2861 (defmacro gnus-data-header (data)
2862   `(nth 3 ,data))
2863
2864 (defmacro gnus-data-set-header (data header)
2865   `(setf (nth 3 ,data) ,header))
2866
2867 (defmacro gnus-data-level (data)
2868   `(nth 4 ,data))
2869
2870 (defmacro gnus-data-unread-p (data)
2871   `(= (nth 1 ,data) gnus-unread-mark))
2872
2873 (defmacro gnus-data-read-p (data)
2874   `(/= (nth 1 ,data) gnus-unread-mark))
2875
2876 (defmacro gnus-data-pseudo-p (data)
2877   `(consp (nth 3 ,data)))
2878
2879 (defmacro gnus-data-find (number)
2880   `(assq ,number gnus-newsgroup-data))
2881
2882 (defmacro gnus-data-find-list (number &optional data)
2883   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2884      (memq (assq ,number bdata)
2885            bdata)))
2886
2887 (defmacro gnus-data-make (number mark pos header level)
2888   `(list ,number ,mark ,pos ,header ,level))
2889
2890 (defun gnus-data-enter (after-article number mark pos header level offset)
2891   (let ((data (gnus-data-find-list after-article)))
2892     (unless data
2893       (error "No such article: %d" after-article))
2894     (setcdr data (cons (gnus-data-make number mark pos header level)
2895                        (cdr data)))
2896     (setq gnus-newsgroup-data-reverse nil)
2897     (gnus-data-update-list (cddr data) offset)))
2898
2899 (defun gnus-data-enter-list (after-article list &optional offset)
2900   (when list
2901     (let ((data (and after-article (gnus-data-find-list after-article)))
2902           (ilist list))
2903       (if (not (or data
2904                    after-article))
2905           (let ((odata gnus-newsgroup-data))
2906             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2907             (when offset
2908               (gnus-data-update-list odata offset)))
2909         ;; Find the last element in the list to be spliced into the main
2910         ;; list.
2911         (setq list (last list))
2912         (if (not data)
2913             (progn
2914               (setcdr list gnus-newsgroup-data)
2915               (setq gnus-newsgroup-data ilist)
2916               (when offset
2917                 (gnus-data-update-list (cdr list) offset)))
2918           (setcdr list (cdr data))
2919           (setcdr data ilist)
2920           (when offset
2921             (gnus-data-update-list (cdr list) offset))))
2922       (setq gnus-newsgroup-data-reverse nil))))
2923
2924 (defun gnus-data-remove (article &optional offset)
2925   (let ((data gnus-newsgroup-data))
2926     (if (= (gnus-data-number (car data)) article)
2927         (progn
2928           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2929                 gnus-newsgroup-data-reverse nil)
2930           (when offset
2931             (gnus-data-update-list gnus-newsgroup-data offset)))
2932       (while (cdr data)
2933         (when (= (gnus-data-number (cadr data)) article)
2934           (setcdr data (cddr data))
2935           (when offset
2936             (gnus-data-update-list (cdr data) offset))
2937           (setq data nil
2938                 gnus-newsgroup-data-reverse nil))
2939         (setq data (cdr data))))))
2940
2941 (defmacro gnus-data-list (backward)
2942   `(if ,backward
2943        (or gnus-newsgroup-data-reverse
2944            (setq gnus-newsgroup-data-reverse
2945                  (reverse gnus-newsgroup-data)))
2946      gnus-newsgroup-data))
2947
2948 (defun gnus-data-update-list (data offset)
2949   "Add OFFSET to the POS of all data entries in DATA."
2950   (setq gnus-newsgroup-data-reverse nil)
2951   (while data
2952     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2953     (setq data (cdr data))))
2954
2955 (defun gnus-summary-article-pseudo-p (article)
2956   "Say whether this article is a pseudo article or not."
2957   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2958
2959 (defmacro gnus-summary-article-sparse-p (article)
2960   "Say whether this article is a sparse article or not."
2961   `(memq ,article gnus-newsgroup-sparse))
2962
2963 (defmacro gnus-summary-article-ancient-p (article)
2964   "Say whether this article is a sparse article or not."
2965   `(memq ,article gnus-newsgroup-ancient))
2966
2967 (defun gnus-article-parent-p (number)
2968   "Say whether this article is a parent or not."
2969   (let ((data (gnus-data-find-list number)))
2970     (and (cdr data)              ; There has to be an article after...
2971          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2972             (gnus-data-level (nth 1 data))))))
2973
2974 (defun gnus-article-children (number)
2975   "Return a list of all children to NUMBER."
2976   (let* ((data (gnus-data-find-list number))
2977          (level (gnus-data-level (car data)))
2978          children)
2979     (setq data (cdr data))
2980     (while (and data
2981                 (= (gnus-data-level (car data)) (1+ level)))
2982       (push (gnus-data-number (car data)) children)
2983       (setq data (cdr data)))
2984     children))
2985
2986 (defmacro gnus-summary-skip-intangible ()
2987   "If the current article is intangible, then jump to a different article."
2988   '(let ((to (get-text-property (point) 'gnus-intangible)))
2989      (and to (gnus-summary-goto-subject to))))
2990
2991 (defmacro gnus-summary-article-intangible-p ()
2992   "Say whether this article is intangible or not."
2993   '(get-text-property (point) 'gnus-intangible))
2994
2995 (defun gnus-article-read-p (article)
2996   "Say whether ARTICLE is read or not."
2997   (not (or (memq article gnus-newsgroup-marked)
2998            (memq article gnus-newsgroup-spam-marked)
2999            (memq article gnus-newsgroup-unreads)
3000            (memq article gnus-newsgroup-unselected)
3001            (memq article gnus-newsgroup-dormant))))
3002
3003 ;; Some summary mode macros.
3004
3005 (defmacro gnus-summary-article-number ()
3006   "The article number of the article on the current line.
3007 If there isn't an article number here, then we return the current
3008 article number."
3009   '(progn
3010      (gnus-summary-skip-intangible)
3011      (or (get-text-property (point) 'gnus-number)
3012          (gnus-summary-last-subject))))
3013
3014 (defmacro gnus-summary-article-header (&optional number)
3015   "Return the header of article NUMBER."
3016   `(gnus-data-header (gnus-data-find
3017                       ,(or number '(gnus-summary-article-number)))))
3018
3019 (defmacro gnus-summary-thread-level (&optional number)
3020   "Return the level of thread that starts with article NUMBER."
3021   `(if (and (eq gnus-summary-make-false-root 'dummy)
3022             (get-text-property (point) 'gnus-intangible))
3023        0
3024      (gnus-data-level (gnus-data-find
3025                        ,(or number '(gnus-summary-article-number))))))
3026
3027 (defmacro gnus-summary-article-mark (&optional number)
3028   "Return the mark of article NUMBER."
3029   `(gnus-data-mark (gnus-data-find
3030                     ,(or number '(gnus-summary-article-number)))))
3031
3032 (defmacro gnus-summary-article-pos (&optional number)
3033   "Return the position of the line of article NUMBER."
3034   `(gnus-data-pos (gnus-data-find
3035                    ,(or number '(gnus-summary-article-number)))))
3036
3037 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3038 (defmacro gnus-summary-article-subject (&optional number)
3039   "Return current subject string or nil if nothing."
3040   `(let ((headers
3041           ,(if number
3042                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3043              '(gnus-data-header (assq (gnus-summary-article-number)
3044                                       gnus-newsgroup-data)))))
3045      (and headers
3046           (vectorp headers)
3047           (mail-header-subject headers))))
3048
3049 (defmacro gnus-summary-article-score (&optional number)
3050   "Return current article score."
3051   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3052                   gnus-newsgroup-scored))
3053        gnus-summary-default-score 0))
3054
3055 (defun gnus-summary-article-children (&optional number)
3056   "Return a list of article numbers that are children of article NUMBER."
3057   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3058          (level (gnus-data-level (car data)))
3059          l children)
3060     (while (and (setq data (cdr data))
3061                 (> (setq l (gnus-data-level (car data))) level))
3062       (and (= (1+ level) l)
3063            (push (gnus-data-number (car data))
3064                  children)))
3065     (nreverse children)))
3066
3067 (defun gnus-summary-article-parent (&optional number)
3068   "Return the article number of the parent of article NUMBER."
3069   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3070                                     (gnus-data-list t)))
3071          (level (gnus-data-level (car data))))
3072     (if (zerop level)
3073         ()                              ; This is a root.
3074       ;; We search until we find an article with a level less than
3075       ;; this one.  That function has to be the parent.
3076       (while (and (setq data (cdr data))
3077                   (not (< (gnus-data-level (car data)) level))))
3078       (and data (gnus-data-number (car data))))))
3079
3080 (defun gnus-unread-mark-p (mark)
3081   "Say whether MARK is the unread mark."
3082   (= mark gnus-unread-mark))
3083
3084 (defun gnus-read-mark-p (mark)
3085   "Say whether MARK is one of the marks that mark as read.
3086 This is all marks except unread, ticked, dormant, and expirable."
3087   (not (or (= mark gnus-unread-mark)
3088            (= mark gnus-ticked-mark)
3089            (= mark gnus-spam-mark)
3090            (= mark gnus-dormant-mark)
3091            (= mark gnus-expirable-mark))))
3092
3093 (defmacro gnus-article-mark (number)
3094   "Return the MARK of article NUMBER.
3095 This macro should only be used when computing the mark the \"first\"
3096 time; i.e., when generating the summary lines.  After that,
3097 `gnus-summary-article-mark' should be used to examine the
3098 marks of articles."
3099   `(cond
3100     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3101     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3102     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3103     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3104     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3105     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3106     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3107     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3108            gnus-ancient-mark))))
3109
3110 ;; Saving hidden threads.
3111
3112 (defmacro gnus-save-hidden-threads (&rest forms)
3113   "Save hidden threads, eval FORMS, and restore the hidden threads."
3114   (let ((config (make-symbol "config")))
3115     `(let ((,config (gnus-hidden-threads-configuration)))
3116        (unwind-protect
3117            (save-excursion
3118              ,@forms)
3119          (gnus-restore-hidden-threads-configuration ,config)))))
3120 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3121 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3122
3123 (defun gnus-data-compute-positions ()
3124   "Compute the positions of all articles."
3125   (setq gnus-newsgroup-data-reverse nil)
3126   (let ((data gnus-newsgroup-data))
3127     (save-excursion
3128       (gnus-save-hidden-threads
3129         (gnus-summary-show-all-threads)
3130         (goto-char (point-min))
3131         (while data
3132           (while (get-text-property (point) 'gnus-intangible)
3133             (forward-line 1))
3134           (gnus-data-set-pos (car data) (+ (point) 3))
3135           (setq data (cdr data))
3136           (forward-line 1))))))
3137
3138 (defun gnus-hidden-threads-configuration ()
3139   "Return the current hidden threads configuration."
3140   (save-excursion
3141     (let (config)
3142       (goto-char (point-min))
3143       (while (search-forward "\r" nil t)
3144         (push (1- (point)) config))
3145       config)))
3146
3147 (defun gnus-restore-hidden-threads-configuration (config)
3148   "Restore hidden threads configuration from CONFIG."
3149   (save-excursion
3150     (let (point buffer-read-only)
3151       (while (setq point (pop config))
3152         (when (and (< point (point-max))
3153                    (goto-char point)
3154                    (eq (char-after) ?\n))
3155           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3156
3157 ;; Various summary mode internalish functions.
3158
3159 (defun gnus-mouse-pick-article (e)
3160   (interactive "e")
3161   (mouse-set-point e)
3162   (gnus-summary-next-page nil t))
3163
3164 (defun gnus-summary-set-display-table ()
3165   "Change the display table.
3166 Odd characters have a tendency to mess
3167 up nicely formatted displays - we make all possible glyphs
3168 display only a single character."
3169
3170   ;; We start from the standard display table, if any.
3171   (let ((table (or (copy-sequence standard-display-table)
3172                    (make-display-table)))
3173         (i 32))
3174     ;; Nix out all the control chars...
3175     (while (>= (setq i (1- i)) 0)
3176       (aset table i [??]))
3177    ;; ... but not newline and cr, of course.  (cr is necessary for the
3178     ;; selective display).
3179     (aset table ?\n nil)
3180     (aset table ?\r nil)
3181     ;; We keep TAB as well.
3182     (aset table ?\t nil)
3183     ;; We nix out any glyphs over 126 that are not set already.
3184     (let ((i 256))
3185       (while (>= (setq i (1- i)) 127)
3186         ;; Only modify if the entry is nil.
3187         (unless (aref table i)
3188           (aset table i [??]))))
3189     (setq buffer-display-table table)))
3190
3191 (defun gnus-summary-set-article-display-arrow (pos)
3192   "Update the overlay arrow to point to line at position POS."
3193   (when (and gnus-summary-display-arrow
3194              (boundp 'overlay-arrow-position)
3195              (boundp 'overlay-arrow-string))
3196     (save-excursion
3197       (goto-char pos)
3198       (beginning-of-line)
3199       (unless overlay-arrow-position
3200         (setq overlay-arrow-position (make-marker)))
3201       (setq overlay-arrow-string "=>"
3202             overlay-arrow-position (set-marker overlay-arrow-position
3203                                                (point)
3204                                                (current-buffer))))))
3205
3206 (defun gnus-summary-setup-buffer (group)
3207   "Initialize summary buffer."
3208   (let ((buffer (gnus-summary-buffer-name group))
3209         (dead-name (concat "*Dead Summary "
3210                            (gnus-group-decoded-name group) "*")))
3211     ;; If a dead summary buffer exists, we kill it.
3212     (when (gnus-buffer-live-p dead-name)
3213       (gnus-kill-buffer dead-name))
3214     (if (get-buffer buffer)
3215         (progn
3216           (set-buffer buffer)
3217           (setq gnus-summary-buffer (current-buffer))
3218           (not gnus-newsgroup-prepared))
3219       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3220       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3221       (gnus-summary-mode group)
3222       (when gnus-carpal
3223         (gnus-carpal-setup-buffer 'summary))
3224       (unless gnus-single-article-buffer
3225         (make-local-variable 'gnus-article-buffer)
3226         (make-local-variable 'gnus-article-current)
3227         (make-local-variable 'gnus-original-article-buffer))
3228       (setq gnus-newsgroup-name group)
3229       ;; Set any local variables in the group parameters.
3230       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3231       t)))
3232
3233 (defun gnus-set-global-variables ()
3234   "Set the global equivalents of the buffer-local variables.
3235 They are set to the latest values they had.  These reflect the summary
3236 buffer that was in action when the last article was fetched."
3237   (when (eq major-mode 'gnus-summary-mode)
3238     (setq gnus-summary-buffer (current-buffer))
3239     (let ((name gnus-newsgroup-name)
3240           (marked gnus-newsgroup-marked)
3241           (spam gnus-newsgroup-spam-marked)
3242           (unread gnus-newsgroup-unreads)
3243           (headers gnus-current-headers)
3244           (data gnus-newsgroup-data)
3245           (summary gnus-summary-buffer)
3246           (article-buffer gnus-article-buffer)
3247           (original gnus-original-article-buffer)
3248           (gac gnus-article-current)
3249           (reffed gnus-reffed-article-number)
3250           (score-file gnus-current-score-file)
3251           (default-charset gnus-newsgroup-charset)
3252           vlist)
3253       (let ((locals gnus-newsgroup-variables))
3254         (while locals
3255           (if (consp (car locals))
3256               (push (eval (caar locals)) vlist)
3257             (push (eval (car locals)) vlist))
3258           (setq locals (cdr locals)))
3259         (setq vlist (nreverse vlist)))
3260       (with-current-buffer gnus-group-buffer
3261         (setq gnus-newsgroup-name name
3262               gnus-newsgroup-marked marked
3263               gnus-newsgroup-spam-marked spam
3264               gnus-newsgroup-unreads unread
3265               gnus-current-headers headers
3266               gnus-newsgroup-data data
3267               gnus-article-current gac
3268               gnus-summary-buffer summary
3269               gnus-article-buffer article-buffer
3270               gnus-original-article-buffer original
3271               gnus-reffed-article-number reffed
3272               gnus-current-score-file score-file
3273               gnus-newsgroup-charset default-charset)
3274         (let ((locals gnus-newsgroup-variables))
3275           (while locals
3276             (if (consp (car locals))
3277                 (set (caar locals) (pop vlist))
3278               (set (car locals) (pop vlist)))
3279             (setq locals (cdr locals))))
3280         ;; The article buffer also has local variables.
3281         (when (gnus-buffer-live-p gnus-article-buffer)
3282           (set-buffer gnus-article-buffer)
3283           (setq gnus-summary-buffer summary))))))
3284
3285 (defun gnus-summary-article-unread-p (article)
3286   "Say whether ARTICLE is unread or not."
3287   (memq article gnus-newsgroup-unreads))
3288
3289 (defun gnus-summary-first-article-p (&optional article)
3290   "Return whether ARTICLE is the first article in the buffer."
3291   (if (not (setq article (or article (gnus-summary-article-number))))
3292       nil
3293     (eq article (caar gnus-newsgroup-data))))
3294
3295 (defun gnus-summary-last-article-p (&optional article)
3296   "Return whether ARTICLE is the last article in the buffer."
3297   (if (not (setq article (or article (gnus-summary-article-number))))
3298       ;; All non-existent numbers are the last article.  :-)
3299       t
3300     (not (cdr (gnus-data-find-list article)))))
3301
3302 (defun gnus-make-thread-indent-array ()
3303   (let ((n 200))
3304     (unless (and gnus-thread-indent-array
3305                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3306       (setq gnus-thread-indent-array (make-vector 201 "")
3307             gnus-thread-indent-array-level gnus-thread-indent-level)
3308       (while (>= n 0)
3309         (aset gnus-thread-indent-array n
3310               (make-string (* n gnus-thread-indent-level) ? ))
3311         (setq n (1- n))))))
3312
3313 (defun gnus-update-summary-mark-positions ()
3314   "Compute where the summary marks are to go."
3315   (save-excursion
3316     (when (gnus-buffer-exists-p gnus-summary-buffer)
3317       (set-buffer gnus-summary-buffer))
3318     (let ((spec gnus-summary-line-format-spec)
3319           pos)
3320       (save-excursion
3321         (gnus-set-work-buffer)
3322         (let ((gnus-tmp-unread ?Z)
3323               (gnus-replied-mark ?Z)
3324               (gnus-score-below-mark ?Z)
3325               (gnus-score-over-mark ?Z)
3326               (gnus-undownloaded-mark ?Z)
3327               (gnus-summary-line-format-spec spec)
3328               (gnus-newsgroup-downloadable '(0))
3329               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3330               case-fold-search ignores)
3331           ;; Here, all marks are bound to Z.
3332           (gnus-summary-insert-line header
3333                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3334           (goto-char (point-min))
3335           ;; Memorize the positions of the same characters as dummy marks.
3336           (while (re-search-forward "[A-D]" nil t)
3337             (push (point) ignores))
3338           (erase-buffer)
3339           ;; We use A-D as dummy marks in order to know column positions
3340           ;; where marks should be inserted.
3341           (setq gnus-tmp-unread ?A
3342                 gnus-replied-mark ?B
3343                 gnus-score-below-mark ?C
3344                 gnus-score-over-mark ?C
3345                 gnus-undownloaded-mark ?D)
3346           (gnus-summary-insert-line header
3347                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3348           ;; Ignore characters which aren't dummy marks.
3349           (dolist (p ignores)
3350             (delete-region (goto-char (1- p)) p)
3351             (insert ?Z))
3352           (goto-char (point-min))
3353           (setq pos (list (cons 'unread
3354                                 (and (search-forward "A" nil t)
3355                                      (- (point) (point-min) 1)))))
3356           (goto-char (point-min))
3357           (push (cons 'replied (and (search-forward "B" nil t)
3358                                     (- (point) (point-min) 1)))
3359                 pos)
3360           (goto-char (point-min))
3361           (push (cons 'score (and (search-forward "C" nil t)
3362                                   (- (point) (point-min) 1)))
3363                 pos)
3364           (goto-char (point-min))
3365           (push (cons 'download (and (search-forward "D" nil t)
3366                                      (- (point) (point-min) 1)))
3367                 pos)))
3368       (setq gnus-summary-mark-positions pos))))
3369
3370 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3371   "Insert a dummy root in the summary buffer."
3372   (beginning-of-line)
3373   (gnus-add-text-properties
3374    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3375    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3376
3377 (defun gnus-summary-extract-address-component (from)
3378   (or (car (funcall gnus-extract-address-components from))
3379       from))
3380
3381 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3382   (let ((mail-parse-charset gnus-newsgroup-charset)
3383         ; Is it really necessary to do this next part for each summary line?
3384         ; Luckily, doesn't seem to slow things down much.
3385         (mail-parse-ignored-charsets
3386          (with-current-buffer gnus-summary-buffer
3387            gnus-newsgroup-ignored-charsets)))
3388     (or
3389      (and gnus-ignored-from-addresses
3390           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3391           (let ((extra-headers (mail-header-extra header))
3392                 to
3393                 newsgroups)
3394             (cond
3395              ((setq to (cdr (assq 'To extra-headers)))
3396               (concat gnus-summary-to-prefix
3397                       (inline
3398                         (gnus-summary-extract-address-component
3399                          (funcall gnus-decode-encoded-word-function to)))))
3400              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3401               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3402      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3403
3404 (defun gnus-summary-insert-line (gnus-tmp-header
3405                                  gnus-tmp-level gnus-tmp-current
3406                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3407                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3408                                  &optional gnus-tmp-dummy gnus-tmp-score
3409                                  gnus-tmp-process)
3410   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3411          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3412          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3413          (gnus-tmp-score-char
3414           (if (or (null gnus-summary-default-score)
3415                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3416                       gnus-summary-zcore-fuzz))
3417               ?                         ;Whitespace
3418             (if (< gnus-tmp-score gnus-summary-default-score)
3419                 gnus-score-below-mark gnus-score-over-mark)))
3420          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3421          (gnus-tmp-replied
3422           (cond (gnus-tmp-process gnus-process-mark)
3423                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3424                  gnus-cached-mark)
3425                 (gnus-tmp-replied gnus-replied-mark)
3426                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3427                  gnus-forwarded-mark)
3428                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3429                  gnus-saved-mark)
3430                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3431                  gnus-recent-mark)
3432                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3433                  gnus-unseen-mark)
3434                 (t gnus-no-mark)))
3435          (gnus-tmp-downloaded
3436           (cond (undownloaded
3437                  gnus-undownloaded-mark)
3438                 (gnus-newsgroup-agentized
3439                  gnus-downloaded-mark)
3440                 (t
3441                  gnus-no-mark)))
3442          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3443          (gnus-tmp-name
3444           (cond
3445            ((string-match "<[^>]+> *$" gnus-tmp-from)
3446             (let ((beg (match-beginning 0)))
3447               (or (and (string-match "^\".+\"" gnus-tmp-from)
3448                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3449                   (substring gnus-tmp-from 0 beg))))
3450            ((string-match "(.+)" gnus-tmp-from)
3451             (substring gnus-tmp-from
3452                        (1+ (match-beginning 0)) (1- (match-end 0))))
3453            (t gnus-tmp-from)))
3454          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3455          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3456          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3457          (buffer-read-only nil))
3458     (when (string= gnus-tmp-name "")
3459       (setq gnus-tmp-name gnus-tmp-from))
3460     (unless (numberp gnus-tmp-lines)
3461       (setq gnus-tmp-lines -1))
3462     (if (= gnus-tmp-lines -1)
3463         (setq gnus-tmp-lines "?")
3464       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3465       (gnus-put-text-property
3466      (point)
3467      (progn (eval gnus-summary-line-format-spec) (point))
3468        'gnus-number gnus-tmp-number)
3469     (when (gnus-visual-p 'summary-highlight 'highlight)
3470       (forward-line -1)
3471       (gnus-run-hooks 'gnus-summary-update-hook)
3472       (forward-line 1))))
3473
3474 (defun gnus-summary-update-line (&optional dont-update)
3475   "Update summary line after change."
3476   (when (and gnus-summary-default-score
3477              (not gnus-summary-inhibit-highlight))
3478     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3479            (article (gnus-summary-article-number))
3480            (score (gnus-summary-article-score article)))
3481       (unless dont-update
3482         (if (and gnus-summary-mark-below
3483                  (< (gnus-summary-article-score)
3484                     gnus-summary-mark-below))
3485             ;; This article has a low score, so we mark it as read.
3486             (when (memq article gnus-newsgroup-unreads)
3487               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3488           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3489             ;; This article was previously marked as read on account
3490             ;; of a low score, but now it has risen, so we mark it as
3491             ;; unread.
3492             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3493         (gnus-summary-update-mark
3494          (if (or (null gnus-summary-default-score)
3495                  (<= (abs (- score gnus-summary-default-score))
3496                      gnus-summary-zcore-fuzz))
3497              ?                          ;Whitespace
3498            (if (< score gnus-summary-default-score)
3499                gnus-score-below-mark gnus-score-over-mark))
3500          'score))
3501       ;; Do visual highlighting.
3502       (when (gnus-visual-p 'summary-highlight 'highlight)
3503         (gnus-run-hooks 'gnus-summary-update-hook)))))
3504
3505 (defvar gnus-tmp-new-adopts nil)
3506
3507 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3508   "Return the number of articles in THREAD.
3509 This may be 0 in some cases -- if none of the articles in
3510 the thread are to be displayed."
3511   (let* ((number
3512          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3513           (cond
3514            ((not (listp thread))
3515             1)
3516            ((and (consp thread) (cdr thread))
3517             (apply
3518              '+ 1 (mapcar
3519                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3520            ((null thread)
3521             1)
3522            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3523             1)
3524            (t 0))))
3525     (when (and level (zerop level) gnus-tmp-new-adopts)
3526       (incf number
3527             (apply '+ (mapcar
3528                        'gnus-summary-number-of-articles-in-thread
3529                        gnus-tmp-new-adopts))))
3530     (if char
3531         (if (> number 1) gnus-not-empty-thread-mark
3532           gnus-empty-thread-mark)
3533       number)))
3534
3535 (defsubst gnus-summary-line-message-size (head)
3536   "Return pretty-printed version of message size.
3537 This function is intended to be used in
3538 `gnus-summary-line-format-alist'."
3539   (let ((c (or (mail-header-chars head) -1)))
3540     (cond ((< c 0) "n/a")               ; chars not available
3541           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3542           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3543           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3544           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3545
3546
3547 (defun gnus-summary-set-local-parameters (group)
3548   "Go through the local params of GROUP and set all variable specs in that list."
3549   (let ((vars '(quit-config)))          ; Ignore quit-config.
3550     (dolist (elem (gnus-group-find-parameter group))
3551       (and (consp elem)                 ; Has to be a cons.
3552            (consp (cdr elem))           ; The cdr has to be a list.
3553            (symbolp (car elem))         ; Has to be a symbol in there.
3554            (not (memq (car elem) vars))
3555            (ignore-errors               ; So we set it.
3556              (push (car elem) vars)
3557              (make-local-variable (car elem))
3558              (set (car elem) (eval (nth 1 elem))))))))
3559
3560 (defun gnus-summary-read-group (group &optional show-all no-article
3561                                       kill-buffer no-display backward
3562                                       select-articles)
3563   "Start reading news in newsgroup GROUP.
3564 If SHOW-ALL is non-nil, already read articles are also listed.
3565 If NO-ARTICLE is non-nil, no article is selected initially.
3566 If NO-DISPLAY, don't generate a summary buffer."
3567   (let (result)
3568     (while (and group
3569                 (null (setq result
3570                             (let ((gnus-auto-select-next nil))
3571                               (or (gnus-summary-read-group-1
3572                                    group show-all no-article
3573                                    kill-buffer no-display
3574                                    select-articles)
3575                                   (setq show-all nil
3576                                         select-articles nil)))))
3577                 (eq gnus-auto-select-next 'quietly))
3578       (set-buffer gnus-group-buffer)
3579       ;; The entry function called above goes to the next
3580       ;; group automatically, so we go two groups back
3581       ;; if we are searching for the previous group.
3582       (when backward
3583         (gnus-group-prev-unread-group 2))
3584       (if (not (equal group (gnus-group-group-name)))
3585           (setq group (gnus-group-group-name))
3586         (setq group nil)))
3587     result))
3588
3589 (defun gnus-summary-read-group-1 (group show-all no-article
3590                                         kill-buffer no-display
3591                                         &optional select-articles)
3592   ;; Killed foreign groups can't be entered.
3593   ;;  (when (and (not (gnus-group-native-p group))
3594   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3595   ;;    (error "Dead non-native groups can't be entered"))
3596   (gnus-message 5 "Retrieving newsgroup: %s..."
3597                 (gnus-group-decoded-name group))
3598   (let* ((new-group (gnus-summary-setup-buffer group))
3599          (quit-config (gnus-group-quit-config group))
3600          (did-select (and new-group (gnus-select-newsgroup
3601                                      group show-all select-articles))))
3602     (cond
3603      ;; This summary buffer exists already, so we just select it.
3604      ((not new-group)
3605       (gnus-set-global-variables)
3606       (when kill-buffer
3607         (gnus-kill-or-deaden-summary kill-buffer))
3608       (gnus-configure-windows 'summary 'force)
3609       (gnus-set-mode-line 'summary)
3610       (gnus-summary-position-point)
3611       (message "")
3612       t)
3613      ;; We couldn't select this group.
3614      ((null did-select)
3615       (when (and (eq major-mode 'gnus-summary-mode)
3616                  (not (equal (current-buffer) kill-buffer)))
3617         (kill-buffer (current-buffer))
3618         (if (not quit-config)
3619             (progn
3620               ;; Update the info -- marks might need to be removed,
3621               ;; for instance.
3622               (gnus-summary-update-info)
3623               (set-buffer gnus-group-buffer)
3624               (gnus-group-jump-to-group group)
3625               (gnus-group-next-unread-group 1))
3626           (gnus-handle-ephemeral-exit quit-config)))
3627       (let ((grpinfo (gnus-get-info group)))
3628         (if (null (gnus-info-read grpinfo))
3629             (gnus-message 3 "Group %s contains no messages"
3630                           (gnus-group-decoded-name group))
3631           (gnus-message 3 "Can't select group")))
3632       nil)
3633      ;; The user did a `C-g' while prompting for number of articles,
3634      ;; so we exit this group.
3635      ((eq did-select 'quit)
3636       (and (eq major-mode 'gnus-summary-mode)
3637            (not (equal (current-buffer) kill-buffer))
3638            (kill-buffer (current-buffer)))
3639       (when kill-buffer
3640         (gnus-kill-or-deaden-summary kill-buffer))
3641       (if (not quit-config)
3642           (progn
3643             (set-buffer gnus-group-buffer)
3644             (gnus-group-jump-to-group group)
3645             (gnus-group-next-unread-group 1)
3646             (gnus-configure-windows 'group 'force))
3647         (gnus-handle-ephemeral-exit quit-config))
3648       ;; Finally signal the quit.
3649       (signal 'quit nil))
3650      ;; The group was successfully selected.
3651      (t
3652       (gnus-set-global-variables)
3653       ;; Save the active value in effect when the group was entered.
3654       (setq gnus-newsgroup-active
3655             (gnus-copy-sequence
3656              (gnus-active gnus-newsgroup-name)))
3657       ;; You can change the summary buffer in some way with this hook.
3658       (gnus-run-hooks 'gnus-select-group-hook)
3659       (when (memq 'summary (gnus-update-format-specifications
3660                             nil 'summary 'summary-mode 'summary-dummy))
3661         ;; The format specification for the summary line was updated,
3662         ;; so we need to update the mark positions as well.
3663         (gnus-update-summary-mark-positions))
3664       ;; Do score processing.
3665       (when gnus-use-scoring
3666         (gnus-possibly-score-headers))
3667       ;; Check whether to fill in the gaps in the threads.
3668       (when gnus-build-sparse-threads
3669         (gnus-build-sparse-threads))
3670       ;; Find the initial limit.
3671       (if show-all
3672           (let ((gnus-newsgroup-dormant nil))
3673             (gnus-summary-initial-limit show-all))
3674         (gnus-summary-initial-limit show-all))
3675       ;; Generate the summary buffer.
3676       (unless no-display
3677         (gnus-summary-prepare))
3678       (when gnus-use-trees
3679         (gnus-tree-open group)
3680         (setq gnus-summary-highlight-line-function
3681               'gnus-tree-highlight-article))
3682       ;; If the summary buffer is empty, but there are some low-scored
3683       ;; articles or some excluded dormants, we include these in the
3684       ;; buffer.
3685       (when (and (zerop (buffer-size))
3686                  (not no-display))
3687         (cond (gnus-newsgroup-dormant
3688                (gnus-summary-limit-include-dormant))
3689               ((and gnus-newsgroup-scored show-all)
3690                (gnus-summary-limit-include-expunged t))))
3691       ;; Function `gnus-apply-kill-file' must be called in this hook.
3692       (gnus-run-hooks 'gnus-apply-kill-hook)
3693       (if (and (zerop (buffer-size))
3694                (not no-display))
3695           (progn
3696             ;; This newsgroup is empty.
3697             (gnus-summary-catchup-and-exit nil t)
3698             (gnus-message 6 "No unread news")
3699             (when kill-buffer
3700               (gnus-kill-or-deaden-summary kill-buffer))
3701             ;; Return nil from this function.
3702             nil)
3703         ;; Hide conversation thread subtrees.  We cannot do this in
3704         ;; gnus-summary-prepare-hook since kill processing may not
3705         ;; work with hidden articles.
3706         (gnus-summary-maybe-hide-threads)
3707         (when kill-buffer
3708           (gnus-kill-or-deaden-summary kill-buffer))
3709         (gnus-summary-auto-select-subject)
3710         ;; Show first unread article if requested.
3711         (if (and (not no-article)
3712                  (not no-display)
3713                  gnus-newsgroup-unreads
3714                  gnus-auto-select-first)
3715             (progn
3716               (gnus-configure-windows 'summary)
3717               (let ((art (gnus-summary-article-number)))
3718                 (unless (and (not gnus-plugged)
3719                              (or (memq art gnus-newsgroup-undownloaded)
3720                                  (memq art gnus-newsgroup-downloadable)))
3721                   (gnus-summary-goto-article art))))
3722           ;; Don't select any articles.
3723           (gnus-summary-position-point)
3724           (gnus-configure-windows 'summary 'force)
3725           (gnus-set-mode-line 'summary))
3726         (when (and gnus-auto-center-group
3727                    (get-buffer-window gnus-group-buffer t))
3728           ;; Gotta use windows, because recenter does weird stuff if
3729           ;; the current buffer ain't the displayed window.
3730           (let ((owin (selected-window)))
3731             (select-window (get-buffer-window gnus-group-buffer t))
3732             (when (gnus-group-goto-group group)
3733               (recenter))
3734             (select-window owin)))
3735         ;; Mark this buffer as "prepared".
3736         (setq gnus-newsgroup-prepared t)
3737         (gnus-run-hooks 'gnus-summary-prepared-hook)
3738         (unless (gnus-ephemeral-group-p group)
3739           (gnus-group-update-group group))
3740         t)))))
3741
3742 (defun gnus-summary-auto-select-subject ()
3743   "Select the subject line on initial group entry."
3744   (goto-char (point-min))
3745   (cond
3746    ((eq gnus-auto-select-subject 'best)
3747     (gnus-summary-best-unread-subject))
3748    ((eq gnus-auto-select-subject 'unread)
3749     (gnus-summary-first-unread-subject))
3750    ((eq gnus-auto-select-subject 'unseen)
3751     (gnus-summary-first-unseen-subject))
3752    ((eq gnus-auto-select-subject 'unseen-or-unread)
3753     (gnus-summary-first-unseen-or-unread-subject))
3754    ((eq gnus-auto-select-subject 'first)
3755     ;; Do nothing.
3756     )
3757    ((functionp gnus-auto-select-subject)
3758     (funcall gnus-auto-select-subject))))
3759
3760 (defun gnus-summary-prepare ()
3761   "Generate the summary buffer."
3762   (interactive)
3763   (let ((buffer-read-only nil))
3764     (erase-buffer)
3765     (setq gnus-newsgroup-data nil
3766           gnus-newsgroup-data-reverse nil)
3767     (gnus-run-hooks 'gnus-summary-generate-hook)
3768     ;; Generate the buffer, either with threads or without.
3769     (when gnus-newsgroup-headers
3770       (gnus-summary-prepare-threads
3771        (if gnus-show-threads
3772            (gnus-sort-gathered-threads
3773             (funcall gnus-summary-thread-gathering-function
3774                      (gnus-sort-threads
3775                       (gnus-cut-threads (gnus-make-threads)))))
3776          ;; Unthreaded display.
3777          (gnus-sort-articles gnus-newsgroup-headers))))
3778     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3779     ;; Call hooks for modifying summary buffer.
3780     (goto-char (point-min))
3781     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3782
3783 (defsubst gnus-general-simplify-subject (subject)
3784   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3785   (setq subject
3786         (cond
3787          ;; Truncate the subject.
3788          (gnus-simplify-subject-functions
3789           (gnus-map-function gnus-simplify-subject-functions subject))
3790          ((numberp gnus-summary-gather-subject-limit)
3791           (setq subject (gnus-simplify-subject-re subject))
3792           (if (> (length subject) gnus-summary-gather-subject-limit)
3793               (substring subject 0 gnus-summary-gather-subject-limit)
3794             subject))
3795          ;; Fuzzily simplify it.
3796          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3797           (gnus-simplify-subject-fuzzy subject))
3798          ;; Just remove the leading "Re:".
3799          (t
3800           (gnus-simplify-subject-re subject))))
3801
3802   (if (and gnus-summary-gather-exclude-subject
3803            (string-match gnus-summary-gather-exclude-subject subject))
3804       nil                         ; This article shouldn't be gathered
3805     subject))
3806
3807 (defun gnus-summary-simplify-subject-query ()
3808   "Query where the respool algorithm would put this article."
3809   (interactive)
3810   (gnus-summary-select-article)
3811   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3812
3813 (defun gnus-gather-threads-by-subject (threads)
3814   "Gather threads by looking at Subject headers."
3815   (if (not gnus-summary-make-false-root)
3816       threads
3817     (let ((hashtb (gnus-make-hashtable 1024))
3818           (prev threads)
3819           (result threads)
3820           subject hthread whole-subject)
3821       (while threads
3822         (setq subject (gnus-general-simplify-subject
3823                        (setq whole-subject (mail-header-subject
3824                                             (caar threads)))))
3825         (when subject
3826           (if (setq hthread (gnus-gethash subject hashtb))
3827               (progn
3828                 ;; We enter a dummy root into the thread, if we
3829                 ;; haven't done that already.
3830                 (unless (stringp (caar hthread))
3831                   (setcar hthread (list whole-subject (car hthread))))
3832                 ;; We add this new gathered thread to this gathered
3833                 ;; thread.
3834                 (setcdr (car hthread)
3835                         (nconc (cdar hthread) (list (car threads))))
3836                 ;; Remove it from the list of threads.
3837                 (setcdr prev (cdr threads))
3838                 (setq threads prev))
3839             ;; Enter this thread into the hash table.
3840             (gnus-sethash subject
3841                           (if gnus-summary-make-false-root-always
3842                               (progn
3843                                 ;; If you want a dummy root above all
3844                                 ;; threads...
3845                                 (setcar threads (list whole-subject
3846                                                       (car threads)))
3847                                 threads)
3848                             threads)
3849                           hashtb)))
3850         (setq prev threads)
3851         (setq threads (cdr threads)))
3852       result)))
3853
3854 (defun gnus-gather-threads-by-references (threads)
3855   "Gather threads by looking at References headers."
3856   (let ((idhashtb (gnus-make-hashtable 1024))
3857         (thhashtb (gnus-make-hashtable 1024))
3858         (prev threads)
3859         (result threads)
3860         ids references id gthread gid entered ref)
3861     (while threads
3862       (when (setq references (mail-header-references (caar threads)))
3863         (setq id (mail-header-id (caar threads))
3864               ids (inline (gnus-split-references references))
3865               entered nil)
3866         (while (setq ref (pop ids))
3867           (setq ids (delete ref ids))
3868           (if (not (setq gid (gnus-gethash ref idhashtb)))
3869               (progn
3870                 (gnus-sethash ref id idhashtb)
3871                 (gnus-sethash id threads thhashtb))
3872             (setq gthread (gnus-gethash gid thhashtb))
3873             (unless entered
3874               ;; We enter a dummy root into the thread, if we
3875               ;; haven't done that already.
3876               (unless (stringp (caar gthread))
3877                 (setcar gthread (list (mail-header-subject (caar gthread))
3878                                       (car gthread))))
3879               ;; We add this new gathered thread to this gathered
3880               ;; thread.
3881               (setcdr (car gthread)
3882                       (nconc (cdar gthread) (list (car threads)))))
3883             ;; Add it into the thread hash table.
3884             (gnus-sethash id gthread thhashtb)
3885             (setq entered t)
3886             ;; Remove it from the list of threads.
3887             (setcdr prev (cdr threads))
3888             (setq threads prev))))
3889       (setq prev threads)
3890       (setq threads (cdr threads)))
3891     result))
3892
3893 (defun gnus-sort-gathered-threads (threads)
3894   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3895   (let ((result threads))
3896     (while threads
3897       (when (stringp (caar threads))
3898         (setcdr (car threads)
3899                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3900       (setq threads (cdr threads)))
3901     result))
3902
3903 (defun gnus-thread-loop-p (root thread)
3904   "Say whether ROOT is in THREAD."
3905   (let ((stack (list thread))
3906         (infloop 0)
3907         th)
3908     (while (setq thread (pop stack))
3909       (setq th (cdr thread))
3910       (while (and th
3911                   (not (eq (caar th) root)))
3912         (pop th))
3913       (if th
3914           ;; We have found a loop.
3915           (let (ref-dep)
3916             (setcdr thread (delq (car th) (cdr thread)))
3917             (if (boundp (setq ref-dep (intern "none"
3918                                               gnus-newsgroup-dependencies)))
3919                 (setcdr (symbol-value ref-dep)
3920                         (nconc (cdr (symbol-value ref-dep))
3921                                (list (car th))))
3922               (set ref-dep (list nil (car th))))
3923             (setq infloop 1
3924                   stack nil))
3925         ;; Push all the subthreads onto the stack.
3926         (push (cdr thread) stack)))
3927     infloop))
3928
3929 (defun gnus-make-threads ()
3930   "Go through the dependency hashtb and find the roots.  Return all threads."
3931   (let (threads)
3932     (while (catch 'infloop
3933              (mapatoms
3934               (lambda (refs)
3935                 ;; Deal with self-referencing References loops.
3936                 (when (and (car (symbol-value refs))
3937                            (not (zerop
3938                                  (apply
3939                                   '+
3940                                   (mapcar
3941                                    (lambda (thread)
3942                                      (gnus-thread-loop-p
3943                                       (car (symbol-value refs)) thread))
3944                                    (cdr (symbol-value refs)))))))
3945                   (setq threads nil)
3946                   (throw 'infloop t))
3947                 (unless (car (symbol-value refs))
3948                   ;; These threads do not refer back to any other
3949                   ;; articles, so they're roots.
3950                   (setq threads (append (cdr (symbol-value refs)) threads))))
3951               gnus-newsgroup-dependencies)))
3952     threads))
3953
3954 ;; Build the thread tree.
3955 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3956   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3957
3958 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3959 if it was already present.
3960
3961 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3962 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3963 Message-IDs will be renamed to a unique Message-ID before being
3964 entered.
3965
3966 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3967   (let* ((id (mail-header-id header))
3968          (id-dep (and id (intern id dependencies)))
3969          parent-id ref ref-dep ref-header replaced)
3970     ;; Enter this `header' in the `dependencies' table.
3971     (cond
3972      ((not id-dep)
3973       (setq header nil))
3974      ;; The first two cases do the normal part: enter a new `header'
3975      ;; in the `dependencies' table.
3976      ((not (boundp id-dep))
3977       (set id-dep (list header)))
3978      ((null (car (symbol-value id-dep)))
3979       (setcar (symbol-value id-dep) header))
3980
3981      ;; From here the `header' was already present in the
3982      ;; `dependencies' table.
3983      (force-new
3984       ;; Overrides an existing entry;
3985       ;; just set the header part of the entry.
3986       (setcar (symbol-value id-dep) header)
3987       (setq replaced t))
3988
3989      ;; Renames the existing `header' to a unique Message-ID.
3990      ((not gnus-summary-ignore-duplicates)
3991       ;; An article with this Message-ID has already been seen.
3992       ;; We rename the Message-ID.
3993       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3994            (list header))
3995       (mail-header-set-id header id))
3996
3997      ;; The last case ignores an existing entry, except it adds any
3998      ;; additional Xrefs (in case the two articles came from different
3999      ;; servers.
4000      ;; Also sets `header' to `nil' meaning that the `dependencies'
4001      ;; table was *not* modified.
4002      (t
4003       (mail-header-set-xref
4004        (car (symbol-value id-dep))
4005        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4006                    "")
4007                (or (mail-header-xref header) "")))
4008       (setq header nil)))
4009
4010     (when (and header (not replaced))
4011       ;; First check that we are not creating a References loop.
4012       (setq parent-id (gnus-parent-id (mail-header-references header)))
4013       (setq ref parent-id)
4014       (while (and ref
4015                   (setq ref-dep (intern-soft ref dependencies))
4016                   (boundp ref-dep)
4017                   (setq ref-header (car (symbol-value ref-dep))))
4018         (if (string= id ref)
4019             ;; Yuk!  This is a reference loop.  Make the article be a
4020             ;; root article.
4021             (progn
4022               (mail-header-set-references (car (symbol-value id-dep)) "none")
4023               (setq ref nil)
4024               (setq parent-id nil))
4025           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4026       (setq ref-dep (intern (or parent-id "none") dependencies))
4027       (if (boundp ref-dep)
4028           (setcdr (symbol-value ref-dep)
4029                   (nconc (cdr (symbol-value ref-dep))
4030                          (list (symbol-value id-dep))))
4031         (set ref-dep (list nil (symbol-value id-dep)))))
4032     header))
4033
4034 (defun gnus-extract-message-id-from-in-reply-to (string)
4035   (if (string-match "<[^>]+>" string)
4036       (substring string (match-beginning 0) (match-end 0))
4037     nil))
4038
4039 (defun gnus-build-sparse-threads ()
4040   (let ((headers gnus-newsgroup-headers)
4041         (mail-parse-charset gnus-newsgroup-charset)
4042         (gnus-summary-ignore-duplicates t)
4043         header references generation relations
4044         subject child end new-child date)
4045     ;; First we create an alist of generations/relations, where
4046     ;; generations is how much we trust the relation, and the relation
4047     ;; is parent/child.
4048     (gnus-message 7 "Making sparse threads...")
4049     (save-excursion
4050       (nnheader-set-temp-buffer " *gnus sparse threads*")
4051       (while (setq header (pop headers))
4052         (when (and (setq references (mail-header-references header))
4053                    (not (string= references "")))
4054           (insert references)
4055           (setq child (mail-header-id header)
4056                 subject (mail-header-subject header)
4057                 date (mail-header-date header)
4058                 generation 0)
4059           (while (search-backward ">" nil t)
4060             (setq end (1+ (point)))
4061             (when (search-backward "<" nil t)
4062               (setq new-child (buffer-substring (point) end))
4063               (push (list (incf generation)
4064                           child (setq child new-child)
4065                           subject date)
4066                     relations)))
4067           (when child
4068             (push (list (1+ generation) child nil subject) relations))
4069           (erase-buffer)))
4070       (kill-buffer (current-buffer)))
4071     ;; Sort over trustworthiness.
4072     (mapcar
4073      (lambda (relation)
4074        (when (gnus-dependencies-add-header
4075               (make-full-mail-header
4076                gnus-reffed-article-number
4077                (nth 3 relation) "" (or (nth 4 relation) "")
4078                (nth 1 relation)
4079                (or (nth 2 relation) "") 0 0 "")
4080               gnus-newsgroup-dependencies nil)
4081          (push gnus-reffed-article-number gnus-newsgroup-limit)
4082          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4083          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4084                gnus-newsgroup-reads)
4085          (decf gnus-reffed-article-number)))
4086      (sort relations 'car-less-than-car))
4087     (gnus-message 7 "Making sparse threads...done")))
4088
4089 (defun gnus-build-old-threads ()
4090   ;; Look at all the articles that refer back to old articles, and
4091   ;; fetch the headers for the articles that aren't there.  This will
4092   ;; build complete threads - if the roots haven't been expired by the
4093   ;; server, that is.
4094   (let ((mail-parse-charset gnus-newsgroup-charset)
4095         id heads)
4096     (mapatoms
4097      (lambda (refs)
4098        (when (not (car (symbol-value refs)))
4099          (setq heads (cdr (symbol-value refs)))
4100          (while heads
4101            (if (memq (mail-header-number (caar heads))
4102                      gnus-newsgroup-dormant)
4103                (setq heads (cdr heads))
4104              (setq id (symbol-name refs))
4105              (while (and (setq id (gnus-build-get-header id))
4106                          (not (car (gnus-id-to-thread id)))))
4107              (setq heads nil)))))
4108      gnus-newsgroup-dependencies)))
4109
4110 (defsubst gnus-remove-odd-characters (string)
4111   "Translate STRING into something that doesn't contain weird characters."
4112   (mm-subst-char-in-string
4113    ?\r ?\-
4114    (mm-subst-char-in-string ?\n ?\- string t) t))
4115
4116 ;; This function has to be called with point after the article number
4117 ;; on the beginning of the line.
4118 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4119   (let ((eol (point-at-eol))
4120         (buffer (current-buffer))
4121         header references in-reply-to)
4122
4123     ;; overview: [num subject from date id refs chars lines misc]
4124     (unwind-protect
4125         (let (x)
4126           (narrow-to-region (point) eol)
4127           (unless (eobp)
4128             (forward-char))
4129
4130           (setq header
4131                 (make-full-mail-header
4132                  number                 ; number
4133                  (condition-case ()     ; subject
4134                      (gnus-remove-odd-characters
4135                       (funcall gnus-decode-encoded-word-function
4136                                (setq x (nnheader-nov-field))))
4137                    (error x))
4138                  (condition-case ()     ; from
4139                      (gnus-remove-odd-characters
4140                       (funcall gnus-decode-encoded-word-function
4141                                (setq x (nnheader-nov-field))))
4142                    (error x))
4143                  (nnheader-nov-field)   ; date
4144                  (nnheader-nov-read-message-id number)  ; id
4145                  (setq references (nnheader-nov-field)) ; refs
4146                  (nnheader-nov-read-integer) ; chars
4147                  (nnheader-nov-read-integer) ; lines
4148                  (unless (eobp)
4149                    (if (looking-at "Xref: ")
4150                        (goto-char (match-end 0)))
4151                    (nnheader-nov-field)) ; Xref
4152                  (nnheader-nov-parse-extra)))) ; extra
4153
4154       (widen))
4155
4156     (when (and (string= references "")
4157                (setq in-reply-to (mail-header-extra header))
4158                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4159       (mail-header-set-references
4160        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4161
4162     (when gnus-alter-header-function
4163       (funcall gnus-alter-header-function header))
4164     (gnus-dependencies-add-header header dependencies force-new)))
4165
4166 (defun gnus-build-get-header (id)
4167   "Look through the buffer of NOV lines and find the header to ID.
4168 Enter this line into the dependencies hash table, and return
4169 the id of the parent article (if any)."
4170   (let ((deps gnus-newsgroup-dependencies)
4171         found header)
4172     (prog1
4173         (save-excursion
4174           (set-buffer nntp-server-buffer)
4175           (let ((case-fold-search nil))
4176             (goto-char (point-min))
4177             (while (and (not found)
4178                         (search-forward id nil t))
4179               (beginning-of-line)
4180               (setq found (looking-at
4181                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4182                                    (regexp-quote id))))
4183               (or found (beginning-of-line 2)))
4184             (when found
4185               (beginning-of-line)
4186               (and
4187                (setq header (gnus-nov-parse-line
4188                              (read (current-buffer)) deps))
4189                (gnus-parent-id (mail-header-references header))))))
4190       (when header
4191         (let ((number (mail-header-number header)))
4192           (push number gnus-newsgroup-limit)
4193           (push header gnus-newsgroup-headers)
4194           (if (memq number gnus-newsgroup-unselected)
4195               (progn
4196                 (setq gnus-newsgroup-unreads
4197                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4198                                                number))
4199                 (setq gnus-newsgroup-unselected
4200                       (delq number gnus-newsgroup-unselected)))
4201             (push number gnus-newsgroup-ancient)))))))
4202
4203 (defun gnus-build-all-threads ()
4204   "Read all the headers."
4205   (let ((gnus-summary-ignore-duplicates t)
4206         (mail-parse-charset gnus-newsgroup-charset)
4207         (dependencies gnus-newsgroup-dependencies)
4208         header article)
4209     (save-excursion
4210       (set-buffer nntp-server-buffer)
4211       (let ((case-fold-search nil))
4212         (goto-char (point-min))
4213         (while (not (eobp))
4214           (ignore-errors
4215             (setq article (read (current-buffer))
4216                   header (gnus-nov-parse-line article dependencies)))
4217           (when header
4218             (with-current-buffer gnus-summary-buffer
4219               (push header gnus-newsgroup-headers)
4220               (if (memq (setq article (mail-header-number header))
4221                         gnus-newsgroup-unselected)
4222                   (progn
4223                     (setq gnus-newsgroup-unreads
4224                           (gnus-add-to-sorted-list
4225                            gnus-newsgroup-unreads article))
4226                     (setq gnus-newsgroup-unselected
4227                           (delq article gnus-newsgroup-unselected)))
4228                 (push article gnus-newsgroup-ancient)))
4229             (forward-line 1)))))))
4230
4231 (defun gnus-summary-update-article-line (article header)
4232   "Update the line for ARTICLE using HEADER."
4233   (let* ((id (mail-header-id header))
4234          (thread (gnus-id-to-thread id)))
4235     (unless thread
4236       (error "Article in no thread"))
4237     ;; Update the thread.
4238     (setcar thread header)
4239     (gnus-summary-goto-subject article)
4240     (let* ((datal (gnus-data-find-list article))
4241            (data (car datal))
4242            (buffer-read-only nil)
4243            (level (gnus-summary-thread-level)))
4244       (gnus-delete-line)
4245       (let ((inserted (- (point)
4246                          (progn
4247                            (gnus-summary-insert-line
4248                             header level nil
4249                             (memq article gnus-newsgroup-undownloaded)
4250                             (gnus-article-mark article)
4251                             (memq article gnus-newsgroup-replied)
4252                             (memq article gnus-newsgroup-expirable)
4253                             ;; Only insert the Subject string when it's different
4254                             ;; from the previous Subject string.
4255                             (if (and
4256                                  gnus-show-threads
4257                                  (gnus-subject-equal
4258                                   (condition-case ()
4259                                       (mail-header-subject
4260                                        (gnus-data-header
4261                                         (cadr
4262                                          (gnus-data-find-list
4263                                           article
4264                                           (gnus-data-list t)))))
4265                                     ;; Error on the side of excessive subjects.
4266                                     (error ""))
4267                                   (mail-header-subject header)))
4268                                 ""
4269                               (mail-header-subject header))
4270                             nil (cdr (assq article gnus-newsgroup-scored))
4271                             (memq article gnus-newsgroup-processable))
4272                            (point)))))
4273         (when (cdr datal)
4274           (gnus-data-update-list
4275            (cdr datal)
4276            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4277
4278 (defun gnus-summary-update-article (article &optional iheader)
4279   "Update ARTICLE in the summary buffer."
4280   (set-buffer gnus-summary-buffer)
4281   (let* ((header (gnus-summary-article-header article))
4282          (id (mail-header-id header))
4283          (data (gnus-data-find article))
4284          (thread (gnus-id-to-thread id))
4285          (references (mail-header-references header))
4286          (parent
4287           (gnus-id-to-thread
4288            (or (gnus-parent-id
4289                 (when (and references
4290                            (not (equal "" references)))
4291                   references))
4292                "none")))
4293          (buffer-read-only nil)
4294          (old (car thread)))
4295     (when thread
4296       (unless iheader
4297         (setcar thread nil)
4298         (when parent
4299           (delq thread parent)))
4300       (if (gnus-summary-insert-subject id header)
4301           ;; Set the (possibly) new article number in the data structure.
4302           (gnus-data-set-number data (gnus-id-to-article id))
4303         (setcar thread old)
4304         nil))))
4305
4306 (defun gnus-rebuild-thread (id &optional line)
4307   "Rebuild the thread containing ID.
4308 If LINE, insert the rebuilt thread starting on line LINE."
4309   (let ((buffer-read-only nil)
4310         old-pos current thread data)
4311     (if (not gnus-show-threads)
4312         (setq thread (list (car (gnus-id-to-thread id))))
4313       ;; Get the thread this article is part of.
4314       (setq thread (gnus-remove-thread id)))
4315     (setq old-pos (point-at-bol))
4316     (setq current (save-excursion
4317                     (and (re-search-backward "[\r\n]" nil t)
4318                          (gnus-summary-article-number))))
4319     ;; If this is a gathered thread, we have to go some re-gathering.
4320     (when (stringp (car thread))
4321       (let ((subject (car thread))
4322             roots thr)
4323         (setq thread (cdr thread))
4324         (while thread
4325           (unless (memq (setq thr (gnus-id-to-thread
4326                                    (gnus-root-id
4327                                     (mail-header-id (caar thread)))))
4328                         roots)
4329             (push thr roots))
4330           (setq thread (cdr thread)))
4331         ;; We now have all (unique) roots.
4332         (if (= (length roots) 1)
4333             ;; All the loose roots are now one solid root.
4334             (setq thread (car roots))
4335           (setq thread (cons subject (gnus-sort-threads roots))))))
4336     (let (threads)
4337       ;; We then insert this thread into the summary buffer.
4338       (when line
4339         (goto-char (point-min))
4340         (forward-line (1- line)))
4341       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4342         (if gnus-show-threads
4343             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4344           (gnus-summary-prepare-unthreaded thread))
4345         (setq data (nreverse gnus-newsgroup-data))
4346         (setq threads gnus-newsgroup-threads))
4347       ;; We splice the new data into the data structure.
4348       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4349       ;;!!! then we want to insert at the beginning of the buffer.
4350       ;;!!! That happens to be true with Gnus now, but that may
4351       ;;!!! change in the future.  Perhaps.
4352       (gnus-data-enter-list
4353        (if line nil current) data (- (point) old-pos))
4354       (setq gnus-newsgroup-threads
4355             (nconc threads gnus-newsgroup-threads))
4356       (gnus-data-compute-positions))))
4357
4358 (defun gnus-number-to-header (number)
4359   "Return the header for article NUMBER."
4360   (let ((headers gnus-newsgroup-headers))
4361     (while (and headers
4362                 (not (= number (mail-header-number (car headers)))))
4363       (pop headers))
4364     (when headers
4365       (car headers))))
4366
4367 (defun gnus-parent-headers (in-headers &optional generation)
4368   "Return the headers of the GENERATIONeth parent of HEADERS."
4369   (unless generation
4370     (setq generation 1))
4371   (let ((parent t)
4372         (headers in-headers)
4373         references)
4374     (while (and parent
4375                 (not (zerop generation))
4376                 (setq references (mail-header-references headers)))
4377       (setq headers (if (and references
4378                              (setq parent (gnus-parent-id references)))
4379                         (car (gnus-id-to-thread parent))
4380                       nil))
4381       (decf generation))
4382     (and (not (eq headers in-headers))
4383          headers)))
4384
4385 (defun gnus-id-to-thread (id)
4386   "Return the (sub-)thread where ID appears."
4387   (gnus-gethash id gnus-newsgroup-dependencies))
4388
4389 (defun gnus-id-to-article (id)
4390   "Return the article number of ID."
4391   (let ((thread (gnus-id-to-thread id)))
4392     (when (and thread
4393                (car thread))
4394       (mail-header-number (car thread)))))
4395
4396 (defun gnus-id-to-header (id)
4397   "Return the article headers of ID."
4398   (car (gnus-id-to-thread id)))
4399
4400 (defun gnus-article-displayed-root-p (article)
4401   "Say whether ARTICLE is a root(ish) article."
4402   (let ((level (gnus-summary-thread-level article))
4403         (refs (mail-header-references  (gnus-summary-article-header article)))
4404         particle)
4405     (cond
4406      ((null level) nil)
4407      ((zerop level) t)
4408      ((null refs) t)
4409      ((null (gnus-parent-id refs)) t)
4410      ((and (= 1 level)
4411            (null (setq particle (gnus-id-to-article
4412                                  (gnus-parent-id refs))))
4413            (null (gnus-summary-thread-level particle)))))))
4414
4415 (defun gnus-root-id (id)
4416   "Return the id of the root of the thread where ID appears."
4417   (let (last-id prev)
4418     (while (and id (setq prev (car (gnus-id-to-thread id))))
4419       (setq last-id id
4420             id (gnus-parent-id (mail-header-references prev))))
4421     last-id))
4422
4423 (defun gnus-articles-in-thread (thread)
4424   "Return the list of articles in THREAD."
4425   (cons (mail-header-number (car thread))
4426         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4427
4428 (defun gnus-remove-thread (id &optional dont-remove)
4429   "Remove the thread that has ID in it."
4430   (let (headers thread last-id)
4431     ;; First go up in this thread until we find the root.
4432     (setq last-id (gnus-root-id id)
4433           headers (message-flatten-list (gnus-id-to-thread last-id)))
4434     ;; We have now found the real root of this thread.  It might have
4435     ;; been gathered into some loose thread, so we have to search
4436     ;; through the threads to find the thread we wanted.
4437     (let ((threads gnus-newsgroup-threads)
4438           sub)
4439       (while threads
4440         (setq sub (car threads))
4441         (if (stringp (car sub))
4442             ;; This is a gathered thread, so we look at the roots
4443             ;; below it to find whether this article is in this
4444             ;; gathered root.
4445             (progn
4446               (setq sub (cdr sub))
4447               (while sub
4448                 (when (member (caar sub) headers)
4449                   (setq thread (car threads)
4450                         threads nil
4451                         sub nil))
4452                 (setq sub (cdr sub))))
4453           ;; It's an ordinary thread, so we check it.
4454           (when (eq (car sub) (car headers))
4455             (setq thread sub
4456                   threads nil)))
4457         (setq threads (cdr threads)))
4458       ;; If this article is in no thread, then it's a root.
4459       (if thread
4460           (unless dont-remove
4461             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4462         (setq thread (gnus-id-to-thread last-id)))
4463       (when thread
4464         (prog1
4465             thread                      ; We return this thread.
4466           (unless dont-remove
4467             (if (stringp (car thread))
4468                 (progn
4469                   ;; If we use dummy roots, then we have to remove the
4470                   ;; dummy root as well.
4471                   (when (eq gnus-summary-make-false-root 'dummy)
4472                     ;; We go to the dummy root by going to
4473                     ;; the first sub-"thread", and then one line up.
4474                     (gnus-summary-goto-article
4475                      (mail-header-number (caadr thread)))
4476                     (forward-line -1)
4477                     (gnus-delete-line)
4478                     (gnus-data-compute-positions))
4479                   (setq thread (cdr thread))
4480                   (while thread
4481                     (gnus-remove-thread-1 (car thread))
4482                     (setq thread (cdr thread))))
4483               (gnus-remove-thread-1 thread))))))))
4484
4485 (defun gnus-remove-thread-1 (thread)
4486   "Remove the thread THREAD recursively."
4487   (let ((number (mail-header-number (pop thread)))
4488         d)
4489     (setq thread (reverse thread))
4490     (while thread
4491       (gnus-remove-thread-1 (pop thread)))
4492     (when (setq d (gnus-data-find number))
4493       (goto-char (gnus-data-pos d))
4494       (gnus-summary-show-thread)
4495       (gnus-data-remove
4496        number
4497        (- (point-at-bol)
4498           (prog1
4499               (1+ (point-at-eol))
4500             (gnus-delete-line)))))))
4501
4502 (defun gnus-sort-threads-1 (threads func)
4503   (sort (mapcar (lambda (thread)
4504                   (cons (car thread)
4505                         (and (cdr thread)
4506                              (gnus-sort-threads-1 (cdr thread) func))))
4507                 threads) func))
4508
4509 (defun gnus-sort-threads (threads)
4510   "Sort THREADS."
4511   (if (not gnus-thread-sort-functions)
4512       threads
4513     (gnus-message 8 "Sorting threads...")
4514     (let ((max-lisp-eval-depth 5000))
4515       (prog1 (gnus-sort-threads-1
4516          threads
4517          (gnus-make-sort-function gnus-thread-sort-functions))
4518         (gnus-message 8 "Sorting threads...done")))))
4519
4520 (defun gnus-sort-articles (articles)
4521   "Sort ARTICLES."
4522   (when gnus-article-sort-functions
4523     (gnus-message 7 "Sorting articles...")
4524     (prog1
4525         (setq gnus-newsgroup-headers
4526               (sort articles (gnus-make-sort-function
4527                               gnus-article-sort-functions)))
4528       (gnus-message 7 "Sorting articles...done"))))
4529
4530 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4531 (defmacro gnus-thread-header (thread)
4532   "Return header of first article in THREAD.
4533 Note that THREAD must never, ever be anything else than a variable -
4534 using some other form will lead to serious barfage."
4535   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4536   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4537   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4538         (vector thread) 2))
4539
4540 (defsubst gnus-article-sort-by-number (h1 h2)
4541   "Sort articles by article number."
4542   (< (mail-header-number h1)
4543      (mail-header-number h2)))
4544
4545 (defun gnus-thread-sort-by-number (h1 h2)
4546   "Sort threads by root article number."
4547   (gnus-article-sort-by-number
4548    (gnus-thread-header h1) (gnus-thread-header h2)))
4549
4550 (defsubst gnus-article-sort-by-random (h1 h2)
4551   "Sort articles by article number."
4552   (zerop (random 2)))
4553
4554 (defun gnus-thread-sort-by-random (h1 h2)
4555   "Sort threads by root article number."
4556   (gnus-article-sort-by-random
4557    (gnus-thread-header h1) (gnus-thread-header h2)))
4558
4559 (defsubst gnus-article-sort-by-lines (h1 h2)
4560   "Sort articles by article Lines header."
4561   (< (mail-header-lines h1)
4562      (mail-header-lines h2)))
4563
4564 (defun gnus-thread-sort-by-lines (h1 h2)
4565   "Sort threads by root article Lines header."
4566   (gnus-article-sort-by-lines
4567    (gnus-thread-header h1) (gnus-thread-header h2)))
4568
4569 (defsubst gnus-article-sort-by-chars (h1 h2)
4570   "Sort articles by octet length."
4571   (< (mail-header-chars h1)
4572      (mail-header-chars h2)))
4573
4574 (defun gnus-thread-sort-by-chars (h1 h2)
4575   "Sort threads by root article octet length."
4576   (gnus-article-sort-by-chars
4577    (gnus-thread-header h1) (gnus-thread-header h2)))
4578
4579 (defsubst gnus-article-sort-by-author (h1 h2)
4580   "Sort articles by root author."
4581   (string-lessp
4582    (let ((extract (funcall
4583                    gnus-extract-address-components
4584                    (mail-header-from h1))))
4585      (or (car extract) (cadr extract) ""))
4586    (let ((extract (funcall
4587                    gnus-extract-address-components
4588                    (mail-header-from h2))))
4589      (or (car extract) (cadr extract) ""))))
4590
4591 (defun gnus-thread-sort-by-author (h1 h2)
4592   "Sort threads by root author."
4593   (gnus-article-sort-by-author
4594    (gnus-thread-header h1)  (gnus-thread-header h2)))
4595
4596 (defsubst gnus-article-sort-by-recipient (h1 h2)
4597   "Sort articles by recipient."
4598   (string-lessp
4599    (let ((extract (funcall
4600                    gnus-extract-address-components
4601                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4602      (or (car extract) (cadr extract)))
4603    (let ((extract (funcall
4604                    gnus-extract-address-components
4605                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4606      (or (car extract) (cadr extract)))))
4607
4608 (defun gnus-thread-sort-by-recipient (h1 h2)
4609   "Sort threads by root recipient."
4610   (gnus-article-sort-by-recipient
4611    (gnus-thread-header h1) (gnus-thread-header h2)))
4612
4613 (defsubst gnus-article-sort-by-subject (h1 h2)
4614   "Sort articles by root subject."
4615   (string-lessp
4616    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4617    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4618
4619 (defun gnus-thread-sort-by-subject (h1 h2)
4620   "Sort threads by root subject."
4621   (gnus-article-sort-by-subject
4622    (gnus-thread-header h1) (gnus-thread-header h2)))
4623
4624 (defsubst gnus-article-sort-by-date (h1 h2)
4625   "Sort articles by root article date."
4626   (time-less-p
4627    (gnus-date-get-time (mail-header-date h1))
4628    (gnus-date-get-time (mail-header-date h2))))
4629
4630 (defun gnus-thread-sort-by-date (h1 h2)
4631   "Sort threads by root article date."
4632   (gnus-article-sort-by-date
4633    (gnus-thread-header h1) (gnus-thread-header h2)))
4634
4635 (defsubst gnus-article-sort-by-score (h1 h2)
4636   "Sort articles by root article score.
4637 Unscored articles will be counted as having a score of zero."
4638   (> (or (cdr (assq (mail-header-number h1)
4639                     gnus-newsgroup-scored))
4640          gnus-summary-default-score 0)
4641      (or (cdr (assq (mail-header-number h2)
4642                     gnus-newsgroup-scored))
4643          gnus-summary-default-score 0)))
4644
4645 (defun gnus-thread-sort-by-score (h1 h2)
4646   "Sort threads by root article score."
4647   (gnus-article-sort-by-score
4648    (gnus-thread-header h1) (gnus-thread-header h2)))
4649
4650 (defun gnus-thread-sort-by-total-score (h1 h2)
4651   "Sort threads by the sum of all scores in the thread.
4652 Unscored articles will be counted as having a score of zero."
4653   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4654
4655 (defun gnus-thread-total-score (thread)
4656   ;; This function find the total score of THREAD.
4657   (cond
4658    ((null thread)
4659     0)
4660    ((consp thread)
4661     (if (stringp (car thread))
4662         (apply gnus-thread-score-function 0
4663                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4664       (gnus-thread-total-score-1 thread)))
4665    (t
4666     (gnus-thread-total-score-1 (list thread)))))
4667
4668 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4669   "Sort threads such that the thread with the most recently arrived article comes first."
4670   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4671
4672 (defun gnus-thread-highest-number (thread)
4673   "Return the highest article number in THREAD."
4674   (apply 'max (mapcar (lambda (header)
4675                         (mail-header-number header))
4676                       (message-flatten-list thread))))
4677
4678 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4679   "Sort threads such that the thread with the most recently dated article comes first."
4680   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4681
4682 (defun gnus-thread-latest-date (thread)
4683   "Return the highest article date in THREAD."
4684   (let ((previous-time 0))
4685     (apply 'max
4686            (mapcar
4687             (lambda (header)
4688               (setq previous-time
4689                     (condition-case ()
4690                         (time-to-seconds (mail-header-parse-date
4691                                           (mail-header-date header)))
4692                       (error previous-time))))
4693             (sort
4694              (message-flatten-list thread)
4695              (lambda (h1 h2)
4696                (< (mail-header-number h1)
4697                   (mail-header-number h2))))))))
4698
4699 (defun gnus-thread-total-score-1 (root)
4700   ;; This function find the total score of the thread below ROOT.
4701   (setq root (car root))
4702   (apply gnus-thread-score-function
4703          (or (append
4704               (mapcar 'gnus-thread-total-score
4705                       (cdr (gnus-id-to-thread (mail-header-id root))))
4706               (when (> (mail-header-number root) 0)
4707                 (list (or (cdr (assq (mail-header-number root)
4708                                      gnus-newsgroup-scored))
4709                           gnus-summary-default-score 0))))
4710              (list gnus-summary-default-score)
4711              '(0))))
4712
4713 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4714 (defvar gnus-tmp-prev-subject nil)
4715 (defvar gnus-tmp-false-parent nil)
4716 (defvar gnus-tmp-root-expunged nil)
4717 (defvar gnus-tmp-dummy-line nil)
4718
4719 (eval-when-compile (defvar gnus-tmp-header))
4720 (defun gnus-extra-header (type &optional header)
4721   "Return the extra header of TYPE."
4722   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4723       ""))
4724
4725 (defvar gnus-tmp-thread-tree-header-string "")
4726
4727 (defcustom gnus-sum-thread-tree-root "> "
4728   "With %B spec, used for the root of a thread.
4729 If nil, use subject instead."
4730   :version "22.1"
4731   :type '(radio (const :format "%v  " nil) string)
4732   :group 'gnus-thread)
4733 (defcustom gnus-sum-thread-tree-false-root "> "
4734   "With %B spec, used for a false root of a thread.
4735 If nil, use subject instead."
4736   :version "22.1"
4737   :type '(radio (const :format "%v  " nil) string)
4738   :group 'gnus-thread)
4739 (defcustom gnus-sum-thread-tree-single-indent ""
4740   "With %B spec, used for a thread with just one message.
4741 If nil, use subject instead."
4742   :version "22.1"
4743   :type '(radio (const :format "%v  " nil) string)
4744   :group 'gnus-thread)
4745 (defcustom gnus-sum-thread-tree-vertical "| "
4746   "With %B spec, used for drawing a vertical line."
4747   :version "22.1"
4748   :type 'string
4749   :group 'gnus-thread)
4750 (defcustom gnus-sum-thread-tree-indent "  "
4751   "With %B spec, used for indenting."
4752   :version "22.1"
4753   :type 'string
4754   :group 'gnus-thread)
4755 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4756   "With %B spec, used for a leaf with brothers."
4757   :version "22.1"
4758   :type 'string
4759   :group 'gnus-thread)
4760 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4761   "With %B spec, used for a leaf without brothers."
4762   :version "22.1"
4763   :type 'string
4764   :group 'gnus-thread)
4765
4766 (defun gnus-summary-prepare-threads (threads)
4767   "Prepare summary buffer from THREADS and indentation LEVEL.
4768 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4769 or a straight list of headers."
4770   (gnus-message 7 "Generating summary...")
4771
4772   (setq gnus-newsgroup-threads threads)
4773   (beginning-of-line)
4774
4775   (let ((gnus-tmp-level 0)
4776         (default-score (or gnus-summary-default-score 0))
4777         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4778         (building-line-count gnus-summary-display-while-building)
4779         (building-count (integerp gnus-summary-display-while-building))
4780         thread number subject stack state gnus-tmp-gathered beg-match
4781         new-roots gnus-tmp-new-adopts thread-end simp-subject
4782         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4783         gnus-tmp-replied gnus-tmp-subject-or-nil
4784         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4785         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4786         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4787         tree-stack)
4788
4789     (setq gnus-tmp-prev-subject nil
4790           gnus-tmp-thread-tree-header-string "")
4791
4792     (if (vectorp (car threads))
4793         ;; If this is a straight (sic) list of headers, then a
4794         ;; threaded summary display isn't required, so we just create
4795         ;; an unthreaded one.
4796         (gnus-summary-prepare-unthreaded threads)
4797
4798       ;; Do the threaded display.
4799
4800       (if gnus-summary-display-while-building
4801           (switch-to-buffer (buffer-name)))
4802       (while (or threads stack gnus-tmp-new-adopts new-roots)
4803
4804         (if (and (= gnus-tmp-level 0)
4805                  (or (not stack)
4806                      (= (caar stack) 0))
4807                  (not gnus-tmp-false-parent)
4808                  (or gnus-tmp-new-adopts new-roots))
4809             (if gnus-tmp-new-adopts
4810                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4811                       thread (list (car gnus-tmp-new-adopts))
4812                       gnus-tmp-header (caar thread)
4813                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4814               (when new-roots
4815                 (setq thread (list (car new-roots))
4816                       gnus-tmp-header (caar thread)
4817                       new-roots (cdr new-roots))))
4818
4819           (if threads
4820               ;; If there are some threads, we do them before the
4821               ;; threads on the stack.
4822               (setq thread threads
4823                     gnus-tmp-header (caar thread))
4824             ;; There were no current threads, so we pop something off
4825             ;; the stack.
4826             (setq state (car stack)
4827                   gnus-tmp-level (car state)
4828                   tree-stack (cadr state)
4829                   thread (caddr state)
4830                   stack (cdr stack)
4831                   gnus-tmp-header (caar thread))))
4832
4833         (setq gnus-tmp-false-parent nil)
4834         (setq gnus-tmp-root-expunged nil)
4835         (setq thread-end nil)
4836
4837         (if (stringp gnus-tmp-header)
4838             ;; The header is a dummy root.
4839             (cond
4840              ((eq gnus-summary-make-false-root 'adopt)
4841               ;; We let the first article adopt the rest.
4842               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4843                                                (cddar thread)))
4844               (setq gnus-tmp-gathered
4845                     (nconc (mapcar
4846                             (lambda (h) (mail-header-number (car h)))
4847                             (cddar thread))
4848                            gnus-tmp-gathered))
4849               (setq thread (cons (list (caar thread)
4850                                        (cadar thread))
4851                                  (cdr thread)))
4852               (setq gnus-tmp-level -1
4853                     gnus-tmp-false-parent t))
4854              ((eq gnus-summary-make-false-root 'empty)
4855               ;; We print adopted articles with empty subject fields.
4856               (setq gnus-tmp-gathered
4857                     (nconc (mapcar
4858                             (lambda (h) (mail-header-number (car h)))
4859                             (cddar thread))
4860                            gnus-tmp-gathered))
4861               (setq gnus-tmp-level -1))
4862              ((eq gnus-summary-make-false-root 'dummy)
4863               ;; We remember that we probably want to output a dummy
4864               ;; root.
4865               (setq gnus-tmp-dummy-line gnus-tmp-header)
4866               (setq gnus-tmp-prev-subject gnus-tmp-header))
4867              (t
4868               ;; We do not make a root for the gathered
4869               ;; sub-threads at all.
4870               (setq gnus-tmp-level -1)))
4871
4872           (setq number (mail-header-number gnus-tmp-header)
4873                 subject (mail-header-subject gnus-tmp-header)
4874                 simp-subject (gnus-simplify-subject-fully subject))
4875
4876           (cond
4877            ;; If the thread has changed subject, we might want to make
4878            ;; this subthread into a root.
4879            ((and (null gnus-thread-ignore-subject)
4880                  (not (zerop gnus-tmp-level))
4881                  gnus-tmp-prev-subject
4882                  (not (string= gnus-tmp-prev-subject simp-subject)))
4883             (setq new-roots (nconc new-roots (list (car thread)))
4884                   thread-end t
4885                   gnus-tmp-header nil))
4886            ;; If the article lies outside the current limit,
4887            ;; then we do not display it.
4888            ((not (memq number gnus-newsgroup-limit))
4889             (setq gnus-tmp-gathered
4890                   (nconc (mapcar
4891                           (lambda (h) (mail-header-number (car h)))
4892                           (cdar thread))
4893                          gnus-tmp-gathered))
4894             (setq gnus-tmp-new-adopts (if (cdar thread)
4895                                           (append gnus-tmp-new-adopts
4896                                                   (cdar thread))
4897                                         gnus-tmp-new-adopts)
4898                   thread-end t
4899                   gnus-tmp-header nil)
4900             (when (zerop gnus-tmp-level)
4901               (setq gnus-tmp-root-expunged t)))
4902            ;; Perhaps this article is to be marked as read?
4903            ((and gnus-summary-mark-below
4904                  (< (or (cdr (assq number gnus-newsgroup-scored))
4905                         default-score)
4906                     gnus-summary-mark-below)
4907                  ;; Don't touch sparse articles.
4908                  (not (gnus-summary-article-sparse-p number))
4909                  (not (gnus-summary-article-ancient-p number)))
4910             (setq gnus-newsgroup-unreads
4911                   (delq number gnus-newsgroup-unreads))
4912             (if gnus-newsgroup-auto-expire
4913                 (setq gnus-newsgroup-expirable
4914                       (gnus-add-to-sorted-list
4915                        gnus-newsgroup-expirable number))
4916               (push (cons number gnus-low-score-mark)
4917                     gnus-newsgroup-reads))))
4918
4919           (when gnus-tmp-header
4920             ;; We may have an old dummy line to output before this
4921             ;; article.
4922             (when (and gnus-tmp-dummy-line
4923                        (gnus-subject-equal
4924                         gnus-tmp-dummy-line
4925                         (mail-header-subject gnus-tmp-header)))
4926               (gnus-summary-insert-dummy-line
4927                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4928               (setq gnus-tmp-dummy-line nil))
4929
4930             ;; Compute the mark.
4931             (setq gnus-tmp-unread (gnus-article-mark number))
4932
4933             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4934                                   gnus-tmp-header gnus-tmp-level)
4935                   gnus-newsgroup-data)
4936
4937             ;; Actually insert the line.
4938             (setq
4939              gnus-tmp-subject-or-nil
4940              (cond
4941               ((and gnus-thread-ignore-subject
4942                     gnus-tmp-prev-subject
4943                     (not (string= gnus-tmp-prev-subject simp-subject)))
4944                subject)
4945               ((zerop gnus-tmp-level)
4946                (if (and (eq gnus-summary-make-false-root 'empty)
4947                         (memq number gnus-tmp-gathered)
4948                         gnus-tmp-prev-subject
4949                         (string= gnus-tmp-prev-subject simp-subject))
4950                    gnus-summary-same-subject
4951                  subject))
4952               (t gnus-summary-same-subject)))
4953             (if (and (eq gnus-summary-make-false-root 'adopt)
4954                      (= gnus-tmp-level 1)
4955                      (memq number gnus-tmp-gathered))
4956                 (setq gnus-tmp-opening-bracket ?\<
4957                       gnus-tmp-closing-bracket ?\>)
4958               (setq gnus-tmp-opening-bracket ?\[
4959                     gnus-tmp-closing-bracket ?\]))
4960             (setq
4961              gnus-tmp-indentation
4962              (aref gnus-thread-indent-array gnus-tmp-level)
4963              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4964              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4965                                 gnus-summary-default-score 0)
4966              gnus-tmp-score-char
4967              (if (or (null gnus-summary-default-score)
4968                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4969                          gnus-summary-zcore-fuzz))
4970                  ?                      ;Whitespace
4971                (if (< gnus-tmp-score gnus-summary-default-score)
4972                    gnus-score-below-mark gnus-score-over-mark))
4973              gnus-tmp-replied
4974              (cond ((memq number gnus-newsgroup-processable)
4975                     gnus-process-mark)
4976                    ((memq number gnus-newsgroup-cached)
4977                     gnus-cached-mark)
4978                    ((memq number gnus-newsgroup-replied)
4979                     gnus-replied-mark)
4980                    ((memq number gnus-newsgroup-forwarded)
4981                     gnus-forwarded-mark)
4982                    ((memq number gnus-newsgroup-saved)
4983                     gnus-saved-mark)
4984                    ((memq number gnus-newsgroup-recent)
4985                     gnus-recent-mark)
4986                    ((memq number gnus-newsgroup-unseen)
4987                     gnus-unseen-mark)
4988                    (t gnus-no-mark))
4989              gnus-tmp-downloaded
4990              (cond ((memq number gnus-newsgroup-undownloaded)
4991                     gnus-undownloaded-mark)
4992                    (gnus-newsgroup-agentized
4993                     gnus-downloaded-mark)
4994                    (t
4995                     gnus-no-mark))
4996              gnus-tmp-from (mail-header-from gnus-tmp-header)
4997              gnus-tmp-name
4998              (cond
4999               ((string-match "<[^>]+> *$" gnus-tmp-from)
5000                (setq beg-match (match-beginning 0))
5001                (or (and (string-match "^\".+\"" gnus-tmp-from)
5002                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5003                    (substring gnus-tmp-from 0 beg-match)))
5004               ((string-match "(.+)" gnus-tmp-from)
5005                (substring gnus-tmp-from
5006                           (1+ (match-beginning 0)) (1- (match-end 0))))
5007               (t gnus-tmp-from))
5008
5009              ;; Do the %B string
5010              gnus-tmp-thread-tree-header-string
5011              (cond
5012               ((not gnus-show-threads) "")
5013               ((zerop gnus-tmp-level)
5014                (cond ((cdar thread)
5015                       (or gnus-sum-thread-tree-root subject))
5016                      (gnus-tmp-new-adopts
5017                       (or gnus-sum-thread-tree-false-root subject))
5018                      (t
5019                       (or gnus-sum-thread-tree-single-indent subject))))
5020               (t
5021                (concat (apply 'concat
5022                               (mapcar (lambda (item)
5023                                         (if (= item 1)
5024                                             gnus-sum-thread-tree-vertical
5025                                           gnus-sum-thread-tree-indent))
5026                                       (cdr (reverse tree-stack))))
5027                        (if (nth 1 thread)
5028                            gnus-sum-thread-tree-leaf-with-other
5029                          gnus-sum-thread-tree-single-leaf)))))
5030             (when (string= gnus-tmp-name "")
5031               (setq gnus-tmp-name gnus-tmp-from))
5032             (unless (numberp gnus-tmp-lines)
5033               (setq gnus-tmp-lines -1))
5034             (if (= gnus-tmp-lines -1)
5035                 (setq gnus-tmp-lines "?")
5036               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5037               (gnus-put-text-property
5038              (point)
5039              (progn (eval gnus-summary-line-format-spec) (point))
5040                'gnus-number number)
5041             (when gnus-visual-p
5042               (forward-line -1)
5043               (gnus-run-hooks 'gnus-summary-update-hook)
5044               (forward-line 1))
5045
5046             (setq gnus-tmp-prev-subject simp-subject)))
5047
5048         (when (nth 1 thread)
5049           (push (list (max 0 gnus-tmp-level)
5050                       (copy-sequence tree-stack)
5051                       (nthcdr 1 thread))
5052                 stack))
5053         (push (if (nth 1 thread) 1 0) tree-stack)
5054         (incf gnus-tmp-level)
5055         (setq threads (if thread-end nil (cdar thread)))
5056         (if gnus-summary-display-while-building
5057             (if building-count
5058                 (progn
5059                   ;; use a set frequency
5060                   (setq building-line-count (1- building-line-count))
5061                   (when (= building-line-count 0)
5062                     (sit-for 0)
5063                     (setq building-line-count
5064                           gnus-summary-display-while-building)))
5065               ;; always
5066               (sit-for 0)))
5067         (unless threads
5068           (setq gnus-tmp-level 0)))))
5069   (gnus-message 7 "Generating summary...done"))
5070
5071 (defun gnus-summary-prepare-unthreaded (headers)
5072   "Generate an unthreaded summary buffer based on HEADERS."
5073   (let (header number mark)
5074
5075     (beginning-of-line)
5076
5077     (while headers
5078       ;; We may have to root out some bad articles...
5079       (when (memq (setq number (mail-header-number
5080                                 (setq header (pop headers))))
5081                   gnus-newsgroup-limit)
5082         ;; Mark article as read when it has a low score.
5083         (when (and gnus-summary-mark-below
5084                    (< (or (cdr (assq number gnus-newsgroup-scored))
5085                           gnus-summary-default-score 0)
5086                       gnus-summary-mark-below)
5087                    (not (gnus-summary-article-ancient-p number)))
5088           (setq gnus-newsgroup-unreads
5089                 (delq number gnus-newsgroup-unreads))
5090           (if gnus-newsgroup-auto-expire
5091               (push number gnus-newsgroup-expirable)
5092             (push (cons number gnus-low-score-mark)
5093                   gnus-newsgroup-reads)))
5094
5095         (setq mark (gnus-article-mark number))
5096         (push (gnus-data-make number mark (1+ (point)) header 0)
5097               gnus-newsgroup-data)
5098         (gnus-summary-insert-line
5099          header 0 number
5100          (memq number gnus-newsgroup-undownloaded)
5101          mark (memq number gnus-newsgroup-replied)
5102          (memq number gnus-newsgroup-expirable)
5103          (mail-header-subject header) nil
5104          (cdr (assq number gnus-newsgroup-scored))
5105          (memq number gnus-newsgroup-processable))))))
5106
5107 (defun gnus-summary-remove-list-identifiers ()
5108   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5109   (let ((regexp (if (consp gnus-list-identifiers)
5110                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5111                   gnus-list-identifiers))
5112         changed subject)
5113     (when regexp
5114       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5115       (dolist (header gnus-newsgroup-headers)
5116         (setq subject (mail-header-subject header)
5117               changed nil)
5118         (while (string-match regexp subject)
5119           (setq subject
5120                 (concat (substring subject 0 (match-beginning 1))
5121                         (substring subject (match-end 0)))
5122                 changed t))
5123         (when changed
5124           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5125             (setq subject
5126                   (concat (substring subject 0 (match-beginning 1))
5127                           (substring subject (match-end 1)))))
5128           (mail-header-set-subject header subject))))))
5129
5130 (defun gnus-fetch-headers (articles)
5131   "Fetch headers of ARTICLES."
5132   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5133     (gnus-message 5 "Fetching headers for %s..." name)
5134     (prog1
5135         (if (eq 'nov
5136                 (setq gnus-headers-retrieved-by
5137                       (gnus-retrieve-headers
5138                        articles gnus-newsgroup-name
5139                        ;; We might want to fetch old headers, but
5140                        ;; not if there is only 1 article.
5141                        (and (or (and
5142                                  (not (eq gnus-fetch-old-headers 'some))
5143                                  (not (numberp gnus-fetch-old-headers)))
5144                                 (> (length articles) 1))
5145                             gnus-fetch-old-headers))))
5146             (gnus-get-newsgroup-headers-xover
5147              articles nil nil gnus-newsgroup-name t)
5148           (gnus-get-newsgroup-headers))
5149       (gnus-message 5 "Fetching headers for %s...done" name))))
5150
5151 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5152   "Select newsgroup GROUP.
5153 If READ-ALL is non-nil, all articles in the group are selected.
5154 If SELECT-ARTICLES, only select those articles from GROUP."
5155   (let* ((entry (gnus-group-entry group))
5156          ;;!!! Dirty hack; should be removed.
5157          (gnus-summary-ignore-duplicates
5158           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5159               t
5160             gnus-summary-ignore-duplicates))
5161          (info (nth 2 entry))
5162          articles fetched-articles cached)
5163
5164     (unless (gnus-check-server
5165              (set (make-local-variable 'gnus-current-select-method)
5166                   (gnus-find-method-for-group group)))
5167       (error "Couldn't open server"))
5168
5169     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5170         (gnus-activate-group group)     ; Or we can activate it...
5171         (progn                          ; Or we bug out.
5172           (when (equal major-mode 'gnus-summary-mode)
5173             (gnus-kill-buffer (current-buffer)))
5174           (error "Couldn't activate group %s: %s"
5175                  group (gnus-status-message group))))
5176
5177     (unless (gnus-request-group group t)
5178       (when (equal major-mode 'gnus-summary-mode)
5179         (gnus-kill-buffer (current-buffer)))
5180       (error "Couldn't request group %s: %s"
5181              group (gnus-status-message group)))
5182
5183     (when gnus-agent
5184       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5185
5186       (setq gnus-summary-use-undownloaded-faces
5187             (gnus-agent-find-parameter
5188              group
5189              'agent-enable-undownloaded-faces)))
5190
5191     (setq gnus-newsgroup-name group
5192           gnus-newsgroup-unselected nil
5193           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5194
5195     (let ((display (gnus-group-find-parameter group 'display)))
5196       (setq gnus-newsgroup-display
5197             (cond
5198              ((not (zerop (or (car-safe read-all) 0)))
5199               ;; The user entered the group with C-u SPC/RET, let's show
5200               ;; all articles.
5201               'gnus-not-ignore)
5202              ((eq display 'all)
5203               'gnus-not-ignore)
5204              ((arrayp display)
5205               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5206              ((numberp display)
5207               ;; The following is probably the "correct" solution, but
5208               ;; it makes Gnus fetch all headers and then limit the
5209               ;; articles (which is slow), so instead we hack the
5210               ;; select-articles parameter instead. -- Simon Josefsson
5211               ;; <jas@kth.se>
5212               ;;
5213               ;; (gnus-byte-compile
5214               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5215               ;;                         display)))))
5216               (setq select-articles
5217                     (gnus-uncompress-range
5218                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5219                              (if (> tmp 0)
5220                                  tmp
5221                                1))
5222                            (cdr (gnus-active group)))))
5223               nil)
5224              (t
5225               nil))))
5226
5227     (gnus-summary-setup-default-charset)
5228
5229     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5230     (when (gnus-virtual-group-p group)
5231       (setq cached gnus-newsgroup-cached))
5232
5233     (setq gnus-newsgroup-unreads
5234           (gnus-sorted-ndifference
5235            (gnus-sorted-ndifference gnus-newsgroup-unreads
5236                                     gnus-newsgroup-marked)
5237            gnus-newsgroup-dormant))
5238
5239     (setq gnus-newsgroup-processable nil)
5240
5241     (gnus-update-read-articles group gnus-newsgroup-unreads)
5242
5243     ;; Adjust and set lists of article marks.
5244     (when info
5245       (gnus-adjust-marked-articles info))
5246     (if (setq articles select-articles)
5247         (setq gnus-newsgroup-unselected
5248               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5249       (setq articles (gnus-articles-to-read group read-all)))
5250
5251     (cond
5252      ((null articles)
5253       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5254       'quit)
5255      ((eq articles 0) nil)
5256      (t
5257       ;; Init the dependencies hash table.
5258       (setq gnus-newsgroup-dependencies
5259             (gnus-make-hashtable (length articles)))
5260       (gnus-set-global-variables)
5261       ;; Retrieve the headers and read them in.
5262
5263       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5264
5265       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5266       (when cached
5267         (setq gnus-newsgroup-cached cached))
5268
5269       ;; Suppress duplicates?
5270       (when gnus-suppress-duplicates
5271         (gnus-dup-suppress-articles))
5272
5273       ;; Set the initial limit.
5274       (setq gnus-newsgroup-limit (copy-sequence articles))
5275       ;; Remove canceled articles from the list of unread articles.
5276       (setq fetched-articles
5277             (mapcar (lambda (headers) (mail-header-number headers))
5278                     gnus-newsgroup-headers))
5279       (setq gnus-newsgroup-articles fetched-articles)
5280       (setq gnus-newsgroup-unreads
5281             (gnus-sorted-nintersection
5282              gnus-newsgroup-unreads fetched-articles))
5283       (gnus-compute-unseen-list)
5284
5285       ;; Removed marked articles that do not exist.
5286       (gnus-update-missing-marks
5287        (gnus-sorted-difference articles fetched-articles))
5288       ;; We might want to build some more threads first.
5289       (when (and gnus-fetch-old-headers
5290                  (eq gnus-headers-retrieved-by 'nov))
5291         (if (eq gnus-fetch-old-headers 'invisible)
5292             (gnus-build-all-threads)
5293           (gnus-build-old-threads)))
5294       ;; Let the Gnus agent mark articles as read.
5295       (when gnus-agent
5296         (gnus-agent-get-undownloaded-list))
5297       ;; Remove list identifiers from subject
5298       (when gnus-list-identifiers
5299         (gnus-summary-remove-list-identifiers))
5300       ;; Check whether auto-expire is to be done in this group.
5301       (setq gnus-newsgroup-auto-expire
5302             (gnus-group-auto-expirable-p group))
5303       ;; Set up the article buffer now, if necessary.
5304       (unless gnus-single-article-buffer
5305         (gnus-article-setup-buffer))
5306       ;; First and last article in this newsgroup.
5307       (when gnus-newsgroup-headers
5308         (setq gnus-newsgroup-begin
5309               (mail-header-number (car gnus-newsgroup-headers))
5310               gnus-newsgroup-end
5311               (mail-header-number
5312                (gnus-last-element gnus-newsgroup-headers))))
5313       ;; GROUP is successfully selected.
5314       (or gnus-newsgroup-headers t)))))
5315
5316 (defun gnus-compute-unseen-list ()
5317   ;; The `seen' marks are treated specially.
5318   (if (not gnus-newsgroup-seen)
5319       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5320     (setq gnus-newsgroup-unseen
5321           (gnus-inverse-list-range-intersection
5322            gnus-newsgroup-articles gnus-newsgroup-seen))))
5323
5324 (defun gnus-summary-display-make-predicate (display)
5325   (require 'gnus-agent)
5326   (when (= (length display) 1)
5327     (setq display (car display)))
5328   (unless gnus-summary-display-cache
5329     (dolist (elem (append '((unread . unread)
5330                             (read . read)
5331                             (unseen . unseen))
5332                           gnus-article-mark-lists))
5333       (push (cons (cdr elem)
5334                   (gnus-byte-compile
5335                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5336             gnus-summary-display-cache)))
5337   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5338         (gnus-category-predicate-cache gnus-summary-display-cache))
5339     (gnus-get-predicate display)))
5340
5341 ;; Uses the dynamically bound `number' variable.
5342 (eval-when-compile
5343   (defvar number))
5344 (defun gnus-article-marked-p (type &optional article)
5345   (let ((article (or article number)))
5346     (cond
5347      ((eq type 'tick)
5348       (memq article gnus-newsgroup-marked))
5349      ((eq type 'spam)
5350       (memq article gnus-newsgroup-spam-marked))
5351      ((eq type 'unsend)
5352       (memq article gnus-newsgroup-unsendable))
5353      ((eq type 'undownload)
5354       (memq article gnus-newsgroup-undownloaded))
5355      ((eq type 'download)
5356       (memq article gnus-newsgroup-downloadable))
5357      ((eq type 'unread)
5358       (memq article gnus-newsgroup-unreads))
5359      ((eq type 'read)
5360       (memq article gnus-newsgroup-reads))
5361      ((eq type 'dormant)
5362       (memq article gnus-newsgroup-dormant) )
5363      ((eq type 'expire)
5364       (memq article gnus-newsgroup-expirable))
5365      ((eq type 'reply)
5366       (memq article gnus-newsgroup-replied))
5367      ((eq type 'killed)
5368       (memq article gnus-newsgroup-killed))
5369      ((eq type 'bookmark)
5370       (assq article gnus-newsgroup-bookmarks))
5371      ((eq type 'score)
5372       (assq article gnus-newsgroup-scored))
5373      ((eq type 'save)
5374       (memq article gnus-newsgroup-saved))
5375      ((eq type 'cache)
5376       (memq article gnus-newsgroup-cached))
5377      ((eq type 'forward)
5378       (memq article gnus-newsgroup-forwarded))
5379      ((eq type 'seen)
5380       (not (memq article gnus-newsgroup-unseen)))
5381      ((eq type 'recent)
5382       (memq article gnus-newsgroup-recent))
5383      (t t))))
5384
5385 (defun gnus-articles-to-read (group &optional read-all)
5386   "Find out what articles the user wants to read."
5387   (let* ((articles
5388           ;; Select all articles if `read-all' is non-nil, or if there
5389           ;; are no unread articles.
5390           (if (or read-all
5391                   (and (zerop (length gnus-newsgroup-marked))
5392                        (zerop (length gnus-newsgroup-unreads)))
5393                   ;; Fetch all if the predicate is non-nil.
5394                   gnus-newsgroup-display)
5395               ;; We want to select the headers for all the articles in
5396               ;; the group, so we select either all the active
5397               ;; articles in the group, or (if that's nil), the
5398               ;; articles in the cache.
5399               (or
5400                (gnus-uncompress-range (gnus-active group))
5401                (gnus-cache-articles-in-group group))
5402             ;; Select only the "normal" subset of articles.
5403             (gnus-sorted-nunion
5404              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5405              gnus-newsgroup-unreads)))
5406          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5407          (scored (length scored-list))
5408          (number (length articles))
5409          (marked (+ (length gnus-newsgroup-marked)
5410                     (length gnus-newsgroup-dormant)))
5411          (select
5412           (cond
5413            ((numberp read-all)
5414             read-all)
5415            ((numberp gnus-newsgroup-display)
5416             gnus-newsgroup-display)
5417            (t
5418             (condition-case ()
5419                 (cond
5420                  ((and (or (<= scored marked) (= scored number))
5421                        (numberp gnus-large-newsgroup)
5422                        (> number gnus-large-newsgroup))
5423                   (let* ((cursor-in-echo-area nil)
5424                          (initial (gnus-parameter-large-newsgroup-initial
5425                                    gnus-newsgroup-name))
5426                          (input
5427                           (read-string
5428                            (format
5429                             "How many articles from %s (%s %d): "
5430                             (gnus-limit-string
5431                              (gnus-group-decoded-name gnus-newsgroup-name)
5432                              35)
5433                             (if initial "max" "default")
5434                             number)
5435                            (if initial
5436                                (cons (number-to-string initial)
5437                                      0)))))
5438                     (if (string-match "^[ \t]*$" input) number input)))
5439                  ((and (> scored marked) (< scored number)
5440                        (> (- scored number) 20))
5441                   (let ((input
5442                          (read-string
5443                           (format "%s %s (%d scored, %d total): "
5444                                   "How many articles from"
5445                                   (gnus-group-decoded-name group)
5446                                   scored number))))
5447                     (if (string-match "^[ \t]*$" input)
5448                         number input)))
5449                  (t number))
5450               (quit
5451                (message "Quit getting the articles to read")
5452                nil))))))
5453     (setq select (if (stringp select) (string-to-number select) select))
5454     (if (or (null select) (zerop select))
5455         select
5456       (if (and (not (zerop scored)) (<= (abs select) scored))
5457           (progn
5458             (setq articles (sort scored-list '<))
5459             (setq number (length articles)))
5460         (setq articles (copy-sequence articles)))
5461
5462       (when (< (abs select) number)
5463         (if (< select 0)
5464             ;; Select the N oldest articles.
5465             (setcdr (nthcdr (1- (abs select)) articles) nil)
5466           ;; Select the N most recent articles.
5467           (setq articles (nthcdr (- number select) articles))))
5468       (setq gnus-newsgroup-unselected
5469             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5470       (when gnus-alter-articles-to-read-function
5471         (setq articles
5472               (sort
5473                (funcall gnus-alter-articles-to-read-function
5474                         gnus-newsgroup-name articles)
5475                '<)))
5476       articles)))
5477
5478 (defun gnus-killed-articles (killed articles)
5479   (let (out)
5480     (while articles
5481       (when (inline (gnus-member-of-range (car articles) killed))
5482         (push (car articles) out))
5483       (setq articles (cdr articles)))
5484     out))
5485
5486 (defun gnus-uncompress-marks (marks)
5487   "Uncompress the mark ranges in MARKS."
5488   (let ((uncompressed '(score bookmark))
5489         out)
5490     (while marks
5491       (if (memq (caar marks) uncompressed)
5492           (push (car marks) out)
5493         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5494       (setq marks (cdr marks)))
5495     out))
5496
5497 (defun gnus-article-mark-to-type (mark)
5498   "Return the type of MARK."
5499   (or (cadr (assq mark gnus-article-special-mark-lists))
5500       'list))
5501
5502 (defun gnus-article-unpropagatable-p (mark)
5503   "Return whether MARK should be propagated to back end."
5504   (memq mark gnus-article-unpropagated-mark-lists))
5505
5506 (defun gnus-adjust-marked-articles (info)
5507   "Set all article lists and remove all marks that are no longer valid."
5508   (let* ((marked-lists (gnus-info-marks info))
5509          (active (gnus-active (gnus-info-group info)))
5510          (min (car active))
5511          (max (cdr active))
5512          (types gnus-article-mark-lists)
5513          marks var articles article mark mark-type
5514          bgn end)
5515
5516     (dolist (marks marked-lists)
5517       (setq mark (car marks)
5518             mark-type (gnus-article-mark-to-type mark)
5519             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5520
5521       ;; We set the variable according to the type of the marks list,
5522       ;; and then adjust the marks to a subset of the active articles.
5523       (cond
5524        ;; Adjust "simple" lists - compressed yet unsorted
5525        ((eq mark-type 'list)
5526         ;; Simultaneously uncompress and clip to active range
5527         ;; See gnus-uncompress-range for a description of possible marks
5528         (let (l lh)
5529           (if (not (cadr marks))
5530               (set var nil)
5531             (setq articles (if (numberp (cddr marks))
5532                                (list (cdr marks))
5533                              (cdr marks))
5534                   lh (cons nil nil)
5535                   l lh)
5536
5537             (while (setq article (pop articles))
5538               (cond ((consp article)
5539                      (setq bgn (max (car article) min)
5540                            end (min (cdr article) max))
5541                      (while (<= bgn end)
5542                        (setq l (setcdr l (cons bgn nil))
5543                              bgn (1+ bgn))))
5544                     ((and (<= min article)
5545                           (>= max article))
5546                      (setq l (setcdr l (cons article nil))))))
5547             (set var (cdr lh)))))
5548        ;; Adjust assocs.
5549        ((eq mark-type 'tuple)
5550         (set var (setq articles (cdr marks)))
5551         (when (not (listp (cdr (symbol-value var))))
5552           (set var (list (symbol-value var))))
5553         (when (not (listp (cdr articles)))
5554           (setq articles (list articles)))
5555         (while articles
5556           (when (or (not (consp (setq article (pop articles))))
5557                     (< (car article) min)
5558                     (> (car article) max))
5559             (set var (delq article (symbol-value var))))))
5560        ;; Adjust ranges (sloppily).
5561        ((eq mark-type 'range)
5562         (cond
5563          ((eq mark 'seen)
5564           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5565           ;; It should be (seen (NUM1 . NUM2)).
5566           (when (numberp (cddr marks))
5567             (setcdr marks (list (cdr marks))))
5568           (setq articles (cdr marks))
5569           (while (and articles
5570                       (or (and (consp (car articles))
5571                                (> min (cdar articles)))
5572                           (and (numberp (car articles))
5573                                (> min (car articles)))))
5574             (pop articles))
5575           (set var articles))))))))
5576
5577 (defun gnus-update-missing-marks (missing)
5578   "Go through the list of MISSING articles and remove them from the mark lists."
5579   (when missing
5580     (let (var m)
5581       ;; Go through all types.
5582       (dolist (elem gnus-article-mark-lists)
5583         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5584           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5585           (when (symbol-value var)
5586             ;; This list has articles.  So we delete all missing
5587             ;; articles from it.
5588             (setq m missing)
5589             (while m
5590               (set var (delq (pop m) (symbol-value var))))))))))
5591
5592 (defun gnus-update-marks ()
5593   "Enter the various lists of marked articles into the newsgroup info list."
5594   (let ((types gnus-article-mark-lists)
5595         (info (gnus-get-info gnus-newsgroup-name))
5596         type list newmarked symbol delta-marks)
5597     (when info
5598       ;; Add all marks lists to the list of marks lists.
5599       (while (setq type (pop types))
5600         (setq list (symbol-value
5601                     (setq symbol
5602                           (intern (format "gnus-newsgroup-%s" (car type))))))
5603
5604         (when list
5605           ;; Get rid of the entries of the articles that have the
5606           ;; default score.
5607           (when (and (eq (cdr type) 'score)
5608                      gnus-save-score
5609                      list)
5610             (let* ((arts list)
5611                    (prev (cons nil list))
5612                    (all prev))
5613               (while arts
5614                 (if (or (not (consp (car arts)))
5615                         (= (cdar arts) gnus-summary-default-score))
5616                     (setcdr prev (cdr arts))
5617                   (setq prev arts))
5618                 (setq arts (cdr arts)))
5619               (setq list (cdr all)))))
5620
5621         (when (eq (cdr type) 'seen)
5622           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5623
5624         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5625           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5626
5627         (when (and (gnus-check-backend-function
5628                     'request-set-mark gnus-newsgroup-name)
5629                    (not (gnus-article-unpropagatable-p (cdr type))))
5630           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5631                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5632                  (add (gnus-remove-from-range
5633                        (gnus-copy-sequence list) old)))
5634             (when add
5635               (push (list add 'add (list (cdr type))) delta-marks))
5636             (when del
5637               (push (list del 'del (list (cdr type))) delta-marks))))
5638
5639         (when list
5640           (push (cons (cdr type) list) newmarked)))
5641
5642       (when delta-marks
5643         (unless (gnus-check-group gnus-newsgroup-name)
5644           (error "Can't open server for %s" gnus-newsgroup-name))
5645         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5646
5647       ;; Enter these new marks into the info of the group.
5648       (if (nthcdr 3 info)
5649           (setcar (nthcdr 3 info) newmarked)
5650         ;; Add the marks lists to the end of the info.
5651         (when newmarked
5652           (setcdr (nthcdr 2 info) (list newmarked))))
5653
5654       ;; Cut off the end of the info if there's nothing else there.
5655       (let ((i 5))
5656         (while (and (> i 2)
5657                     (not (nth i info)))
5658           (when (nthcdr (decf i) info)
5659             (setcdr (nthcdr i info) nil)))))))
5660
5661 (defun gnus-set-mode-line (where)
5662   "Set the mode line of the article or summary buffers.
5663 If WHERE is `summary', the summary mode line format will be used."
5664   ;; Is this mode line one we keep updated?
5665   (when (and (memq where gnus-updated-mode-lines)
5666              (symbol-value
5667               (intern (format "gnus-%s-mode-line-format-spec" where))))
5668     (let (mode-string)
5669       (save-excursion
5670         ;; We evaluate this in the summary buffer since these
5671         ;; variables are buffer-local to that buffer.
5672         (set-buffer gnus-summary-buffer)
5673        ;; We bind all these variables that are used in the `eval' form
5674         ;; below.
5675         (let* ((mformat (symbol-value
5676                          (intern
5677                           (format "gnus-%s-mode-line-format-spec" where))))
5678                (gnus-tmp-group-name (gnus-group-decoded-name
5679                                      gnus-newsgroup-name))
5680                (gnus-tmp-article-number (or gnus-current-article 0))
5681                (gnus-tmp-unread gnus-newsgroup-unreads)
5682                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5683                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5684                (gnus-tmp-unread-and-unselected
5685                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5686                             (zerop gnus-tmp-unselected))
5687                        "")
5688                       ((zerop gnus-tmp-unselected)
5689                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5690                       (t (format "{%d(+%d) more}"
5691                                  gnus-tmp-unread-and-unticked
5692                                  gnus-tmp-unselected))))
5693                (gnus-tmp-subject
5694                 (if (and gnus-current-headers
5695                          (vectorp gnus-current-headers))
5696                     (gnus-mode-string-quote
5697                      (mail-header-subject gnus-current-headers))
5698                   ""))
5699                bufname-length max-len
5700                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5701           (setq mode-string (eval mformat))
5702           (setq bufname-length (if (string-match "%b" mode-string)
5703                                    (- (length
5704                                        (buffer-name
5705                                         (if (eq where 'summary)
5706                                             nil
5707                                           (get-buffer gnus-article-buffer))))
5708                                       2)
5709                                  0))
5710           (setq max-len (max 4 (if gnus-mode-non-string-length
5711                                    (- (window-width)
5712                                       gnus-mode-non-string-length
5713                                       bufname-length)
5714                                  (length mode-string))))
5715           ;; We might have to chop a bit of the string off...
5716           (when (> (length mode-string) max-len)
5717             (setq mode-string
5718                   (concat (truncate-string-to-width mode-string (- max-len 3))
5719                           "...")))
5720           ;; Pad the mode string a bit.
5721           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5722       ;; Update the mode line.
5723       (setq mode-line-buffer-identification
5724             (gnus-mode-line-buffer-identification (list mode-string)))
5725       (set-buffer-modified-p t))))
5726
5727 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5728   "Go through the HEADERS list and add all Xrefs to a hash table.
5729 The resulting hash table is returned, or nil if no Xrefs were found."
5730   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5731          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5732          (xref-hashtb (gnus-make-hashtable))
5733          start group entry number xrefs header)
5734     (while headers
5735       (setq header (pop headers))
5736       (when (and (setq xrefs (mail-header-xref header))
5737                  (not (memq (setq number (mail-header-number header))
5738                             unreads)))
5739         (setq start 0)
5740         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5741           (setq start (match-end 0))
5742           (setq group (if prefix
5743                           (concat prefix (substring xrefs (match-beginning 1)
5744                                                     (match-end 1)))
5745                         (substring xrefs (match-beginning 1) (match-end 1))))
5746           (setq number
5747                 (string-to-number (substring xrefs (match-beginning 2)
5748                                           (match-end 2))))
5749           (if (setq entry (gnus-gethash group xref-hashtb))
5750               (setcdr entry (cons number (cdr entry)))
5751             (gnus-sethash group (cons number nil) xref-hashtb)))))
5752     (and start xref-hashtb)))
5753
5754 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5755   "Look through all the headers and mark the Xrefs as read."
5756   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5757         name info xref-hashtb idlist method nth4)
5758     (save-excursion
5759       (set-buffer gnus-group-buffer)
5760       (when (setq xref-hashtb
5761                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5762         (mapatoms
5763          (lambda (group)
5764            (unless (string= from-newsgroup (setq name (symbol-name group)))
5765              (setq idlist (symbol-value group))
5766              ;; Dead groups are not updated.
5767              (and (prog1
5768                       (setq info (gnus-get-info name))
5769                     (when (stringp (setq nth4 (gnus-info-method info)))
5770                       (setq nth4 (gnus-server-to-method nth4))))
5771                   ;; Only do the xrefs if the group has the same
5772                   ;; select method as the group we have just read.
5773                   (or (gnus-methods-equal-p
5774                        nth4 (gnus-find-method-for-group from-newsgroup))
5775                       virtual
5776                       (equal nth4 (setq method (gnus-find-method-for-group
5777                                                 from-newsgroup)))
5778                       (and (equal (car nth4) (car method))
5779                            (equal (nth 1 nth4) (nth 1 method))))
5780                   gnus-use-cross-reference
5781                   (or (not (eq gnus-use-cross-reference t))
5782                       virtual
5783                       ;; Only do cross-references on subscribed
5784                       ;; groups, if that is what is wanted.
5785                       (<= (gnus-info-level info) gnus-level-subscribed))
5786                   (gnus-group-make-articles-read name idlist))))
5787          xref-hashtb)))))
5788
5789 (defun gnus-compute-read-articles (group articles)
5790   (let* ((entry (gnus-group-entry group))
5791          (info (nth 2 entry))
5792          (active (gnus-active group))
5793          ninfo)
5794     (when entry
5795       ;; First peel off all invalid article numbers.
5796       (when active
5797         (let ((ids articles)
5798               id first)
5799           (while (setq id (pop ids))
5800             (when (and first (> id (cdr active)))
5801               ;; We'll end up in this situation in one particular
5802               ;; obscure situation.  If you re-scan a group and get
5803               ;; a new article that is cross-posted to a different
5804               ;; group that has not been re-scanned, you might get
5805               ;; crossposted article that has a higher number than
5806               ;; Gnus believes possible.  So we re-activate this
5807               ;; group as well.  This might mean doing the
5808               ;; crossposting thingy will *increase* the number
5809               ;; of articles in some groups.  Tsk, tsk.
5810               (setq active (or (gnus-activate-group group) active)))
5811             (when (or (> id (cdr active))
5812                       (< id (car active)))
5813               (setq articles (delq id articles))))))
5814       ;; If the read list is nil, we init it.
5815       (if (and active
5816                (null (gnus-info-read info))
5817                (> (car active) 1))
5818           (setq ninfo (cons 1 (1- (car active))))
5819         (setq ninfo (gnus-info-read info)))
5820       ;; Then we add the read articles to the range.
5821       (gnus-add-to-range
5822        ninfo (setq articles (sort articles '<))))))
5823
5824 (defun gnus-group-make-articles-read (group articles)
5825   "Update the info of GROUP to say that ARTICLES are read."
5826   (let* ((num 0)
5827          (entry (gnus-group-entry group))
5828          (info (nth 2 entry))
5829          (active (gnus-active group))
5830          range)
5831     (when entry
5832       (setq range (gnus-compute-read-articles group articles))
5833       (with-current-buffer gnus-group-buffer
5834         (gnus-undo-register
5835           `(progn
5836              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5837              (gnus-info-set-read ',info ',(gnus-info-read info))
5838              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5839              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5840              (gnus-group-update-group ,group t))))
5841       ;; Add the read articles to the range.
5842       (gnus-info-set-read info range)
5843       (gnus-request-set-mark group (list (list range 'add '(read))))
5844       ;; Then we have to re-compute how many unread
5845       ;; articles there are in this group.
5846       (when active
5847         (cond
5848          ((not range)
5849           (setq num (- (1+ (cdr active)) (car active))))
5850          ((not (listp (cdr range)))
5851           (setq num (- (cdr active) (- (1+ (cdr range))
5852                                        (car range)))))
5853          (t
5854           (while range
5855             (if (numberp (car range))
5856                 (setq num (1+ num))
5857               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5858             (setq range (cdr range)))
5859           (setq num (- (cdr active) num))))
5860         ;; Update the number of unread articles.
5861         (setcar entry num)
5862         ;; Update the group buffer.
5863         (unless (gnus-ephemeral-group-p group)
5864           (gnus-group-update-group group t))))))
5865
5866 (defvar gnus-newsgroup-none-id 0)
5867
5868 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5869   (let ((cur nntp-server-buffer)
5870         (dependencies
5871          (or dependencies
5872              (with-current-buffer gnus-summary-buffer
5873                gnus-newsgroup-dependencies)))
5874         headers id end ref number
5875         (mail-parse-charset gnus-newsgroup-charset)
5876         (mail-parse-ignored-charsets
5877          (save-excursion (condition-case nil
5878                              (set-buffer gnus-summary-buffer)
5879                            (error))
5880                          gnus-newsgroup-ignored-charsets)))
5881     (save-excursion
5882       (set-buffer nntp-server-buffer)
5883       ;; Translate all TAB characters into SPACE characters.
5884       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5885       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5886       (ietf-drums-unfold-fws)
5887       (gnus-run-hooks 'gnus-parse-headers-hook)
5888       (let ((case-fold-search t)
5889             in-reply-to header p lines chars)
5890         (goto-char (point-min))
5891         ;; Search to the beginning of the next header.  Error messages
5892         ;; do not begin with 2 or 3.
5893         (while (re-search-forward "^[23][0-9]+ " nil t)
5894           (setq id nil
5895                 ref nil)
5896           ;; This implementation of this function, with nine
5897           ;; search-forwards instead of the one re-search-forward and
5898           ;; a case (which basically was the old function) is actually
5899           ;; about twice as fast, even though it looks messier.  You
5900           ;; can't have everything, I guess.  Speed and elegance
5901           ;; doesn't always go hand in hand.
5902           (setq
5903            header
5904            (vector
5905             ;; Number.
5906             (prog1
5907                 (setq number (read cur))
5908               (end-of-line)
5909               (setq p (point))
5910               (narrow-to-region (point)
5911                                 (or (and (search-forward "\n.\n" nil t)
5912                                          (- (point) 2))
5913                                     (point))))
5914             ;; Subject.
5915             (progn
5916               (goto-char p)
5917               (if (search-forward "\nsubject:" nil t)
5918                   (funcall gnus-decode-encoded-word-function
5919                            (nnheader-header-value))
5920                 "(none)"))
5921             ;; From.
5922             (progn
5923               (goto-char p)
5924               (if (search-forward "\nfrom:" nil t)
5925                   (funcall gnus-decode-encoded-word-function
5926                            (nnheader-header-value))
5927                 "(nobody)"))
5928             ;; Date.
5929             (progn
5930               (goto-char p)
5931               (if (search-forward "\ndate:" nil t)
5932                   (nnheader-header-value) ""))
5933             ;; Message-ID.
5934             (progn
5935               (goto-char p)
5936               (setq id (if (re-search-forward
5937                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5938                            ;; We do it this way to make sure the Message-ID
5939                            ;; is (somewhat) syntactically valid.
5940                            (buffer-substring (match-beginning 1)
5941                                              (match-end 1))
5942                          ;; If there was no message-id, we just fake one
5943                          ;; to make subsequent routines simpler.
5944                          (nnheader-generate-fake-message-id number))))
5945             ;; References.
5946             (progn
5947               (goto-char p)
5948               (if (search-forward "\nreferences:" nil t)
5949                   (progn
5950                     (setq end (point))
5951                     (prog1
5952                         (nnheader-header-value)
5953                       (setq ref
5954                             (buffer-substring
5955                              (progn
5956                                (end-of-line)
5957                                (search-backward ">" end t)
5958                                (1+ (point)))
5959                              (progn
5960                                (search-backward "<" end t)
5961                                (point))))))
5962                 ;; Get the references from the in-reply-to header if there
5963                 ;; were no references and the in-reply-to header looks
5964                 ;; promising.
5965                 (if (and (search-forward "\nin-reply-to:" nil t)
5966                          (setq in-reply-to (nnheader-header-value))
5967                          (string-match "<[^>]+>" in-reply-to))
5968                     (let (ref2)
5969                       (setq ref (substring in-reply-to (match-beginning 0)
5970                                            (match-end 0)))
5971                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5972                         (setq ref2 (substring in-reply-to (match-beginning 0)
5973                                               (match-end 0)))
5974                         (when (> (length ref2) (length ref))
5975                           (setq ref ref2)))
5976                       ref)
5977                   (setq ref nil))))
5978             ;; Chars.
5979             (progn
5980               (goto-char p)
5981               (if (search-forward "\nchars: " nil t)
5982                   (if (numberp (setq chars (ignore-errors (read cur))))
5983                       chars -1)
5984                 -1))
5985             ;; Lines.
5986             (progn
5987               (goto-char p)
5988               (if (search-forward "\nlines: " nil t)
5989                   (if (numberp (setq lines (ignore-errors (read cur))))
5990                       lines -1)
5991                 -1))
5992             ;; Xref.
5993             (progn
5994               (goto-char p)
5995               (and (search-forward "\nxref:" nil t)
5996                    (nnheader-header-value)))
5997             ;; Extra.
5998             (when gnus-extra-headers
5999               (let ((extra gnus-extra-headers)
6000                     out)
6001                 (while extra
6002                   (goto-char p)
6003                   (when (search-forward
6004                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6005                     (push (cons (car extra) (nnheader-header-value))
6006                           out))
6007                   (pop extra))
6008                 out))))
6009           (when (equal id ref)
6010             (setq ref nil))
6011
6012           (when gnus-alter-header-function
6013             (funcall gnus-alter-header-function header)
6014             (setq id (mail-header-id header)
6015                   ref (gnus-parent-id (mail-header-references header))))
6016
6017           (when (setq header
6018                       (gnus-dependencies-add-header
6019                        header dependencies force-new))
6020             (push header headers))
6021           (goto-char (point-max))
6022           (widen))
6023         (nreverse headers)))))
6024
6025 ;; Goes through the xover lines and returns a list of vectors
6026 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6027                                                   force-new dependencies
6028                                                   group also-fetch-heads)
6029   "Parse the news overview data in the server buffer.
6030 Return a list of headers that match SEQUENCE (see
6031 `nntp-retrieve-headers')."
6032   ;; Get the Xref when the users reads the articles since most/some
6033   ;; NNTP servers do not include Xrefs when using XOVER.
6034   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6035   (let ((mail-parse-charset gnus-newsgroup-charset)
6036         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6037         (cur nntp-server-buffer)
6038         (dependencies (or dependencies gnus-newsgroup-dependencies))
6039         (allp (cond
6040                ((eq gnus-read-all-available-headers t)
6041                 t)
6042                ((stringp gnus-read-all-available-headers)
6043                 (string-match gnus-read-all-available-headers group))
6044                (t
6045                 nil)))
6046         number headers header)
6047     (save-excursion
6048       (set-buffer nntp-server-buffer)
6049       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6050       ;; Allow the user to mangle the headers before parsing them.
6051       (gnus-run-hooks 'gnus-parse-headers-hook)
6052       (goto-char (point-min))
6053       (gnus-parse-without-error
6054         (while (and (or sequence allp)
6055                     (not (eobp)))
6056           (setq number (read cur))
6057           (when (not allp)
6058             (while (and sequence
6059                         (< (car sequence) number))
6060               (setq sequence (cdr sequence))))
6061           (when (and (or allp
6062                          (and sequence
6063                               (eq number (car sequence))))
6064                      (progn
6065                        (setq sequence (cdr sequence))
6066                        (setq header (inline
6067                                       (gnus-nov-parse-line
6068                                        number dependencies force-new)))))
6069             (push header headers))
6070           (forward-line 1)))
6071       ;; A common bug in inn is that if you have posted an article and
6072       ;; then retrieves the active file, it will answer correctly --
6073       ;; the new article is included.  However, a NOV entry for the
6074       ;; article may not have been generated yet, so this may fail.
6075       ;; We work around this problem by retrieving the last few
6076       ;; headers using HEAD.
6077       (if (or (not also-fetch-heads)
6078               (not sequence))
6079           ;; We (probably) got all the headers.
6080           (nreverse headers)
6081         (let ((gnus-nov-is-evil t))
6082           (nconc
6083            (nreverse headers)
6084            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6085              (gnus-get-newsgroup-headers))))))))
6086
6087 (defun gnus-article-get-xrefs ()
6088   "Fill in the Xref value in `gnus-current-headers', if necessary.
6089 This is meant to be called in `gnus-article-internal-prepare-hook'."
6090   (let ((headers (with-current-buffer gnus-summary-buffer
6091                    gnus-current-headers)))
6092     (or (not gnus-use-cross-reference)
6093         (not headers)
6094         (and (mail-header-xref headers)
6095              (not (string= (mail-header-xref headers) "")))
6096         (let ((case-fold-search t)
6097               xref)
6098           (save-restriction
6099             (nnheader-narrow-to-headers)
6100             (goto-char (point-min))
6101             (when (or (and (not (eobp))
6102                            (eq (downcase (char-after)) ?x)
6103                            (looking-at "Xref:"))
6104                       (search-forward "\nXref:" nil t))
6105               (goto-char (1+ (match-end 0)))
6106               (setq xref (buffer-substring (point) (point-at-eol)))
6107               (mail-header-set-xref headers xref)))))))
6108
6109 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6110   "Find article ID and insert the summary line for that article.
6111 OLD-HEADER can either be a header or a line number to insert
6112 the subject line on."
6113   (let* ((line (and (numberp old-header) old-header))
6114          (old-header (and (vectorp old-header) old-header))
6115          (header (cond ((and old-header use-old-header)
6116                         old-header)
6117                        ((and (numberp id)
6118                              (gnus-number-to-header id))
6119                         (gnus-number-to-header id))
6120                        (t
6121                         (gnus-read-header id))))
6122          (number (and (numberp id) id))
6123          d)
6124     (when header
6125       ;; Rebuild the thread that this article is part of and go to the
6126       ;; article we have fetched.
6127       (when (and (not gnus-show-threads)
6128                  old-header)
6129         (when (and number
6130                    (setq d (gnus-data-find (mail-header-number old-header))))
6131           (goto-char (gnus-data-pos d))
6132           (gnus-data-remove
6133            number
6134            (- (point-at-bol)
6135               (prog1
6136                   (1+ (point-at-eol))
6137                 (gnus-delete-line))))))
6138       ;; Remove list identifiers from subject.
6139       (when gnus-list-identifiers
6140         (let ((gnus-newsgroup-headers (list header)))
6141           (gnus-summary-remove-list-identifiers)))
6142       (when old-header
6143         (mail-header-set-number header (mail-header-number old-header)))
6144       (setq gnus-newsgroup-sparse
6145             (delq (setq number (mail-header-number header))
6146                   gnus-newsgroup-sparse))
6147       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6148       (push number gnus-newsgroup-limit)
6149       (gnus-rebuild-thread (mail-header-id header) line)
6150       (gnus-summary-goto-subject number nil t))
6151     (when (and (numberp number)
6152                (> number 0))
6153       ;; We have to update the boundaries even if we can't fetch the
6154       ;; article if ID is a number -- so that the next `P' or `N'
6155       ;; command will fetch the previous (or next) article even
6156       ;; if the one we tried to fetch this time has been canceled.
6157       (when (> number gnus-newsgroup-end)
6158         (setq gnus-newsgroup-end number))
6159       (when (< number gnus-newsgroup-begin)
6160         (setq gnus-newsgroup-begin number))
6161       (setq gnus-newsgroup-unselected
6162             (delq number gnus-newsgroup-unselected)))
6163     ;; Report back a success?
6164     (and header (mail-header-number header))))
6165
6166 ;;; Process/prefix in the summary buffer
6167
6168 (defun gnus-summary-work-articles (n)
6169   "Return a list of articles to be worked upon.
6170 The prefix argument, the list of process marked articles, and the
6171 current article will be taken into consideration."
6172   (save-excursion
6173     (set-buffer gnus-summary-buffer)
6174     (cond
6175      (n
6176       ;; A numerical prefix has been given.
6177       (setq n (prefix-numeric-value n))
6178       (let ((backward (< n 0))
6179             (n (abs (prefix-numeric-value n)))
6180             articles article)
6181         (save-excursion
6182           (while
6183               (and (> n 0)
6184                    (push (setq article (gnus-summary-article-number))
6185                          articles)
6186                    (if backward
6187                        (gnus-summary-find-prev nil article)
6188                      (gnus-summary-find-next nil article)))
6189             (decf n)))
6190         (nreverse articles)))
6191      ((and (gnus-region-active-p) (mark))
6192       (message "region active")
6193       ;; Work on the region between point and mark.
6194       (let ((max (max (point) (mark)))
6195             articles article)
6196         (save-excursion
6197           (goto-char (min (min (point) (mark))))
6198           (while
6199               (and
6200                (push (setq article (gnus-summary-article-number)) articles)
6201                (gnus-summary-find-next nil article)
6202                (< (point) max)))
6203           (nreverse articles))))
6204      (gnus-newsgroup-processable
6205       ;; There are process-marked articles present.
6206       ;; Save current state.
6207       (gnus-summary-save-process-mark)
6208       ;; Return the list.
6209       (reverse gnus-newsgroup-processable))
6210      (t
6211       ;; Just return the current article.
6212       (list (gnus-summary-article-number))))))
6213
6214 (defmacro gnus-summary-iterate (arg &rest forms)
6215   "Iterate over the process/prefixed articles and do FORMS.
6216 ARG is the interactive prefix given to the command.  FORMS will be
6217 executed with point over the summary line of the articles."
6218   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6219     `(let ((,articles (gnus-summary-work-articles ,arg)))
6220        (while ,articles
6221          (gnus-summary-goto-subject (car ,articles))
6222          ,@forms
6223          (pop ,articles)))))
6224
6225 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6226 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6227
6228 (defun gnus-summary-save-process-mark ()
6229   "Push the current set of process marked articles on the stack."
6230   (interactive)
6231   (push (copy-sequence gnus-newsgroup-processable)
6232         gnus-newsgroup-process-stack))
6233
6234 (defun gnus-summary-kill-process-mark ()
6235   "Push the current set of process marked articles on the stack and unmark."
6236   (interactive)
6237   (gnus-summary-save-process-mark)
6238   (gnus-summary-unmark-all-processable))
6239
6240 (defun gnus-summary-yank-process-mark ()
6241   "Pop the last process mark state off the stack and restore it."
6242   (interactive)
6243   (unless gnus-newsgroup-process-stack
6244     (error "Empty mark stack"))
6245   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6246
6247 (defun gnus-summary-process-mark-set (set)
6248   "Make SET into the current process marked articles."
6249   (gnus-summary-unmark-all-processable)
6250   (mapc 'gnus-summary-set-process-mark set))
6251
6252 ;;; Searching and stuff
6253
6254 (defun gnus-summary-search-group (&optional backward use-level)
6255   "Search for next unread newsgroup.
6256 If optional argument BACKWARD is non-nil, search backward instead."
6257   (save-excursion
6258     (set-buffer gnus-group-buffer)
6259     (when (gnus-group-search-forward
6260            backward nil (if use-level (gnus-group-group-level) nil))
6261       (gnus-group-group-name))))
6262
6263 (defun gnus-summary-best-group (&optional exclude-group)
6264   "Find the name of the best unread group.
6265 If EXCLUDE-GROUP, do not go to this group."
6266   (with-current-buffer gnus-group-buffer
6267     (save-excursion
6268       (gnus-group-best-unread-group exclude-group))))
6269
6270 (defun gnus-summary-find-next (&optional unread article backward)
6271   (if backward
6272       (gnus-summary-find-prev unread article)
6273     (let* ((dummy (gnus-summary-article-intangible-p))
6274            (article (or article (gnus-summary-article-number)))
6275            (data (gnus-data-find-list article))
6276            result)
6277       (when (and (not dummy)
6278                  (or (not gnus-summary-check-current)
6279                      (not unread)
6280                      (not (gnus-data-unread-p (car data)))))
6281         (setq data (cdr data)))
6282       (when (setq result
6283                   (if unread
6284                       (progn
6285                         (while data
6286                           (unless (memq (gnus-data-number (car data))
6287                                         (cond
6288                                          ((eq gnus-auto-goto-ignores
6289                                               'always-undownloaded)
6290                                           gnus-newsgroup-undownloaded)
6291                                          (gnus-plugged
6292                                           nil)
6293                                          ((eq gnus-auto-goto-ignores
6294                                               'unfetched)
6295                                           gnus-newsgroup-unfetched)
6296                                          ((eq gnus-auto-goto-ignores
6297                                               'undownloaded)
6298                                           gnus-newsgroup-undownloaded)))
6299                             (when (gnus-data-unread-p (car data))
6300                               (setq result (car data)
6301                                     data nil)))
6302                           (setq data (cdr data)))
6303                         result)
6304                     (car data)))
6305         (goto-char (gnus-data-pos result))
6306         (gnus-data-number result)))))
6307
6308 (defun gnus-summary-find-prev (&optional unread article)
6309   (let* ((eobp (eobp))
6310          (article (or article (gnus-summary-article-number)))
6311          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6312          result)
6313     (when (and (not eobp)
6314                (or (not gnus-summary-check-current)
6315                    (not unread)
6316                    (not (gnus-data-unread-p (car data)))))
6317       (setq data (cdr data)))
6318     (when (setq result
6319                 (if unread
6320                     (progn
6321                       (while data
6322                         (unless (memq (gnus-data-number (car data))
6323                                       (cond
6324                                        ((eq gnus-auto-goto-ignores
6325                                             'always-undownloaded)
6326                                         gnus-newsgroup-undownloaded)
6327                                        (gnus-plugged
6328                                         nil)
6329                                        ((eq gnus-auto-goto-ignores
6330                                             'unfetched)
6331                                         gnus-newsgroup-unfetched)
6332                                        ((eq gnus-auto-goto-ignores
6333                                             'undownloaded)
6334                                         gnus-newsgroup-undownloaded)))
6335                           (when (gnus-data-unread-p (car data))
6336                             (setq result (car data)
6337                                   data nil)))
6338                         (setq data (cdr data)))
6339                       result)
6340                   (car data)))
6341       (goto-char (gnus-data-pos result))
6342       (gnus-data-number result))))
6343
6344 (defun gnus-summary-find-subject (subject &optional unread backward article)
6345   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6346          (article (or article (gnus-summary-article-number)))
6347          (articles (gnus-data-list backward))
6348          (arts (gnus-data-find-list article articles))
6349          result)
6350     (when (or (not gnus-summary-check-current)
6351               (not unread)
6352               (not (gnus-data-unread-p (car arts))))
6353       (setq arts (cdr arts)))
6354     (while arts
6355       (and (or (not unread)
6356                (gnus-data-unread-p (car arts)))
6357            (vectorp (gnus-data-header (car arts)))
6358            (gnus-subject-equal
6359             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6360            (setq result (car arts)
6361                  arts nil))
6362       (setq arts (cdr arts)))
6363     (and result
6364          (goto-char (gnus-data-pos result))
6365          (gnus-data-number result))))
6366
6367 (defun gnus-summary-search-forward (&optional unread subject backward)
6368   "Search forward for an article.
6369 If UNREAD, look for unread articles.  If SUBJECT, look for
6370 articles with that subject.  If BACKWARD, search backward instead."
6371   (cond (subject (gnus-summary-find-subject subject unread backward))
6372         (backward (gnus-summary-find-prev unread))
6373         (t (gnus-summary-find-next unread))))
6374
6375 (defun gnus-recenter (&optional n)
6376   "Center point in window and redisplay frame.
6377 Also do horizontal recentering."
6378   (interactive "P")
6379   (when (and gnus-auto-center-summary
6380              (not (eq gnus-auto-center-summary 'vertical)))
6381     (gnus-horizontal-recenter))
6382   (recenter n))
6383
6384 (defun gnus-summary-recenter ()
6385   "Center point in the summary window.
6386 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6387 displayed, no centering will be performed."
6388   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6389   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6390   (interactive)
6391   ;; The user has to want it.
6392   (when gnus-auto-center-summary
6393     (let* ((top (cond ((< (window-height) 4) 0)
6394                       ((< (window-height) 7) 1)
6395                       (t (if (numberp gnus-auto-center-summary)
6396                              gnus-auto-center-summary
6397                            (/ (1- (window-height)) 2)))))
6398            (height (1- (window-height)))
6399            (bottom (save-excursion (goto-char (point-max))
6400                                    (forward-line (- height))
6401                                    (point)))
6402            (window (get-buffer-window (current-buffer))))
6403       (when (get-buffer-window gnus-article-buffer)
6404         ;; Only do recentering when the article buffer is displayed,
6405         ;; Set the window start to either `bottom', which is the biggest
6406         ;; possible valid number, or the second line from the top,
6407         ;; whichever is the least.
6408         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6409           (if (> bottom top-pos)
6410               ;; Keep the second line from the top visible
6411               (set-window-start window top-pos t)
6412             ;; Try to keep the bottom line visible; if it's partially
6413             ;; obscured, either scroll one more line to make it fully
6414             ;; visible, or revert to using TOP-POS.
6415             (save-excursion
6416               (goto-char (point-max))
6417               (forward-line -1)
6418               (let ((last-line-start (point)))
6419                 (goto-char bottom)
6420                 (set-window-start window (point) t)
6421                 (when (not (pos-visible-in-window-p last-line-start window))
6422                   (forward-line 1)
6423                   (set-window-start window (min (point) top-pos) t)))))))
6424       ;; Do horizontal recentering while we're at it.
6425       (when (and (get-buffer-window (current-buffer) t)
6426                  (not (eq gnus-auto-center-summary 'vertical)))
6427         (let ((selected (selected-window)))
6428           (select-window (get-buffer-window (current-buffer) t))
6429           (gnus-summary-position-point)
6430           (gnus-horizontal-recenter)
6431           (select-window selected))))))
6432
6433 (defun gnus-summary-jump-to-group (newsgroup)
6434   "Move point to NEWSGROUP in group mode buffer."
6435   ;; Keep update point of group mode buffer if visible.
6436   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6437       (save-window-excursion
6438         ;; Take care of tree window mode.
6439         (when (get-buffer-window gnus-group-buffer)
6440           (pop-to-buffer gnus-group-buffer))
6441         (gnus-group-jump-to-group newsgroup))
6442     (save-excursion
6443       ;; Take care of tree window mode.
6444       (if (get-buffer-window gnus-group-buffer)
6445           (pop-to-buffer gnus-group-buffer)
6446         (set-buffer gnus-group-buffer))
6447       (gnus-group-jump-to-group newsgroup))))
6448
6449 ;; This function returns a list of article numbers based on the
6450 ;; difference between the ranges of read articles in this group and
6451 ;; the range of active articles.
6452 (defun gnus-list-of-unread-articles (group)
6453   (let* ((read (gnus-info-read (gnus-get-info group)))
6454          (active (or (gnus-active group) (gnus-activate-group group)))
6455          (last (or (cdr active)
6456                    (error "Group %s couldn't be activated " group)))
6457          first nlast unread)
6458     ;; If none are read, then all are unread.
6459     (if (not read)
6460         (setq first (car active))
6461       ;; If the range of read articles is a single range, then the
6462       ;; first unread article is the article after the last read
6463       ;; article.  Sounds logical, doesn't it?
6464       (if (and (not (listp (cdr read)))
6465                (or (< (car read) (car active))
6466                    (progn (setq read (list read))
6467                           nil)))
6468           (setq first (max (car active) (1+ (cdr read))))
6469         ;; `read' is a list of ranges.
6470         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6471                                   (caar read)))
6472                   1)
6473           (setq first (car active)))
6474         (while read
6475           (when first
6476             (while (< first nlast)
6477               (setq unread (cons first unread)
6478                     first (1+ first))))
6479           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6480           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6481           (setq read (cdr read)))))
6482     ;; And add the last unread articles.
6483     (while (<= first last)
6484       (setq unread (cons first unread)
6485             first (1+ first)))
6486     ;; Return the list of unread articles.
6487     (delq 0 (nreverse unread))))
6488
6489 (defun gnus-list-of-read-articles (group)
6490   "Return a list of unread, unticked and non-dormant articles."
6491   (let* ((info (gnus-get-info group))
6492          (marked (gnus-info-marks info))
6493          (active (gnus-active group)))
6494     (and info active
6495          (gnus-list-range-difference
6496           (gnus-list-range-difference
6497            (gnus-sorted-complement
6498             (gnus-uncompress-range active)
6499             (gnus-list-of-unread-articles group))
6500            (cdr (assq 'dormant marked)))
6501           (cdr (assq 'tick marked))))))
6502
6503 ;; This function returns a sequence of article numbers based on the
6504 ;; difference between the ranges of read articles in this group and
6505 ;; the range of active articles.
6506 (defun gnus-sequence-of-unread-articles (group)
6507   (let* ((read (gnus-info-read (gnus-get-info group)))
6508          (active (or (gnus-active group) (gnus-activate-group group)))
6509          (last (cdr active))
6510          first nlast unread)
6511     ;; If none are read, then all are unread.
6512     (if (not read)
6513         (setq first (car active))
6514       ;; If the range of read articles is a single range, then the
6515       ;; first unread article is the article after the last read
6516       ;; article.  Sounds logical, doesn't it?
6517       (if (and (not (listp (cdr read)))
6518                (or (< (car read) (car active))
6519                    (progn (setq read (list read))
6520                           nil)))
6521           (setq first (max (car active) (1+ (cdr read))))
6522         ;; `read' is a list of ranges.
6523         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6524                                   (caar read)))
6525                   1)
6526           (setq first (car active)))
6527         (while read
6528           (when first
6529             (push (cons first nlast) unread))
6530           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6531           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6532           (setq read (cdr read)))))
6533     ;; And add the last unread articles.
6534     (cond ((< first last)
6535            (push (cons first last) unread))
6536           ((= first last)
6537            (push first unread)))
6538     ;; Return the sequence of unread articles.
6539     (delq 0 (nreverse unread))))
6540
6541 ;; Various summary commands
6542
6543 (defun gnus-summary-select-article-buffer ()
6544   "Reconfigure windows to show article buffer."
6545   (interactive)
6546   (if (not (gnus-buffer-live-p gnus-article-buffer))
6547       (error "There is no article buffer for this summary buffer")
6548     (gnus-configure-windows 'article)
6549     (select-window (get-buffer-window gnus-article-buffer))))
6550
6551 (defun gnus-summary-universal-argument (arg)
6552   "Perform any operation on all articles that are process/prefixed."
6553   (interactive "P")
6554   (let ((articles (gnus-summary-work-articles arg))
6555         func article)
6556     (if (eq
6557          (setq
6558           func
6559           (key-binding
6560            (read-key-sequence
6561             (substitute-command-keys
6562              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6563          'undefined)
6564         (gnus-error 1 "Undefined key")
6565       (save-excursion
6566         (while articles
6567           (gnus-summary-goto-subject (setq article (pop articles)))
6568           (let (gnus-newsgroup-processable)
6569             (command-execute func))
6570           (gnus-summary-remove-process-mark article)))))
6571   (gnus-summary-position-point))
6572
6573 (defun gnus-summary-toggle-truncation (&optional arg)
6574   "Toggle truncation of summary lines.
6575 With ARG, turn line truncation on if ARG is positive."
6576   (interactive "P")
6577   (setq truncate-lines
6578         (if (null arg) (not truncate-lines)
6579           (> (prefix-numeric-value arg) 0)))
6580   (redraw-display))
6581
6582 (defun gnus-summary-find-for-reselect ()
6583   "Return the number of an article to stay on across a reselect.
6584 The current article is considered, then following articles, then previous
6585 articles.  An article is sought which is not cancelled and isn't a temporary
6586 insertion from another group.  If there's no such then return a dummy 0."
6587   (let (found)
6588     (dolist (rev '(nil t))
6589       (unless found      ; don't demand the reverse list if we don't need it
6590         (let ((data (gnus-data-find-list
6591                      (gnus-summary-article-number) (gnus-data-list rev))))
6592           (while (and data (not found))
6593             (if (and (< 0 (gnus-data-number (car data)))
6594                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6595                 (setq found (gnus-data-number (car data))))
6596             (setq data (cdr data))))))
6597     (or found 0)))
6598
6599 (defun gnus-summary-reselect-current-group (&optional all rescan)
6600   "Exit and then reselect the current newsgroup.
6601 The prefix argument ALL means to select all articles."
6602   (interactive "P")
6603   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6604     (error "Ephemeral groups can't be reselected"))
6605   (let ((current-subject (gnus-summary-find-for-reselect))
6606         (group gnus-newsgroup-name))
6607     (setq gnus-newsgroup-begin nil)
6608     (gnus-summary-exit nil 'leave-hidden)
6609     ;; We have to adjust the point of group mode buffer because
6610     ;; point was moved to the next unread newsgroup by exiting.
6611     (gnus-summary-jump-to-group group)
6612     (when rescan
6613       (save-excursion
6614         (gnus-group-get-new-news-this-group 1)))
6615     (gnus-group-read-group all t)
6616     (gnus-summary-goto-subject current-subject nil t)))
6617
6618 (defun gnus-summary-rescan-group (&optional all)
6619   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6620   (interactive "P")
6621   (gnus-summary-reselect-current-group all t))
6622
6623 (defun gnus-summary-update-info (&optional non-destructive)
6624   (save-excursion
6625     (let ((group gnus-newsgroup-name))
6626       (when group
6627         (when gnus-newsgroup-kill-headers
6628           (setq gnus-newsgroup-killed
6629                 (gnus-compress-sequence
6630                  (gnus-sorted-union
6631                   (gnus-list-range-intersection
6632                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6633                   gnus-newsgroup-unreads)
6634                  t)))
6635         (unless (listp (cdr gnus-newsgroup-killed))
6636           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6637         (let ((headers gnus-newsgroup-headers))
6638           ;; Set the new ranges of read articles.
6639           (with-current-buffer gnus-group-buffer
6640             (gnus-undo-force-boundary))
6641           (gnus-update-read-articles
6642            group (gnus-sorted-union
6643                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6644           ;; Set the current article marks.
6645           (let ((gnus-newsgroup-scored
6646                  (if (and (not gnus-save-score)
6647                           (not non-destructive))
6648                      nil
6649                    gnus-newsgroup-scored)))
6650             (save-excursion
6651               (gnus-update-marks)))
6652           ;; Do the cross-ref thing.
6653           (when gnus-use-cross-reference
6654             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6655           ;; Do not switch windows but change the buffer to work.
6656           (set-buffer gnus-group-buffer)
6657           (unless (gnus-ephemeral-group-p group)
6658             (gnus-group-update-group group)))))))
6659
6660 (defun gnus-summary-save-newsrc (&optional force)
6661   "Save the current number of read/marked articles in the dribble buffer.
6662 The dribble buffer will then be saved.
6663 If FORCE (the prefix), also save the .newsrc file(s)."
6664   (interactive "P")
6665   (gnus-summary-update-info t)
6666   (if force
6667       (gnus-save-newsrc-file)
6668     (gnus-dribble-save)))
6669
6670 (defun gnus-summary-exit (&optional temporary leave-hidden)
6671   "Exit reading current newsgroup, and then return to group selection mode.
6672 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6673   (interactive)
6674   (gnus-set-global-variables)
6675   (when (gnus-buffer-live-p gnus-article-buffer)
6676     (save-excursion
6677       (set-buffer gnus-article-buffer)
6678       (mm-destroy-parts gnus-article-mime-handles)
6679       ;; Set it to nil for safety reason.
6680       (setq gnus-article-mime-handle-alist nil)
6681       (setq gnus-article-mime-handles nil)))
6682   (gnus-kill-save-kill-buffer)
6683   (gnus-async-halt-prefetch)
6684   (let* ((group gnus-newsgroup-name)
6685          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6686          (gnus-group-is-exiting-p t)
6687          (mode major-mode)
6688          (group-point nil)
6689          (buf (current-buffer)))
6690     (unless quit-config
6691       ;; Do adaptive scoring, and possibly save score files.
6692       (when gnus-newsgroup-adaptive
6693         (gnus-score-adaptive))
6694       (when gnus-use-scoring
6695         (gnus-score-save)))
6696     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6697     ;; If we have several article buffers, we kill them at exit.
6698     (unless gnus-single-article-buffer
6699       (gnus-kill-buffer gnus-original-article-buffer)
6700       (setq gnus-article-current nil))
6701     (when gnus-use-cache
6702       (gnus-cache-possibly-remove-articles)
6703       (gnus-cache-save-buffers))
6704     (gnus-async-prefetch-remove-group group)
6705     (when gnus-suppress-duplicates
6706       (gnus-dup-enter-articles))
6707     (when gnus-use-trees
6708       (gnus-tree-close group))
6709     (when gnus-use-cache
6710       (gnus-cache-write-active))
6711     ;; Remove entries for this group.
6712     (nnmail-purge-split-history (gnus-group-real-name group))
6713     ;; Make all changes in this group permanent.
6714     (unless quit-config
6715       (gnus-run-hooks 'gnus-exit-group-hook)
6716       (gnus-summary-update-info))
6717     (gnus-close-group group)
6718     ;; Make sure where we were, and go to next newsgroup.
6719     (set-buffer gnus-group-buffer)
6720     (unless quit-config
6721       (gnus-group-jump-to-group group))
6722     (gnus-run-hooks 'gnus-summary-exit-hook)
6723     (unless (or quit-config
6724                 ;; If this group has disappeared from the summary
6725                 ;; buffer, don't skip forwards.
6726                 (not (string= group (gnus-group-group-name))))
6727       (gnus-group-next-unread-group 1))
6728     (setq group-point (point))
6729     (if temporary
6730         nil                             ;Nothing to do.
6731       ;; If we have several article buffers, we kill them at exit.
6732       (unless gnus-single-article-buffer
6733         (gnus-kill-buffer gnus-article-buffer)
6734         (gnus-kill-buffer gnus-original-article-buffer)
6735         (setq gnus-article-current nil))
6736       (set-buffer buf)
6737       (if (not gnus-kill-summary-on-exit)
6738           (progn
6739             (gnus-deaden-summary)
6740             (setq mode nil))
6741         ;; We set all buffer-local variables to nil.  It is unclear why
6742         ;; this is needed, but if we don't, buffer-local variables are
6743         ;; not garbage-collected, it seems.  This would the lead to en
6744         ;; ever-growing Emacs.
6745         (gnus-summary-clear-local-variables)
6746         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6747           (gnus-summary-clear-local-variables))
6748         (when (get-buffer gnus-article-buffer)
6749           (bury-buffer gnus-article-buffer))
6750         ;; Return to group mode buffer.
6751         (when (eq mode 'gnus-summary-mode)
6752           (gnus-kill-buffer buf)))
6753       (setq gnus-current-select-method gnus-select-method)
6754       (set-buffer gnus-group-buffer)
6755       (if quit-config
6756           (gnus-handle-ephemeral-exit quit-config)
6757         (goto-char group-point)
6758         ;; If gnus-group-buffer is already displayed, make sure we also move
6759         ;; the cursor in the window that displays it.
6760         (let ((win (get-buffer-window (current-buffer) 0)))
6761           (if win (set-window-point win (point))))
6762         (unless leave-hidden
6763           (gnus-configure-windows 'group 'force)))
6764       ;; Clear the current group name.
6765       (unless quit-config
6766         (setq gnus-newsgroup-name nil)))))
6767
6768 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6769 (defun gnus-summary-exit-no-update (&optional no-questions)
6770   "Quit reading current newsgroup without updating read article info."
6771   (interactive)
6772   (let* ((group gnus-newsgroup-name)
6773          (gnus-group-is-exiting-p t)
6774          (gnus-group-is-exiting-without-update-p t)
6775          (quit-config (gnus-group-quit-config group)))
6776     (when (or no-questions
6777               gnus-expert-user
6778               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6779       (gnus-async-halt-prefetch)
6780       (run-hooks 'gnus-summary-prepare-exit-hook)
6781       (when (gnus-buffer-live-p gnus-article-buffer)
6782         (save-excursion
6783           (set-buffer gnus-article-buffer)
6784           (mm-destroy-parts gnus-article-mime-handles)
6785           ;; Set it to nil for safety reason.
6786           (setq gnus-article-mime-handle-alist nil)
6787           (setq gnus-article-mime-handles nil)))
6788       ;; If we have several article buffers, we kill them at exit.
6789       (unless gnus-single-article-buffer
6790         (gnus-kill-buffer gnus-article-buffer)
6791         (gnus-kill-buffer gnus-original-article-buffer)
6792         (setq gnus-article-current nil))
6793       (if (not gnus-kill-summary-on-exit)
6794           (gnus-deaden-summary)
6795         (gnus-close-group group)
6796         (gnus-summary-clear-local-variables)
6797         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6798           (gnus-summary-clear-local-variables))
6799         (gnus-kill-buffer gnus-summary-buffer))
6800       (unless gnus-single-article-buffer
6801         (setq gnus-article-current nil))
6802       (when gnus-use-trees
6803         (gnus-tree-close group))
6804       (gnus-async-prefetch-remove-group group)
6805       (when (get-buffer gnus-article-buffer)
6806         (bury-buffer gnus-article-buffer))
6807       ;; Return to the group buffer.
6808       (gnus-configure-windows 'group 'force)
6809       ;; Clear the current group name.
6810       (setq gnus-newsgroup-name nil)
6811       (unless (gnus-ephemeral-group-p group)
6812         (gnus-group-update-group group))
6813       (when (equal (gnus-group-group-name) group)
6814         (gnus-group-next-unread-group 1))
6815       (when quit-config
6816         (gnus-handle-ephemeral-exit quit-config)))))
6817
6818 (defun gnus-handle-ephemeral-exit (quit-config)
6819   "Handle movement when leaving an ephemeral group.
6820 The state which existed when entering the ephemeral is reset."
6821   (if (not (buffer-name (car quit-config)))
6822       (gnus-configure-windows 'group 'force)
6823     (set-buffer (car quit-config))
6824     (cond ((eq major-mode 'gnus-summary-mode)
6825            (gnus-set-global-variables))
6826           ((eq major-mode 'gnus-article-mode)
6827            (save-excursion
6828              ;; The `gnus-summary-buffer' variable may point
6829              ;; to the old summary buffer when using a single
6830              ;; article buffer.
6831              (unless (gnus-buffer-live-p gnus-summary-buffer)
6832                (set-buffer gnus-group-buffer))
6833              (set-buffer gnus-summary-buffer)
6834              (gnus-set-global-variables))))
6835     (if (or (eq (cdr quit-config) 'article)
6836             (eq (cdr quit-config) 'pick))
6837         (progn
6838           ;; The current article may be from the ephemeral group
6839           ;; thus it is best that we reload this article
6840           ;;
6841           ;; If we're exiting from a large digest, this can be
6842           ;; extremely slow.  So, it's better not to reload it. -- jh.
6843           ;;(gnus-summary-show-article)
6844           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6845               (gnus-configure-windows 'pick 'force)
6846             (gnus-configure-windows (cdr quit-config) 'force)))
6847       (gnus-configure-windows (cdr quit-config) 'force))
6848     (when (eq major-mode 'gnus-summary-mode)
6849       (gnus-summary-next-subject 1 nil t)
6850       (gnus-summary-recenter)
6851       (gnus-summary-position-point))))
6852
6853 ;;; Dead summaries.
6854
6855 (defvar gnus-dead-summary-mode-map nil)
6856
6857 (unless gnus-dead-summary-mode-map
6858   (setq gnus-dead-summary-mode-map (make-keymap))
6859   (suppress-keymap gnus-dead-summary-mode-map)
6860   (substitute-key-definition
6861    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6862   (dolist (key '("\C-d" "\r" "\177" [delete]))
6863     (define-key gnus-dead-summary-mode-map
6864       key 'gnus-summary-wake-up-the-dead))
6865   (dolist (key '("q" "Q"))
6866     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6867
6868 (defvar gnus-dead-summary-mode nil
6869   "Minor mode for Gnus summary buffers.")
6870
6871 (defun gnus-dead-summary-mode (&optional arg)
6872   "Minor mode for Gnus summary buffers."
6873   (interactive "P")
6874   (when (eq major-mode 'gnus-summary-mode)
6875     (make-local-variable 'gnus-dead-summary-mode)
6876     (setq gnus-dead-summary-mode
6877           (if (null arg) (not gnus-dead-summary-mode)
6878             (> (prefix-numeric-value arg) 0)))
6879     (when gnus-dead-summary-mode
6880       (add-minor-mode
6881        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6882
6883 (defun gnus-deaden-summary ()
6884   "Make the current summary buffer into a dead summary buffer."
6885   ;; Kill any previous dead summary buffer.
6886   (when (and gnus-dead-summary
6887              (buffer-name gnus-dead-summary))
6888     (with-current-buffer gnus-dead-summary
6889       (when gnus-dead-summary-mode
6890         (kill-buffer (current-buffer)))))
6891   ;; Make this the current dead summary.
6892   (setq gnus-dead-summary (current-buffer))
6893   (gnus-dead-summary-mode 1)
6894   (let ((name (buffer-name)))
6895     (when (string-match "Summary" name)
6896       (rename-buffer
6897        (concat (substring name 0 (match-beginning 0)) "Dead "
6898                (substring name (match-beginning 0)))
6899        t)
6900       (bury-buffer))))
6901
6902 (defun gnus-kill-or-deaden-summary (buffer)
6903   "Kill or deaden the summary BUFFER."
6904   (save-excursion
6905     (when (and (buffer-name buffer)
6906                (not gnus-single-article-buffer))
6907       (with-current-buffer buffer
6908         (gnus-kill-buffer gnus-article-buffer)
6909         (gnus-kill-buffer gnus-original-article-buffer)))
6910     (cond
6911      ;; Kill the buffer.
6912      (gnus-kill-summary-on-exit
6913       (when (and gnus-use-trees
6914                  (gnus-buffer-exists-p buffer))
6915         (save-excursion
6916           (set-buffer buffer)
6917           (gnus-tree-close gnus-newsgroup-name)))
6918       (gnus-kill-buffer buffer))
6919      ;; Deaden the buffer.
6920      ((gnus-buffer-exists-p buffer)
6921       (save-excursion
6922         (set-buffer buffer)
6923         (gnus-deaden-summary))))))
6924
6925 (defun gnus-summary-wake-up-the-dead (&rest args)
6926   "Wake up the dead summary buffer."
6927   (interactive)
6928   (gnus-dead-summary-mode -1)
6929   (let ((name (buffer-name)))
6930     (when (string-match "Dead " name)
6931       (rename-buffer
6932        (concat (substring name 0 (match-beginning 0))
6933                (substring name (match-end 0)))
6934        t)))
6935   (gnus-message 3 "This dead summary is now alive again"))
6936
6937 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6938 (defun gnus-summary-fetch-faq (&optional faq-dir)
6939   "Fetch the FAQ for the current group.
6940 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6941 in."
6942   (interactive
6943    (list
6944     (when current-prefix-arg
6945       (completing-read
6946        "FAQ dir: " (and (listp gnus-group-faq-directory)
6947                         (mapcar 'list
6948                                 gnus-group-faq-directory))))))
6949   (let (gnus-faq-buffer)
6950     (when (setq gnus-faq-buffer
6951                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6952       (gnus-configure-windows 'summary-faq))))
6953
6954 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6955 (defun gnus-summary-describe-group (&optional force)
6956   "Describe the current newsgroup."
6957   (interactive "P")
6958   (gnus-group-describe-group force gnus-newsgroup-name))
6959
6960 (defun gnus-summary-describe-briefly ()
6961   "Describe summary mode commands briefly."
6962   (interactive)
6963   (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")))
6964
6965 ;; Walking around group mode buffer from summary mode.
6966
6967 (defun gnus-summary-next-group (&optional no-article target-group backward)
6968   "Exit current newsgroup and then select next unread newsgroup.
6969 If prefix argument NO-ARTICLE is non-nil, no article is selected
6970 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6971 previous group instead."
6972   (interactive "P")
6973   ;; Stop pre-fetching.
6974   (gnus-async-halt-prefetch)
6975   (let ((current-group gnus-newsgroup-name)
6976         (current-buffer (current-buffer))
6977         entered)
6978     ;; First we semi-exit this group to update Xrefs and all variables.
6979     ;; We can't do a real exit, because the window conf must remain
6980     ;; the same in case the user is prompted for info, and we don't
6981     ;; want the window conf to change before that...
6982     (gnus-summary-exit t)
6983     (while (not entered)
6984       ;; Then we find what group we are supposed to enter.
6985       (set-buffer gnus-group-buffer)
6986       (gnus-group-jump-to-group current-group)
6987       (setq target-group
6988             (or target-group
6989                 (if (eq gnus-keep-same-level 'best)
6990                     (gnus-summary-best-group gnus-newsgroup-name)
6991                   (gnus-summary-search-group backward gnus-keep-same-level))))
6992       (if (not target-group)
6993           ;; There are no further groups, so we return to the group
6994           ;; buffer.
6995           (progn
6996             (gnus-message 5 "Returning to the group buffer")
6997             (setq entered t)
6998             (when (gnus-buffer-live-p current-buffer)
6999               (set-buffer current-buffer)
7000               (gnus-summary-exit))
7001             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7002         ;; We try to enter the target group.
7003         (gnus-group-jump-to-group target-group)
7004         (let ((unreads (gnus-group-group-unread)))
7005           (if (and (or (eq t unreads)
7006                        (and unreads (not (zerop unreads))))
7007                    (gnus-summary-read-group
7008                     target-group nil no-article
7009                     (and (buffer-name current-buffer) current-buffer)
7010                     nil backward))
7011               (setq entered t)
7012             (setq current-group target-group
7013                   target-group nil)))))))
7014
7015 (defun gnus-summary-prev-group (&optional no-article)
7016   "Exit current newsgroup and then select previous unread newsgroup.
7017 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7018   (interactive "P")
7019   (gnus-summary-next-group no-article nil t))
7020
7021 ;; Walking around summary lines.
7022
7023 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7024   "Go to the first subject satisfying any non-nil constraint.
7025 If UNREAD is non-nil, the article should be unread.
7026 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7027 If UNSEEN is non-nil, the article should be unseen.
7028 Returns the article selected or nil if there are no matching articles."
7029   (interactive "P")
7030   (cond
7031    ;; Empty summary.
7032    ((null gnus-newsgroup-data)
7033     (gnus-message 3 "No articles in the group")
7034     nil)
7035    ;; Pick the first article.
7036    ((not (or unread undownloaded unseen))
7037     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7038     (gnus-data-number (car gnus-newsgroup-data)))
7039    ;; Find the first unread article.
7040    (t
7041     (let ((data gnus-newsgroup-data))
7042       (while (and data
7043                   (let ((num (gnus-data-number (car data))))
7044                     (or (memq num gnus-newsgroup-unfetched)
7045                         (not (or (and unread
7046                                       (memq num gnus-newsgroup-unreads))
7047                                  (and undownloaded
7048                                       (memq num gnus-newsgroup-undownloaded))
7049                                  (and unseen
7050                                       (memq num gnus-newsgroup-unseen)))))))
7051         (setq data (cdr data)))
7052       (prog1
7053           (if data
7054               (progn
7055                 (goto-char (gnus-data-pos (car data)))
7056                 (gnus-data-number (car data)))
7057             (gnus-message 3 "No more%s articles"
7058                           (let* ((r (when unread " unread"))
7059                                  (d (when undownloaded " undownloaded"))
7060                                  (s (when unseen " unseen"))
7061                                  (l (delq nil (list r d s))))
7062                             (cond ((= 3 (length l))
7063                                    (concat r "," d ", or" s))
7064                                   ((= 2 (length l))
7065                                    (concat (car l) ", or" (cadr l)))
7066                                   ((= 1 (length l))
7067                                    (car l))
7068                                   (t
7069                                    ""))))
7070             nil
7071             )
7072         (gnus-summary-position-point))))))
7073
7074 (defun gnus-summary-next-subject (n &optional unread dont-display)
7075   "Go to next N'th summary line.
7076 If N is negative, go to the previous N'th subject line.
7077 If UNREAD is non-nil, only unread articles are selected.
7078 The difference between N and the actual number of steps taken is
7079 returned."
7080   (interactive "p")
7081   (let ((backward (< n 0))
7082         (n (abs n)))
7083     (while (and (> n 0)
7084                 (if backward
7085                     (gnus-summary-find-prev unread)
7086                   (gnus-summary-find-next unread)))
7087       (unless (zerop (setq n (1- n)))
7088         (gnus-summary-show-thread)))
7089     (when (/= 0 n)
7090       (gnus-message 7 "No more%s articles"
7091                     (if unread " unread" "")))
7092     (unless dont-display
7093       (gnus-summary-recenter)
7094       (gnus-summary-position-point))
7095     n))
7096
7097 (defun gnus-summary-next-unread-subject (n)
7098   "Go to next N'th unread summary line."
7099   (interactive "p")
7100   (gnus-summary-next-subject n t))
7101
7102 (defun gnus-summary-prev-subject (n &optional unread)
7103   "Go to previous N'th summary line.
7104 If optional argument UNREAD is non-nil, only unread article is selected."
7105   (interactive "p")
7106   (gnus-summary-next-subject (- n) unread))
7107
7108 (defun gnus-summary-prev-unread-subject (n)
7109   "Go to previous N'th unread summary line."
7110   (interactive "p")
7111   (gnus-summary-next-subject (- n) t))
7112
7113 (defun gnus-summary-goto-subjects (articles)
7114   "Insert the subject header for ARTICLES in the current buffer."
7115   (save-excursion
7116     (dolist (article articles)
7117       (gnus-summary-goto-subject article t)))
7118   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7119   (gnus-summary-position-point))
7120
7121 (defun gnus-summary-goto-subject (article &optional force silent)
7122   "Go the subject line of ARTICLE.
7123 If FORCE, also allow jumping to articles not currently shown."
7124   (interactive "nArticle number: ")
7125   (unless (numberp article)
7126     (error "Article %s is not a number" article))
7127   (let ((b (point))
7128         (data (gnus-data-find article)))
7129     ;; We read in the article if we have to.
7130     (and (not data)
7131          force
7132          (gnus-summary-insert-subject
7133           article
7134           (if (or (numberp force) (vectorp force)) force)
7135           t)
7136          (setq data (gnus-data-find article)))
7137     (goto-char b)
7138     (if (not data)
7139         (progn
7140           (unless silent
7141             (gnus-message 3 "Can't find article %d" article))
7142           nil)
7143       (let ((pt (gnus-data-pos data)))
7144         (goto-char pt)
7145         (gnus-summary-set-article-display-arrow pt))
7146       (gnus-summary-position-point)
7147       article)))
7148
7149 ;; Walking around summary lines with displaying articles.
7150
7151 (defun gnus-summary-expand-window (&optional arg)
7152   "Make the summary buffer take up the entire Emacs frame.
7153 Given a prefix, will force an `article' buffer configuration."
7154   (interactive "P")
7155   (if arg
7156       (gnus-configure-windows 'article 'force)
7157     (gnus-configure-windows 'summary 'force)))
7158
7159 (defun gnus-summary-display-article (article &optional all-header)
7160   "Display ARTICLE in article buffer."
7161   (when (gnus-buffer-live-p gnus-article-buffer)
7162     (with-current-buffer gnus-article-buffer
7163       (mm-enable-multibyte)))
7164   (gnus-set-global-variables)
7165   (when (gnus-buffer-live-p gnus-article-buffer)
7166     (with-current-buffer gnus-article-buffer
7167       (setq gnus-article-charset gnus-newsgroup-charset)
7168       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7169       (mm-enable-multibyte)))
7170   (if (null article)
7171       nil
7172     (prog1
7173         (if gnus-summary-display-article-function
7174             (funcall gnus-summary-display-article-function article all-header)
7175           (gnus-article-prepare article all-header))
7176       (gnus-run-hooks 'gnus-select-article-hook)
7177       (when (and gnus-current-article
7178                  (not (zerop gnus-current-article)))
7179         (gnus-summary-goto-subject gnus-current-article))
7180       (gnus-summary-recenter)
7181       (when (and gnus-use-trees gnus-show-threads)
7182         (gnus-possibly-generate-tree article)
7183         (gnus-highlight-selected-tree article))
7184       ;; Successfully display article.
7185       (gnus-article-set-window-start
7186        (cdr (assq article gnus-newsgroup-bookmarks))))))
7187
7188 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7189   "Select the current article.
7190 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7191 non-nil, the article will be re-fetched even if it already present in
7192 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7193 be displayed."
7194   ;; Make sure we are in the summary buffer to work around bbdb bug.
7195   (unless (eq major-mode 'gnus-summary-mode)
7196     (set-buffer gnus-summary-buffer))
7197   (let ((article (or article (gnus-summary-article-number)))
7198         (all-headers (not (not all-headers))) ;Must be t or nil.
7199         gnus-summary-display-article-function)
7200     (and (not pseudo)
7201          (gnus-summary-article-pseudo-p article)
7202          (error "This is a pseudo-article"))
7203     (save-excursion
7204       (set-buffer gnus-summary-buffer)
7205       (if (or (and gnus-single-article-buffer
7206                    (or (null gnus-current-article)
7207                        (null gnus-article-current)
7208                        (null (get-buffer gnus-article-buffer))
7209                        (not (eq article (cdr gnus-article-current)))
7210                        (not (equal (car gnus-article-current)
7211                                    gnus-newsgroup-name))))
7212               (and (not gnus-single-article-buffer)
7213                    (or (null gnus-current-article)
7214                        (not (eq gnus-current-article article))))
7215               force)
7216           ;; The requested article is different from the current article.
7217           (progn
7218             (gnus-summary-display-article article all-headers)
7219             (when (gnus-buffer-live-p gnus-article-buffer)
7220               (with-current-buffer gnus-article-buffer
7221                 (if (not gnus-article-decoded-p) ;; a local variable
7222                     (mm-disable-multibyte))))
7223             (gnus-article-set-window-start
7224              (cdr (assq article gnus-newsgroup-bookmarks)))
7225             article)
7226         'old))))
7227
7228 (defun gnus-summary-force-verify-and-decrypt ()
7229   "Display buttons for signed/encrypted parts and verify/decrypt them."
7230   (interactive)
7231   (let ((mm-verify-option 'known)
7232         (mm-decrypt-option 'known)
7233         (gnus-article-emulate-mime t)
7234         (gnus-buttonized-mime-types (append (list "multipart/signed"
7235                                                   "multipart/encrypted")
7236                                             gnus-buttonized-mime-types)))
7237     (gnus-summary-select-article nil 'force)))
7238
7239 (defun gnus-summary-set-current-mark (&optional current-mark)
7240   "Obsolete function."
7241   nil)
7242
7243 (defun gnus-summary-next-article (&optional unread subject backward push)
7244   "Select the next article.
7245 If UNREAD, only unread articles are selected.
7246 If SUBJECT, only articles with SUBJECT are selected.
7247 If BACKWARD, the previous article is selected instead of the next."
7248   (interactive "P")
7249   (cond
7250    ;; Is there such an article?
7251    ((and (gnus-summary-search-forward unread subject backward)
7252          (or (gnus-summary-display-article (gnus-summary-article-number))
7253              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7254     (gnus-summary-position-point))
7255    ;; If not, we try the first unread, if that is wanted.
7256    ((and subject
7257          gnus-auto-select-same
7258          (gnus-summary-first-unread-article))
7259     (gnus-summary-position-point)
7260     (gnus-message 6 "Wrapped"))
7261    ;; Try to get next/previous article not displayed in this group.
7262    ((and gnus-auto-extend-newsgroup
7263          (not unread) (not subject))
7264     (gnus-summary-goto-article
7265      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7266      nil (count-lines (point-min) (point))))
7267    ;; Go to next/previous group.
7268    (t
7269     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7270       (gnus-summary-jump-to-group gnus-newsgroup-name))
7271     (let ((cmd last-command-char)
7272           (point
7273            (with-current-buffer gnus-group-buffer
7274              (point)))
7275           (group
7276            (if (eq gnus-keep-same-level 'best)
7277                (gnus-summary-best-group gnus-newsgroup-name)
7278              (gnus-summary-search-group backward gnus-keep-same-level))))
7279       ;; For some reason, the group window gets selected.  We change
7280       ;; it back.
7281       (select-window (get-buffer-window (current-buffer)))
7282       ;; Select next unread newsgroup automagically.
7283       (cond
7284        ((or (not gnus-auto-select-next)
7285             (not cmd))
7286         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7287        ((or (eq gnus-auto-select-next 'quietly)
7288             (and (eq gnus-auto-select-next 'slightly-quietly)
7289                  push)
7290             (and (eq gnus-auto-select-next 'almost-quietly)
7291                  (gnus-summary-last-article-p)))
7292         ;; Select quietly.
7293         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7294             (gnus-summary-exit)
7295           (gnus-message 7 "No more%s articles (%s)..."
7296                         (if unread " unread" "")
7297                         (if group (concat "selecting " group)
7298                           "exiting"))
7299           (gnus-summary-next-group nil group backward)))
7300        (t
7301         (when (gnus-key-press-event-p last-input-event)
7302           (gnus-summary-walk-group-buffer
7303            gnus-newsgroup-name cmd unread backward point))))))))
7304
7305 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7306   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7307                       (?\C-p (gnus-group-prev-unread-group 1))))
7308         (cursor-in-echo-area t)
7309         keve key group ended prompt)
7310     (save-excursion
7311       (set-buffer gnus-group-buffer)
7312       (goto-char start)
7313       (setq group
7314             (if (eq gnus-keep-same-level 'best)
7315                 (gnus-summary-best-group gnus-newsgroup-name)
7316               (gnus-summary-search-group backward gnus-keep-same-level))))
7317     (while (not ended)
7318       (setq prompt
7319             (format
7320              "No more%s articles%s " (if unread " unread" "")
7321              (if (and group
7322                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7323                  (format " (Type %s for %s [%s])"
7324                          (single-key-description cmd)
7325                          (gnus-group-decoded-name group)
7326                          (gnus-group-unread group))
7327                (format " (Type %s to exit %s)"
7328                        (single-key-description cmd)
7329                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7330       ;; Confirm auto selection.
7331       (setq key (car (setq keve (gnus-read-event-char prompt)))
7332             ended t)
7333       (cond
7334        ((assq key keystrokes)
7335         (let ((obuf (current-buffer)))
7336           (switch-to-buffer gnus-group-buffer)
7337           (when group
7338             (gnus-group-jump-to-group group))
7339           (eval (cadr (assq key keystrokes)))
7340           (setq group (gnus-group-group-name))
7341           (switch-to-buffer obuf))
7342         (setq ended nil))
7343        ((equal key cmd)
7344         (if (or (not group)
7345                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7346             (gnus-summary-exit)
7347           (gnus-summary-next-group nil group backward)))
7348        (t
7349         (push (cdr keve) unread-command-events))))))
7350
7351 (defun gnus-summary-next-unread-article ()
7352   "Select unread article after current one."
7353   (interactive)
7354   (gnus-summary-next-article
7355    (or (not (eq gnus-summary-goto-unread 'never))
7356        (gnus-summary-last-article-p (gnus-summary-article-number)))
7357    (and gnus-auto-select-same
7358         (gnus-summary-article-subject))))
7359
7360 (defun gnus-summary-prev-article (&optional unread subject)
7361   "Select the article after the current one.
7362 If UNREAD is non-nil, only unread articles are selected."
7363   (interactive "P")
7364   (gnus-summary-next-article unread subject t))
7365
7366 (defun gnus-summary-prev-unread-article ()
7367   "Select unread article before current one."
7368   (interactive)
7369   (gnus-summary-prev-article
7370    (or (not (eq gnus-summary-goto-unread 'never))
7371        (gnus-summary-first-article-p (gnus-summary-article-number)))
7372    (and gnus-auto-select-same
7373         (gnus-summary-article-subject))))
7374
7375 (defun gnus-summary-next-page (&optional lines circular stop)
7376   "Show next page of the selected article.
7377 If at the end of the current article, select the next article.
7378 LINES says how many lines should be scrolled up.
7379
7380 If CIRCULAR is non-nil, go to the start of the article instead of
7381 selecting the next article when reaching the end of the current
7382 article.
7383
7384 If STOP is non-nil, just stop when reaching the end of the message.
7385
7386 Also see the variable `gnus-article-skip-boring'."
7387   (interactive "P")
7388   (setq gnus-summary-buffer (current-buffer))
7389   (gnus-set-global-variables)
7390   (let ((article (gnus-summary-article-number))
7391         (article-window (get-buffer-window gnus-article-buffer t))
7392         endp)
7393     ;; If the buffer is empty, we have no article.
7394     (unless article
7395       (error "No article to select"))
7396     (gnus-configure-windows 'article)
7397     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7398         (if (and (eq gnus-summary-goto-unread 'never)
7399                  (not (gnus-summary-last-article-p article)))
7400             (gnus-summary-next-article)
7401           (gnus-summary-next-unread-article))
7402       (if (or (null gnus-current-article)
7403               (null gnus-article-current)
7404               (/= article (cdr gnus-article-current))
7405               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7406           ;; Selected subject is different from current article's.
7407           (gnus-summary-display-article article)
7408         (when article-window
7409           (gnus-eval-in-buffer-window gnus-article-buffer
7410             (setq endp (or (gnus-article-next-page lines)
7411                            (gnus-article-only-boring-p))))
7412           (when endp
7413             (cond (stop
7414                    (gnus-message 3 "End of message"))
7415                   (circular
7416                    (gnus-summary-beginning-of-article))
7417                   (lines
7418                    (gnus-message 3 "End of message"))
7419                   ((null lines)
7420                    (if (and (eq gnus-summary-goto-unread 'never)
7421                             (not (gnus-summary-last-article-p article)))
7422                        (gnus-summary-next-article)
7423                      (gnus-summary-next-unread-article))))))))
7424     (gnus-summary-recenter)
7425     (gnus-summary-position-point)))
7426
7427 (defun gnus-summary-prev-page (&optional lines move)
7428   "Show previous page of selected article.
7429 Argument LINES specifies lines to be scrolled down.
7430 If MOVE, move to the previous unread article if point is at
7431 the beginning of the buffer."
7432   (interactive "P")
7433   (let ((article (gnus-summary-article-number))
7434         (article-window (get-buffer-window gnus-article-buffer t))
7435         endp)
7436     (gnus-configure-windows 'article)
7437     (if (or (null gnus-current-article)
7438             (null gnus-article-current)
7439             (/= article (cdr gnus-article-current))
7440             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7441         ;; Selected subject is different from current article's.
7442         (gnus-summary-display-article article)
7443       (gnus-summary-recenter)
7444       (when article-window
7445         (gnus-eval-in-buffer-window gnus-article-buffer
7446           (setq endp (gnus-article-prev-page lines)))
7447         (when (and move endp)
7448           (cond (lines
7449                  (gnus-message 3 "Beginning of message"))
7450                 ((null lines)
7451                  (if (and (eq gnus-summary-goto-unread 'never)
7452                           (not (gnus-summary-first-article-p article)))
7453                      (gnus-summary-prev-article)
7454                    (gnus-summary-prev-unread-article))))))))
7455   (gnus-summary-position-point))
7456
7457 (defun gnus-summary-prev-page-or-article (&optional lines)
7458   "Show previous page of selected article.
7459 Argument LINES specifies lines to be scrolled down.
7460 If at the beginning of the article, go to the next article."
7461   (interactive "P")
7462   (gnus-summary-prev-page lines t))
7463
7464 (defun gnus-summary-scroll-up (lines)
7465   "Scroll up (or down) one line current article.
7466 Argument LINES specifies lines to be scrolled up (or down if negative)."
7467   (interactive "p")
7468   (gnus-configure-windows 'article)
7469   (gnus-summary-show-thread)
7470   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7471     (gnus-eval-in-buffer-window gnus-article-buffer
7472       (cond ((> lines 0)
7473              (when (gnus-article-next-page lines)
7474                (gnus-message 3 "End of message")))
7475             ((< lines 0)
7476              (gnus-article-prev-page (- lines))))))
7477   (gnus-summary-recenter)
7478   (gnus-summary-position-point))
7479
7480 (defun gnus-summary-scroll-down (lines)
7481   "Scroll down (or up) one line current article.
7482 Argument LINES specifies lines to be scrolled down (or up if negative)."
7483   (interactive "p")
7484   (gnus-summary-scroll-up (- lines)))
7485
7486 (defun gnus-summary-next-same-subject ()
7487   "Select next article which has the same subject as current one."
7488   (interactive)
7489   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7490
7491 (defun gnus-summary-prev-same-subject ()
7492   "Select previous article which has the same subject as current one."
7493   (interactive)
7494   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7495
7496 (defun gnus-summary-next-unread-same-subject ()
7497   "Select next unread article which has the same subject as current one."
7498   (interactive)
7499   (gnus-summary-next-article t (gnus-summary-article-subject)))
7500
7501 (defun gnus-summary-prev-unread-same-subject ()
7502   "Select previous unread article which has the same subject as current one."
7503   (interactive)
7504   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7505
7506 (defun gnus-summary-first-unread-article ()
7507   "Select the first unread article.
7508 Return nil if there are no unread articles."
7509   (interactive)
7510   (prog1
7511       (when (gnus-summary-first-subject t)
7512         (gnus-summary-show-thread)
7513         (gnus-summary-first-subject t)
7514         (gnus-summary-display-article (gnus-summary-article-number)))
7515     (gnus-summary-position-point)))
7516
7517 (defun gnus-summary-first-unread-subject ()
7518   "Place the point on the subject line of 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-position-point)))
7526
7527 (defun gnus-summary-first-unseen-subject ()
7528   "Place the point on the subject line of the first unseen article.
7529 Return nil if there are no unseen articles."
7530   (interactive)
7531   (prog1
7532       (when (gnus-summary-first-subject nil nil t)
7533         (gnus-summary-show-thread)
7534         (gnus-summary-first-subject nil nil t))
7535     (gnus-summary-position-point)))
7536
7537 (defun gnus-summary-first-unseen-or-unread-subject ()
7538   "Place the point on the subject line of the first unseen article or,
7539 if all article have been seen, on the subject line of the first unread
7540 article."
7541   (interactive)
7542   (prog1
7543       (unless (when (gnus-summary-first-subject nil nil t)
7544                 (gnus-summary-show-thread)
7545                 (gnus-summary-first-subject nil nil t))
7546         (when (gnus-summary-first-subject t)
7547           (gnus-summary-show-thread)
7548           (gnus-summary-first-subject t)))
7549     (gnus-summary-position-point)))
7550
7551 (defun gnus-summary-first-article ()
7552   "Select the first article.
7553 Return nil if there are no articles."
7554   (interactive)
7555   (prog1
7556       (when (gnus-summary-first-subject)
7557         (gnus-summary-show-thread)
7558         (gnus-summary-first-subject)
7559         (gnus-summary-display-article (gnus-summary-article-number)))
7560     (gnus-summary-position-point)))
7561
7562 (defun gnus-summary-best-unread-article (&optional arg)
7563   "Select the unread article with the highest score.
7564 If given a prefix argument, select the next unread article that has a
7565 score higher than the default score."
7566   (interactive "P")
7567   (let ((article (if arg
7568                      (gnus-summary-better-unread-subject)
7569                    (gnus-summary-best-unread-subject))))
7570     (if article
7571         (gnus-summary-goto-article article)
7572       (error "No unread articles"))))
7573
7574 (defun gnus-summary-best-unread-subject ()
7575   "Select the unread subject with the highest score."
7576   (interactive)
7577   (let ((best -1000000)
7578         (data gnus-newsgroup-data)
7579         article score)
7580     (while data
7581       (and (gnus-data-unread-p (car data))
7582            (> (setq score
7583                     (gnus-summary-article-score (gnus-data-number (car data))))
7584               best)
7585            (setq best score
7586                  article (gnus-data-number (car data))))
7587       (setq data (cdr data)))
7588     (when article
7589       (gnus-summary-goto-subject article))
7590     (gnus-summary-position-point)
7591     article))
7592
7593 (defun gnus-summary-better-unread-subject ()
7594   "Select the first unread subject that has a score over the default score."
7595   (interactive)
7596   (let ((data gnus-newsgroup-data)
7597         article score)
7598     (while (and (setq article (gnus-data-number (car data)))
7599                 (or (gnus-data-read-p (car data))
7600                     (not (> (gnus-summary-article-score article)
7601                             gnus-summary-default-score))))
7602       (setq data (cdr data)))
7603     (when article
7604       (gnus-summary-goto-subject article))
7605     (gnus-summary-position-point)
7606     article))
7607
7608 (defun gnus-summary-last-subject ()
7609   "Go to the last displayed subject line in the group."
7610   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7611     (when article
7612       (gnus-summary-goto-subject article))))
7613
7614 (defun gnus-summary-goto-article (article &optional all-headers force)
7615   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7616 If ALL-HEADERS is non-nil, no header lines are hidden.
7617 If FORCE, go to the article even if it isn't displayed.  If FORCE
7618 is a number, it is the line the article is to be displayed on."
7619   (interactive
7620    (list
7621     (completing-read
7622      "Article number or Message-ID: "
7623      (mapcar (lambda (number) (list (int-to-string number)))
7624              gnus-newsgroup-limit))
7625     current-prefix-arg
7626     t))
7627   (prog1
7628       (if (and (stringp article)
7629                (string-match "@\\|%40" article))
7630           (gnus-summary-refer-article article)
7631         (when (stringp article)
7632           (setq article (string-to-number article)))
7633         (if (gnus-summary-goto-subject article force)
7634             (gnus-summary-display-article article all-headers)
7635           (gnus-message 4 "Couldn't go to article %s" article) nil))
7636     (gnus-summary-position-point)))
7637
7638 (defun gnus-summary-goto-last-article ()
7639   "Go to the previously read article."
7640   (interactive)
7641   (prog1
7642       (when gnus-last-article
7643         (gnus-summary-goto-article gnus-last-article nil t))
7644     (gnus-summary-position-point)))
7645
7646 (defun gnus-summary-pop-article (number)
7647   "Pop one article off the history and go to the previous.
7648 NUMBER articles will be popped off."
7649   (interactive "p")
7650   (let (to)
7651     (setq gnus-newsgroup-history
7652           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7653     (if to
7654         (gnus-summary-goto-article (car to) nil t)
7655       (error "Article history empty")))
7656   (gnus-summary-position-point))
7657
7658 ;; Summary commands and functions for limiting the summary buffer.
7659
7660 (defun gnus-summary-limit-to-articles (n)
7661   "Limit the summary buffer to the next N articles.
7662 If not given a prefix, use the process marked articles instead."
7663   (interactive "P")
7664   (prog1
7665       (let ((articles (gnus-summary-work-articles n)))
7666         (setq gnus-newsgroup-processable nil)
7667         (gnus-summary-limit articles))
7668     (gnus-summary-position-point)))
7669
7670 (defun gnus-summary-pop-limit (&optional total)
7671   "Restore the previous limit.
7672 If given a prefix, remove all limits."
7673   (interactive "P")
7674   (when total
7675     (setq gnus-newsgroup-limits
7676           (list (mapcar (lambda (h) (mail-header-number h))
7677                         gnus-newsgroup-headers))))
7678   (unless gnus-newsgroup-limits
7679     (error "No limit to pop"))
7680   (prog1
7681       (gnus-summary-limit nil 'pop)
7682     (gnus-summary-position-point)))
7683
7684 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7685   "Limit the summary buffer to articles that have subjects that match a regexp.
7686 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7687   (interactive
7688    (list (read-string (if current-prefix-arg
7689                           "Exclude subject (regexp): "
7690                         "Limit to subject (regexp): "))
7691          nil current-prefix-arg))
7692   (unless header
7693     (setq header "subject"))
7694   (when (not (equal "" subject))
7695     (prog1
7696         (let ((articles (gnus-summary-find-matching
7697                          (or header "subject") subject 'all nil nil
7698                          not-matching)))
7699           (unless articles
7700             (error "Found no matches for \"%s\"" subject))
7701           (gnus-summary-limit articles))
7702       (gnus-summary-position-point))))
7703
7704 (defun gnus-summary-limit-to-author (from &optional not-matching)
7705   "Limit the summary buffer to articles that have authors that match a regexp.
7706 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7707   (interactive
7708    (list (read-string (if current-prefix-arg
7709                           "Exclude author (regexp): "
7710                         "Limit to author (regexp): "))
7711          current-prefix-arg))
7712   (gnus-summary-limit-to-subject from "from" not-matching))
7713
7714 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
7715   "Limit the summary buffer to articles with the given RECIPIENT.
7716
7717 If NOT-MATCHING, exclude RECIPIENT.
7718
7719 To and Cc headers are checked.  You need to include them in
7720 `nnmail-extra-headers'."
7721   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
7722   (interactive
7723    (list (read-string (format "%s recipient (regexp): "
7724                               (if current-prefix-arg "Exclude" "Limit to")))
7725          current-prefix-arg))
7726   (when (not (equal "" recipient))
7727     (prog1 (let* ((to
7728                    (if (memq 'To nnmail-extra-headers)
7729                        (gnus-summary-find-matching
7730                         (cons 'extra 'To) recipient 'all nil nil
7731                         not-matching)
7732                      (gnus-message
7733                       1 "`To' isn't present in `nnmail-extra-headers'")
7734                      (sit-for 1)
7735                      nil))
7736                   (cc
7737                    (if (memq 'Cc nnmail-extra-headers)
7738                        (gnus-summary-find-matching
7739                         (cons 'extra 'Cc) recipient 'all nil nil
7740                         not-matching)
7741                      (gnus-message
7742                       1 "`Cc' isn't present in `nnmail-extra-headers'")
7743                      (sit-for 1)
7744                      nil))
7745                   (articles
7746                    (if not-matching
7747                        ;; We need the numbers that are in both lists:
7748                        (mapcar (lambda (a)
7749                                  (and (memq a to) a))
7750                                cc)
7751                      (nconc to cc))))
7752              (unless articles
7753                (error "Found no matches for \"%s\"" recipient))
7754              (gnus-summary-limit articles))
7755       (gnus-summary-position-point))))
7756
7757 (defun gnus-summary-limit-to-age (age &optional younger-p)
7758   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7759 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7760 articles that are younger than AGE days."
7761   (interactive
7762    (let ((younger current-prefix-arg)
7763          (days-got nil)
7764          days)
7765      (while (not days-got)
7766        (setq days (if younger
7767                       (read-string "Limit to articles younger than (in days, older when negative): ")
7768                     (read-string
7769                      "Limit to articles older than (in days, younger when negative): ")))
7770        (when (> (length days) 0)
7771          (setq days (read days)))
7772        (if (numberp days)
7773            (progn
7774              (setq days-got t)
7775              (when (< days 0)
7776                (setq younger (not younger))
7777                (setq days (* days -1))))
7778          (message "Please enter a number.")
7779          (sleep-for 1)))
7780      (list days younger)))
7781   (prog1
7782       (let ((data gnus-newsgroup-data)
7783             (cutoff (days-to-time age))
7784             articles d date is-younger)
7785         (while (setq d (pop data))
7786           (when (and (vectorp (gnus-data-header d))
7787                      (setq date (mail-header-date (gnus-data-header d))))
7788             (setq is-younger (time-less-p
7789                               (time-since (condition-case ()
7790                                               (date-to-time date)
7791                                             (error '(0 0))))
7792                               cutoff))
7793             (when (if younger-p
7794                       is-younger
7795                     (not is-younger))
7796               (push (gnus-data-number d) articles))))
7797         (gnus-summary-limit (nreverse articles)))
7798     (gnus-summary-position-point)))
7799
7800 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7801   "Limit the summary buffer to articles that match an 'extra' header."
7802   (interactive
7803    (let ((header
7804           (intern
7805            (gnus-completing-read-with-default
7806             (symbol-name (car gnus-extra-headers))
7807             (if current-prefix-arg
7808                 "Exclude extra header:"
7809               "Limit extra header:")
7810             (mapcar (lambda (x)
7811                       (cons (symbol-name x) x))
7812                     gnus-extra-headers)
7813             nil
7814             t))))
7815      (list header
7816            (read-string (format "%s header %s (regexp): "
7817                                 (if current-prefix-arg "Exclude" "Limit to")
7818                                 header))
7819            current-prefix-arg)))
7820   (when (not (equal "" regexp))
7821     (prog1
7822         (let ((articles (gnus-summary-find-matching
7823                          (cons 'extra header) regexp 'all nil nil
7824                          not-matching)))
7825           (unless articles
7826             (error "Found no matches for \"%s\"" regexp))
7827           (gnus-summary-limit articles))
7828       (gnus-summary-position-point))))
7829
7830 (defun gnus-summary-limit-to-display-predicate ()
7831   "Limit the summary buffer to the predicated in the `display' group parameter."
7832   (interactive)
7833   (unless gnus-newsgroup-display
7834     (error "There is no `display' group parameter"))
7835   (let (articles)
7836     (dolist (number gnus-newsgroup-articles)
7837       (when (funcall gnus-newsgroup-display)
7838         (push number articles)))
7839     (gnus-summary-limit articles))
7840   (gnus-summary-position-point))
7841
7842 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7843 (make-obsolete
7844  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7845
7846 (defun gnus-summary-limit-to-unread (&optional all)
7847   "Limit the summary buffer to articles that are not marked as read.
7848 If ALL is non-nil, limit strictly to unread articles."
7849   (interactive "P")
7850   (if all
7851       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7852     (gnus-summary-limit-to-marks
7853      ;; Concat all the marks that say that an article is read and have
7854      ;; those removed.
7855      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7856            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7857            gnus-low-score-mark gnus-expirable-mark
7858            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7859            gnus-duplicate-mark gnus-souped-mark)
7860      'reverse)))
7861
7862 (defun gnus-summary-limit-to-replied (&optional unreplied)
7863   "Limit the summary buffer to replied articles.
7864 If UNREPLIED (the prefix), limit to unreplied articles."
7865   (interactive "P")
7866   (if unreplied
7867       (gnus-summary-limit
7868        (gnus-set-difference gnus-newsgroup-articles
7869         gnus-newsgroup-replied))
7870     (gnus-summary-limit gnus-newsgroup-replied))
7871   (gnus-summary-position-point))
7872
7873 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7874 (make-obsolete 'gnus-summary-delete-marked-with
7875                'gnus-summary-limit-exclude-marks)
7876
7877 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7878   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7879 If REVERSE, limit the summary buffer to articles that are marked
7880 with MARKS.  MARKS can either be a string of marks or a list of marks.
7881 Returns how many articles were removed."
7882   (interactive "sMarks: ")
7883   (gnus-summary-limit-to-marks marks t))
7884
7885 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7886   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7887 If REVERSE (the prefix), limit the summary buffer to articles that are
7888 not marked with MARKS.  MARKS can either be a string of marks or a
7889 list of marks.
7890 Returns how many articles were removed."
7891   (interactive "sMarks: \nP")
7892   (prog1
7893       (let ((data gnus-newsgroup-data)
7894             (marks (if (listp marks) marks
7895                      (append marks nil))) ; Transform to list.
7896             articles)
7897         (while data
7898           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7899                   (memq (gnus-data-mark (car data)) marks))
7900             (push (gnus-data-number (car data)) articles))
7901           (setq data (cdr data)))
7902         (gnus-summary-limit articles))
7903     (gnus-summary-position-point)))
7904
7905 (defun gnus-summary-limit-to-score (score)
7906   "Limit to articles with score at or above SCORE."
7907   (interactive "NLimit to articles with score of at least: ")
7908   (let ((data gnus-newsgroup-data)
7909         articles)
7910     (while data
7911       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7912                 score)
7913         (push (gnus-data-number (car data)) articles))
7914       (setq data (cdr data)))
7915     (prog1
7916         (gnus-summary-limit articles)
7917       (gnus-summary-position-point))))
7918
7919 (defun gnus-summary-limit-to-unseen ()
7920   "Limit to unseen articles."
7921   (interactive)
7922   (prog1
7923       (gnus-summary-limit gnus-newsgroup-unseen)
7924     (gnus-summary-position-point)))
7925
7926 (defun gnus-summary-limit-include-thread (id)
7927   "Display all the hidden articles that is in the thread with ID in it.
7928 When called interactively, ID is the Message-ID of the current
7929 article."
7930   (interactive (list (mail-header-id (gnus-summary-article-header))))
7931   (let ((articles (gnus-articles-in-thread
7932                    (gnus-id-to-thread (gnus-root-id id)))))
7933     (prog1
7934         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7935       (gnus-summary-limit-include-matching-articles
7936        "subject"
7937        (regexp-quote (gnus-simplify-subject-re
7938                       (mail-header-subject (gnus-id-to-header id)))))
7939       (gnus-summary-position-point))))
7940
7941 (defun gnus-summary-limit-include-matching-articles (header regexp)
7942   "Display all the hidden articles that have HEADERs that match REGEXP."
7943   (interactive (list (read-string "Match on header: ")
7944                      (read-string "Regexp: ")))
7945   (let ((articles (gnus-find-matching-articles header regexp)))
7946     (prog1
7947         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7948       (gnus-summary-position-point))))
7949
7950 (defun gnus-summary-insert-dormant-articles ()
7951   "Insert all the dormant articles for this group into the current buffer."
7952   (interactive)
7953   (let ((gnus-verbose (max 6 gnus-verbose)))
7954     (if (not gnus-newsgroup-dormant)
7955         (gnus-message 3 "No cached articles for this group")
7956       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7957
7958 (defun gnus-summary-limit-include-dormant ()
7959   "Display all the hidden articles that are marked as dormant.
7960 Note that this command only works on a subset of the articles currently
7961 fetched for this group."
7962   (interactive)
7963   (unless gnus-newsgroup-dormant
7964     (error "There are no dormant articles in this group"))
7965   (prog1
7966       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7967     (gnus-summary-position-point)))
7968
7969 (defun gnus-summary-limit-exclude-dormant ()
7970   "Hide all dormant articles."
7971   (interactive)
7972   (prog1
7973       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7974     (gnus-summary-position-point)))
7975
7976 (defun gnus-summary-limit-exclude-childless-dormant ()
7977   "Hide all dormant articles that have no children."
7978   (interactive)
7979   (let ((data (gnus-data-list t))
7980         articles d children)
7981     ;; Find all articles that are either not dormant or have
7982     ;; children.
7983     (while (setq d (pop data))
7984       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7985                 (and (setq children
7986                            (gnus-article-children (gnus-data-number d)))
7987                      (let (found)
7988                        (while children
7989                          (when (memq (car children) articles)
7990                            (setq children nil
7991                                  found t))
7992                          (pop children))
7993                        found)))
7994         (push (gnus-data-number d) articles)))
7995     ;; Do the limiting.
7996     (prog1
7997         (gnus-summary-limit articles)
7998       (gnus-summary-position-point))))
7999
8000 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8001   "Mark all unread excluded articles as read.
8002 If ALL, mark even excluded ticked and dormants as read."
8003   (interactive "P")
8004   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8005   (let ((articles (gnus-sorted-ndifference
8006                    (sort
8007                     (mapcar (lambda (h) (mail-header-number h))
8008                             gnus-newsgroup-headers)
8009                     '<)
8010                    gnus-newsgroup-limit))
8011         article)
8012     (setq gnus-newsgroup-unreads
8013           (gnus-sorted-intersection gnus-newsgroup-unreads
8014                                     gnus-newsgroup-limit))
8015     (if all
8016         (setq gnus-newsgroup-dormant nil
8017               gnus-newsgroup-marked nil
8018               gnus-newsgroup-reads
8019               (nconc
8020                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8021                gnus-newsgroup-reads))
8022       (while (setq article (pop articles))
8023         (unless (or (memq article gnus-newsgroup-dormant)
8024                     (memq article gnus-newsgroup-marked))
8025           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8026
8027 (defun gnus-summary-limit (articles &optional pop)
8028   (if pop
8029       ;; We pop the previous limit off the stack and use that.
8030       (setq articles (car gnus-newsgroup-limits)
8031             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8032     ;; We use the new limit, so we push the old limit on the stack.
8033     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8034   ;; Set the limit.
8035   (setq gnus-newsgroup-limit articles)
8036   (let ((total (length gnus-newsgroup-data))
8037         (data (gnus-data-find-list (gnus-summary-article-number)))
8038         (gnus-summary-mark-below nil)   ; Inhibit this.
8039         found)
8040     ;; This will do all the work of generating the new summary buffer
8041     ;; according to the new limit.
8042     (gnus-summary-prepare)
8043     ;; Hide any threads, possibly.
8044     (gnus-summary-maybe-hide-threads)
8045     ;; Try to return to the article you were at, or one in the
8046     ;; neighborhood.
8047     (when data
8048       ;; We try to find some article after the current one.
8049       (while data
8050         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8051           (setq data nil
8052                 found t))
8053         (setq data (cdr data))))
8054     (unless found
8055       ;; If there is no data, that means that we were after the last
8056       ;; article.  The same goes when we can't find any articles
8057       ;; after the current one.
8058       (goto-char (point-max))
8059       (gnus-summary-find-prev))
8060     (gnus-set-mode-line 'summary)
8061     ;; We return how many articles were removed from the summary
8062     ;; buffer as a result of the new limit.
8063     (- total (length gnus-newsgroup-data))))
8064
8065 (defsubst gnus-invisible-cut-children (threads)
8066   (let ((num 0))
8067     (while threads
8068       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8069         (incf num))
8070       (pop threads))
8071     (< num 2)))
8072
8073 (defsubst gnus-cut-thread (thread)
8074   "Go forwards in the thread until we find an article that we want to display."
8075   (when (or (eq gnus-fetch-old-headers 'some)
8076             (eq gnus-fetch-old-headers 'invisible)
8077             (numberp gnus-fetch-old-headers)
8078             (eq gnus-build-sparse-threads 'some)
8079             (eq gnus-build-sparse-threads 'more))
8080     ;; Deal with old-fetched headers and sparse threads.
8081     (while (and
8082             thread
8083             (or
8084              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8085              (gnus-summary-article-ancient-p
8086               (mail-header-number (car thread))))
8087             (if (or (<= (length (cdr thread)) 1)
8088                     (eq gnus-fetch-old-headers 'invisible))
8089                 (setq gnus-newsgroup-limit
8090                       (delq (mail-header-number (car thread))
8091                             gnus-newsgroup-limit)
8092                       thread (cadr thread))
8093               (when (gnus-invisible-cut-children (cdr thread))
8094                 (let ((th (cdr thread)))
8095                   (while th
8096                     (if (memq (mail-header-number (caar th))
8097                               gnus-newsgroup-limit)
8098                         (setq thread (car th)
8099                               th nil)
8100                       (setq th (cdr th))))))))))
8101   thread)
8102
8103 (defun gnus-cut-threads (threads)
8104   "Cut off all uninteresting articles from the beginning of THREADS."
8105   (when (or (eq gnus-fetch-old-headers 'some)
8106             (eq gnus-fetch-old-headers 'invisible)
8107             (numberp gnus-fetch-old-headers)
8108             (eq gnus-build-sparse-threads 'some)
8109             (eq gnus-build-sparse-threads 'more))
8110     (let ((th threads))
8111       (while th
8112         (setcar th (gnus-cut-thread (car th)))
8113         (setq th (cdr th)))))
8114   ;; Remove nixed out threads.
8115   (delq nil threads))
8116
8117 (defun gnus-summary-initial-limit (&optional show-if-empty)
8118   "Figure out what the initial limit is supposed to be on group entry.
8119 This entails weeding out unwanted dormants, low-scored articles,
8120 fetch-old-headers verbiage, and so on."
8121   ;; Most groups have nothing to remove.
8122   (if (or gnus-inhibit-limiting
8123           (and (null gnus-newsgroup-dormant)
8124                (eq gnus-newsgroup-display 'gnus-not-ignore)
8125                (not (eq gnus-fetch-old-headers 'some))
8126                (not (numberp gnus-fetch-old-headers))
8127                (not (eq gnus-fetch-old-headers 'invisible))
8128                (null gnus-summary-expunge-below)
8129                (not (eq gnus-build-sparse-threads 'some))
8130                (not (eq gnus-build-sparse-threads 'more))
8131                (null gnus-thread-expunge-below)
8132                (not gnus-use-nocem)))
8133       ()                                ; Do nothing.
8134     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8135     (setq gnus-newsgroup-limit nil)
8136     (mapatoms
8137      (lambda (node)
8138        (unless (car (symbol-value node))
8139          ;; These threads have no parents -- they are roots.
8140          (let ((nodes (cdr (symbol-value node)))
8141                thread)
8142            (while nodes
8143              (if (and gnus-thread-expunge-below
8144                       (< (gnus-thread-total-score (car nodes))
8145                          gnus-thread-expunge-below))
8146                  (gnus-expunge-thread (pop nodes))
8147                (setq thread (pop nodes))
8148                (gnus-summary-limit-children thread))))))
8149      gnus-newsgroup-dependencies)
8150     ;; If this limitation resulted in an empty group, we might
8151     ;; pop the previous limit and use it instead.
8152     (when (and (not gnus-newsgroup-limit)
8153                show-if-empty)
8154       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8155     gnus-newsgroup-limit))
8156
8157 (defun gnus-summary-limit-children (thread)
8158   "Return 1 if this subthread is visible and 0 if it is not."
8159   ;; First we get the number of visible children to this thread.  This
8160   ;; is done by recursing down the thread using this function, so this
8161   ;; will really go down to a leaf article first, before slowly
8162   ;; working its way up towards the root.
8163   (when thread
8164     (let* ((max-lisp-eval-depth 5000)
8165            (children
8166            (if (cdr thread)
8167                (apply '+ (mapcar 'gnus-summary-limit-children
8168                                  (cdr thread)))
8169              0))
8170           (number (mail-header-number (car thread)))
8171           score)
8172       (if (and
8173            (not (memq number gnus-newsgroup-marked))
8174            (or
8175             ;; If this article is dormant and has absolutely no visible
8176             ;; children, then this article isn't visible.
8177             (and (memq number gnus-newsgroup-dormant)
8178                  (zerop children))
8179             ;; If this is "fetch-old-headered" and there is no
8180             ;; visible children, then we don't want this article.
8181             (and (or (eq gnus-fetch-old-headers 'some)
8182                      (numberp gnus-fetch-old-headers))
8183                  (gnus-summary-article-ancient-p number)
8184                  (zerop children))
8185             ;; If this is "fetch-old-headered" and `invisible', then
8186             ;; we don't want this article.
8187             (and (eq gnus-fetch-old-headers 'invisible)
8188                  (gnus-summary-article-ancient-p number))
8189             ;; If this is a sparsely inserted article with no children,
8190             ;; we don't want it.
8191             (and (eq gnus-build-sparse-threads 'some)
8192                  (gnus-summary-article-sparse-p number)
8193                  (zerop children))
8194             ;; If we use expunging, and this article is really
8195             ;; low-scored, then we don't want this article.
8196             (when (and gnus-summary-expunge-below
8197                        (< (setq score
8198                                 (or (cdr (assq number gnus-newsgroup-scored))
8199                                     gnus-summary-default-score))
8200                           gnus-summary-expunge-below))
8201               ;; We increase the expunge-tally here, but that has
8202               ;; nothing to do with the limits, really.
8203               (incf gnus-newsgroup-expunged-tally)
8204               ;; We also mark as read here, if that's wanted.
8205               (when (and gnus-summary-mark-below
8206                          (< score gnus-summary-mark-below))
8207                 (setq gnus-newsgroup-unreads
8208                       (delq number gnus-newsgroup-unreads))
8209                 (if gnus-newsgroup-auto-expire
8210                     (push number gnus-newsgroup-expirable)
8211                   (push (cons number gnus-low-score-mark)
8212                         gnus-newsgroup-reads)))
8213               t)
8214             ;; Do the `display' group parameter.
8215             (and gnus-newsgroup-display
8216                  (not (funcall gnus-newsgroup-display)))
8217             ;; Check NoCeM things.
8218             (when (and gnus-use-nocem
8219                        (gnus-nocem-unwanted-article-p
8220                         (mail-header-id (car thread))))
8221               (setq gnus-newsgroup-unreads
8222                     (delq number gnus-newsgroup-unreads))
8223               t)))
8224           ;; Nope, invisible article.
8225           0
8226         ;; Ok, this article is to be visible, so we add it to the limit
8227         ;; and return 1.
8228         (push number gnus-newsgroup-limit)
8229         1))))
8230
8231 (defun gnus-expunge-thread (thread)
8232   "Mark all articles in THREAD as read."
8233   (let* ((number (mail-header-number (car thread))))
8234     (incf gnus-newsgroup-expunged-tally)
8235     ;; We also mark as read here, if that's wanted.
8236     (setq gnus-newsgroup-unreads
8237           (delq number gnus-newsgroup-unreads))
8238     (if gnus-newsgroup-auto-expire
8239         (push number gnus-newsgroup-expirable)
8240       (push (cons number gnus-low-score-mark)
8241             gnus-newsgroup-reads)))
8242   ;; Go recursively through all subthreads.
8243   (mapcar 'gnus-expunge-thread (cdr thread)))
8244
8245 ;; Summary article oriented commands
8246
8247 (defun gnus-summary-refer-parent-article (n)
8248   "Refer parent article N times.
8249 If N is negative, go to ancestor -N instead.
8250 The difference between N and the number of articles fetched is returned."
8251   (interactive "p")
8252   (let ((skip 1)
8253         error header ref)
8254     (when (not (natnump n))
8255       (setq skip (abs n)
8256             n 1))
8257     (while (and (> n 0)
8258                 (not error))
8259       (setq header (gnus-summary-article-header))
8260       (if (and (eq (mail-header-number header)
8261                    (cdr gnus-article-current))
8262                (equal gnus-newsgroup-name
8263                       (car gnus-article-current)))
8264           ;; If we try to find the parent of the currently
8265           ;; displayed article, then we take a look at the actual
8266           ;; References header, since this is slightly more
8267           ;; reliable than the References field we got from the
8268           ;; server.
8269           (save-excursion
8270             (set-buffer gnus-original-article-buffer)
8271             (nnheader-narrow-to-headers)
8272             (unless (setq ref (message-fetch-field "references"))
8273               (when (setq ref (message-fetch-field "in-reply-to"))
8274                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8275             (widen))
8276         (setq ref
8277               ;; It's not the current article, so we take a bet on
8278               ;; the value we got from the server.
8279               (mail-header-references header)))
8280       (if (and ref
8281                (not (equal ref "")))
8282           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8283             (gnus-message 1 "Couldn't find parent"))
8284         (gnus-message 1 "No references in article %d"
8285                       (gnus-summary-article-number))
8286         (setq error t))
8287       (decf n))
8288     (gnus-summary-position-point)
8289     n))
8290
8291 (defun gnus-summary-refer-references ()
8292   "Fetch all articles mentioned in the References header.
8293 Return the number of articles fetched."
8294   (interactive)
8295   (let ((ref (mail-header-references (gnus-summary-article-header)))
8296         (current (gnus-summary-article-number))
8297         (n 0))
8298     (if (or (not ref)
8299             (equal ref ""))
8300         (error "No References in the current article")
8301       ;; For each Message-ID in the References header...
8302       (while (string-match "<[^>]*>" ref)
8303         (incf n)
8304         ;; ... fetch that article.
8305         (gnus-summary-refer-article
8306          (prog1 (match-string 0 ref)
8307            (setq ref (substring ref (match-end 0))))))
8308       (gnus-summary-goto-subject current)
8309       (gnus-summary-position-point)
8310       n)))
8311
8312 (defun gnus-summary-refer-thread (&optional limit)
8313   "Fetch all articles in the current thread.
8314 If LIMIT (the numerical prefix), fetch that many old headers instead
8315 of what's specified by the `gnus-refer-thread-limit' variable."
8316   (interactive "P")
8317   (let ((id (mail-header-id (gnus-summary-article-header)))
8318         (limit (if limit (prefix-numeric-value limit)
8319                  gnus-refer-thread-limit)))
8320     (unless (eq gnus-fetch-old-headers 'invisible)
8321       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8322       ;; Retrieve the headers and read them in.
8323       (if (eq (if (numberp limit)
8324                   (gnus-retrieve-headers
8325                    (list (min
8326                           (+ (mail-header-number
8327                               (gnus-summary-article-header))
8328                              limit)
8329                           gnus-newsgroup-end))
8330                    gnus-newsgroup-name (* limit 2))
8331                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8332                 ;; headers.
8333                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8334                                        gnus-newsgroup-name limit))
8335               'nov)
8336           (gnus-build-all-threads)
8337         (error "Can't fetch thread from back ends that don't support NOV"))
8338       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8339     (gnus-summary-limit-include-thread id)))
8340
8341 (defun gnus-summary-refer-article (message-id)
8342   "Fetch an article specified by MESSAGE-ID."
8343   (interactive "sMessage-ID: ")
8344   (when (and (stringp message-id)
8345              (not (zerop (length message-id))))
8346     (setq message-id (gnus-replace-in-string message-id " " ""))
8347     ;; Construct the correct Message-ID if necessary.
8348     ;; Suggested by tale@pawl.rpi.edu.
8349     (unless (string-match "^<" message-id)
8350       (setq message-id (concat "<" message-id)))
8351     (unless (string-match ">$" message-id)
8352       (setq message-id (concat message-id ">")))
8353     ;; People often post MIDs from URLs, so unhex it:
8354     (unless (string-match "@" message-id)
8355       (setq message-id (gnus-url-unhex-string message-id)))
8356     (let* ((header (gnus-id-to-header message-id))
8357            (sparse (and header
8358                         (gnus-summary-article-sparse-p
8359                          (mail-header-number header))
8360                         (memq (mail-header-number header)
8361                               gnus-newsgroup-limit)))
8362            number)
8363       (cond
8364        ;; If the article is present in the buffer we just go to it.
8365        ((and header
8366              (or (not (gnus-summary-article-sparse-p
8367                        (mail-header-number header)))
8368                  sparse))
8369         (prog1
8370             (gnus-summary-goto-article
8371              (mail-header-number header) nil t)
8372           (when sparse
8373             (gnus-summary-update-article (mail-header-number header)))))
8374        (t
8375         ;; We fetch the article.
8376         (catch 'found
8377           (dolist (gnus-override-method (gnus-refer-article-methods))
8378             (when (and (gnus-check-server gnus-override-method)
8379                        ;; Fetch the header,
8380                        (setq number (gnus-summary-insert-subject message-id)))
8381               ;; and display the article.
8382               (gnus-summary-select-article nil nil nil number)
8383               (throw 'found t)))
8384           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8385
8386 (defun gnus-refer-article-methods ()
8387   "Return a list of referable methods."
8388   (cond
8389    ;; No method, so we default to current and native.
8390    ((null gnus-refer-article-method)
8391     (list gnus-current-select-method gnus-select-method))
8392    ;; Current.
8393    ((eq 'current gnus-refer-article-method)
8394     (list gnus-current-select-method))
8395    ;; List of select methods.
8396    ((not (and (symbolp (car gnus-refer-article-method))
8397               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8398     (let (out)
8399       (dolist (method gnus-refer-article-method)
8400         (push (if (eq 'current method)
8401                   gnus-current-select-method
8402                 method)
8403               out))
8404       (nreverse out)))
8405    ;; One single select method.
8406    (t
8407     (list gnus-refer-article-method))))
8408
8409 (defun gnus-summary-edit-parameters ()
8410   "Edit the group parameters of the current group."
8411   (interactive)
8412   (gnus-group-edit-group gnus-newsgroup-name 'params))
8413
8414 (defun gnus-summary-customize-parameters ()
8415   "Customize the group parameters of the current group."
8416   (interactive)
8417   (gnus-group-customize gnus-newsgroup-name))
8418
8419 (defun gnus-summary-enter-digest-group (&optional force)
8420   "Enter an nndoc group based on the current article.
8421 If FORCE, force a digest interpretation.  If not, try
8422 to guess what the document format is."
8423   (interactive "P")
8424   (let ((conf gnus-current-window-configuration))
8425     (save-window-excursion
8426       (save-excursion
8427         (let (gnus-article-prepare-hook
8428               gnus-display-mime-function
8429               gnus-break-pages)
8430           (gnus-summary-select-article))))
8431     (setq gnus-current-window-configuration conf)
8432     (let* ((name (format "%s-%d"
8433                          (gnus-group-prefixed-name
8434                           gnus-newsgroup-name (list 'nndoc ""))
8435                          (with-current-buffer gnus-summary-buffer
8436                            gnus-current-article)))
8437            (ogroup gnus-newsgroup-name)
8438            (params (append (gnus-info-params (gnus-get-info ogroup))
8439                            (list (cons 'to-group ogroup))
8440                            (list (cons 'parent-group ogroup))
8441                            (list (cons 'save-article-group ogroup))))
8442            (case-fold-search t)
8443            (buf (current-buffer))
8444            dig to-address)
8445       (save-excursion
8446         (set-buffer gnus-original-article-buffer)
8447         ;; Have the digest group inherit the main mail address of
8448         ;; the parent article.
8449         (when (setq to-address (or (gnus-fetch-field "reply-to")
8450                                    (gnus-fetch-field "from")))
8451           (setq params (append
8452                         (list (cons 'to-address
8453                                     (funcall gnus-decode-encoded-word-function
8454                                              to-address))))))
8455         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8456         (insert-buffer-substring gnus-original-article-buffer)
8457         ;; Remove lines that may lead nndoc to misinterpret the
8458         ;; document type.
8459         (narrow-to-region
8460          (goto-char (point-min))
8461          (or (search-forward "\n\n" nil t) (point)))
8462         (goto-char (point-min))
8463         (delete-matching-lines "^Path:\\|^From ")
8464         (widen))
8465       (unwind-protect
8466           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8467                     (gnus-newsgroup-ephemeral-ignored-charsets
8468                      gnus-newsgroup-ignored-charsets))
8469                 (gnus-group-read-ephemeral-group
8470                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8471                               (nndoc-article-type
8472                                ,(if force 'mbox 'guess)))
8473                  t nil nil nil
8474                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8475                                                         "ADAPT")))))
8476               ;; Make all postings to this group go to the parent group.
8477               (nconc (gnus-info-params (gnus-get-info name))
8478                      params)
8479             ;; Couldn't select this doc group.
8480             (switch-to-buffer buf)
8481             (gnus-set-global-variables)
8482             (gnus-configure-windows 'summary)
8483             (gnus-message 3 "Article couldn't be entered?"))
8484         (kill-buffer dig)))))
8485
8486 (defun gnus-summary-read-document (n)
8487   "Open a new group based on the current article(s).
8488 This will allow you to read digests and other similar
8489 documents as newsgroups.
8490 Obeys the standard process/prefix convention."
8491   (interactive "P")
8492   (let* ((ogroup gnus-newsgroup-name)
8493          (params (append (gnus-info-params (gnus-get-info ogroup))
8494                          (list (cons 'to-group ogroup))))
8495          group egroup groups vgroup)
8496     (dolist (article (gnus-summary-work-articles n))
8497       (setq group (format "%s-%d" gnus-newsgroup-name article))
8498       (gnus-summary-remove-process-mark article)
8499       (when (gnus-summary-display-article article)
8500         (save-excursion
8501           (with-temp-buffer
8502             (insert-buffer-substring gnus-original-article-buffer)
8503             ;; Remove some headers that may lead nndoc to make
8504             ;; the wrong guess.
8505             (message-narrow-to-head)
8506             (goto-char (point-min))
8507             (delete-matching-lines "^Path:\\|^From ")
8508             (widen)
8509             (if (setq egroup
8510                       (gnus-group-read-ephemeral-group
8511                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8512                                      (nndoc-article-type guess))
8513                        t nil t))
8514                 (progn
8515             ;; Make all postings to this group go to the parent group.
8516                   (nconc (gnus-info-params (gnus-get-info egroup))
8517                          params)
8518                   (push egroup groups))
8519               ;; Couldn't select this doc group.
8520               (gnus-error 3 "Article couldn't be entered"))))))
8521     ;; Now we have selected all the documents.
8522     (cond
8523      ((not groups)
8524       (error "None of the articles could be interpreted as documents"))
8525      ((gnus-group-read-ephemeral-group
8526        (setq vgroup (format
8527                      "nnvirtual:%s-%s" gnus-newsgroup-name
8528                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8529        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8530        t
8531        (cons (current-buffer) 'summary)))
8532      (t
8533       (error "Couldn't select virtual nndoc group")))))
8534
8535 (defun gnus-summary-isearch-article (&optional regexp-p)
8536   "Do incremental search forward on the current article.
8537 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8538   (interactive "P")
8539   (gnus-summary-select-article)
8540   (gnus-configure-windows 'article)
8541   (gnus-eval-in-buffer-window gnus-article-buffer
8542     (save-restriction
8543       (widen)
8544       (isearch-forward regexp-p))))
8545
8546 (defun gnus-summary-search-article-forward (regexp &optional backward)
8547   "Search for an article containing REGEXP forward.
8548 If BACKWARD, search backward instead."
8549   (interactive
8550    (list (read-string
8551           (format "Search article %s (regexp%s): "
8552                   (if current-prefix-arg "backward" "forward")
8553                   (if gnus-last-search-regexp
8554                       (concat ", default " gnus-last-search-regexp)
8555                     "")))
8556          current-prefix-arg))
8557   (if (string-equal regexp "")
8558       (setq regexp (or gnus-last-search-regexp ""))
8559     (setq gnus-last-search-regexp regexp)
8560     (setq gnus-article-before-search gnus-current-article))
8561   ;; Intentionally set gnus-last-article.
8562   (setq gnus-last-article gnus-article-before-search)
8563   (let ((gnus-last-article gnus-last-article))
8564     (if (gnus-summary-search-article regexp backward)
8565         (gnus-summary-show-thread)
8566       (signal 'search-failed (list regexp)))))
8567
8568 (defun gnus-summary-search-article-backward (regexp)
8569   "Search for an article containing REGEXP backward."
8570   (interactive
8571    (list (read-string
8572           (format "Search article backward (regexp%s): "
8573                   (if gnus-last-search-regexp
8574                       (concat ", default " gnus-last-search-regexp)
8575                     "")))))
8576   (gnus-summary-search-article-forward regexp 'backward))
8577
8578 (defun gnus-summary-search-article (regexp &optional backward)
8579   "Search for an article containing REGEXP.
8580 Optional argument BACKWARD means do search for backward.
8581 `gnus-select-article-hook' is not called during the search."
8582   ;; We have to require this here to make sure that the following
8583   ;; dynamic binding isn't shadowed by autoloading.
8584   (require 'gnus-async)
8585   (require 'gnus-art)
8586   (let ((gnus-select-article-hook nil)  ;Disable hook.
8587         (gnus-article-prepare-hook nil)
8588         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8589         (gnus-use-article-prefetch nil)
8590         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8591         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8592         (gnus-visual nil)
8593         (gnus-keep-backlog nil)
8594         (gnus-break-pages nil)
8595         (gnus-summary-display-arrow nil)
8596         (gnus-updated-mode-lines nil)
8597         (gnus-auto-center-summary nil)
8598         (sum (current-buffer))
8599         (gnus-display-mime-function nil)
8600         (found nil)
8601         point)
8602     (gnus-save-hidden-threads
8603       (gnus-summary-select-article)
8604       (set-buffer gnus-article-buffer)
8605       (goto-char (window-point (get-buffer-window (current-buffer))))
8606       (when backward
8607         (forward-line -1))
8608       (while (not found)
8609         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8610         (if (if backward
8611                 (re-search-backward regexp nil t)
8612               (re-search-forward regexp nil t))
8613             ;; We found the regexp.
8614             (progn
8615               (setq found 'found)
8616               (beginning-of-line)
8617               (set-window-start
8618                (get-buffer-window (current-buffer))
8619                (point))
8620               (forward-line 1)
8621               (set-window-point
8622                (get-buffer-window (current-buffer))
8623                (point))
8624               (set-buffer sum)
8625               (setq point (point)))
8626           ;; We didn't find it, so we go to the next article.
8627           (set-buffer sum)
8628           (setq found 'not)
8629           (while (eq found 'not)
8630             (if (not (if backward (gnus-summary-find-prev)
8631                        (gnus-summary-find-next)))
8632                 ;; No more articles.
8633                 (setq found t)
8634               ;; Select the next article and adjust point.
8635               (unless (gnus-summary-article-sparse-p
8636                        (gnus-summary-article-number))
8637                 (setq found nil)
8638                 (gnus-summary-select-article)
8639                 (set-buffer gnus-article-buffer)
8640                 (widen)
8641                 (goto-char (if backward (point-max) (point-min))))))))
8642       (gnus-message 7 ""))
8643     ;; Return whether we found the regexp.
8644     (when (eq found 'found)
8645       (goto-char point)
8646       (gnus-summary-show-thread)
8647       (gnus-summary-goto-subject gnus-current-article)
8648       (gnus-summary-position-point)
8649       t)))
8650
8651 (defun gnus-find-matching-articles (header regexp)
8652   "Return a list of all articles that match REGEXP on HEADER.
8653 This search includes all articles in the current group that Gnus has
8654 fetched headers for, whether they are displayed or not."
8655   (let ((articles nil)
8656         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8657         (case-fold-search t))
8658     (dolist (header gnus-newsgroup-headers)
8659       (when (string-match regexp (funcall func header))
8660         (push (mail-header-number header) articles)))
8661     (nreverse articles)))
8662
8663 (defun gnus-summary-find-matching (header regexp &optional backward unread
8664                                           not-case-fold not-matching)
8665   "Return a list of all articles that match REGEXP on HEADER.
8666 The search stars on the current article and goes forwards unless
8667 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8668 If UNREAD is non-nil, only unread articles will
8669 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8670 in the comparisons. If NOT-MATCHING, return a list of all articles that
8671 not match REGEXP on HEADER."
8672   (let ((case-fold-search (not not-case-fold))
8673         articles d func)
8674     (if (consp header)
8675         (if (eq (car header) 'extra)
8676             (setq func
8677                   `(lambda (h)
8678                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8679                          "")))
8680           (error "%s is an invalid header" header))
8681       (unless (fboundp (intern (concat "mail-header-" header)))
8682         (error "%s is not a valid header" header))
8683       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8684     (dolist (d (if (eq backward 'all)
8685                    gnus-newsgroup-data
8686                  (gnus-data-find-list
8687                   (gnus-summary-article-number)
8688                   (gnus-data-list backward))))
8689       (when (and (or (not unread)       ; We want all articles...
8690                      (gnus-data-unread-p d)) ; Or just unreads.
8691                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8692                  (if not-matching
8693                      (not (string-match
8694                            regexp
8695                            (funcall func (gnus-data-header d))))
8696                    (string-match regexp
8697                                  (funcall func (gnus-data-header d)))))
8698         (push (gnus-data-number d) articles))) ; Success!
8699     (nreverse articles)))
8700
8701 (defun gnus-summary-execute-command (header regexp command &optional backward)
8702   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8703 If HEADER is an empty string (or nil), the match is done on the entire
8704 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8705   (interactive
8706    (list (let ((completion-ignore-case t))
8707            (completing-read
8708             "Header name: "
8709             (mapcar (lambda (header) (list (format "%s" header)))
8710                     (append
8711                      '("Number" "Subject" "From" "Lines" "Date"
8712                        "Message-ID" "Xref" "References" "Body")
8713                      gnus-extra-headers))
8714             nil 'require-match))
8715          (read-string "Regexp: ")
8716          (read-key-sequence "Command: ")
8717          current-prefix-arg))
8718   (when (equal header "Body")
8719     (setq header ""))
8720   ;; Hidden thread subtrees must be searched as well.
8721   (gnus-summary-show-all-threads)
8722   ;; We don't want to change current point nor window configuration.
8723   (save-excursion
8724     (save-window-excursion
8725       (let (gnus-visual
8726             gnus-treat-strip-trailing-blank-lines
8727             gnus-treat-strip-leading-blank-lines
8728             gnus-treat-strip-multiple-blank-lines
8729             gnus-treat-hide-boring-headers
8730             gnus-treat-fold-newsgroups
8731             gnus-article-prepare-hook)
8732         (gnus-message 6 "Executing %s..." (key-description command))
8733         ;; We'd like to execute COMMAND interactively so as to give arguments.
8734         (gnus-execute header regexp
8735                       `(call-interactively ',(key-binding command))
8736                       backward)
8737         (gnus-message 6 "Executing %s...done" (key-description command))))))
8738
8739 (defun gnus-summary-beginning-of-article ()
8740   "Scroll the article back to the beginning."
8741   (interactive)
8742   (gnus-summary-select-article)
8743   (gnus-configure-windows 'article)
8744   (gnus-eval-in-buffer-window gnus-article-buffer
8745     (widen)
8746     (goto-char (point-min))
8747     (when gnus-break-pages
8748       (gnus-narrow-to-page))))
8749
8750 (defun gnus-summary-end-of-article ()
8751   "Scroll to the end of the article."
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-max))
8758     (recenter -3)
8759     (when gnus-break-pages
8760       (when (re-search-backward page-delimiter nil t)
8761         (narrow-to-region (match-end 0) (point-max)))
8762       (gnus-narrow-to-page))))
8763
8764 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8765   "Truncate to LEN and quote all \"(\"'s in STRING."
8766   (gnus-replace-in-string (if (and len (> (length string) len))
8767                               (substring string 0 len)
8768                             string)
8769                           "[()]" "\\\\\\&"))
8770
8771 (defun gnus-summary-print-article (&optional filename n)
8772   "Generate and print a PostScript image of the process-marked (mail) articles.
8773
8774 If used interactively, print the current article if none are
8775 process-marked.  With prefix arg, prompt the user for the name of the
8776 file to save in.
8777
8778 When used from Lisp, accept two optional args FILENAME and N.  N means
8779 to print the next N articles.  If N is negative, print the N previous
8780 articles.  If N is nil and articles have been marked with the process
8781 mark, print these instead.
8782
8783 If the optional first argument FILENAME is nil, send the image to the
8784 printer.  If FILENAME is a string, save the PostScript image in a file with
8785 that name.  If FILENAME is a number, prompt the user for the name of the file
8786 to save in."
8787   (interactive (list (ps-print-preprint current-prefix-arg)))
8788   (dolist (article (gnus-summary-work-articles n))
8789     (gnus-summary-select-article nil nil 'pseudo article)
8790     (gnus-eval-in-buffer-window gnus-article-buffer
8791       (gnus-print-buffer))
8792     (gnus-summary-remove-process-mark article))
8793   (ps-despool filename))
8794
8795 (defun gnus-print-buffer ()
8796   (let ((buffer (generate-new-buffer " *print*")))
8797     (unwind-protect
8798         (progn
8799           (copy-to-buffer buffer (point-min) (point-max))
8800           (set-buffer buffer)
8801           (gnus-remove-text-with-property 'gnus-decoration)
8802           (when (gnus-visual-p 'article-highlight 'highlight)
8803             ;; Copy-to-buffer doesn't copy overlay.  So redo
8804             ;; highlight.
8805             (let ((gnus-article-buffer buffer))
8806               (gnus-article-highlight-citation t)
8807               (gnus-article-highlight-signature)
8808               (gnus-article-emphasize)
8809               (gnus-article-delete-invisible-text)))
8810           (let ((ps-left-header
8811                  (list
8812                   (concat "("
8813                           (gnus-summary-print-truncate-and-quote
8814                            (mail-header-subject gnus-current-headers)
8815                            66) ")")
8816                   (concat "("
8817                           (gnus-summary-print-truncate-and-quote
8818                            (mail-header-from gnus-current-headers)
8819                            45) ")")))
8820                 (ps-right-header
8821                  (list
8822                   "/pagenumberstring load"
8823                   (concat "("
8824                           (mail-header-date gnus-current-headers) ")"))))
8825             (gnus-run-hooks 'gnus-ps-print-hook)
8826             (save-excursion
8827               (if window-system
8828                   (ps-spool-buffer-with-faces)
8829                 (ps-spool-buffer)))))
8830       (kill-buffer buffer))))
8831
8832 (defun gnus-summary-show-article (&optional arg)
8833   "Force redisplaying of the current article.
8834 If ARG (the prefix) is a number, show the article with the charset
8835 defined in `gnus-summary-show-article-charset-alist', or the charset
8836 input.
8837 If ARG (the prefix) is non-nil and not a number, show the raw article
8838 without any article massaging functions being run.  Normally, the key
8839 strokes are `C-u g'."
8840   (interactive "P")
8841   (cond
8842    ((numberp arg)
8843     (gnus-summary-show-article t)
8844     (let ((gnus-newsgroup-charset
8845            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8846                (mm-read-coding-system
8847                 "View as charset: " ;; actually it is coding system.
8848                 (with-current-buffer gnus-article-buffer
8849                   (mm-detect-coding-region (point) (point-max))))))
8850           (gnus-newsgroup-ignored-charsets 'gnus-all))
8851       (gnus-summary-select-article nil 'force)
8852       (let ((deps gnus-newsgroup-dependencies)
8853             head header lines)
8854         (save-excursion
8855           (set-buffer gnus-original-article-buffer)
8856           (save-restriction
8857             (message-narrow-to-head)
8858             (setq head (buffer-string))
8859             (goto-char (point-min))
8860             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8861               (goto-char (point-max))
8862               (widen)
8863               (setq lines (1- (count-lines (point) (point-max))))))
8864           (with-temp-buffer
8865             (insert (format "211 %d Article retrieved.\n"
8866                             (cdr gnus-article-current)))
8867             (insert head)
8868             (if lines (insert (format "Lines: %d\n" lines)))
8869             (insert ".\n")
8870             (let ((nntp-server-buffer (current-buffer)))
8871               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8872         (gnus-data-set-header
8873          (gnus-data-find (cdr gnus-article-current))
8874          header)
8875         (gnus-summary-update-article-line
8876          (cdr gnus-article-current) header)
8877         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8878           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8879    ((not arg)
8880     ;; Select the article the normal way.
8881     (gnus-summary-select-article nil 'force))
8882    (t
8883     ;; We have to require this here to make sure that the following
8884     ;; dynamic binding isn't shadowed by autoloading.
8885     (require 'gnus-async)
8886     (require 'gnus-art)
8887     ;; Bind the article treatment functions to nil.
8888     (let ((gnus-have-all-headers t)
8889           gnus-article-prepare-hook
8890           gnus-article-decode-hook
8891           gnus-display-mime-function
8892           gnus-break-pages)
8893       ;; Destroy any MIME parts.
8894       (when (gnus-buffer-live-p gnus-article-buffer)
8895         (save-excursion
8896           (set-buffer gnus-article-buffer)
8897           (mm-destroy-parts gnus-article-mime-handles)
8898           ;; Set it to nil for safety reason.
8899           (setq gnus-article-mime-handle-alist nil)
8900           (setq gnus-article-mime-handles nil)))
8901       (gnus-summary-select-article nil 'force))))
8902   (gnus-summary-goto-subject gnus-current-article)
8903   (gnus-summary-position-point))
8904
8905 (defun gnus-summary-show-raw-article ()
8906   "Show the raw article without any article massaging functions being run."
8907   (interactive)
8908   (gnus-summary-show-article t))
8909
8910 (defun gnus-summary-verbose-headers (&optional arg)
8911   "Toggle permanent full header display.
8912 If ARG is a positive number, turn header display on.
8913 If ARG is a negative number, turn header display off."
8914   (interactive "P")
8915   (setq gnus-show-all-headers
8916         (cond ((or (not (numberp arg))
8917                    (zerop arg))
8918                (not gnus-show-all-headers))
8919               ((natnump arg)
8920                t)))
8921   (gnus-summary-show-article))
8922
8923 (defun gnus-summary-toggle-header (&optional arg)
8924   "Show the headers if they are hidden, or hide them if they are shown.
8925 If ARG is a positive number, show the entire header.
8926 If ARG is a negative number, hide the unwanted header lines."
8927   (interactive "P")
8928   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8929                      (get-buffer-window gnus-article-buffer t))))
8930     (with-current-buffer gnus-article-buffer
8931       (widen)
8932       (article-narrow-to-head)
8933       (let* ((buffer-read-only nil)
8934              (inhibit-point-motion-hooks t)
8935              (hidden (if (numberp arg)
8936                          (>= arg 0)
8937                        (or (not (looking-at "[^ \t\n]+:"))
8938                            (gnus-article-hidden-text-p 'headers))))
8939              s e)
8940         (delete-region (point-min) (point-max))
8941         (with-current-buffer gnus-original-article-buffer
8942           (goto-char (setq s (point-min)))
8943           (setq e (if (search-forward "\n\n" nil t)
8944                       (1- (point))
8945                     (point-max))))
8946         (insert-buffer-substring gnus-original-article-buffer s e)
8947         (run-hooks 'gnus-article-decode-hook)
8948         (if hidden
8949             (let ((gnus-treat-hide-headers nil)
8950                   (gnus-treat-hide-boring-headers nil))
8951               (gnus-delete-wash-type 'headers)
8952               (gnus-treat-article 'head))
8953           (gnus-treat-article 'head))
8954         (widen)
8955         (if window
8956             (set-window-start window (goto-char (point-min))))
8957         (if gnus-break-pages
8958             (gnus-narrow-to-page)
8959           (when (gnus-visual-p 'page-marker)
8960             (let ((buffer-read-only nil))
8961               (gnus-remove-text-with-property 'gnus-prev)
8962               (gnus-remove-text-with-property 'gnus-next))))
8963         (gnus-set-mode-line 'article)))))
8964
8965 (defun gnus-summary-show-all-headers ()
8966   "Make all header lines visible."
8967   (interactive)
8968   (gnus-summary-toggle-header 1))
8969
8970 (defun gnus-summary-caesar-message (&optional arg)
8971   "Caesar rotate the current article by 13.
8972 The numerical prefix specifies how many places to rotate each letter
8973 forward."
8974   (interactive "P")
8975   (gnus-summary-select-article)
8976   (let ((mail-header-separator ""))
8977     (gnus-eval-in-buffer-window gnus-article-buffer
8978       (save-restriction
8979         (widen)
8980         (let ((start (window-start))
8981               buffer-read-only)
8982           (message-caesar-buffer-body arg)
8983           (set-window-start (get-buffer-window (current-buffer)) start)))))
8984   ;; Create buttons and stuff...
8985   (gnus-treat-article nil))
8986
8987 (defun gnus-summary-idna-message (&optional arg)
8988   "Decode IDNA encoded domain names in the current articles.
8989 IDNA encoded domain names looks like `xn--bar'.  If a string
8990 remain unencoded after running this function, it is likely an
8991 invalid IDNA string (`xn--bar' is invalid).
8992
8993 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
8994 installed for this command to work."
8995   (interactive "P")
8996   (if (not (and (condition-case nil (require 'idna)
8997                   (file-error))
8998                 (mm-coding-system-p 'utf-8)
8999                 (executable-find (symbol-value 'idna-program))))
9000       (gnus-message
9001        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9002     (gnus-summary-select-article)
9003     (let ((mail-header-separator ""))
9004       (gnus-eval-in-buffer-window gnus-article-buffer
9005         (save-restriction
9006           (widen)
9007           (let ((start (window-start))
9008                 buffer-read-only)
9009             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9010               (replace-match (idna-to-unicode (match-string 1))))
9011             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9012
9013 (autoload 'unmorse-region "morse"
9014   "Convert morse coded text in region to ordinary ASCII text."
9015   t)
9016
9017 (defun gnus-summary-morse-message (&optional arg)
9018   "Morse decode the current article."
9019   (interactive "P")
9020   (gnus-summary-select-article)
9021   (let ((mail-header-separator ""))
9022     (gnus-eval-in-buffer-window gnus-article-buffer
9023       (save-excursion
9024         (save-restriction
9025           (widen)
9026           (let ((pos (window-start))
9027                 buffer-read-only)
9028             (goto-char (point-min))
9029             (when (message-goto-body)
9030               (gnus-narrow-to-body))
9031             (goto-char (point-min))
9032             (while (search-forward "·" (point-max) t)
9033               (replace-match "."))
9034             (unmorse-region (point-min) (point-max))
9035             (widen)
9036             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9037
9038 (defun gnus-summary-stop-page-breaking ()
9039   "Stop page breaking in the current article."
9040   (interactive)
9041   (gnus-summary-select-article)
9042   (gnus-eval-in-buffer-window gnus-article-buffer
9043     (widen)
9044     (when (gnus-visual-p 'page-marker)
9045       (let ((buffer-read-only nil))
9046         (gnus-remove-text-with-property 'gnus-prev)
9047         (gnus-remove-text-with-property 'gnus-next))
9048       (setq gnus-page-broken nil))))
9049
9050 (defun gnus-summary-move-article (&optional n to-newsgroup
9051                                             select-method action)
9052   "Move the current article to a different newsgroup.
9053 If N is a positive number, move the N next articles.
9054 If N is a negative number, move the N previous articles.
9055 If N is nil and any articles have been marked with the process mark,
9056 move those articles instead.
9057 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9058 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9059 re-spool using this method.
9060
9061 When called interactively with TO-NEWSGROUP being nil, the value of
9062 the variable `gnus-move-split-methods' is used for finding a default
9063 for the target newsgroup.
9064
9065 For this function to work, both the current newsgroup and the
9066 newsgroup that you want to move to have to support the `request-move'
9067 and `request-accept' functions.
9068
9069 ACTION can be either `move' (the default), `crosspost' or `copy'."
9070   (interactive "P")
9071   (unless action
9072     (setq action 'move))
9073   ;; Check whether the source group supports the required functions.
9074   (cond ((and (eq action 'move)
9075               (not (gnus-check-backend-function
9076                     'request-move-article gnus-newsgroup-name)))
9077          (error "The current group does not support article moving"))
9078         ((and (eq action 'crosspost)
9079               (not (gnus-check-backend-function
9080                     'request-replace-article gnus-newsgroup-name)))
9081          (error "The current group does not support article editing")))
9082   (let ((articles (gnus-summary-work-articles n))
9083         (prefix (if (gnus-check-backend-function
9084                      'request-move-article gnus-newsgroup-name)
9085                     (funcall gnus-move-group-prefix-function
9086                              gnus-newsgroup-name)
9087                   ""))
9088         (names '((move "Move" "Moving")
9089                  (copy "Copy" "Copying")
9090                  (crosspost "Crosspost" "Crossposting")))
9091         (copy-buf (save-excursion
9092                     (nnheader-set-temp-buffer " *copy article*")))
9093         art-group to-method new-xref article to-groups articles-to-update-marks)
9094     (unless (assq action names)
9095       (error "Unknown action %s" action))
9096     ;; Read the newsgroup name.
9097     (when (and (not to-newsgroup)
9098                (not select-method))
9099       (if (and gnus-move-split-methods
9100                (not
9101                 (and (memq gnus-current-article articles)
9102                      (gnus-buffer-live-p gnus-original-article-buffer))))
9103           ;; When `gnus-move-split-methods' is non-nil, we have to
9104           ;; select an article to give `gnus-read-move-group-name' an
9105           ;; opportunity to suggest an appropriate default.  However,
9106           ;; we needn't render or mark the article.
9107           (let ((gnus-display-mime-function nil)
9108                 (gnus-article-prepare-hook nil)
9109                 (gnus-mark-article-hook nil))
9110             (gnus-summary-select-article nil nil nil (car articles))))
9111       (setq to-newsgroup
9112             (gnus-read-move-group-name
9113              (cadr (assq action names))
9114              (symbol-value (intern (format "gnus-current-%s-group" action)))
9115              articles prefix))
9116       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9117     (setq to-method (or select-method
9118                         (gnus-server-to-method
9119                          (gnus-group-method to-newsgroup))))
9120     ;; Check the method we are to move this article to...
9121     (unless (gnus-check-backend-function
9122              'request-accept-article (car to-method))
9123       (error "%s does not support article copying" (car to-method)))
9124     (unless (gnus-check-server to-method)
9125       (error "Can't open server %s" (car to-method)))
9126     (gnus-message 6 "%s to %s: %s..."
9127                   (caddr (assq action names))
9128                   (or (car select-method) to-newsgroup) articles)
9129     (while articles
9130       (setq article (pop articles))
9131       (setq
9132        art-group
9133        (cond
9134         ;; Move the article.
9135         ((eq action 'move)
9136          ;; Remove this article from future suppression.
9137          (gnus-dup-unsuppress-article article)
9138          (let* ((from-method (gnus-find-method-for-group
9139                               gnus-newsgroup-name))
9140                 (to-method (gnus-find-method-for-group
9141                             to-newsgroup))
9142                 (move-is-internal (gnus-method-equal from-method to-method)))
9143          (gnus-request-move-article
9144           article                       ; Article to move
9145           gnus-newsgroup-name           ; From newsgroup
9146           (nth 1 (gnus-find-method-for-group
9147                   gnus-newsgroup-name)) ; Server
9148           (list 'gnus-request-accept-article
9149                 to-newsgroup (list 'quote select-method)
9150                 (not articles) t)       ; Accept form
9151           (not articles)                ; Only save nov last time
9152           move-is-internal)))           ; is this move internal?
9153         ;; Copy the article.
9154         ((eq action 'copy)
9155          (save-excursion
9156            (set-buffer copy-buf)
9157            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9158              (save-restriction
9159                (nnheader-narrow-to-headers)
9160                (dolist (hdr gnus-copy-article-ignored-headers)
9161                  (message-remove-header hdr t)))
9162              (gnus-request-accept-article
9163               to-newsgroup select-method (not articles) t))))
9164         ;; Crosspost the article.
9165         ((eq action 'crosspost)
9166          (let ((xref (message-tokenize-header
9167                       (mail-header-xref (gnus-summary-article-header article))
9168                       " ")))
9169            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9170                                   ":" (number-to-string article)))
9171            (unless xref
9172              (setq xref (list (system-name))))
9173            (setq new-xref
9174                  (concat
9175                   (mapconcat 'identity
9176                              (delete "Xref:" (delete new-xref xref))
9177                              " ")
9178                   " " new-xref))
9179            (save-excursion
9180              (set-buffer copy-buf)
9181              ;; First put the article in the destination group.
9182              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9183              (when (consp (setq art-group
9184                                 (gnus-request-accept-article
9185                                  to-newsgroup select-method (not articles))))
9186                (setq new-xref (concat new-xref " " (car art-group)
9187                                       ":"
9188                                       (number-to-string (cdr art-group))))
9189                ;; Now we have the new Xrefs header, so we insert
9190                ;; it and replace the new article.
9191                (nnheader-replace-header "Xref" new-xref)
9192                (gnus-request-replace-article
9193                 (cdr art-group) to-newsgroup (current-buffer))
9194                art-group))))))
9195       (cond
9196        ((not art-group)
9197         (gnus-message 1 "Couldn't %s article %s: %s"
9198                       (cadr (assq action names)) article
9199                       (nnheader-get-report (car to-method))))
9200        ((eq art-group 'junk)
9201         (when (eq action 'move)
9202           (gnus-summary-mark-article article gnus-canceled-mark)
9203           (gnus-message 4 "Deleted article %s" article)
9204           ;; run the delete hook
9205           (run-hook-with-args 'gnus-summary-article-delete-hook
9206                               action
9207                               (gnus-data-header
9208                                (assoc article (gnus-data-list nil)))
9209                               gnus-newsgroup-name nil
9210                               select-method)))
9211        (t
9212         (let* ((pto-group (gnus-group-prefixed-name
9213                            (car art-group) to-method))
9214                (info (gnus-get-info pto-group))
9215                (to-group (gnus-info-group info))
9216                to-marks)
9217           ;; Update the group that has been moved to.
9218           (when (and info
9219                      (memq action '(move copy)))
9220             (unless (member to-group to-groups)
9221               (push to-group to-groups))
9222
9223             (unless (memq article gnus-newsgroup-unreads)
9224               (push 'read to-marks)
9225               (gnus-info-set-read
9226                info (gnus-add-to-range (gnus-info-read info)
9227                                        (list (cdr art-group)))))
9228
9229             ;; See whether the article is to be put in the cache.
9230             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9231                              gnus-article-mark-lists
9232                            (delete '(expirable . expire)
9233                                    (copy-sequence gnus-article-mark-lists))))
9234                   (to-article (cdr art-group)))
9235
9236               ;; Enter the article into the cache in the new group,
9237               ;; if that is required.
9238               (when gnus-use-cache
9239                 (gnus-cache-possibly-enter-article
9240                  to-group to-article
9241                  (memq article gnus-newsgroup-marked)
9242                  (memq article gnus-newsgroup-dormant)
9243                  (memq article gnus-newsgroup-unreads)))
9244
9245               (when gnus-preserve-marks
9246                 ;; Copy any marks over to the new group.
9247                 (when (and (equal to-group gnus-newsgroup-name)
9248                            (not (memq article gnus-newsgroup-unreads)))
9249                   ;; Mark this article as read in this group.
9250                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9251                   (setcdr (gnus-active to-group) to-article)
9252                   (setcdr gnus-newsgroup-active to-article))
9253
9254                 (while marks
9255                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9256                     (when (memq article (symbol-value
9257                                          (intern (format "gnus-newsgroup-%s"
9258                                                          (caar marks)))))
9259                       (push (cdar marks) to-marks)
9260                       ;; If the other group is the same as this group,
9261                       ;; then we have to add the mark to the list.
9262                       (when (equal to-group gnus-newsgroup-name)
9263                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9264                              (cons to-article
9265                                    (symbol-value
9266                                     (intern (format "gnus-newsgroup-%s"
9267                                                     (caar marks)))))))
9268                       ;; Copy the marks to other group.
9269                       (gnus-add-marked-articles
9270                        to-group (cdar marks) (list to-article) info)))
9271                   (setq marks (cdr marks)))
9272
9273                 (gnus-request-set-mark
9274                  to-group (list (list (list to-article) 'add to-marks))))
9275
9276               (gnus-dribble-enter
9277                (concat "(gnus-group-set-info '"
9278                        (gnus-prin1-to-string (gnus-get-info to-group))
9279                        ")"))))
9280
9281           ;; Update the Xref header in this article to point to
9282           ;; the new crossposted article we have just created.
9283           (when (eq action 'crosspost)
9284             (save-excursion
9285               (set-buffer copy-buf)
9286               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9287               (nnheader-replace-header "Xref" new-xref)
9288               (gnus-request-replace-article
9289                article gnus-newsgroup-name (current-buffer))))
9290
9291           ;; run the move/copy/crosspost/respool hook
9292           (run-hook-with-args 'gnus-summary-article-move-hook
9293                               action
9294                               (gnus-data-header
9295                                (assoc article (gnus-data-list nil)))
9296                               gnus-newsgroup-name
9297                               to-newsgroup
9298                               select-method))
9299
9300         ;;;!!!Why is this necessary?
9301         (set-buffer gnus-summary-buffer)
9302         
9303         (gnus-summary-goto-subject article)
9304         (when (eq action 'move)
9305           (gnus-summary-mark-article article gnus-canceled-mark))))
9306       (push article articles-to-update-marks))
9307
9308     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9309     ;; Re-activate all groups that have been moved to.
9310     (save-excursion
9311       (set-buffer gnus-group-buffer)
9312       (let ((gnus-group-marked to-groups))
9313         (gnus-group-get-new-news-this-group nil t)))
9314     
9315     (gnus-kill-buffer copy-buf)
9316     (gnus-summary-position-point)
9317     (gnus-set-mode-line 'summary)))
9318
9319 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9320   "Copy the current article to some other group.
9321 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9322 When called interactively, if TO-NEWSGROUP is nil, use the value of
9323 the variable `gnus-move-split-methods' for finding a default target
9324 newsgroup.
9325 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9326 re-spool using this method."
9327   (interactive "P")
9328   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9329
9330 (defun gnus-summary-crosspost-article (&optional n)
9331   "Crosspost the current article to some other group."
9332   (interactive "P")
9333   (gnus-summary-move-article n nil nil 'crosspost))
9334
9335 (defcustom gnus-summary-respool-default-method nil
9336   "Default method type for respooling an article.
9337 If nil, use to the current newsgroup method."
9338   :type 'symbol
9339   :group 'gnus-summary-mail)
9340
9341 (defcustom gnus-summary-display-while-building nil
9342   "If non-nil, show and update the summary buffer as it's being built.
9343 If the value is t, update the buffer after every line is inserted.  If
9344 the value is an integer (N), update the display every N lines."
9345   :version "22.1"
9346   :group 'gnus-thread
9347   :type '(choice (const :tag "off" nil)
9348                  number
9349                  (const :tag "frequently" t)))
9350
9351 (defun gnus-summary-respool-article (&optional n method)
9352   "Respool the current article.
9353 The article will be squeezed through the mail spooling process again,
9354 which means that it will be put in some mail newsgroup or other
9355 depending on `nnmail-split-methods'.
9356 If N is a positive number, respool the N next articles.
9357 If N is a negative number, respool the N previous articles.
9358 If N is nil and any articles have been marked with the process mark,
9359 respool those articles instead.
9360
9361 Respooling can be done both from mail groups and \"real\" newsgroups.
9362 In the former case, the articles in question will be moved from the
9363 current group into whatever groups they are destined to.  In the
9364 latter case, they will be copied into the relevant groups."
9365   (interactive
9366    (list current-prefix-arg
9367          (let* ((methods (gnus-methods-using 'respool))
9368                 (methname
9369                  (symbol-name (or gnus-summary-respool-default-method
9370                                   (car (gnus-find-method-for-group
9371                                         gnus-newsgroup-name)))))
9372                 (method
9373                  (gnus-completing-read-with-default
9374                   methname "What backend do you want to use when respooling?"
9375                   methods nil t nil 'gnus-mail-method-history))
9376                 ms)
9377            (cond
9378             ((zerop (length (setq ms (gnus-servers-using-backend
9379                                       (intern method)))))
9380              (list (intern method) ""))
9381             ((= 1 (length ms))
9382              (car ms))
9383             (t
9384              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9385                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9386                            ms-alist))))))))
9387   (unless method
9388     (error "No method given for respooling"))
9389   (if (assoc (symbol-name
9390               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9391              (gnus-methods-using 'respool))
9392       (gnus-summary-move-article n nil method)
9393     (gnus-summary-copy-article n nil method)))
9394
9395 (defun gnus-summary-import-article (file &optional edit)
9396   "Import an arbitrary file into a mail newsgroup."
9397   (interactive "fImport file: \nP")
9398   (let ((group gnus-newsgroup-name)
9399         (now (current-time))
9400         atts lines group-art)
9401     (unless (gnus-check-backend-function 'request-accept-article group)
9402       (error "%s does not support article importing" group))
9403     (or (file-readable-p file)
9404         (not (file-regular-p file))
9405         (error "Can't read %s" file))
9406     (save-excursion
9407       (set-buffer (gnus-get-buffer-create " *import file*"))
9408       (erase-buffer)
9409       (nnheader-insert-file-contents file)
9410       (goto-char (point-min))
9411       (if (nnheader-article-p)
9412           (save-restriction
9413             (goto-char (point-min))
9414             (search-forward "\n\n" nil t)
9415             (narrow-to-region (point-min) (1- (point)))
9416             (goto-char (point-min))
9417             (unless (re-search-forward "^date:" nil t)
9418               (goto-char (point-max))
9419               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9420        ;; This doesn't look like an article, so we fudge some headers.
9421         (setq atts (file-attributes file)
9422               lines (count-lines (point-min) (point-max)))
9423         (insert "From: " (read-string "From: ") "\n"
9424                 "Subject: " (read-string "Subject: ") "\n"
9425                 "Date: " (message-make-date (nth 5 atts)) "\n"
9426                 "Message-ID: " (message-make-message-id) "\n"
9427                 "Lines: " (int-to-string lines) "\n"
9428                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9429       (setq group-art (gnus-request-accept-article group nil t))
9430       (kill-buffer (current-buffer)))
9431     (setq gnus-newsgroup-active (gnus-activate-group group))
9432     (forward-line 1)
9433     (gnus-summary-goto-article (cdr group-art) nil t)
9434     (when edit
9435       (gnus-summary-edit-article))))
9436
9437 (defun gnus-summary-create-article ()
9438   "Create an article in a mail newsgroup."
9439   (interactive)
9440   (let ((group gnus-newsgroup-name)
9441         (now (current-time))
9442         group-art)
9443     (unless (gnus-check-backend-function 'request-accept-article group)
9444       (error "%s does not support article importing" group))
9445     (save-excursion
9446       (set-buffer (gnus-get-buffer-create " *import file*"))
9447       (erase-buffer)
9448       (goto-char (point-min))
9449       ;; This doesn't look like an article, so we fudge some headers.
9450       (insert "From: " (read-string "From: ") "\n"
9451               "Subject: " (read-string "Subject: ") "\n"
9452               "Date: " (message-make-date now) "\n"
9453               "Message-ID: " (message-make-message-id) "\n")
9454       (setq group-art (gnus-request-accept-article group nil t))
9455       (kill-buffer (current-buffer)))
9456     (setq gnus-newsgroup-active (gnus-activate-group group))
9457     (forward-line 1)
9458     (gnus-summary-goto-article (cdr group-art) nil t)
9459     (gnus-summary-edit-article)))
9460
9461 (defun gnus-summary-article-posted-p ()
9462   "Say whether the current (mail) article is available from news as well.
9463 This will be the case if the article has both been mailed and posted."
9464   (interactive)
9465   (let ((id (mail-header-references (gnus-summary-article-header)))
9466         (gnus-override-method (car (gnus-refer-article-methods))))
9467     (if (gnus-request-head id "")
9468         (gnus-message 2 "The current message was found on %s"
9469                       gnus-override-method)
9470       (gnus-message 2 "The current message couldn't be found on %s"
9471                     gnus-override-method)
9472       nil)))
9473
9474 (defun gnus-summary-expire-articles (&optional now)
9475   "Expire all articles that are marked as expirable in the current group."
9476   (interactive)
9477   (when (and (not gnus-group-is-exiting-without-update-p)
9478              (gnus-check-backend-function
9479               'request-expire-articles gnus-newsgroup-name))
9480     ;; This backend supports expiry.
9481     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9482            (expirable (if total
9483                           (progn
9484                             ;; We need to update the info for
9485                             ;; this group for `gnus-list-of-read-articles'
9486                             ;; to give us the right answer.
9487                             (gnus-run-hooks 'gnus-exit-group-hook)
9488                             (gnus-summary-update-info)
9489                             (gnus-list-of-read-articles gnus-newsgroup-name))
9490                         (setq gnus-newsgroup-expirable
9491                               (sort gnus-newsgroup-expirable '<))))
9492            (expiry-wait (if now 'immediate
9493                           (gnus-group-find-parameter
9494                            gnus-newsgroup-name 'expiry-wait)))
9495            (nnmail-expiry-target
9496             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9497                 nnmail-expiry-target))
9498            es)
9499       (when expirable
9500         ;; There are expirable articles in this group, so we run them
9501         ;; through the expiry process.
9502         (gnus-message 6 "Expiring articles...")
9503         (unless (gnus-check-group gnus-newsgroup-name)
9504           (error "Can't open server for %s" gnus-newsgroup-name))
9505         ;; The list of articles that weren't expired is returned.
9506         (save-excursion
9507           (if expiry-wait
9508               (let ((nnmail-expiry-wait-function nil)
9509                     (nnmail-expiry-wait expiry-wait))
9510                 (setq es (gnus-request-expire-articles
9511                           expirable gnus-newsgroup-name)))
9512             (setq es (gnus-request-expire-articles
9513                       expirable gnus-newsgroup-name)))
9514           (unless total
9515             (setq gnus-newsgroup-expirable es))
9516           ;; We go through the old list of expirable, and mark all
9517           ;; really expired articles as nonexistent.
9518           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9519             (let ((gnus-use-cache nil))
9520               (dolist (article expirable)
9521                 (when (and (not (memq article es))
9522                            (gnus-data-find article))
9523                   (gnus-summary-mark-article article gnus-canceled-mark)
9524                   (run-hook-with-args 'gnus-summary-article-expire-hook
9525                                       'delete
9526                                       (gnus-data-header
9527                                        (assoc article (gnus-data-list nil)))
9528                                       gnus-newsgroup-name
9529                                       nil
9530                                       nil))))))
9531         (gnus-message 6 "Expiring articles...done")))))
9532
9533 (defun gnus-summary-expire-articles-now ()
9534   "Expunge all expirable articles in the current group.
9535 This means that *all* articles that are marked as expirable will be
9536 deleted forever, right now."
9537   (interactive)
9538   (or gnus-expert-user
9539       (gnus-yes-or-no-p
9540        "Are you really, really, really sure you want to delete all these messages? ")
9541       (error "Phew!"))
9542   (gnus-summary-expire-articles t))
9543
9544 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9545 (defun gnus-summary-delete-article (&optional n)
9546   "Delete the N next (mail) articles.
9547 This command actually deletes articles.  This is not a marking
9548 command.  The article will disappear forever from your life, never to
9549 return.
9550
9551 If N is negative, delete backwards.
9552 If N is nil and articles have been marked with the process mark,
9553 delete these instead.
9554
9555 If `gnus-novice-user' is non-nil you will be asked for
9556 confirmation before the articles are deleted."
9557   (interactive "P")
9558   (unless (gnus-check-backend-function 'request-expire-articles
9559                                        gnus-newsgroup-name)
9560     (error "The current newsgroup does not support article deletion"))
9561   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9562     (error "Couldn't open server"))
9563   ;; Compute the list of articles to delete.
9564   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9565         (nnmail-expiry-target 'delete)
9566         not-deleted)
9567     (if (and gnus-novice-user
9568              (not (gnus-yes-or-no-p
9569                    (format "Do you really want to delete %s forever? "
9570                            (if (> (length articles) 1)
9571                                (format "these %s articles" (length articles))
9572                              "this article")))))
9573         ()
9574       ;; Delete the articles.
9575       (setq not-deleted (gnus-request-expire-articles
9576                          articles gnus-newsgroup-name 'force))
9577       (while articles
9578         (gnus-summary-remove-process-mark (car articles))
9579         ;; The backend might not have been able to delete the article
9580         ;; after all.
9581         (unless (memq (car articles) not-deleted)
9582           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9583         (let* ((article (car articles))
9584                (ghead  (gnus-data-header
9585                                     (assoc article (gnus-data-list nil)))))
9586           (run-hook-with-args 'gnus-summary-article-delete-hook
9587                               'delete ghead gnus-newsgroup-name nil
9588                               nil))
9589         (setq articles (cdr articles)))
9590       (when not-deleted
9591         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9592     (gnus-summary-position-point)
9593     (gnus-set-mode-line 'summary)
9594     not-deleted))
9595
9596 (defun gnus-summary-edit-article (&optional arg)
9597   "Edit the current article.
9598 This will have permanent effect only in mail groups.
9599 If ARG is nil, edit the decoded articles.
9600 If ARG is 1, edit the raw articles.
9601 If ARG is 2, edit the raw articles even in read-only groups.
9602 If ARG is 3, edit the articles with the current handles.
9603 Otherwise, allow editing of articles even in read-only
9604 groups."
9605   (interactive "P")
9606   (let (force raw current-handles)
9607     (cond
9608      ((null arg))
9609      ((eq arg 1)
9610       (setq raw t))
9611      ((eq arg 2)
9612       (setq raw t
9613             force t))
9614      ((eq arg 3)
9615       (setq current-handles
9616             (and (gnus-buffer-live-p gnus-article-buffer)
9617                  (with-current-buffer gnus-article-buffer
9618                    (prog1
9619                        gnus-article-mime-handles
9620                      (setq gnus-article-mime-handles nil))))))
9621      (t
9622       (setq force t)))
9623     (when (and raw (not force)
9624                (member gnus-newsgroup-name '("nndraft:delayed"
9625                                              "nndraft:drafts"
9626                                              "nndraft:queue")))
9627       (error "Can't edit the raw article in group %s"
9628              gnus-newsgroup-name))
9629     (save-excursion
9630       (set-buffer gnus-summary-buffer)
9631       (let ((mail-parse-charset gnus-newsgroup-charset)
9632             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9633         (gnus-set-global-variables)
9634         (when (and (not force)
9635                    (gnus-group-read-only-p))
9636           (error "The current newsgroup does not support article editing"))
9637         (gnus-summary-show-article t)
9638         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
9639           (with-current-buffer gnus-article-buffer
9640             (mm-enable-multibyte)))
9641         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
9642             (setq raw t))
9643         (gnus-article-edit-article
9644          (if raw 'ignore
9645            `(lambda ()
9646               (let ((mbl mml-buffer-list))
9647                 (setq mml-buffer-list nil)
9648                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
9649                   (mime-to-mml ,'current-handles))
9650                 (let ((mbl1 mml-buffer-list))
9651                   (setq mml-buffer-list mbl)
9652                   (set (make-local-variable 'mml-buffer-list) mbl1))
9653                 (gnus-make-local-hook 'kill-buffer-hook)
9654                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9655          `(lambda (no-highlight)
9656             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9657                   (message-options message-options)
9658                   (message-options-set-recipient)
9659                   (mail-parse-ignored-charsets
9660                    ',gnus-newsgroup-ignored-charsets))
9661               ,(if (not raw) '(progn
9662                                 (mml-to-mime)
9663                                 (mml-destroy-buffers)
9664                                 (remove-hook 'kill-buffer-hook
9665                                              'mml-destroy-buffers t)
9666                                 (kill-local-variable 'mml-buffer-list)))
9667               (gnus-summary-edit-article-done
9668                ,(or (mail-header-references gnus-current-headers) "")
9669                ,(gnus-group-read-only-p)
9670                ,gnus-summary-buffer no-highlight))))))))
9671
9672 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9673
9674 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9675                                                  no-highlight)
9676   "Make edits to the current article permanent."
9677   (interactive)
9678   (save-excursion
9679    ;; The buffer restriction contains the entire article if it exists.
9680     (when (article-goto-body)
9681       (let ((lines (count-lines (point) (point-max)))
9682             (length (- (point-max) (point)))
9683             (case-fold-search t)
9684             (body (copy-marker (point))))
9685         (goto-char (point-min))
9686         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9687           (delete-region (match-beginning 1) (match-end 1))
9688           (insert (number-to-string length)))
9689         (goto-char (point-min))
9690         (when (re-search-forward
9691                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9692           (delete-region (match-beginning 1) (match-end 1))
9693           (insert (number-to-string length)))
9694         (goto-char (point-min))
9695         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9696           (delete-region (match-beginning 1) (match-end 1))
9697           (insert (number-to-string lines))))))
9698   ;; Replace the article.
9699   (let ((buf (current-buffer)))
9700     (with-temp-buffer
9701       (insert-buffer-substring buf)
9702
9703       (if (and (not read-only)
9704                (not (gnus-request-replace-article
9705                      (cdr gnus-article-current) (car gnus-article-current)
9706                      (current-buffer) t)))
9707           (error "Couldn't replace article")
9708         ;; Update the summary buffer.
9709         (if (and references
9710                  (equal (message-tokenize-header references " ")
9711                         (message-tokenize-header
9712                          (or (message-fetch-field "references") "") " ")))
9713             ;; We only have to update this line.
9714             (save-excursion
9715               (save-restriction
9716                 (message-narrow-to-head)
9717                 (let ((head (buffer-substring-no-properties
9718                              (point-min) (point-max)))
9719                       header)
9720                   (with-temp-buffer
9721                     (insert (format "211 %d Article retrieved.\n"
9722                                     (cdr gnus-article-current)))
9723                     (insert head)
9724                     (insert ".\n")
9725                     (let ((nntp-server-buffer (current-buffer)))
9726                       (setq header (car (gnus-get-newsgroup-headers
9727                                          nil t))))
9728                     (save-excursion
9729                       (set-buffer gnus-summary-buffer)
9730                       (gnus-data-set-header
9731                        (gnus-data-find (cdr gnus-article-current))
9732                        header)
9733                       (gnus-summary-update-article-line
9734                        (cdr gnus-article-current) header)
9735                       (if (gnus-summary-goto-subject
9736                            (cdr gnus-article-current) nil t)
9737                           (gnus-summary-update-secondary-mark
9738                            (cdr gnus-article-current))))))))
9739           ;; Update threads.
9740           (set-buffer (or buffer gnus-summary-buffer))
9741           (gnus-summary-update-article (cdr gnus-article-current))
9742           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9743               (gnus-summary-update-secondary-mark
9744                (cdr gnus-article-current))))
9745         ;; Prettify the article buffer again.
9746         (unless no-highlight
9747           (save-excursion
9748             (set-buffer gnus-article-buffer)
9749             ;;;!!! Fix this -- article should be rehighlighted.
9750             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9751             (set-buffer gnus-original-article-buffer)
9752             (gnus-request-article
9753              (cdr gnus-article-current)
9754              (car gnus-article-current) (current-buffer))))
9755         ;; Prettify the summary buffer line.
9756         (when (gnus-visual-p 'summary-highlight 'highlight)
9757           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9758
9759 (defun gnus-summary-edit-wash (key)
9760   "Perform editing command KEY in the article buffer."
9761   (interactive
9762    (list
9763     (progn
9764       (message "%s" (concat (this-command-keys) "- "))
9765       (read-char))))
9766   (message "")
9767   (gnus-summary-edit-article)
9768   (execute-kbd-macro (concat (this-command-keys) key))
9769   (gnus-article-edit-done))
9770
9771 ;;; Respooling
9772
9773 (defun gnus-summary-respool-query (&optional silent trace)
9774   "Query where the respool algorithm would put this article."
9775   (interactive)
9776   (let (gnus-mark-article-hook)
9777     (gnus-summary-select-article)
9778     (save-excursion
9779       (set-buffer gnus-original-article-buffer)
9780       (let ((groups (nnmail-article-group 'identity trace)))
9781         (unless silent
9782           (if groups
9783               (message "This message would go to %s"
9784                        (mapconcat 'car groups ", "))
9785             (message "This message would go to no groups"))
9786           groups)))))
9787
9788 (defun gnus-summary-respool-trace ()
9789   "Trace where the respool algorithm would put this article.
9790 Display a buffer showing all fancy splitting patterns which matched."
9791   (interactive)
9792   (gnus-summary-respool-query nil t))
9793
9794 ;; Summary marking commands.
9795
9796 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9797   "Mark articles which has the same subject as read, and then select the next.
9798 If UNMARK is positive, remove any kind of mark.
9799 If UNMARK is negative, tick articles."
9800   (interactive "P")
9801   (when unmark
9802     (setq unmark (prefix-numeric-value unmark)))
9803   (let ((count
9804          (gnus-summary-mark-same-subject
9805           (gnus-summary-article-subject) unmark)))
9806     ;; Select next unread article.  If auto-select-same mode, should
9807     ;; select the first unread article.
9808     (gnus-summary-next-article t (and gnus-auto-select-same
9809                                       (gnus-summary-article-subject)))
9810     (gnus-message 7 "%d article%s marked as %s"
9811                   count (if (= count 1) " is" "s are")
9812                   (if unmark "unread" "read"))))
9813
9814 (defun gnus-summary-kill-same-subject (&optional unmark)
9815   "Mark articles which has the same subject as read.
9816 If UNMARK is positive, remove any kind of mark.
9817 If UNMARK is negative, tick articles."
9818   (interactive "P")
9819   (when unmark
9820     (setq unmark (prefix-numeric-value unmark)))
9821   (let ((count
9822          (gnus-summary-mark-same-subject
9823           (gnus-summary-article-subject) unmark)))
9824     ;; If marked as read, go to next unread subject.
9825     (when (null unmark)
9826       ;; Go to next unread subject.
9827       (gnus-summary-next-subject 1 t))
9828     (gnus-message 7 "%d articles are marked as %s"
9829                   count (if unmark "unread" "read"))))
9830
9831 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9832   "Mark articles with same SUBJECT as read, and return marked number.
9833 If optional argument UNMARK is positive, remove any kinds of marks.
9834 If optional argument UNMARK is negative, mark articles as unread instead."
9835   (let ((count 1))
9836     (save-excursion
9837       (cond
9838        ((null unmark)                   ; Mark as read.
9839         (while (and
9840                 (progn
9841                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9842                   (gnus-summary-show-thread) t)
9843                 (gnus-summary-find-subject subject))
9844           (setq count (1+ count))))
9845        ((> unmark 0)                    ; Tick.
9846         (while (and
9847                 (progn
9848                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9849                   (gnus-summary-show-thread) t)
9850                 (gnus-summary-find-subject subject))
9851           (setq count (1+ count))))
9852        (t                               ; Mark as unread.
9853         (while (and
9854                 (progn
9855                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9856                   (gnus-summary-show-thread) t)
9857                 (gnus-summary-find-subject subject))
9858           (setq count (1+ count)))))
9859       (gnus-set-mode-line 'summary)
9860       ;; Return the number of marked articles.
9861       count)))
9862
9863 (defun gnus-summary-mark-as-processable (n &optional unmark)
9864   "Set the process mark on the next N articles.
9865 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9866 the process mark instead.  The difference between N and the actual
9867 number of articles marked is returned."
9868   (interactive "P")
9869   (if (and (null n) (gnus-region-active-p))
9870       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9871     (setq n (prefix-numeric-value n))
9872     (let ((backward (< n 0))
9873           (n (abs n)))
9874       (while (and
9875               (> n 0)
9876               (if unmark
9877                   (gnus-summary-remove-process-mark
9878                    (gnus-summary-article-number))
9879                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9880               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9881         (setq n (1- n)))
9882       (when (/= 0 n)
9883         (gnus-message 7 "No more articles"))
9884       (gnus-summary-recenter)
9885       (gnus-summary-position-point)
9886       n)))
9887
9888 (defun gnus-summary-unmark-as-processable (n)
9889   "Remove the process mark from the next N articles.
9890 If N is negative, unmark backward instead.  The difference between N and
9891 the actual number of articles unmarked is returned."
9892   (interactive "P")
9893   (gnus-summary-mark-as-processable n t))
9894
9895 (defun gnus-summary-unmark-all-processable ()
9896   "Remove the process mark from all articles."
9897   (interactive)
9898   (save-excursion
9899     (while gnus-newsgroup-processable
9900       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9901   (gnus-summary-position-point))
9902
9903 (defun gnus-summary-add-mark (article type)
9904   "Mark ARTICLE with a mark of TYPE."
9905   (let ((vtype (car (assq type gnus-article-mark-lists)))
9906         var)
9907     (if (not vtype)
9908         (error "No such mark type: %s" type)
9909       (setq var (intern (format "gnus-newsgroup-%s" type)))
9910       (set var (cons article (symbol-value var)))
9911       (if (memq type '(processable cached replied forwarded recent saved))
9912           (gnus-summary-update-secondary-mark article)
9913         ;;; !!! This is bogus.  We should find out what primary
9914         ;;; !!! mark we want to set.
9915         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9916
9917 (defun gnus-summary-mark-as-expirable (n)
9918   "Mark N articles forward as expirable.
9919 If N is negative, mark backward instead.  The difference between N and
9920 the actual number of articles marked is returned."
9921   (interactive "p")
9922   (gnus-summary-mark-forward n gnus-expirable-mark))
9923
9924 (defun gnus-summary-mark-as-spam (n)
9925   "Mark N articles forward as spam.
9926 If N is negative, mark backward instead.  The difference between N and
9927 the actual number of articles marked is returned."
9928   (interactive "p")
9929   (gnus-summary-mark-forward n gnus-spam-mark))
9930
9931 (defun gnus-summary-mark-article-as-replied (article)
9932   "Mark ARTICLE as replied to and update the summary line.
9933 ARTICLE can also be a list of articles."
9934   (interactive (list (gnus-summary-article-number)))
9935   (let ((articles (if (listp article) article (list article))))
9936     (dolist (article articles)
9937       (unless (numberp article)
9938         (error "%s is not a number" article))
9939       (push article gnus-newsgroup-replied)
9940       (let ((buffer-read-only nil))
9941         (when (gnus-summary-goto-subject article nil t)
9942           (gnus-summary-update-secondary-mark article))))))
9943
9944 (defun gnus-summary-mark-article-as-forwarded (article)
9945   "Mark ARTICLE as forwarded and update the summary line.
9946 ARTICLE can also be a list of articles."
9947   (let ((articles (if (listp article) article (list article))))
9948     (dolist (article articles)
9949       (push article gnus-newsgroup-forwarded)
9950       (let ((buffer-read-only nil))
9951         (when (gnus-summary-goto-subject article nil t)
9952           (gnus-summary-update-secondary-mark article))))))
9953
9954 (defun gnus-summary-set-bookmark (article)
9955   "Set a bookmark in current article."
9956   (interactive (list (gnus-summary-article-number)))
9957   (when (or (not (get-buffer gnus-article-buffer))
9958             (not gnus-current-article)
9959             (not gnus-article-current)
9960             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9961     (error "No current article selected"))
9962   ;; Remove old bookmark, if one exists.
9963   (gnus-pull article gnus-newsgroup-bookmarks)
9964   ;; Set the new bookmark, which is on the form
9965   ;; (article-number . line-number-in-body).
9966   (push
9967    (cons article
9968          (with-current-buffer gnus-article-buffer
9969            (count-lines
9970             (min (point)
9971                  (save-excursion
9972                    (article-goto-body)
9973                    (point)))
9974             (point))))
9975    gnus-newsgroup-bookmarks)
9976   (gnus-message 6 "A bookmark has been added to the current article."))
9977
9978 (defun gnus-summary-remove-bookmark (article)
9979   "Remove the bookmark from the current article."
9980   (interactive (list (gnus-summary-article-number)))
9981   ;; Remove old bookmark, if one exists.
9982   (if (not (assq article gnus-newsgroup-bookmarks))
9983       (gnus-message 6 "No bookmark in current article.")
9984     (gnus-pull article gnus-newsgroup-bookmarks)
9985     (gnus-message 6 "Removed bookmark.")))
9986
9987 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9988 (defun gnus-summary-mark-as-dormant (n)
9989   "Mark N articles forward as dormant.
9990 If N is negative, mark backward instead.  The difference between N and
9991 the actual number of articles marked is returned."
9992   (interactive "p")
9993   (gnus-summary-mark-forward n gnus-dormant-mark))
9994
9995 (defun gnus-summary-set-process-mark (article)
9996   "Set the process mark on ARTICLE and update the summary line."
9997   (setq gnus-newsgroup-processable
9998         (cons article
9999               (delq article gnus-newsgroup-processable)))
10000   (when (gnus-summary-goto-subject article)
10001     (gnus-summary-show-thread)
10002     (gnus-summary-goto-subject article)
10003     (gnus-summary-update-secondary-mark article)))
10004
10005 (defun gnus-summary-remove-process-mark (&rest articles)
10006   "Remove the process mark from ARTICLES and update the summary line."
10007   (dolist (article articles)
10008     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10009     (when (gnus-summary-goto-subject article)
10010       (gnus-summary-show-thread)
10011       (gnus-summary-goto-subject article)
10012       (gnus-summary-update-secondary-mark article))))
10013
10014 (defun gnus-summary-set-saved-mark (article)
10015   "Set the process mark on ARTICLE and update the summary line."
10016   (push article gnus-newsgroup-saved)
10017   (when (gnus-summary-goto-subject article)
10018     (gnus-summary-update-secondary-mark article)))
10019
10020 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10021   "Mark N articles as read forwards.
10022 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10023 The difference between N and the actual number of articles marked is
10024 returned.
10025 If NO-EXPIRE, auto-expiry will be inhibited."
10026   (interactive "p")
10027   (gnus-summary-show-thread)
10028   (let ((backward (< n 0))
10029         (gnus-summary-goto-unread
10030          (and gnus-summary-goto-unread
10031               (not (eq gnus-summary-goto-unread 'never))
10032               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10033                                     gnus-ticked-mark gnus-dormant-mark)))))
10034         (n (abs n))
10035         (mark (or mark gnus-del-mark)))
10036     (while (and (> n 0)
10037                 (gnus-summary-mark-article nil mark no-expire)
10038                 (zerop (gnus-summary-next-subject
10039                         (if backward -1 1)
10040                         (and gnus-summary-goto-unread
10041                              (not (eq gnus-summary-goto-unread 'never)))
10042                         t)))
10043       (setq n (1- n)))
10044     (when (/= 0 n)
10045       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10046     (gnus-summary-recenter)
10047     (gnus-summary-position-point)
10048     (gnus-set-mode-line 'summary)
10049     n))
10050
10051 (defun gnus-summary-mark-article-as-read (mark)
10052   "Mark the current article quickly as read with MARK."
10053   (let ((article (gnus-summary-article-number)))
10054     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10055     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10056     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10057     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10058     (push (cons article mark) gnus-newsgroup-reads)
10059     ;; Possibly remove from cache, if that is used.
10060     (when gnus-use-cache
10061       (gnus-cache-enter-remove-article article))
10062     ;; Allow the backend to change the mark.
10063     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10064     ;; Check for auto-expiry.
10065     (when (and gnus-newsgroup-auto-expire
10066                (memq mark gnus-auto-expirable-marks))
10067       (setq mark gnus-expirable-mark)
10068       ;; Let the backend know about the mark change.
10069       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10070       (push article gnus-newsgroup-expirable))
10071     ;; Set the mark in the buffer.
10072     (gnus-summary-update-mark mark 'unread)
10073     t))
10074
10075 (defun gnus-summary-mark-article-as-unread (mark)
10076   "Mark the current article quickly as unread with MARK."
10077   (let* ((article (gnus-summary-article-number))
10078          (old-mark (gnus-summary-article-mark article)))
10079     ;; Allow the backend to change the mark.
10080     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10081     (if (eq mark old-mark)
10082         t
10083       (if (<= article 0)
10084           (progn
10085             (gnus-error 1 "Can't mark negative article numbers")
10086             nil)
10087         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10088         (setq gnus-newsgroup-spam-marked
10089               (delq article gnus-newsgroup-spam-marked))
10090         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10091         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10092         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10093         (cond ((= mark gnus-ticked-mark)
10094                (setq gnus-newsgroup-marked
10095                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10096                                               article)))
10097               ((= mark gnus-spam-mark)
10098                (setq gnus-newsgroup-spam-marked
10099                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10100                                               article)))
10101               ((= mark gnus-dormant-mark)
10102                (setq gnus-newsgroup-dormant
10103                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10104                                               article)))
10105               (t
10106                (setq gnus-newsgroup-unreads
10107                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10108                                               article))))
10109         (gnus-pull article gnus-newsgroup-reads)
10110
10111         ;; See whether the article is to be put in the cache.
10112         (and gnus-use-cache
10113              (vectorp (gnus-summary-article-header article))
10114              (save-excursion
10115                (gnus-cache-possibly-enter-article
10116                 gnus-newsgroup-name article
10117                 (= mark gnus-ticked-mark)
10118                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10119
10120         ;; Fix the mark.
10121         (gnus-summary-update-mark mark 'unread)
10122         t))))
10123
10124 (defun gnus-summary-mark-article (&optional article mark no-expire)
10125   "Mark ARTICLE with MARK.  MARK can be any character.
10126 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10127 `??' (dormant) and `?E' (expirable).
10128 If MARK is nil, then the default character `?r' is used.
10129 If ARTICLE is nil, then the article on the current line will be
10130 marked.
10131 If NO-EXPIRE, auto-expiry will be inhibited."
10132   ;; The mark might be a string.
10133   (when (stringp mark)
10134     (setq mark (aref mark 0)))
10135   ;; If no mark is given, then we check auto-expiring.
10136   (when (null mark)
10137     (setq mark gnus-del-mark))
10138   (when (and (not no-expire)
10139              gnus-newsgroup-auto-expire
10140              (memq mark gnus-auto-expirable-marks))
10141     (setq mark gnus-expirable-mark))
10142   (let ((article (or article (gnus-summary-article-number)))
10143         (old-mark (gnus-summary-article-mark article)))
10144     ;; Allow the backend to change the mark.
10145     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10146     (if (eq mark old-mark)
10147         t
10148       (unless article
10149         (error "No article on current line"))
10150       (if (not (if (or (= mark gnus-unread-mark)
10151                        (= mark gnus-ticked-mark)
10152                        (= mark gnus-spam-mark)
10153                        (= mark gnus-dormant-mark))
10154                    (gnus-mark-article-as-unread article mark)
10155                  (gnus-mark-article-as-read article mark)))
10156           t
10157         ;; See whether the article is to be put in the cache.
10158         (and gnus-use-cache
10159              (not (= mark gnus-canceled-mark))
10160              (vectorp (gnus-summary-article-header article))
10161              (save-excursion
10162                (gnus-cache-possibly-enter-article
10163                 gnus-newsgroup-name article
10164                 (= mark gnus-ticked-mark)
10165                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10166
10167         (when (gnus-summary-goto-subject article nil t)
10168           (let ((buffer-read-only nil))
10169             (gnus-summary-show-thread)
10170             ;; Fix the mark.
10171             (gnus-summary-update-mark mark 'unread)
10172             t))))))
10173
10174 (defun gnus-summary-update-secondary-mark (article)
10175   "Update the secondary (read, process, cache) mark."
10176   (gnus-summary-update-mark
10177    (cond ((memq article gnus-newsgroup-processable)
10178           gnus-process-mark)
10179          ((memq article gnus-newsgroup-cached)
10180           gnus-cached-mark)
10181          ((memq article gnus-newsgroup-replied)
10182           gnus-replied-mark)
10183          ((memq article gnus-newsgroup-forwarded)
10184           gnus-forwarded-mark)
10185          ((memq article gnus-newsgroup-saved)
10186           gnus-saved-mark)
10187          ((memq article gnus-newsgroup-recent)
10188           gnus-recent-mark)
10189          ((memq article gnus-newsgroup-unseen)
10190           gnus-unseen-mark)
10191          (t gnus-no-mark))
10192    'replied)
10193   (when (gnus-visual-p 'summary-highlight 'highlight)
10194     (gnus-run-hooks 'gnus-summary-update-hook))
10195   t)
10196
10197 (defun gnus-summary-update-download-mark (article)
10198   "Update the download mark."
10199   (gnus-summary-update-mark
10200    (cond ((memq article gnus-newsgroup-undownloaded)
10201           gnus-undownloaded-mark)
10202          (gnus-newsgroup-agentized
10203           gnus-downloaded-mark)
10204          (t
10205           gnus-no-mark))
10206    'download)
10207   (gnus-summary-update-line t)
10208   t)
10209
10210 (defun gnus-summary-update-mark (mark type)
10211   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10212         (buffer-read-only nil))
10213     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10214     (when forward
10215       (when (looking-at "\r")
10216         (incf forward))
10217       (when (<= (+ forward (point)) (point-max))
10218         ;; Go to the right position on the line.
10219         (goto-char (+ forward (point)))
10220         ;; Replace the old mark with the new mark.
10221         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10222         ;; Optionally update the marks by some user rule.
10223         (when (eq type 'unread)
10224           (gnus-data-set-mark
10225            (gnus-data-find (gnus-summary-article-number)) mark)
10226           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10227
10228 (defun gnus-mark-article-as-read (article &optional mark)
10229   "Enter ARTICLE in the pertinent lists and remove it from others."
10230   ;; Make the article expirable.
10231   (let ((mark (or mark gnus-del-mark)))
10232     (setq gnus-newsgroup-expirable
10233           (if (= mark gnus-expirable-mark)
10234               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10235             (delq article gnus-newsgroup-expirable)))
10236     ;; Remove from unread and marked lists.
10237     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10238     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10239     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10240     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10241     (push (cons article mark) gnus-newsgroup-reads)
10242     ;; Possibly remove from cache, if that is used.
10243     (when gnus-use-cache
10244       (gnus-cache-enter-remove-article article))
10245     t))
10246
10247 (defun gnus-mark-article-as-unread (article &optional mark)
10248   "Enter ARTICLE in the pertinent lists and remove it from others."
10249   (let ((mark (or mark gnus-ticked-mark)))
10250     (if (<= article 0)
10251         (progn
10252           (gnus-error 1 "Can't mark negative article numbers")
10253           nil)
10254       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10255             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10256             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10257             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10258             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10259
10260       ;; Unsuppress duplicates?
10261       (when gnus-suppress-duplicates
10262         (gnus-dup-unsuppress-article article))
10263
10264       (cond ((= mark gnus-ticked-mark)
10265              (setq gnus-newsgroup-marked
10266                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10267             ((= mark gnus-spam-mark)
10268              (setq gnus-newsgroup-spam-marked
10269                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10270                                             article)))
10271             ((= mark gnus-dormant-mark)
10272              (setq gnus-newsgroup-dormant
10273                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10274             (t
10275              (setq gnus-newsgroup-unreads
10276                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10277       (gnus-pull article gnus-newsgroup-reads)
10278       t)))
10279
10280 (defalias 'gnus-summary-mark-as-unread-forward
10281   'gnus-summary-tick-article-forward)
10282 (make-obsolete 'gnus-summary-mark-as-unread-forward
10283                'gnus-summary-tick-article-forward)
10284 (defun gnus-summary-tick-article-forward (n)
10285   "Tick N articles forwards.
10286 If N is negative, tick backwards instead.
10287 The difference between N and the number of articles ticked is returned."
10288   (interactive "p")
10289   (gnus-summary-mark-forward n gnus-ticked-mark))
10290
10291 (defalias 'gnus-summary-mark-as-unread-backward
10292   'gnus-summary-tick-article-backward)
10293 (make-obsolete 'gnus-summary-mark-as-unread-backward
10294                'gnus-summary-tick-article-backward)
10295 (defun gnus-summary-tick-article-backward (n)
10296   "Tick N articles backwards.
10297 The difference between N and the number of articles ticked is returned."
10298   (interactive "p")
10299   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10300
10301 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10302 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10303 (defun gnus-summary-tick-article (&optional article clear-mark)
10304   "Mark current article as unread.
10305 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10306 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10307   (interactive)
10308   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10309                                        gnus-ticked-mark)))
10310
10311 (defun gnus-summary-mark-as-read-forward (n)
10312   "Mark N articles as read forwards.
10313 If N is negative, mark backwards instead.
10314 The difference between N and the actual number of articles marked is
10315 returned."
10316   (interactive "p")
10317   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10318
10319 (defun gnus-summary-mark-as-read-backward (n)
10320   "Mark the N articles as read backwards.
10321 The difference between N and the actual number of articles marked is
10322 returned."
10323   (interactive "p")
10324   (gnus-summary-mark-forward
10325    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10326
10327 (defun gnus-summary-mark-as-read (&optional article mark)
10328   "Mark current article as read.
10329 ARTICLE specifies the article to be marked as read.
10330 MARK specifies a string to be inserted at the beginning of the line."
10331   (gnus-summary-mark-article article mark))
10332
10333 (defun gnus-summary-clear-mark-forward (n)
10334   "Clear marks from N articles forward.
10335 If N is negative, clear backward instead.
10336 The difference between N and the number of marks cleared is returned."
10337   (interactive "p")
10338   (gnus-summary-mark-forward n gnus-unread-mark))
10339
10340 (defun gnus-summary-clear-mark-backward (n)
10341   "Clear marks from N articles backward.
10342 The difference between N and the number of marks cleared is returned."
10343   (interactive "p")
10344   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10345
10346 (defun gnus-summary-mark-unread-as-read ()
10347   "Intended to be used by `gnus-summary-mark-article-hook'."
10348   (when (memq gnus-current-article gnus-newsgroup-unreads)
10349     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10350
10351 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10352   "Intended to be used by `gnus-summary-mark-article-hook'."
10353   (let ((mark (gnus-summary-article-mark)))
10354     (when (or (gnus-unread-mark-p mark)
10355               (gnus-read-mark-p mark))
10356       (gnus-summary-mark-article gnus-current-article
10357                                  (or new-mark gnus-read-mark)))))
10358
10359 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10360   "Intended to be used by `gnus-summary-mark-article-hook'."
10361   (let ((mark (gnus-summary-article-mark)))
10362     (when (or (gnus-unread-mark-p mark)
10363               (gnus-read-mark-p mark))
10364       (gnus-summary-mark-article (gnus-summary-article-number)
10365                                  (or new-mark gnus-read-mark)))))
10366
10367 (defun gnus-summary-mark-unread-as-ticked ()
10368   "Intended to be used by `gnus-summary-mark-article-hook'."
10369   (when (memq gnus-current-article gnus-newsgroup-unreads)
10370     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10371
10372 (defun gnus-summary-mark-region-as-read (point mark all)
10373   "Mark all unread articles between point and mark as read.
10374 If given a prefix, mark all articles between point and mark as read,
10375 even ticked and dormant ones."
10376   (interactive "r\nP")
10377   (save-excursion
10378     (let (article)
10379       (goto-char point)
10380       (beginning-of-line)
10381       (while (and
10382               (< (point) mark)
10383               (progn
10384                 (when (or all
10385                           (memq (setq article (gnus-summary-article-number))
10386                                 gnus-newsgroup-unreads))
10387                   (gnus-summary-mark-article article gnus-del-mark))
10388                 t)
10389               (gnus-summary-find-next))))))
10390
10391 (defun gnus-summary-mark-below (score mark)
10392   "Mark articles with score less than SCORE with MARK."
10393   (interactive "P\ncMark: ")
10394   (setq score (if score
10395                   (prefix-numeric-value score)
10396                 (or gnus-summary-default-score 0)))
10397   (save-excursion
10398     (set-buffer gnus-summary-buffer)
10399     (goto-char (point-min))
10400     (while
10401         (progn
10402           (and (< (gnus-summary-article-score) score)
10403                (gnus-summary-mark-article nil mark))
10404           (gnus-summary-find-next)))))
10405
10406 (defun gnus-summary-kill-below (&optional score)
10407   "Mark articles with score below SCORE as read."
10408   (interactive "P")
10409   (gnus-summary-mark-below score gnus-killed-mark))
10410
10411 (defun gnus-summary-clear-above (&optional score)
10412   "Clear all marks from articles with score above SCORE."
10413   (interactive "P")
10414   (gnus-summary-mark-above score gnus-unread-mark))
10415
10416 (defun gnus-summary-tick-above (&optional score)
10417   "Tick all articles with score above SCORE."
10418   (interactive "P")
10419   (gnus-summary-mark-above score gnus-ticked-mark))
10420
10421 (defun gnus-summary-mark-above (score mark)
10422   "Mark articles with score over SCORE with MARK."
10423   (interactive "P\ncMark: ")
10424   (setq score (if score
10425                   (prefix-numeric-value score)
10426                 (or gnus-summary-default-score 0)))
10427   (save-excursion
10428     (set-buffer gnus-summary-buffer)
10429     (goto-char (point-min))
10430     (while (and (progn
10431                   (when (> (gnus-summary-article-score) score)
10432                     (gnus-summary-mark-article nil mark))
10433                   t)
10434                 (gnus-summary-find-next)))))
10435
10436 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10437 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10438 (defun gnus-summary-limit-include-expunged (&optional no-error)
10439   "Display all the hidden articles that were expunged for low scores."
10440   (interactive)
10441   (let ((buffer-read-only nil))
10442     (let ((scored gnus-newsgroup-scored)
10443           headers h)
10444       (while scored
10445         (unless (gnus-summary-article-header (caar scored))
10446           (and (setq h (gnus-number-to-header (caar scored)))
10447                (< (cdar scored) gnus-summary-expunge-below)
10448                (push h headers)))
10449         (setq scored (cdr scored)))
10450       (if (not headers)
10451           (when (not no-error)
10452             (error "No expunged articles hidden"))
10453         (goto-char (point-min))
10454         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10455         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10456         (mapcar (lambda (x) (push (mail-header-number x)
10457                                   gnus-newsgroup-limit))
10458                 headers)
10459         (gnus-summary-prepare-unthreaded (nreverse headers))
10460         (goto-char (point-min))
10461         (gnus-summary-position-point)
10462         t))))
10463
10464 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10465   "Mark all unread articles in this newsgroup as read.
10466 If prefix argument ALL is non-nil, ticked and dormant articles will
10467 also be marked as read.
10468 If QUIETLY is non-nil, no questions will be asked.
10469
10470 If TO-HERE is non-nil, it should be a point in the buffer.  All
10471 articles before (after, if REVERSE is set) this point will be marked
10472 as read.
10473
10474 Note that this function will only catch up the unread article
10475 in the current summary buffer limitation.
10476
10477 The number of articles marked as read is returned."
10478   (interactive "P")
10479   (prog1
10480       (save-excursion
10481         (when (or quietly
10482                   (not gnus-interactive-catchup) ;Without confirmation?
10483                   gnus-expert-user
10484                   (gnus-y-or-n-p
10485                    (if all
10486                        "Mark absolutely all articles as read? "
10487                      "Mark all unread articles as read? ")))
10488           (if (and not-mark
10489                    (not gnus-newsgroup-adaptive)
10490                    (not gnus-newsgroup-auto-expire)
10491                    (not gnus-suppress-duplicates)
10492                    (or (not gnus-use-cache)
10493                        (eq gnus-use-cache 'passive)))
10494               (progn
10495                 (when all
10496                   (setq gnus-newsgroup-marked nil
10497                         gnus-newsgroup-spam-marked nil
10498                         gnus-newsgroup-dormant nil))
10499                 (setq gnus-newsgroup-unreads
10500                       (gnus-sorted-nunion
10501                        (gnus-intersection gnus-newsgroup-unreads
10502                                           gnus-newsgroup-downloadable)
10503                        gnus-newsgroup-unfetched)))
10504             ;; We actually mark all articles as canceled, which we
10505             ;; have to do when using auto-expiry or adaptive scoring.
10506             (gnus-summary-show-all-threads)
10507             (if (and to-here reverse)
10508                 (progn
10509                   (goto-char to-here)
10510                   (gnus-summary-mark-current-read-and-unread-as-read
10511                    gnus-catchup-mark)
10512                   (while (gnus-summary-find-next (not all))
10513                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10514               (when (gnus-summary-first-subject (not all))
10515                 (while (and
10516                         (if to-here (< (point) to-here) t)
10517                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10518                         (gnus-summary-find-next (not all))))))
10519             (gnus-set-mode-line 'summary))
10520           t))
10521     (gnus-summary-position-point)))
10522
10523 (defun gnus-summary-catchup-to-here (&optional all)
10524   "Mark all unticked articles before the current one as read.
10525 If ALL is non-nil, also mark ticked and dormant articles as read."
10526   (interactive "P")
10527   (save-excursion
10528     (gnus-save-hidden-threads
10529       (let ((beg (point)))
10530         ;; We check that there are unread articles.
10531         (when (or all (gnus-summary-find-prev))
10532           (gnus-summary-catchup all t beg)))))
10533   (gnus-summary-position-point))
10534
10535 (defun gnus-summary-catchup-from-here (&optional all)
10536   "Mark all unticked articles after (and including) 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-next))
10544           (gnus-summary-catchup all t beg nil t)))))
10545   (gnus-summary-position-point))
10546
10547 (defun gnus-summary-catchup-all (&optional quietly)
10548   "Mark all articles in this newsgroup as read.
10549 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10550 instead, which marks only unread articles as read."
10551   (interactive "P")
10552   (gnus-summary-catchup t quietly))
10553
10554 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10555   "Mark all unread articles in this group as read, then exit.
10556 If prefix argument ALL is non-nil, all articles are marked as read.
10557 If QUIETLY is non-nil, no questions will be asked."
10558   (interactive "P")
10559   (when (gnus-summary-catchup all quietly nil 'fast)
10560     ;; Select next newsgroup or exit.
10561     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10562              (eq gnus-auto-select-next 'quietly))
10563         (gnus-summary-next-group nil)
10564       (gnus-summary-exit))))
10565
10566 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10567   "Mark all articles in this newsgroup as read, and then exit.
10568 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10569 instead, which marks only unread articles as read."
10570   (interactive "P")
10571   (gnus-summary-catchup-and-exit t quietly))
10572
10573 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10574   "Mark all articles in this group as read and select the next group.
10575 If given a prefix, mark all articles, unread as well as ticked, as
10576 read."
10577   (interactive "P")
10578   (save-excursion
10579     (gnus-summary-catchup all))
10580   (gnus-summary-next-group))
10581
10582 ;;;
10583 ;;; with article
10584 ;;;
10585
10586 (defmacro gnus-with-article (article &rest forms)
10587   "Select ARTICLE and perform FORMS in the original article buffer.
10588 Then replace the article with the result."
10589   `(progn
10590      ;; We don't want the article to be marked as read.
10591      (let (gnus-mark-article-hook)
10592        (gnus-summary-select-article t t nil ,article))
10593      (set-buffer gnus-original-article-buffer)
10594      ,@forms
10595      (if (not (gnus-check-backend-function
10596                'request-replace-article (car gnus-article-current)))
10597          (gnus-message 5 "Read-only group; not replacing")
10598        (unless (gnus-request-replace-article
10599                 ,article (car gnus-article-current)
10600                 (current-buffer) t)
10601          (error "Couldn't replace article")))
10602      ;; The cache and backlog have to be flushed somewhat.
10603      (when gnus-keep-backlog
10604        (gnus-backlog-remove-article
10605         (car gnus-article-current) (cdr gnus-article-current)))
10606      (when gnus-use-cache
10607        (gnus-cache-update-article
10608         (car gnus-article-current) (cdr gnus-article-current)))))
10609
10610 (put 'gnus-with-article 'lisp-indent-function 1)
10611 (put 'gnus-with-article 'edebug-form-spec '(form body))
10612
10613 ;; Thread-based commands.
10614
10615 (defun gnus-summary-articles-in-thread (&optional article)
10616   "Return a list of all articles in the current thread.
10617 If ARTICLE is non-nil, return all articles in the thread that starts
10618 with that article."
10619   (let* ((article (or article (gnus-summary-article-number)))
10620          (data (gnus-data-find-list article))
10621          (top-level (gnus-data-level (car data)))
10622          (top-subject
10623           (cond ((null gnus-thread-operation-ignore-subject)
10624                  (gnus-simplify-subject-re
10625                   (mail-header-subject (gnus-data-header (car data)))))
10626                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10627                  (gnus-simplify-subject-fuzzy
10628                   (mail-header-subject (gnus-data-header (car data)))))
10629                 (t nil)))
10630          (end-point (save-excursion
10631                       (if (gnus-summary-go-to-next-thread)
10632                           (point) (point-max))))
10633          articles)
10634     (while (and data
10635                 (< (gnus-data-pos (car data)) end-point))
10636       (when (or (not top-subject)
10637                 (string= top-subject
10638                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10639                              (gnus-simplify-subject-fuzzy
10640                               (mail-header-subject
10641                                (gnus-data-header (car data))))
10642                            (gnus-simplify-subject-re
10643                             (mail-header-subject
10644                              (gnus-data-header (car data)))))))
10645         (push (gnus-data-number (car data)) articles))
10646       (unless (and (setq data (cdr data))
10647                    (> (gnus-data-level (car data)) top-level))
10648         (setq data nil)))
10649     ;; Return the list of articles.
10650     (nreverse articles)))
10651
10652 (defun gnus-summary-rethread-current ()
10653   "Rethread the thread the current article is part of."
10654   (interactive)
10655   (let* ((gnus-show-threads t)
10656          (article (gnus-summary-article-number))
10657          (id (mail-header-id (gnus-summary-article-header)))
10658          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10659     (unless id
10660       (error "No article on the current line"))
10661     (gnus-rebuild-thread id)
10662     (gnus-summary-goto-subject article)))
10663
10664 (defun gnus-summary-reparent-thread ()
10665   "Make the current article child of the marked (or previous) article.
10666
10667 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10668 is non-nil or the Subject: of both articles are the same."
10669   (interactive)
10670   (unless (not (gnus-group-read-only-p))
10671     (error "The current newsgroup does not support article editing"))
10672   (unless (<= (length gnus-newsgroup-processable) 1)
10673     (error "No more than one article may be marked"))
10674   (save-window-excursion
10675     (let ((gnus-article-buffer " *reparent*")
10676           (current-article (gnus-summary-article-number))
10677           ;; First grab the marked article, otherwise one line up.
10678           (parent-article (if (not (null gnus-newsgroup-processable))
10679                               (car gnus-newsgroup-processable)
10680                             (save-excursion
10681                               (if (eq (forward-line -1) 0)
10682                                   (gnus-summary-article-number)
10683                                 (error "Beginning of summary buffer"))))))
10684       (unless (not (eq current-article parent-article))
10685         (error "An article may not be self-referential"))
10686       (let ((message-id (mail-header-id
10687                          (gnus-summary-article-header parent-article))))
10688         (unless (and message-id (not (equal message-id "")))
10689           (error "No message-id in desired parent"))
10690         (gnus-with-article current-article
10691           (save-restriction
10692             (goto-char (point-min))
10693             (message-narrow-to-head)
10694             (if (re-search-forward "^References: " nil t)
10695                 (progn
10696                   (re-search-forward "^[^ \t]" nil t)
10697                   (forward-line -1)
10698                   (end-of-line)
10699                   (insert " " message-id))
10700               (insert "References: " message-id "\n"))))
10701         (set-buffer gnus-summary-buffer)
10702         (gnus-summary-unmark-all-processable)
10703         (gnus-summary-update-article current-article)
10704         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10705             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10706         (gnus-summary-rethread-current)
10707         (gnus-message 3 "Article %d is now the child of article %d"
10708                       current-article parent-article)))))
10709
10710 (defun gnus-summary-toggle-threads (&optional arg)
10711   "Toggle showing conversation threads.
10712 If ARG is positive number, turn showing conversation threads on."
10713   (interactive "P")
10714   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10715     (setq gnus-show-threads
10716           (if (null arg) (not gnus-show-threads)
10717             (> (prefix-numeric-value arg) 0)))
10718     (gnus-summary-prepare)
10719     (gnus-summary-goto-subject current)
10720     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10721     (gnus-summary-position-point)))
10722
10723 (defun gnus-summary-show-all-threads ()
10724   "Show all threads."
10725   (interactive)
10726   (save-excursion
10727     (let ((buffer-read-only nil))
10728       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10729   (gnus-summary-position-point))
10730
10731 (defun gnus-summary-show-thread ()
10732   "Show thread subtrees.
10733 Returns nil if no thread was there to be shown."
10734   (interactive)
10735   (let ((buffer-read-only nil)
10736         (orig (point))
10737         (end (point-at-eol))
10738         ;; Leave point at bol
10739         (beg (progn (beginning-of-line) (point))))
10740     (prog1
10741         ;; Any hidden lines here?
10742         (search-forward "\r" end t)
10743       (subst-char-in-region beg end ?\^M ?\n t)
10744       (goto-char orig)
10745       (gnus-summary-position-point))))
10746
10747 (defun gnus-summary-maybe-hide-threads ()
10748   "If requested, hide the threads that should be hidden."
10749   (when (and gnus-show-threads
10750              gnus-thread-hide-subtree)
10751     (gnus-summary-hide-all-threads
10752      (if (or (consp gnus-thread-hide-subtree)
10753              (functionp gnus-thread-hide-subtree))
10754          (gnus-make-predicate gnus-thread-hide-subtree)
10755        nil))))
10756
10757 ;;; Hiding predicates.
10758
10759 (defun gnus-article-unread-p (header)
10760   (memq (mail-header-number header) gnus-newsgroup-unreads))
10761
10762 (defun gnus-article-unseen-p (header)
10763   (memq (mail-header-number header) gnus-newsgroup-unseen))
10764
10765 (defun gnus-map-articles (predicate articles)
10766   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10767   (apply 'gnus-or (mapcar predicate
10768                           (mapcar 'gnus-summary-article-header articles))))
10769
10770 (defun gnus-summary-hide-all-threads (&optional predicate)
10771   "Hide all thread subtrees.
10772 If PREDICATE is supplied, threads that satisfy this predicate
10773 will not be hidden."
10774   (interactive)
10775   (save-excursion
10776     (goto-char (point-min))
10777     (let ((end nil))
10778       (while (not end)
10779         (when (or (not predicate)
10780                   (gnus-map-articles
10781                    predicate (gnus-summary-article-children)))
10782             (gnus-summary-hide-thread))
10783         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10784   (gnus-summary-position-point))
10785
10786 (defun gnus-summary-hide-thread ()
10787   "Hide thread subtrees.
10788 If PREDICATE is supplied, threads that satisfy this predicate
10789 will not be hidden.
10790 Returns nil if no threads were there to be hidden."
10791   (interactive)
10792   (let ((buffer-read-only nil)
10793         (start (point))
10794         (article (gnus-summary-article-number)))
10795     (goto-char start)
10796     ;; Go forward until either the buffer ends or the subthread
10797     ;; ends.
10798     (when (and (not (eobp))
10799                (or (zerop (gnus-summary-next-thread 1 t))
10800                    (goto-char (point-max))))
10801       (prog1
10802           (if (and (> (point) start)
10803                    (search-backward "\n" start t))
10804               (progn
10805                 (subst-char-in-region start (point) ?\n ?\^M)
10806                 (gnus-summary-goto-subject article))
10807             (goto-char start)
10808             nil)))))
10809
10810 (defun gnus-summary-go-to-next-thread (&optional previous)
10811   "Go to the same level (or less) next thread.
10812 If PREVIOUS is non-nil, go to previous thread instead.
10813 Return the article number moved to, or nil if moving was impossible."
10814   (let ((level (gnus-summary-thread-level))
10815         (way (if previous -1 1))
10816         (beg (point)))
10817     (forward-line way)
10818     (while (and (not (eobp))
10819                 (< level (gnus-summary-thread-level)))
10820       (forward-line way))
10821     (if (eobp)
10822         (progn
10823           (goto-char beg)
10824           nil)
10825       (setq beg (point))
10826       (prog1
10827           (gnus-summary-article-number)
10828         (goto-char beg)))))
10829
10830 (defun gnus-summary-next-thread (n &optional silent)
10831   "Go to the same level next N'th thread.
10832 If N is negative, search backward instead.
10833 Returns the difference between N and the number of skips actually
10834 done.
10835
10836 If SILENT, don't output messages."
10837   (interactive "p")
10838   (let ((backward (< n 0))
10839         (n (abs n)))
10840     (while (and (> n 0)
10841                 (gnus-summary-go-to-next-thread backward))
10842       (decf n))
10843     (unless silent
10844       (gnus-summary-position-point))
10845     (when (and (not silent) (/= 0 n))
10846       (gnus-message 7 "No more threads"))
10847     n))
10848
10849 (defun gnus-summary-prev-thread (n)
10850   "Go to the same level previous N'th thread.
10851 Returns the difference between N and the number of skips actually
10852 done."
10853   (interactive "p")
10854   (gnus-summary-next-thread (- n)))
10855
10856 (defun gnus-summary-go-down-thread ()
10857   "Go down one level in the current thread."
10858   (let ((children (gnus-summary-article-children)))
10859     (when children
10860       (gnus-summary-goto-subject (car children)))))
10861
10862 (defun gnus-summary-go-up-thread ()
10863   "Go up one level in the current thread."
10864   (let ((parent (gnus-summary-article-parent)))
10865     (when parent
10866       (gnus-summary-goto-subject parent))))
10867
10868 (defun gnus-summary-down-thread (n)
10869   "Go down thread N steps.
10870 If N is negative, go up instead.
10871 Returns the difference between N and how many steps down that were
10872 taken."
10873   (interactive "p")
10874   (let ((up (< n 0))
10875         (n (abs n)))
10876     (while (and (> n 0)
10877                 (if up (gnus-summary-go-up-thread)
10878                   (gnus-summary-go-down-thread)))
10879       (setq n (1- n)))
10880     (gnus-summary-position-point)
10881     (when (/= 0 n)
10882       (gnus-message 7 "Can't go further"))
10883     n))
10884
10885 (defun gnus-summary-up-thread (n)
10886   "Go up thread N steps.
10887 If N is negative, go down instead.
10888 Returns the difference between N and how many steps down that were
10889 taken."
10890   (interactive "p")
10891   (gnus-summary-down-thread (- n)))
10892
10893 (defun gnus-summary-top-thread ()
10894   "Go to the top of the thread."
10895   (interactive)
10896   (while (gnus-summary-go-up-thread))
10897   (gnus-summary-article-number))
10898
10899 (defun gnus-summary-kill-thread (&optional unmark)
10900   "Mark articles under current thread as read.
10901 If the prefix argument is positive, remove any kinds of marks.
10902 If the prefix argument is negative, tick articles instead."
10903   (interactive "P")
10904   (when unmark
10905     (setq unmark (prefix-numeric-value unmark)))
10906   (let ((articles (gnus-summary-articles-in-thread)))
10907     (save-excursion
10908       ;; Expand the thread.
10909       (gnus-summary-show-thread)
10910       ;; Mark all the articles.
10911       (while articles
10912         (gnus-summary-goto-subject (car articles))
10913         (cond ((null unmark)
10914                (gnus-summary-mark-article-as-read gnus-killed-mark))
10915               ((> unmark 0)
10916                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10917               (t
10918                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10919         (setq articles (cdr articles))))
10920     ;; Hide killed subtrees.
10921     (and (null unmark)
10922          gnus-thread-hide-killed
10923          (gnus-summary-hide-thread))
10924     ;; If marked as read, go to next unread subject.
10925     (when (null unmark)
10926       ;; Go to next unread subject.
10927       (gnus-summary-next-subject 1 t)))
10928   (gnus-set-mode-line 'summary))
10929
10930 ;; Summary sorting commands
10931
10932 (defun gnus-summary-sort-by-number (&optional reverse)
10933   "Sort the summary buffer by article number.
10934 Argument REVERSE means reverse order."
10935   (interactive "P")
10936   (gnus-summary-sort 'number reverse))
10937
10938 (defun gnus-summary-sort-by-random (&optional reverse)
10939   "Randomize the order in the summary buffer.
10940 Argument REVERSE means to randomize in reverse order."
10941   (interactive "P")
10942   (gnus-summary-sort 'random reverse))
10943
10944 (defun gnus-summary-sort-by-author (&optional reverse)
10945   "Sort the summary buffer by author name alphabetically.
10946 If `case-fold-search' is non-nil, case of letters is ignored.
10947 Argument REVERSE means reverse order."
10948   (interactive "P")
10949   (gnus-summary-sort 'author reverse))
10950
10951 (defun gnus-summary-sort-by-recipient (&optional reverse)
10952   "Sort the summary buffer by recipient name alphabetically.
10953 If `case-fold-search' is non-nil, case of letters is ignored.
10954 Argument REVERSE means reverse order."
10955   (interactive "P")
10956   (gnus-summary-sort 'recipient reverse))
10957
10958 (defun gnus-summary-sort-by-subject (&optional reverse)
10959   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10960 If `case-fold-search' is non-nil, case of letters is ignored.
10961 Argument REVERSE means reverse order."
10962   (interactive "P")
10963   (gnus-summary-sort 'subject reverse))
10964
10965 (defun gnus-summary-sort-by-date (&optional reverse)
10966   "Sort the summary buffer by date.
10967 Argument REVERSE means reverse order."
10968   (interactive "P")
10969   (gnus-summary-sort 'date reverse))
10970
10971 (defun gnus-summary-sort-by-score (&optional reverse)
10972   "Sort the summary buffer by score.
10973 Argument REVERSE means reverse order."
10974   (interactive "P")
10975   (gnus-summary-sort 'score reverse))
10976
10977 (defun gnus-summary-sort-by-lines (&optional reverse)
10978   "Sort the summary buffer by the number of lines.
10979 Argument REVERSE means reverse order."
10980   (interactive "P")
10981   (gnus-summary-sort 'lines reverse))
10982
10983 (defun gnus-summary-sort-by-chars (&optional reverse)
10984   "Sort the summary buffer by article length.
10985 Argument REVERSE means reverse order."
10986   (interactive "P")
10987   (gnus-summary-sort 'chars reverse))
10988
10989 (defun gnus-summary-sort-by-original (&optional reverse)
10990   "Sort the summary buffer using the default sorting method.
10991 Argument REVERSE means reverse order."
10992   (interactive "P")
10993   (let* ((buffer-read-only)
10994          (gnus-summary-prepare-hook nil))
10995     ;; We do the sorting by regenerating the threads.
10996     (gnus-summary-prepare)
10997     ;; Hide subthreads if needed.
10998     (gnus-summary-maybe-hide-threads)))
10999
11000 (defun gnus-summary-sort (predicate reverse)
11001   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11002   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11003          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11004          (gnus-thread-sort-functions
11005           (if (not reverse)
11006               thread
11007             `(lambda (t1 t2)
11008                (,thread t2 t1))))
11009          (gnus-sort-gathered-threads-function
11010           gnus-thread-sort-functions)
11011          (gnus-article-sort-functions
11012           (if (not reverse)
11013               article
11014             `(lambda (t1 t2)
11015                (,article t2 t1))))
11016          (buffer-read-only)
11017          (gnus-summary-prepare-hook nil))
11018     ;; We do the sorting by regenerating the threads.
11019     (gnus-summary-prepare)
11020     ;; Hide subthreads if needed.
11021     (gnus-summary-maybe-hide-threads)))
11022
11023 ;; Summary saving commands.
11024
11025 (defun gnus-summary-save-article (&optional n not-saved)
11026   "Save the current article using the default saver function.
11027 If N is a positive number, save the N next articles.
11028 If N is a negative number, save the N previous articles.
11029 If N is nil and any articles have been marked with the process mark,
11030 save those articles instead.
11031 The variable `gnus-default-article-saver' specifies the saver function."
11032   (interactive "P")
11033   (let* ((articles (gnus-summary-work-articles n))
11034          (save-buffer (save-excursion
11035                         (nnheader-set-temp-buffer " *Gnus Save*")))
11036          (num (length articles))
11037          header file)
11038     (dolist (article articles)
11039       (setq header (gnus-summary-article-header article))
11040       (if (not (vectorp header))
11041           ;; This is a pseudo-article.
11042           (if (assq 'name header)
11043               (gnus-copy-file (cdr (assq 'name header)))
11044             (gnus-message 1 "Article %d is unsaveable" article))
11045         ;; This is a real article.
11046         (save-window-excursion
11047           (let ((gnus-display-mime-function nil)
11048                 (gnus-article-prepare-hook nil))
11049             (gnus-summary-select-article t nil nil article)))
11050         (save-excursion
11051           (set-buffer save-buffer)
11052           (erase-buffer)
11053           (insert-buffer-substring gnus-original-article-buffer))
11054         (setq file (gnus-article-save save-buffer file num))
11055         (gnus-summary-remove-process-mark article)
11056         (unless not-saved
11057           (gnus-summary-set-saved-mark article))))
11058     (gnus-kill-buffer save-buffer)
11059     (gnus-summary-position-point)
11060     (gnus-set-mode-line 'summary)
11061     n))
11062
11063 (defun gnus-summary-pipe-output (&optional arg headers)
11064   "Pipe the current article to a subprocess.
11065 If N is a positive number, pipe the N next articles.
11066 If N is a negative number, pipe the N previous articles.
11067 If N is nil and any articles have been marked with the process mark,
11068 pipe those articles instead.
11069 If HEADERS (the symbolic prefix), include the headers, too."
11070   (interactive (gnus-interactive "P\ny"))
11071   (require 'gnus-art)
11072   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11073         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11074     (gnus-summary-save-article arg t))
11075   (let ((buffer (get-buffer "*Shell Command Output*")))
11076     (when (and buffer
11077                (not (zerop (buffer-size buffer))))
11078       (gnus-configure-windows 'pipe))))
11079
11080 (defun gnus-summary-save-article-mail (&optional arg)
11081   "Append the current article to an mail file.
11082 If N is a positive number, save the N next articles.
11083 If N is a negative number, save the N previous articles.
11084 If N is nil and any articles have been marked with the process mark,
11085 save those articles instead."
11086   (interactive "P")
11087   (require 'gnus-art)
11088   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11089     (gnus-summary-save-article arg)))
11090
11091 (defun gnus-summary-save-article-rmail (&optional arg)
11092   "Append the current article to an rmail file.
11093 If N is a positive number, save the N next articles.
11094 If N is a negative number, save the N previous articles.
11095 If N is nil and any articles have been marked with the process mark,
11096 save those articles instead."
11097   (interactive "P")
11098   (require 'gnus-art)
11099   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11100     (gnus-summary-save-article arg)))
11101
11102 (defun gnus-summary-save-article-file (&optional arg)
11103   "Append the current article to a file.
11104 If N is a positive number, save the N next articles.
11105 If N is a negative number, save the N previous articles.
11106 If N is nil and any articles have been marked with the process mark,
11107 save those articles instead."
11108   (interactive "P")
11109   (require 'gnus-art)
11110   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11111     (gnus-summary-save-article arg)))
11112
11113 (defun gnus-summary-write-article-file (&optional arg)
11114   "Write the current article to a file, deleting the previous file.
11115 If N is a positive number, save the N next articles.
11116 If N is a negative number, save the N previous articles.
11117 If N is nil and any articles have been marked with the process mark,
11118 save those articles instead."
11119   (interactive "P")
11120   (require 'gnus-art)
11121   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11122     (gnus-summary-save-article arg)))
11123
11124 (defun gnus-summary-save-article-body-file (&optional arg)
11125   "Append the current article body to a file.
11126 If N is a positive number, save the N next articles.
11127 If N is a negative number, save the N previous articles.
11128 If N is nil and any articles have been marked with the process mark,
11129 save those articles instead."
11130   (interactive "P")
11131   (require 'gnus-art)
11132   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11133     (gnus-summary-save-article arg)))
11134
11135 (defun gnus-summary-muttprint (&optional arg)
11136   "Print the current article using Muttprint.
11137 If N is a positive number, save the N next articles.
11138 If N is a negative number, save the N previous articles.
11139 If N is nil and any articles have been marked with the process mark,
11140 save those articles instead."
11141   (interactive "P")
11142   (require 'gnus-art)
11143   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11144     (gnus-summary-save-article arg t)))
11145
11146 (defun gnus-summary-pipe-message (program)
11147   "Pipe the current article through PROGRAM."
11148   (interactive "sProgram: ")
11149   (gnus-summary-select-article)
11150   (let ((mail-header-separator ""))
11151     (gnus-eval-in-buffer-window gnus-article-buffer
11152       (save-restriction
11153         (widen)
11154         (let ((start (window-start))
11155               buffer-read-only)
11156           (message-pipe-buffer-body program)
11157           (set-window-start (get-buffer-window (current-buffer)) start))))))
11158
11159 (defun gnus-get-split-value (methods)
11160   "Return a value based on the split METHODS."
11161   (let (split-name method result match)
11162     (when methods
11163       (save-excursion
11164         (set-buffer gnus-original-article-buffer)
11165         (save-restriction
11166           (nnheader-narrow-to-headers)
11167           (while (and methods (not split-name))
11168             (goto-char (point-min))
11169             (setq method (pop methods))
11170             (setq match (car method))
11171             (when (cond
11172                    ((stringp match)
11173                     ;; Regular expression.
11174                     (ignore-errors
11175                       (re-search-forward match nil t)))
11176                    ((functionp match)
11177                     ;; Function.
11178                     (save-restriction
11179                       (widen)
11180                       (setq result (funcall match gnus-newsgroup-name))))
11181                    ((consp match)
11182                     ;; Form.
11183                     (save-restriction
11184                       (widen)
11185                       (setq result (eval match)))))
11186               (setq split-name (cdr method))
11187               (cond ((stringp result)
11188                      (push (expand-file-name
11189                             result gnus-article-save-directory)
11190                            split-name))
11191                     ((consp result)
11192                      (setq split-name (append result split-name)))))))))
11193     (nreverse split-name)))
11194
11195 (defun gnus-valid-move-group-p (group)
11196   (and (boundp group)
11197        (symbol-name group)
11198        (symbol-value group)
11199        (gnus-get-function (gnus-find-method-for-group
11200                            (symbol-name group)) 'request-accept-article t)))
11201
11202 (defun gnus-read-move-group-name (prompt default articles prefix)
11203   "Read a group name."
11204   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11205          (minibuffer-confirm-incomplete nil) ; XEmacs
11206          (prom
11207           (format "%s %s to:"
11208                   prompt
11209                   (if (> (length articles) 1)
11210                       (format "these %d articles" (length articles))
11211                     "this article")))
11212          (to-newsgroup
11213           (cond
11214            ((null split-name)
11215             (gnus-completing-read-with-default
11216              default prom
11217              gnus-active-hashtb
11218              'gnus-valid-move-group-p
11219              nil prefix
11220              'gnus-group-history))
11221            ((= 1 (length split-name))
11222             (gnus-completing-read-with-default
11223              (car split-name) prom
11224              gnus-active-hashtb
11225              'gnus-valid-move-group-p
11226              nil nil
11227              'gnus-group-history))
11228            (t
11229             (gnus-completing-read-with-default
11230              nil prom
11231              (mapcar 'list (nreverse split-name))
11232              nil nil nil
11233              'gnus-group-history))))
11234          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11235     (when to-newsgroup
11236       (if (or (string= to-newsgroup "")
11237               (string= to-newsgroup prefix))
11238           (setq to-newsgroup default))
11239       (unless to-newsgroup
11240         (error "No group name entered"))
11241       (or (gnus-active to-newsgroup)
11242           (gnus-activate-group to-newsgroup nil nil to-method)
11243           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11244                                      to-newsgroup))
11245               (or (and (gnus-request-create-group to-newsgroup to-method)
11246                        (gnus-activate-group
11247                         to-newsgroup nil nil to-method)
11248                        (gnus-subscribe-group to-newsgroup))
11249                   (error "Couldn't create group %s" to-newsgroup)))
11250           (error "No such group: %s" to-newsgroup)))
11251     to-newsgroup))
11252
11253 (defun gnus-summary-save-parts (type dir n &optional reverse)
11254   "Save parts matching TYPE to DIR.
11255 If REVERSE, save parts that do not match TYPE."
11256   (interactive
11257    (list (read-string "Save parts of type: "
11258                       (or (car gnus-summary-save-parts-type-history)
11259                           gnus-summary-save-parts-default-mime)
11260                       'gnus-summary-save-parts-type-history)
11261          (setq gnus-summary-save-parts-last-directory
11262                (read-file-name "Save to directory: "
11263                                gnus-summary-save-parts-last-directory
11264                                nil t))
11265          current-prefix-arg))
11266   (gnus-summary-iterate n
11267     (let ((gnus-display-mime-function nil)
11268           (gnus-inhibit-treatment t))
11269       (gnus-summary-select-article))
11270     (save-excursion
11271       (set-buffer gnus-article-buffer)
11272       (let ((handles (or gnus-article-mime-handles
11273                          (mm-dissect-buffer nil gnus-article-loose-mime)
11274                          (and gnus-article-emulate-mime
11275                               (mm-uu-dissect)))))
11276         (when handles
11277           (gnus-summary-save-parts-1 type dir handles reverse)
11278           (unless gnus-article-mime-handles ;; Don't destroy this case.
11279             (mm-destroy-parts handles)))))))
11280
11281 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11282   (if (stringp (car handle))
11283       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11284               (cdr handle))
11285     (when (if reverse
11286               (not (string-match type (mm-handle-media-type handle)))
11287             (string-match type (mm-handle-media-type handle)))
11288       (let ((file (expand-file-name
11289                    (gnus-map-function
11290                     mm-file-name-rewrite-functions
11291                     (file-name-nondirectory
11292                      (or
11293                       (mail-content-type-get
11294                        (mm-handle-disposition handle) 'filename)
11295                       (mail-content-type-get
11296                        (mm-handle-type handle) 'name)
11297                       (concat gnus-newsgroup-name
11298                               "." (number-to-string
11299                                    (cdr gnus-article-current))))))
11300                    dir)))
11301         (unless (file-exists-p file)
11302           (mm-save-part-to-file handle file))))))
11303
11304 ;; Summary extract commands
11305
11306 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11307   (let ((buffer-read-only nil)
11308         (article (gnus-summary-article-number))
11309         after-article b e)
11310     (unless (gnus-summary-goto-subject article)
11311       (error "No such article: %d" article))
11312     (gnus-summary-position-point)
11313     ;; If all commands are to be bunched up on one line, we collect
11314     ;; them here.
11315     (unless gnus-view-pseudos-separately
11316       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11317             files action)
11318         (while ps
11319           (setq action (cdr (assq 'action (car ps))))
11320           (setq files (list (cdr (assq 'name (car ps)))))
11321           (while (and ps (cdr ps)
11322                       (string= (or action "1")
11323                                (or (cdr (assq 'action (cadr ps))) "2")))
11324             (push (cdr (assq 'name (cadr ps))) files)
11325             (setcdr ps (cddr ps)))
11326           (when files
11327             (when (not (string-match "%s" action))
11328               (push " " files))
11329             (push " " files)
11330             (when (assq 'execute (car ps))
11331               (setcdr (assq 'execute (car ps))
11332                       (funcall (if (string-match "%s" action)
11333                                    'format 'concat)
11334                                action
11335                                (mapconcat
11336                                 (lambda (f)
11337                                   (if (equal f " ")
11338                                       f
11339                                     (shell-quote-argument f)))
11340                                 files " ")))))
11341           (setq ps (cdr ps)))))
11342     (if (and gnus-view-pseudos (not not-view))
11343         (while pslist
11344           (when (assq 'execute (car pslist))
11345             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11346                                   (eq gnus-view-pseudos 'not-confirm)))
11347           (setq pslist (cdr pslist)))
11348       (save-excursion
11349         (while pslist
11350           (setq after-article (or (cdr (assq 'article (car pslist)))
11351                                   (gnus-summary-article-number)))
11352           (gnus-summary-goto-subject after-article)
11353           (forward-line 1)
11354           (setq b (point))
11355           (insert "    " (file-name-nondirectory
11356                           (cdr (assq 'name (car pslist))))
11357                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11358           (setq e (point))
11359           (forward-line -1)             ; back to `b'
11360           (gnus-add-text-properties
11361            b (1- e) (list 'gnus-number gnus-reffed-article-number
11362                           gnus-mouse-face-prop gnus-mouse-face))
11363           (gnus-data-enter
11364            after-article gnus-reffed-article-number
11365            gnus-unread-mark b (car pslist) 0 (- e b))
11366           (setq gnus-newsgroup-unreads
11367                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11368                                          gnus-reffed-article-number))
11369           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11370           (setq pslist (cdr pslist)))))))
11371
11372 (defun gnus-pseudos< (p1 p2)
11373   (let ((c1 (cdr (assq 'action p1)))
11374         (c2 (cdr (assq 'action p2))))
11375     (and c1 c2 (string< c1 c2))))
11376
11377 (defun gnus-request-pseudo-article (props)
11378   (cond ((assq 'execute props)
11379          (gnus-execute-command (cdr (assq 'execute props)))))
11380   (let ((gnus-current-article (gnus-summary-article-number)))
11381     (gnus-run-hooks 'gnus-mark-article-hook)))
11382
11383 (defun gnus-execute-command (command &optional automatic)
11384   (save-excursion
11385     (gnus-article-setup-buffer)
11386     (set-buffer gnus-article-buffer)
11387     (setq buffer-read-only nil)
11388     (let ((command (if automatic command
11389                      (read-string "Command: " (cons command 0)))))
11390       (erase-buffer)
11391       (insert "$ " command "\n\n")
11392       (if gnus-view-pseudo-asynchronously
11393           (start-process "gnus-execute" (current-buffer) shell-file-name
11394                          shell-command-switch command)
11395         (call-process shell-file-name nil t nil
11396                       shell-command-switch command)))))
11397
11398 ;; Summary kill commands.
11399
11400 (defun gnus-summary-edit-global-kill (article)
11401   "Edit the \"global\" kill file."
11402   (interactive (list (gnus-summary-article-number)))
11403   (gnus-group-edit-global-kill article))
11404
11405 (defun gnus-summary-edit-local-kill ()
11406   "Edit a local kill file applied to the current newsgroup."
11407   (interactive)
11408   (setq gnus-current-headers (gnus-summary-article-header))
11409   (gnus-group-edit-local-kill
11410    (gnus-summary-article-number) gnus-newsgroup-name))
11411
11412 ;;; Header reading.
11413
11414 (defun gnus-read-header (id &optional header)
11415   "Read the headers of article ID and enter them into the Gnus system."
11416   (let ((group gnus-newsgroup-name)
11417         (gnus-override-method
11418          (or
11419           gnus-override-method
11420           (and (gnus-news-group-p gnus-newsgroup-name)
11421                (car (gnus-refer-article-methods)))))
11422         where)
11423     ;; First we check to see whether the header in question is already
11424     ;; fetched.
11425     (if (stringp id)
11426         ;; This is a Message-ID.
11427         (setq header (or header (gnus-id-to-header id)))
11428       ;; This is an article number.
11429       (setq header (or header (gnus-summary-article-header id))))
11430     (if (and header
11431              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11432         ;; We have found the header.
11433         header
11434       ;; We have to really fetch the header to this article.
11435       (save-excursion
11436         (set-buffer nntp-server-buffer)
11437         (when (setq where (gnus-request-head id group))
11438           (nnheader-fold-continuation-lines)
11439           (goto-char (point-max))
11440           (insert ".\n")
11441           (goto-char (point-min))
11442           (insert "211 ")
11443           (princ (cond
11444                   ((numberp id) id)
11445                   ((cdr where) (cdr where))
11446                   (header (mail-header-number header))
11447                   (t gnus-reffed-article-number))
11448                  (current-buffer))
11449           (insert " Article retrieved.\n"))
11450         (if (or (not where)
11451                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11452             ()                          ; Malformed head.
11453           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11454             (when (and (stringp id)
11455                        (not (string= (gnus-group-real-name group)
11456                                      (car where))))
11457               ;; If we fetched by Message-ID and the article came
11458               ;; from a different group, we fudge some bogus article
11459               ;; numbers for this article.
11460               (mail-header-set-number header gnus-reffed-article-number))
11461             (save-excursion
11462               (set-buffer gnus-summary-buffer)
11463               (decf gnus-reffed-article-number)
11464               (gnus-remove-header (mail-header-number header))
11465               (push header gnus-newsgroup-headers)
11466               (setq gnus-current-headers header)
11467               (push (mail-header-number header) gnus-newsgroup-limit)))
11468           header)))))
11469
11470 (defun gnus-remove-header (number)
11471   "Remove header NUMBER from `gnus-newsgroup-headers'."
11472   (if (and gnus-newsgroup-headers
11473            (= number (mail-header-number (car gnus-newsgroup-headers))))
11474       (pop gnus-newsgroup-headers)
11475     (let ((headers gnus-newsgroup-headers))
11476       (while (and (cdr headers)
11477                   (not (= number (mail-header-number (cadr headers)))))
11478         (pop headers))
11479       (when (cdr headers)
11480         (setcdr headers (cddr headers))))))
11481
11482 ;;;
11483 ;;; summary highlights
11484 ;;;
11485
11486 (defun gnus-highlight-selected-summary ()
11487   "Highlight selected article in summary buffer."
11488   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11489   (when gnus-summary-selected-face
11490     (save-excursion
11491       (let* ((beg (point-at-bol))
11492              (end (point-at-eol))
11493              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11494              (from (if (get-text-property beg gnus-mouse-face-prop)
11495                        beg
11496                      (or (next-single-property-change
11497                           beg gnus-mouse-face-prop nil end)
11498                          beg)))
11499              (to
11500               (if (= from end)
11501                   (- from 2)
11502                 (or (next-single-property-change
11503                      from gnus-mouse-face-prop nil end)
11504                     end))))
11505         ;; If no mouse-face prop on line we will have to = from = end,
11506         ;; so we highlight the entire line instead.
11507         (when (= (+ to 2) from)
11508           (setq from beg)
11509           (setq to end))
11510         (if gnus-newsgroup-selected-overlay
11511             ;; Move old overlay.
11512             (gnus-move-overlay
11513              gnus-newsgroup-selected-overlay from to (current-buffer))
11514           ;; Create new overlay.
11515           (gnus-overlay-put
11516            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11517            'face gnus-summary-selected-face))))))
11518
11519 (defvar gnus-summary-highlight-line-cached nil)
11520 (defvar gnus-summary-highlight-line-trigger nil)
11521
11522 (defun gnus-summary-highlight-line-0 ()
11523   (if (and (eq gnus-summary-highlight-line-trigger
11524                gnus-summary-highlight)
11525            gnus-summary-highlight-line-cached)
11526       gnus-summary-highlight-line-cached
11527     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11528           gnus-summary-highlight-line-cached
11529           (let* ((cond (list 'cond))
11530                  (c cond)
11531                  (list gnus-summary-highlight))
11532             (while list
11533               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11534                               nil))
11535               (setq c (cdr c)
11536                     list (cdr list)))
11537             (gnus-byte-compile (list 'lambda nil cond))))))
11538
11539 (defun gnus-summary-highlight-line ()
11540   "Highlight current line according to `gnus-summary-highlight'."
11541   (let* ((beg (point-at-bol))
11542          (article (or (gnus-summary-article-number) gnus-current-article))
11543          (score (or (cdr (assq article
11544                                gnus-newsgroup-scored))
11545                     gnus-summary-default-score 0))
11546          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11547          (inhibit-read-only t)
11548          (default gnus-summary-default-score)
11549          (default-high gnus-summary-default-high-score)
11550          (default-low gnus-summary-default-low-score)
11551          (uncached (and gnus-summary-use-undownloaded-faces
11552                         (memq article gnus-newsgroup-undownloaded)
11553                         (not (memq article gnus-newsgroup-cached)))))
11554     (let ((face (funcall (gnus-summary-highlight-line-0))))
11555       (unless (eq face (get-text-property beg 'face))
11556         (gnus-put-text-property-excluding-characters-with-faces
11557          beg (point-at-eol) 'face
11558          (setq face (if (boundp face) (symbol-value face) face)))
11559         (when gnus-summary-highlight-line-function
11560           (funcall gnus-summary-highlight-line-function article face))))))
11561
11562 (defun gnus-update-read-articles (group unread &optional compute)
11563   "Update the list of read articles in GROUP.
11564 UNREAD is a sorted list."
11565   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11566         (info (gnus-get-info group))
11567         (prev 1)
11568         read)
11569     (if (or (not info) (not active))
11570         ;; There is no info on this group if it was, in fact,
11571         ;; killed.  Gnus stores no information on killed groups, so
11572         ;; there's nothing to be done.
11573         ;; One could store the information somewhere temporarily,
11574         ;; perhaps...  Hmmm...
11575         ()
11576       ;; Remove any negative articles numbers.
11577       (while (and unread (< (car unread) 0))
11578         (setq unread (cdr unread)))
11579       ;; Remove any expired article numbers
11580       (while (and unread (< (car unread) (car active)))
11581         (setq unread (cdr unread)))
11582       ;; Compute the ranges of read articles by looking at the list of
11583       ;; unread articles.
11584       (while unread
11585         (when (/= (car unread) prev)
11586           (push (if (= prev (1- (car unread))) prev
11587                   (cons prev (1- (car unread))))
11588                 read))
11589         (setq prev (1+ (car unread)))
11590         (setq unread (cdr unread)))
11591       (when (<= prev (cdr active))
11592         (push (cons prev (cdr active)) read))
11593       (setq read (if (> (length read) 1) (nreverse read) read))
11594       (if compute
11595           read
11596         (save-excursion
11597           (let (setmarkundo)
11598             ;; Propagate the read marks to the backend.
11599             (when (gnus-check-backend-function 'request-set-mark group)
11600               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11601                     (add (gnus-remove-from-range read (gnus-info-read info))))
11602                 (when (or add del)
11603                   (unless (gnus-check-group group)
11604                     (error "Can't open server for %s" group))
11605                   (gnus-request-set-mark
11606                    group (delq nil (list (if add (list add 'add '(read)))
11607                                          (if del (list del 'del '(read))))))
11608                   (setq setmarkundo
11609                         `(gnus-request-set-mark
11610                           ,group
11611                           ',(delq nil (list
11612                                        (if del (list del 'add '(read)))
11613                                        (if add (list add 'del '(read))))))))))
11614             (set-buffer gnus-group-buffer)
11615             (gnus-undo-register
11616               `(progn
11617                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11618                  (gnus-info-set-read ',info ',(gnus-info-read info))
11619                  (gnus-get-unread-articles-in-group ',info
11620                                                     (gnus-active ,group))
11621                  (gnus-group-update-group ,group t)
11622                  ,setmarkundo))))
11623         ;; Enter this list into the group info.
11624         (gnus-info-set-read info read)
11625         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11626         (gnus-get-unread-articles-in-group info (gnus-active group))
11627         t))))
11628
11629 (defun gnus-offer-save-summaries ()
11630   "Offer to save all active summary buffers."
11631   (let (buffers)
11632     ;; Go through all buffers and find all summaries.
11633     (dolist (buffer (buffer-list))
11634       (when (and (setq buffer (buffer-name buffer))
11635                  (string-match "Summary" buffer)
11636                  (with-current-buffer buffer
11637                    ;; We check that this is, indeed, a summary buffer.
11638                    (and (eq major-mode 'gnus-summary-mode)
11639                         ;; Also make sure this isn't bogus.
11640                         gnus-newsgroup-prepared
11641                         ;; Also make sure that this isn't a
11642                         ;; dead summary buffer.
11643                         (not gnus-dead-summary-mode))))
11644         (push buffer buffers)))
11645     ;; Go through all these summary buffers and offer to save them.
11646     (when buffers
11647       (save-excursion
11648         (map-y-or-n-p
11649          "Update summary buffer %s? "
11650          (lambda (buf)
11651            (switch-to-buffer buf)
11652            (gnus-summary-exit))
11653          buffers)))))
11654
11655 (defun gnus-summary-setup-default-charset ()
11656   "Setup newsgroup default charset."
11657   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11658       (setq gnus-newsgroup-charset nil)
11659     (let* ((ignored-charsets
11660             (or gnus-newsgroup-ephemeral-ignored-charsets
11661                 (append
11662                  (and gnus-newsgroup-name
11663                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11664                  gnus-newsgroup-ignored-charsets))))
11665       (setq gnus-newsgroup-charset
11666             (or gnus-newsgroup-ephemeral-charset
11667                 (and gnus-newsgroup-name
11668                      (gnus-parameter-charset gnus-newsgroup-name))
11669                 gnus-default-charset))
11670       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11671            ignored-charsets))))
11672
11673 ;;;
11674 ;;; Mime Commands
11675 ;;;
11676
11677 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11678   "Display the current article buffer fully MIME-buttonized.
11679 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11680 treated as multipart/mixed."
11681   (interactive "P")
11682   (require 'gnus-art)
11683   (let ((gnus-unbuttonized-mime-types nil)
11684         (gnus-mime-display-multipart-as-mixed show-all-parts))
11685     (gnus-summary-show-article)))
11686
11687 (defun gnus-summary-repair-multipart (article)
11688   "Add a Content-Type header to a multipart article without one."
11689   (interactive (list (gnus-summary-article-number)))
11690   (gnus-with-article article
11691     (message-narrow-to-head)
11692     (message-remove-header "Mime-Version")
11693     (goto-char (point-max))
11694     (insert "Mime-Version: 1.0\n")
11695     (widen)
11696     (when (search-forward "\n--" nil t)
11697       (let ((separator (buffer-substring (point) (point-at-eol))))
11698         (message-narrow-to-head)
11699         (message-remove-header "Content-Type")
11700         (goto-char (point-max))
11701         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11702                         separator))
11703         (widen))))
11704   (let (gnus-mark-article-hook)
11705     (gnus-summary-select-article t t nil article)))
11706
11707 (defun gnus-summary-toggle-display-buttonized ()
11708   "Toggle the buttonizing of the article buffer."
11709   (interactive)
11710   (require 'gnus-art)
11711   (if (setq gnus-inhibit-mime-unbuttonizing
11712             (not gnus-inhibit-mime-unbuttonizing))
11713       (let ((gnus-unbuttonized-mime-types nil))
11714         (gnus-summary-show-article))
11715     (gnus-summary-show-article)))
11716
11717 ;;;
11718 ;;; Generic summary marking commands
11719 ;;;
11720
11721 (defvar gnus-summary-marking-alist
11722   '((read gnus-del-mark "d")
11723     (unread gnus-unread-mark "u")
11724     (ticked gnus-ticked-mark "!")
11725     (dormant gnus-dormant-mark "?")
11726     (expirable gnus-expirable-mark "e"))
11727   "An alist of names/marks/keystrokes.")
11728
11729 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11730 (defvar gnus-summary-mark-map)
11731
11732 (defun gnus-summary-make-all-marking-commands ()
11733   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11734   (dolist (elem gnus-summary-marking-alist)
11735     (apply 'gnus-summary-make-marking-command elem)))
11736
11737 (defun gnus-summary-make-marking-command (name mark keystroke)
11738   (let ((map (make-sparse-keymap)))
11739     (define-key gnus-summary-generic-mark-map keystroke map)
11740     (dolist (lway `((next "next" next nil "n")
11741                     (next-unread "next unread" next t "N")
11742                     (prev "previous" prev nil "p")
11743                     (prev-unread "previous unread" prev t "P")
11744                     (nomove "" nil nil ,keystroke)))
11745       (let ((func (gnus-summary-make-marking-command-1
11746                    mark (car lway) lway name)))
11747         (setq func (eval func))
11748         (define-key map (nth 4 lway) func)))))
11749
11750 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11751   `(defun ,(intern
11752             (format "gnus-summary-put-mark-as-%s%s"
11753                     name (if (eq way 'nomove)
11754                              ""
11755                            (concat "-" (symbol-name way)))))
11756      (n)
11757      ,(format
11758        "Mark the current article as %s%s.
11759 If N, the prefix, then repeat N times.
11760 If N is negative, move in reverse order.
11761 The difference between N and the actual number of articles marked is
11762 returned."
11763        name (cadr lway))
11764      (interactive "p")
11765      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11766
11767 (defun gnus-summary-generic-mark (n mark move unread)
11768   "Mark N articles with MARK."
11769   (unless (eq major-mode 'gnus-summary-mode)
11770     (error "This command can only be used in the summary buffer"))
11771   (gnus-summary-show-thread)
11772   (let ((nummove
11773          (cond
11774           ((eq move 'next) 1)
11775           ((eq move 'prev) -1)
11776           (t 0))))
11777     (if (zerop nummove)
11778         (setq n 1)
11779       (when (< n 0)
11780         (setq n (abs n)
11781               nummove (* -1 nummove))))
11782     (while (and (> n 0)
11783                 (gnus-summary-mark-article nil mark)
11784                 (zerop (gnus-summary-next-subject nummove unread t)))
11785       (setq n (1- n)))
11786     (when (/= 0 n)
11787       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11788     (gnus-summary-recenter)
11789     (gnus-summary-position-point)
11790     (gnus-set-mode-line 'summary)
11791     n))
11792
11793 (defun gnus-summary-insert-articles (articles)
11794   (when (setq articles
11795               (gnus-sorted-difference articles
11796                                       (mapcar (lambda (h)
11797                                                 (mail-header-number h))
11798                                               gnus-newsgroup-headers)))
11799     (setq gnus-newsgroup-headers
11800           (gnus-merge 'list
11801                       gnus-newsgroup-headers
11802                       (gnus-fetch-headers articles)
11803                       'gnus-article-sort-by-number))
11804     ;; Suppress duplicates?
11805     (when gnus-suppress-duplicates
11806       (gnus-dup-suppress-articles))
11807
11808     ;; We might want to build some more threads first.
11809     (when (and gnus-fetch-old-headers
11810                (eq gnus-headers-retrieved-by 'nov))
11811       (if (eq gnus-fetch-old-headers 'invisible)
11812           (gnus-build-all-threads)
11813         (gnus-build-old-threads)))
11814     ;; Let the Gnus agent mark articles as read.
11815     (when gnus-agent
11816       (gnus-agent-get-undownloaded-list))
11817     ;; Remove list identifiers from subject
11818     (when gnus-list-identifiers
11819       (gnus-summary-remove-list-identifiers))
11820     ;; First and last article in this newsgroup.
11821     (when gnus-newsgroup-headers
11822       (setq gnus-newsgroup-begin
11823             (mail-header-number (car gnus-newsgroup-headers))
11824             gnus-newsgroup-end
11825             (mail-header-number
11826              (gnus-last-element gnus-newsgroup-headers))))
11827     (when gnus-use-scoring
11828       (gnus-possibly-score-headers))))
11829
11830 (defun gnus-summary-insert-old-articles (&optional all)
11831   "Insert all old articles in this group.
11832 If ALL is non-nil, already read articles become readable.
11833 If ALL is a number, fetch this number of articles."
11834   (interactive "P")
11835   (prog1
11836       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11837             older len)
11838         (setq older
11839               ;; Some nntp servers lie about their active range.  When
11840               ;; this happens, the active range can be in the millions.
11841               ;; Use a compressed range to avoid creating a huge list.
11842               (gnus-range-difference (list gnus-newsgroup-active) old))
11843         (setq len (gnus-range-length older))
11844         (cond
11845          ((null older) nil)
11846          ((numberp all)
11847           (if (< all len)
11848               (let ((older-range (nreverse older)))
11849                 (setq older nil)
11850
11851                 (while (> all 0)
11852                   (let* ((r (pop older-range))
11853                          (min (if (numberp r) r (car r)))
11854                          (max (if (numberp r) r (cdr r))))
11855                     (while (and (<= min max)
11856                                 (> all 0))
11857                       (push max older)
11858                       (setq all (1- all)
11859                             max (1- max))))))
11860             (setq older (gnus-uncompress-range older))))
11861          (all
11862           (setq older (gnus-uncompress-range older)))
11863          (t
11864           (when (and (numberp gnus-large-newsgroup)
11865                    (> len gnus-large-newsgroup))
11866               (let* ((cursor-in-echo-area nil)
11867                      (initial (gnus-parameter-large-newsgroup-initial
11868                                gnus-newsgroup-name))
11869                      (input
11870                       (read-string
11871                        (format
11872                         "How many articles from %s (%s %d): "
11873                         (gnus-limit-string
11874                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11875                         (if initial "max" "default")
11876                         len)
11877                        (if initial
11878                            (cons (number-to-string initial)
11879                                  0)))))
11880                 (unless (string-match "^[ \t]*$" input)
11881                   (setq all (string-to-number input))
11882                   (if (< all len)
11883                       (let ((older-range (nreverse older)))
11884                         (setq older nil)
11885
11886                         (while (> all 0)
11887                           (let* ((r (pop older-range))
11888                                  (min (if (numberp r) r (car r)))
11889                                  (max (if (numberp r) r (cdr r))))
11890                             (while (and (<= min max)
11891                                         (> all 0))
11892                               (push max older)
11893                               (setq all (1- all)
11894                                     max (1- max))))))))))
11895           (setq older (gnus-uncompress-range older))))
11896         (if (not older)
11897             (message "No old news.")
11898           (gnus-summary-insert-articles older)
11899           (gnus-summary-limit (gnus-sorted-nunion old older))))
11900     (gnus-summary-position-point)))
11901
11902 (defun gnus-summary-insert-new-articles ()
11903   "Insert all new articles in this group."
11904   (interactive)
11905   (prog1
11906       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11907             (old-active gnus-newsgroup-active)
11908             (nnmail-fetched-sources (list t))
11909             i new)
11910         (setq gnus-newsgroup-active
11911               (gnus-activate-group gnus-newsgroup-name 'scan))
11912         (setq i (cdr gnus-newsgroup-active))
11913         (while (> i (cdr old-active))
11914           (push i new)
11915           (decf i))
11916         (if (not new)
11917             (message "No gnus is bad news")
11918           (gnus-summary-insert-articles new)
11919           (setq gnus-newsgroup-unreads
11920                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11921           (gnus-summary-limit (gnus-sorted-nunion old new))))
11922     (gnus-summary-position-point)))
11923
11924 (gnus-summary-make-all-marking-commands)
11925
11926 (gnus-ems-redefine)
11927
11928 (provide 'gnus-sum)
11929
11930 (run-hooks 'gnus-sum-load-hook)
11931
11932 ;; Local Variables:
11933 ;; coding: iso-8859-1
11934 ;; End:
11935
11936 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
11937 ;;; gnus-sum.el ends here