(gnus-summary-mode-map, gnus-summary-thread-map): Remove `M-C-e' and `T
[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, 2006, 2007 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-mode))
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 'gmm-utils)
42 (require 'mm-decode)
43 (require 'nnoo)
44
45 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
46 (autoload 'gnus-cache-write-active "gnus-cache")
47 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
48 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
49 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
50 (autoload 'mm-uu-dissect "mm-uu")
51 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
52   "Deuglify broken Outlook (Express) articles and redisplay."
53   t)
54 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
55 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
56 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
57
58 (defcustom gnus-kill-summary-on-exit t
59   "*If non-nil, kill the summary buffer when you exit from it.
60 If nil, the summary will become a \"*Dead Summary*\" buffer, and
61 it will be killed sometime later."
62   :group 'gnus-summary-exit
63   :type 'boolean)
64
65 (defcustom gnus-summary-next-group-on-exit t
66   "If non-nil, go to the next unread newsgroup on summary exit.
67 See `gnus-group-goto-unread'."
68   :link '(custom-manual "(gnus)Group Maneuvering")
69   :group 'gnus-summary-exit
70   :version "23.0" ;; No Gnus
71   :type 'boolean)
72
73 (defcustom gnus-fetch-old-headers nil
74   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
75 If an unread article in the group refers to an older, already
76 read (or just marked as read) article, the old article will not
77 normally be displayed in the Summary buffer.  If this variable is
78 t, Gnus will attempt to grab the headers to the old articles, and
79 thereby build complete threads.  If it has the value `some', all
80 old headers will be fetched but only enough headers to connect
81 otherwise loose threads will be displayed.  This variable can
82 also be a number.  In that case, no more than that number of old
83 headers will be fetched.  If it has the value `invisible', all
84 old headers will be fetched, but none will be displayed.
85
86 The server has to support NOV for any of this to work.
87
88 This feature can seriously impact performance it ignores all
89 locally cached header entries."
90   :group 'gnus-thread
91   :type '(choice (const :tag "off" nil)
92                  (const :tag "on" t)
93                  (const some)
94                  (const invisible)
95                  number
96                  (sexp :menu-tag "other" t)))
97
98 (defcustom gnus-refer-thread-limit 500
99   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
100 If t, fetch all the available old headers."
101   :group 'gnus-thread
102   :type '(choice number
103                  (sexp :menu-tag "other" t)))
104
105 (defcustom gnus-summary-make-false-root 'adopt
106   "*nil means that Gnus won't gather loose threads.
107 If the root of a thread has expired or been read in a previous
108 session, the information necessary to build a complete thread has been
109 lost.  Instead of having many small sub-threads from this original thread
110 scattered all over the summary buffer, Gnus can gather them.
111
112 If non-nil, Gnus will try to gather all loose sub-threads from an
113 original thread into one large thread.
114
115 If this variable is non-nil, it should be one of `none', `adopt',
116 `dummy' or `empty'.
117
118 If this variable is `none', Gnus will not make a false root, but just
119 present the sub-threads after another.
120 If this variable is `dummy', Gnus will create a dummy root that will
121 have all the sub-threads as children.
122 If this variable is `adopt', Gnus will make one of the \"children\"
123 the parent and mark all the step-children as such.
124 If this variable is `empty', the \"children\" are printed with empty
125 subject fields.  (Or rather, they will be printed with a string
126 given by the `gnus-summary-same-subject' variable.)"
127   :group 'gnus-thread
128   :type '(choice (const :tag "off" nil)
129                  (const none)
130                  (const dummy)
131                  (const adopt)
132                  (const empty)))
133
134 (defcustom gnus-summary-make-false-root-always nil
135   "Always make a false dummy root."
136   :version "22.1"
137   :group 'gnus-thread
138   :type 'boolean)
139
140 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
141   "*A regexp to match subjects to be excluded from loose thread gathering.
142 As loose thread gathering is done on subjects only, that means that
143 there can be many false gatherings performed.  By rooting out certain
144 common subjects, gathering might become saner."
145   :group 'gnus-thread
146   :type 'regexp)
147
148 (defcustom gnus-summary-gather-subject-limit nil
149   "*Maximum length of subject comparisons when gathering loose threads.
150 Use nil to compare full subjects.  Setting this variable to a low
151 number will help gather threads that have been corrupted by
152 newsreaders chopping off subject lines, but it might also mean that
153 unrelated articles that have subject that happen to begin with the
154 same few characters will be incorrectly gathered.
155
156 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
157 comparing subjects."
158   :group 'gnus-thread
159   :type '(choice (const :tag "off" nil)
160                  (const fuzzy)
161                  (sexp :menu-tag "on" t)))
162
163 (defcustom gnus-simplify-subject-functions nil
164   "List of functions taking a string argument that simplify subjects.
165 The functions are applied recursively.
166
167 Useful functions to put in this list include:
168 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
169 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
170   :group 'gnus-thread
171   :type '(repeat function))
172
173 (defcustom gnus-simplify-ignored-prefixes nil
174   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
175   :group 'gnus-thread
176   :type '(choice (const :tag "off" nil)
177                  regexp))
178
179 (defcustom gnus-build-sparse-threads nil
180   "*If non-nil, fill in the gaps in threads.
181 If `some', only fill in the gaps that are needed to tie loose threads
182 together.  If `more', fill in all leaf nodes that Gnus can find.  If
183 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
184   :group 'gnus-thread
185   :type '(choice (const :tag "off" nil)
186                  (const some)
187                  (const more)
188                  (sexp :menu-tag "all" t)))
189
190 (defcustom gnus-summary-thread-gathering-function
191   'gnus-gather-threads-by-subject
192   "*Function used for gathering loose threads.
193 There are two pre-defined functions: `gnus-gather-threads-by-subject',
194 which only takes Subjects into consideration; and
195 `gnus-gather-threads-by-references', which compared the References
196 headers of the articles to find matches."
197   :group 'gnus-thread
198   :type '(radio (function-item gnus-gather-threads-by-subject)
199                 (function-item gnus-gather-threads-by-references)
200                 (function :tag "other")))
201
202 (defcustom gnus-summary-same-subject ""
203   "*String indicating that the current article has the same subject as the previous.
204 This variable will only be used if the value of
205 `gnus-summary-make-false-root' is `empty'."
206   :group 'gnus-summary-format
207   :type 'string)
208
209 (defcustom gnus-summary-goto-unread t
210   "*If t, many commands will go to the next unread article.
211 This applies to marking commands as well as other commands that
212 \"naturally\" select the next article, like, for instance, `SPC' at
213 the end of an article.
214
215 If nil, the marking commands do NOT go to the next unread article
216 \(they go to the next article instead).  If `never', commands that
217 usually go to the next unread article, will go to the next article,
218 whether it is read or not."
219   :group 'gnus-summary-marks
220   :link '(custom-manual "(gnus)Setting Marks")
221   :type '(choice (const :tag "off" nil)
222                  (const never)
223                  (sexp :menu-tag "on" t)))
224
225 (defcustom gnus-summary-default-score 0
226   "*Default article score level.
227 All scores generated by the score files will be added to this score.
228 If this variable is nil, scoring will be disabled."
229   :group 'gnus-score-default
230   :type '(choice (const :tag "disable")
231                  integer))
232
233 (defcustom gnus-summary-default-high-score 0
234   "*Default threshold for a high scored article.
235 An article will be highlighted as high scored if its score is greater
236 than this score."
237   :version "22.1"
238   :group 'gnus-score-default
239   :type 'integer)
240
241 (defcustom gnus-summary-default-low-score 0
242   "*Default threshold for a low scored article.
243 An article will be highlighted as low scored if its score is smaller
244 than this score."
245   :version "22.1"
246   :group 'gnus-score-default
247   :type 'integer)
248
249 (defcustom gnus-summary-zcore-fuzz 0
250   "*Fuzziness factor for the zcore in the summary buffer.
251 Articles with scores closer than this to `gnus-summary-default-score'
252 will not be marked."
253   :group 'gnus-summary-format
254   :type 'integer)
255
256 (defcustom gnus-simplify-subject-fuzzy-regexp nil
257   "*Strings to be removed when doing fuzzy matches.
258 This can either be a regular expression or list of regular expressions
259 that will be removed from subject strings if fuzzy subject
260 simplification is selected."
261   :group 'gnus-thread
262   :type '(repeat regexp))
263
264 (defcustom gnus-show-threads t
265   "*If non-nil, display threads in summary mode."
266   :group 'gnus-thread
267   :type 'boolean)
268
269 (defcustom gnus-thread-hide-subtree nil
270   "*If non-nil, hide all threads initially.
271 This can be a predicate specifier which says which threads to hide.
272 If threads are hidden, you have to run the command
273 `gnus-summary-show-thread' by hand or select an article."
274   :group 'gnus-thread
275   :type '(radio (sexp :format "Non-nil\n"
276                       :match (lambda (widget value)
277                                (not (or (consp value) (functionp value))))
278                       :value t)
279                 (const nil)
280                 (sexp :tag "Predicate specifier")))
281
282 (defcustom gnus-thread-hide-killed t
283   "*If non-nil, hide killed threads automatically."
284   :group 'gnus-thread
285   :type 'boolean)
286
287 (defcustom gnus-thread-ignore-subject t
288   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
289 If nil, articles that have different subjects from their parents will
290 start separate threads."
291   :group 'gnus-thread
292   :type 'boolean)
293
294 (defcustom gnus-thread-operation-ignore-subject t
295   "*If non-nil, subjects will be ignored when doing thread commands.
296 This affects commands like `gnus-summary-kill-thread' and
297 `gnus-summary-lower-thread'.
298
299 If this variable is nil, articles in the same thread with different
300 subjects will not be included in the operation in question.  If this
301 variable is `fuzzy', only articles that have subjects that are fuzzily
302 equal will be included."
303   :group 'gnus-thread
304   :type '(choice (const :tag "off" nil)
305                  (const fuzzy)
306                  (sexp :tag "on" t)))
307
308 (defcustom gnus-thread-indent-level 4
309   "*Number that says how much each sub-thread should be indented."
310   :group 'gnus-thread
311   :type 'integer)
312
313 (defcustom gnus-auto-extend-newsgroup t
314   "*If non-nil, extend newsgroup forward and backward when requested."
315   :group 'gnus-summary-choose
316   :type 'boolean)
317
318 (defcustom gnus-auto-select-first t
319   "If non-nil, select an article on group entry.
320 An article is selected automatically when entering a group
321 e.g. with \\<gnus-group-mode-map>\\[gnus-group-read-group], or via `gnus-summary-next-page' or
322 `gnus-summary-catchup-and-goto-next-group'.
323
324 Which article is selected is controlled by the variable
325 `gnus-auto-select-subject'.
326
327 If you want to prevent automatic selection of articles in some
328 newsgroups, set the variable to nil in `gnus-select-group-hook'."
329   ;; Commands include...
330   ;; \\<gnus-group-mode-map>\\[gnus-group-read-group]
331   ;; \\<gnus-summary-mode-map>\\[gnus-summary-next-page]
332   ;; \\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]
333   :group 'gnus-group-select
334   :type '(choice (const :tag "none" nil)
335                  (sexp :menu-tag "first" t)))
336
337 (defcustom gnus-auto-select-subject 'unread
338   "*Says what subject to place under point when entering a group.
339
340 This variable can either be the symbols `first' (place point on the
341 first subject), `unread' (place point on the subject line of the first
342 unread article), `best' (place point on the subject line of the
343 higest-scored article), `unseen' (place point on the subject line of
344 the first unseen article), `unseen-or-unread' (place point on the subject
345 line of the first unseen article or, if all article have been seen, on the
346 subject line of the first unread article), or a function to be called to
347 place point on some subject line."
348   :version "22.1"
349   :group 'gnus-group-select
350   :type '(choice (const best)
351                  (const unread)
352                  (const first)
353                  (const unseen)
354                  (const unseen-or-unread)))
355
356 (defcustom gnus-auto-select-next t
357   "*If non-nil, offer to go to the next group from the end of the previous.
358 If the value is t and the next newsgroup is empty, Gnus will exit
359 summary mode and go back to group mode.  If the value is neither nil
360 nor t, Gnus will select the following unread newsgroup.  In
361 particular, if the value is the symbol `quietly', the next unread
362 newsgroup will be selected without any confirmation, and if it is
363 `almost-quietly', the next group will be selected without any
364 confirmation if you are located on the last article in the group.
365 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
366 will go to the next group without confirmation."
367   :group 'gnus-summary-maneuvering
368   :type '(choice (const :tag "off" nil)
369                  (const quietly)
370                  (const almost-quietly)
371                  (const slightly-quietly)
372                  (sexp :menu-tag "on" t)))
373
374 (defcustom gnus-auto-select-same nil
375   "*If non-nil, select the next article with the same subject.
376 If there are no more articles with the same subject, go to
377 the first unread article."
378   :group 'gnus-summary-maneuvering
379   :type 'boolean)
380
381 (defcustom gnus-auto-select-on-ephemeral-exit 'next-noselect
382   "What article should be selected after exiting an ephemeral group.
383 Valid values include:
384
385 `next'
386   Select the next article.
387 `next-unread'
388   Select the next unread article.
389 `next-noselect'
390   Move the cursor to the next article.  This is the default.
391 `next-unread-noselect'
392   Move the cursor to the next unread article.
393
394 If it has any other value or there is no next (unread) article, the
395 article selected before entering to the ephemeral group will appear."
396   :version "23.0" ;; No Gnus
397   :group 'gnus-summary-maneuvering
398   :type '(choice :format "%{%t%}:\n %[Value Menu%] %v"
399                  (const next) (const next-unread)
400                  (const next-noselect) (const next-unread-noselect)
401                  (sexp :tag "other" :value nil)))
402
403 (defcustom gnus-auto-goto-ignores 'unfetched
404   "*Says how to handle unfetched articles when maneuvering.
405
406 This variable can either be the symbols nil (maneuver to any
407 article), `undownloaded' (maneuvering while unplugged ignores articles
408 that have not been fetched), `always-undownloaded' (maneuvering always
409 ignores articles that have not been fetched), `unfetched' (maneuvering
410 ignores articles whose headers have not been fetched).
411
412 NOTE: The list of unfetched articles will always be nil when plugged
413 and, when unplugged, a subset of the undownloaded article list."
414   :version "22.1"
415   :group 'gnus-summary-maneuvering
416   :type '(choice (const :tag "None" nil)
417                  (const :tag "Undownloaded when unplugged" undownloaded)
418                  (const :tag "Undownloaded" always-undownloaded)
419                  (const :tag "Unfetched" unfetched)))
420
421 (defcustom gnus-summary-check-current nil
422   "*If non-nil, consider the current article when moving.
423 The \"unread\" movement commands will stay on the same line if the
424 current article is unread."
425   :group 'gnus-summary-maneuvering
426   :type 'boolean)
427
428 (defcustom gnus-auto-center-summary 2
429   "*If non-nil, always center the current summary buffer.
430 In particular, if `vertical' do only vertical recentering.  If non-nil
431 and non-`vertical', do both horizontal and vertical recentering."
432   :group 'gnus-summary-maneuvering
433   :type '(choice (const :tag "none" nil)
434                  (const vertical)
435                  (integer :tag "height")
436                  (sexp :menu-tag "both" t)))
437
438 (defvar gnus-auto-center-group t
439   "*If non-nil, always center the group buffer.")
440
441 (defcustom gnus-show-all-headers nil
442   "*If non-nil, don't hide any headers."
443   :group 'gnus-article-hiding
444   :group 'gnus-article-headers
445   :type 'boolean)
446
447 (defcustom gnus-summary-ignore-duplicates nil
448   "*If non-nil, ignore articles with identical Message-ID headers."
449   :group 'gnus-summary
450   :type 'boolean)
451
452 (defcustom gnus-single-article-buffer t
453   "*If non-nil, display all articles in the same buffer.
454 If nil, each group will get its own article buffer."
455   :group 'gnus-article-various
456   :type 'boolean)
457
458 (defcustom gnus-break-pages t
459   "*If non-nil, do page breaking on articles.
460 The page delimiter is specified by the `gnus-page-delimiter'
461 variable."
462   :group 'gnus-article-various
463   :type 'boolean)
464
465 (defcustom gnus-move-split-methods nil
466   "*Variable used to suggest where articles are to be moved to.
467 It uses the same syntax as the `gnus-split-methods' variable.
468 However, whereas `gnus-split-methods' specifies file names as targets,
469 this variable specifies group names."
470   :group 'gnus-summary-mail
471   :type '(repeat (choice (list :value (fun) function)
472                          (cons :value ("" "") regexp (repeat string))
473                          (sexp :value nil))))
474
475 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
476   "Function used to compute default prefix for article move/copy/etc prompts.
477 The function should take one argument, a group name, and return a
478 string with the suggested prefix."
479   :group 'gnus-summary-mail
480   :type 'function)
481
482 ;; FIXME: Although the custom type is `character' for the following variables,
483 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
484
485 (defcustom gnus-unread-mark ?           ;Whitespace
486   "*Mark used for unread articles."
487   :group 'gnus-summary-marks
488   :type 'character)
489
490 (defcustom gnus-ticked-mark ?!
491   "*Mark used for ticked articles."
492   :group 'gnus-summary-marks
493   :type 'character)
494
495 (defcustom gnus-dormant-mark ??
496   "*Mark used for dormant articles."
497   :group 'gnus-summary-marks
498   :type 'character)
499
500 (defcustom gnus-del-mark ?r
501   "*Mark used for del'd articles."
502   :group 'gnus-summary-marks
503   :type 'character)
504
505 (defcustom gnus-read-mark ?R
506   "*Mark used for read articles."
507   :group 'gnus-summary-marks
508   :type 'character)
509
510 (defcustom gnus-expirable-mark ?E
511   "*Mark used for expirable articles."
512   :group 'gnus-summary-marks
513   :type 'character)
514
515 (defcustom gnus-killed-mark ?K
516   "*Mark used for killed articles."
517   :group 'gnus-summary-marks
518   :type 'character)
519
520 (defcustom gnus-spam-mark ?$
521   "*Mark used for spam articles."
522   :version "22.1"
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-souped-mark ?F
527   "*Mark used for souped articles."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-kill-file-mark ?X
532   "*Mark used for articles killed by kill files."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-low-score-mark ?Y
537   "*Mark used for articles with a low score."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-catchup-mark ?C
542   "*Mark used for articles that are caught up."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-replied-mark ?A
547   "*Mark used for articles that have been replied to."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-forwarded-mark ?F
552   "*Mark used for articles that have been forwarded."
553   :version "22.1"
554   :group 'gnus-summary-marks
555   :type 'character)
556
557 (defcustom gnus-recent-mark ?N
558   "*Mark used for articles that are recent."
559   :version "22.1"
560   :group 'gnus-summary-marks
561   :type 'character)
562
563 (defcustom gnus-cached-mark ?*
564   "*Mark used for articles that are in the cache."
565   :group 'gnus-summary-marks
566   :type 'character)
567
568 (defcustom gnus-saved-mark ?S
569   "*Mark used for articles that have been saved."
570   :group 'gnus-summary-marks
571   :type 'character)
572
573 (defcustom gnus-unseen-mark ?.
574   "*Mark used for articles that haven't been seen."
575   :version "22.1"
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-no-mark ?               ;Whitespace
580   "*Mark used for articles that have no other secondary mark."
581   :version "22.1"
582   :group 'gnus-summary-marks
583   :type 'character)
584
585 (defcustom gnus-ancient-mark ?O
586   "*Mark used for ancient articles."
587   :group 'gnus-summary-marks
588   :type 'character)
589
590 (defcustom gnus-sparse-mark ?Q
591   "*Mark used for sparsely reffed articles."
592   :group 'gnus-summary-marks
593   :type 'character)
594
595 (defcustom gnus-canceled-mark ?G
596   "*Mark used for canceled articles."
597   :group 'gnus-summary-marks
598   :type 'character)
599
600 (defcustom gnus-duplicate-mark ?M
601   "*Mark used for duplicate articles."
602   :group 'gnus-summary-marks
603   :type 'character)
604
605 (defcustom gnus-undownloaded-mark ?-
606   "*Mark used for articles that weren't downloaded."
607   :version "22.1"
608   :group 'gnus-summary-marks
609   :type 'character)
610
611 (defcustom gnus-downloaded-mark ?+
612   "*Mark used for articles that were downloaded."
613   :group 'gnus-summary-marks
614   :type 'character)
615
616 (defcustom gnus-downloadable-mark ?%
617   "*Mark used for articles that are to be downloaded."
618   :group 'gnus-summary-marks
619   :type 'character)
620
621 (defcustom gnus-unsendable-mark ?=
622   "*Mark used for articles that won't be sent."
623   :group 'gnus-summary-marks
624   :type 'character)
625
626 (defcustom gnus-score-over-mark ?+
627   "*Score mark used for articles with high scores."
628   :group 'gnus-summary-marks
629   :type 'character)
630
631 (defcustom gnus-score-below-mark ?-
632   "*Score mark used for articles with low scores."
633   :group 'gnus-summary-marks
634   :type 'character)
635
636 (defcustom gnus-empty-thread-mark ?     ;Whitespace
637   "*There is no thread under the article."
638   :group 'gnus-summary-marks
639   :type 'character)
640
641 (defcustom gnus-not-empty-thread-mark ?=
642   "*There is a thread under the article."
643   :group 'gnus-summary-marks
644   :type 'character)
645
646 (defcustom gnus-view-pseudo-asynchronously nil
647   "*If non-nil, Gnus will view pseudo-articles asynchronously."
648   :group 'gnus-extract-view
649   :type 'boolean)
650
651 (defcustom gnus-auto-expirable-marks
652   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
653         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
654         gnus-souped-mark gnus-duplicate-mark)
655   "*The list of marks converted into expiration if a group is auto-expirable."
656   :version "21.1"
657   :group 'gnus-summary
658   :type '(repeat character))
659
660 (defcustom gnus-inhibit-user-auto-expire t
661   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
662   :version "21.1"
663   :group 'gnus-summary
664   :type 'boolean)
665
666 (defcustom gnus-view-pseudos nil
667   "*If `automatic', pseudo-articles will be viewed automatically.
668 If `not-confirm', pseudos will be viewed automatically, and the user
669 will not be asked to confirm the command."
670   :group 'gnus-extract-view
671   :type '(choice (const :tag "off" nil)
672                  (const automatic)
673                  (const not-confirm)))
674
675 (defcustom gnus-view-pseudos-separately t
676   "*If non-nil, one pseudo-article will be created for each file to be viewed.
677 If nil, all files that use the same viewing command will be given as a
678 list of parameters to that command."
679   :group 'gnus-extract-view
680   :type 'boolean)
681
682 (defcustom gnus-insert-pseudo-articles t
683   "*If non-nil, insert pseudo-articles when decoding articles."
684   :group 'gnus-extract-view
685   :type 'boolean)
686
687 (defcustom gnus-summary-dummy-line-format
688   "   %(:                             :%) %S\n"
689   "*The format specification for the dummy roots in the summary buffer.
690 It works along the same lines as a normal formatting string,
691 with some simple extensions.
692
693 %S  The subject
694
695 General format specifiers can also be used.
696 See `(gnus)Formatting Variables'."
697   :link '(custom-manual "(gnus)Formatting Variables")
698   :group 'gnus-threading
699   :type 'string)
700
701 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
702   "*The format specification for the summary mode line.
703 It works along the same lines as a normal formatting string,
704 with some simple extensions:
705
706 %G  Group name
707 %p  Unprefixed group name
708 %A  Current article number
709 %z  Current article score
710 %V  Gnus version
711 %U  Number of unread articles in the group
712 %e  Number of unselected articles in the group
713 %Z  A string with unread/unselected article counts
714 %g  Shortish group name
715 %S  Subject of the current article
716 %u  User-defined spec
717 %s  Current score file name
718 %d  Number of dormant articles
719 %r  Number of articles that have been marked as read in this session
720 %E  Number of articles expunged by the score files"
721   :group 'gnus-summary-format
722   :type 'string)
723
724 (defcustom gnus-list-identifiers nil
725   "Regexp that matches list identifiers to be removed from subject.
726 This can also be a list of regexps."
727   :version "21.1"
728   :group 'gnus-summary-format
729   :group 'gnus-article-hiding
730   :type '(choice (const :tag "none" nil)
731                  (regexp :value ".*")
732                  (repeat :value (".*") regexp)))
733
734 (defcustom gnus-summary-mark-below 0
735   "*Mark all articles with a score below this variable as read.
736 This variable is local to each summary buffer and usually set by the
737 score file."
738   :group 'gnus-score-default
739   :type 'integer)
740
741 (defun gnus-widget-reversible-match (widget value)
742   "Ignoring WIDGET, convert VALUE to internal form.
743 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
744   ;; (debug value)
745   (or (symbolp value)
746       (and (listp value)
747            (eq (length value) 2)
748            (eq (nth 0 value) 'not)
749            (symbolp (nth 1 value)))))
750
751 (defun gnus-widget-reversible-to-internal (widget value)
752   "Ignoring WIDGET, convert VALUE to internal form.
753 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
754 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
755   ;; (debug value)
756   (if (atom value)
757       (list value nil)
758     (list (nth 1 value) t)))
759
760 (defun gnus-widget-reversible-to-external (widget value)
761   "Ignoring WIDGET, convert VALUE to external form.
762 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
763 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
764   ;; (debug value)
765   (if (nth 1 value)
766       (list 'not (nth 0 value))
767     (nth 0 value)))
768
769 (define-widget 'gnus-widget-reversible 'group
770   "A `group' that convert values."
771   :match 'gnus-widget-reversible-match
772   :value-to-internal 'gnus-widget-reversible-to-internal
773   :value-to-external 'gnus-widget-reversible-to-external)
774
775 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
776   "*List of functions used for sorting articles in the summary buffer.
777
778 Each function takes two articles and returns non-nil if the first
779 article should be sorted before the other.  If you use more than one
780 function, the primary sort function should be the last.  You should
781 probably always include `gnus-article-sort-by-number' in the list of
782 sorting functions -- preferably first.  Also note that sorting by date
783 is often much slower than sorting by number, and the sorting order is
784 very similar.  (Sorting by date means sorting by the time the message
785 was sent, sorting by number means sorting by arrival time.)
786
787 Each item can also be a list `(not F)' where F is a function;
788 this reverses the sort order.
789
790 Ready-made functions include `gnus-article-sort-by-number',
791 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
792 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
793 and `gnus-article-sort-by-score'.
794
795 When threading is turned on, the variable `gnus-thread-sort-functions'
796 controls how articles are sorted."
797   :group 'gnus-summary-sort
798   :type '(repeat (gnus-widget-reversible
799                   (choice (function-item gnus-article-sort-by-number)
800                           (function-item gnus-article-sort-by-author)
801                           (function-item gnus-article-sort-by-subject)
802                           (function-item gnus-article-sort-by-date)
803                           (function-item gnus-article-sort-by-score)
804                           (function-item gnus-article-sort-by-random)
805                           (function :tag "other"))
806                   (boolean :tag "Reverse order"))))
807
808
809 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
810   "*List of functions used for sorting threads in the summary buffer.
811 By default, threads are sorted by article number.
812
813 Each function takes two threads and returns non-nil if the first
814 thread should be sorted before the other.  If you use more than one
815 function, the primary sort function should be the last.  You should
816 probably always include `gnus-thread-sort-by-number' in the list of
817 sorting functions -- preferably first.  Also note that sorting by date
818 is often much slower than sorting by number, and the sorting order is
819 very similar.  (Sorting by date means sorting by the time the message
820 was sent, sorting by number means sorting by arrival time.)
821
822 Each list item can also be a list `(not F)' where F is a
823 function; this specifies reversed sort order.
824
825 Ready-made functions include `gnus-thread-sort-by-number',
826 `gnus-thread-sort-by-author', `gnus-thread-sort-by-recipient'
827 `gnus-thread-sort-by-subject', `gnus-thread-sort-by-date',
828 `gnus-thread-sort-by-score', `gnus-thread-sort-by-most-recent-number',
829 `gnus-thread-sort-by-most-recent-date', `gnus-thread-sort-by-random',
830 and `gnus-thread-sort-by-total-score' (see
831 `gnus-thread-score-function').
832
833 When threading is turned off, the variable
834 `gnus-article-sort-functions' controls how articles are sorted."
835   :group 'gnus-summary-sort
836   :type '(repeat
837           (gnus-widget-reversible
838            (choice (function-item gnus-thread-sort-by-number)
839                    (function-item gnus-thread-sort-by-author)
840                    (function-item gnus-thread-sort-by-recipient)
841                    (function-item gnus-thread-sort-by-subject)
842                    (function-item gnus-thread-sort-by-date)
843                    (function-item gnus-thread-sort-by-score)
844                    (function-item gnus-thread-sort-by-most-recent-number)
845                    (function-item gnus-thread-sort-by-most-recent-date)
846                    (function-item gnus-thread-sort-by-random)
847                    (function-item gnus-thread-sort-by-total-score)
848                    (function :tag "other"))
849            (boolean :tag "Reverse order"))))
850
851 (defcustom gnus-thread-score-function '+
852   "*Function used for calculating the total score of a thread.
853
854 The function is called with the scores of the article and each
855 subthread and should then return the score of the thread.
856
857 Some functions you can use are `+', `max', or `min'."
858   :group 'gnus-summary-sort
859   :type 'function)
860
861 (defcustom gnus-summary-expunge-below nil
862   "All articles that have a score less than this variable will be expunged.
863 This variable is local to the summary buffers."
864   :group 'gnus-score-default
865   :type '(choice (const :tag "off" nil)
866                  integer))
867
868 (defcustom gnus-thread-expunge-below nil
869   "All threads that have a total score less than this variable will be expunged.
870 See `gnus-thread-score-function' for en explanation of what a
871 \"thread score\" is.
872
873 This variable is local to the summary buffers."
874   :group 'gnus-threading
875   :group 'gnus-score-default
876   :type '(choice (const :tag "off" nil)
877                  integer))
878
879 (defcustom gnus-summary-mode-hook nil
880   "*A hook for Gnus summary mode.
881 This hook is run before any variables are set in the summary buffer."
882   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
883   :group 'gnus-summary-various
884   :type 'hook)
885
886 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
887 (when (featurep 'xemacs)
888   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
889   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
890   (add-hook 'gnus-summary-mode-hook
891             'gnus-xmas-switch-horizontal-scrollbar-off))
892
893 (defcustom gnus-summary-menu-hook nil
894   "*Hook run after the creation of the summary mode menu."
895   :group 'gnus-summary-visual
896   :type 'hook)
897
898 (defcustom gnus-summary-exit-hook nil
899   "*A hook called on exit from the summary buffer.
900 It will be called with point in the group buffer."
901   :group 'gnus-summary-exit
902   :type 'hook)
903
904 (defcustom gnus-summary-prepare-hook nil
905   "*A hook called after the summary buffer has been generated.
906 If you want to modify the summary buffer, you can use this hook."
907   :group 'gnus-summary-various
908   :type 'hook)
909
910 (defcustom gnus-summary-prepared-hook nil
911   "*A hook called as the last thing after the summary buffer has been generated."
912   :group 'gnus-summary-various
913   :type 'hook)
914
915 (defcustom gnus-summary-generate-hook nil
916   "*A hook run just before generating the summary buffer.
917 This hook is commonly used to customize threading variables and the
918 like."
919   :group 'gnus-summary-various
920   :type 'hook)
921
922 (defcustom gnus-select-group-hook nil
923   "*A hook called when a newsgroup is selected.
924
925 If you'd like to simplify subjects like the
926 `gnus-summary-next-same-subject' command does, you can use the
927 following hook:
928
929  (add-hook gnus-select-group-hook
930            (lambda ()
931              (mapcar (lambda (header)
932                        (mail-header-set-subject
933                         header
934                         (gnus-simplify-subject
935                          (mail-header-subject header) 're-only)))
936                      gnus-newsgroup-headers)))"
937   :group 'gnus-group-select
938   :type 'hook)
939
940 (defcustom gnus-select-article-hook nil
941   "*A hook called when an article is selected."
942   :group 'gnus-summary-choose
943   :options '(gnus-agent-fetch-selected-article)
944   :type 'hook)
945
946 (defcustom gnus-visual-mark-article-hook
947   (list 'gnus-highlight-selected-summary)
948   "*Hook run after selecting an article in the summary buffer.
949 It is meant to be used for highlighting the article in some way.  It
950 is not run if `gnus-visual' is nil."
951   :group 'gnus-summary-visual
952   :type 'hook)
953
954 (defcustom gnus-parse-headers-hook nil
955   "*A hook called before parsing the headers."
956   :group 'gnus-various
957   :type 'hook)
958
959 (defcustom gnus-exit-group-hook nil
960   "*A hook called when exiting summary mode.
961 This hook is not called from the non-updating exit commands like `Q'."
962   :group 'gnus-various
963   :type 'hook)
964
965 (defcustom gnus-summary-update-hook
966   (list 'gnus-summary-highlight-line)
967   "*A hook called when a summary line is changed.
968 The hook will not be called if `gnus-visual' is nil.
969
970 The default function `gnus-summary-highlight-line' will
971 highlight the line according to the `gnus-summary-highlight'
972 variable."
973   :group 'gnus-summary-visual
974   :type 'hook)
975
976 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
977   "*A hook called when an article is selected for the first time.
978 The hook is intended to mark an article as read (or unread)
979 automatically when it is selected."
980   :group 'gnus-summary-choose
981   :type 'hook)
982
983 (defcustom gnus-group-no-more-groups-hook nil
984   "*A hook run when returning to group mode having no more (unread) groups."
985   :group 'gnus-group-select
986   :type 'hook)
987
988 (defcustom gnus-ps-print-hook nil
989   "*A hook run before ps-printing something from Gnus."
990   :group 'gnus-summary
991   :type 'hook)
992
993 (defcustom gnus-summary-article-move-hook nil
994   "*A hook called after an article is moved, copied, respooled, or crossposted."
995   :version "22.1"
996   :group 'gnus-summary
997   :type 'hook)
998
999 (defcustom gnus-summary-article-delete-hook nil
1000   "*A hook called after an article is deleted."
1001   :version "22.1"
1002   :group 'gnus-summary
1003   :type 'hook)
1004
1005 (defcustom gnus-summary-article-expire-hook nil
1006   "*A hook called after an article is expired."
1007   :version "22.1"
1008   :group 'gnus-summary
1009   :type 'hook)
1010
1011 (defcustom gnus-summary-display-arrow
1012   (and (fboundp 'display-graphic-p)
1013        (display-graphic-p))
1014   "*If non-nil, display an arrow highlighting the current article."
1015   :version "22.1"
1016   :group 'gnus-summary
1017   :type 'boolean)
1018
1019 (defcustom gnus-summary-selected-face 'gnus-summary-selected
1020   "Face used for highlighting the current article in the summary buffer."
1021   :group 'gnus-summary-visual
1022   :type 'face)
1023
1024 (defvar gnus-tmp-downloaded nil)
1025
1026 (defcustom gnus-summary-highlight
1027   '(((eq mark gnus-canceled-mark)
1028      . gnus-summary-cancelled)
1029     ((and uncached (> score default-high))
1030      . gnus-summary-high-undownloaded)
1031     ((and uncached (< score default-low))
1032      . gnus-summary-low-undownloaded)
1033     (uncached
1034      . gnus-summary-normal-undownloaded)
1035     ((and (> score default-high)
1036           (or (eq mark gnus-dormant-mark)
1037               (eq mark gnus-ticked-mark)))
1038      . gnus-summary-high-ticked)
1039     ((and (< score default-low)
1040           (or (eq mark gnus-dormant-mark)
1041               (eq mark gnus-ticked-mark)))
1042      . gnus-summary-low-ticked)
1043     ((or (eq mark gnus-dormant-mark)
1044          (eq mark gnus-ticked-mark))
1045      . gnus-summary-normal-ticked)
1046     ((and (> score default-high) (eq mark gnus-ancient-mark))
1047      . gnus-summary-high-ancient)
1048     ((and (< score default-low) (eq mark gnus-ancient-mark))
1049      . gnus-summary-low-ancient)
1050     ((eq mark gnus-ancient-mark)
1051      . gnus-summary-normal-ancient)
1052     ((and (> score default-high) (eq mark gnus-unread-mark))
1053      . gnus-summary-high-unread)
1054     ((and (< score default-low) (eq mark gnus-unread-mark))
1055      . gnus-summary-low-unread)
1056     ((eq mark gnus-unread-mark)
1057      . gnus-summary-normal-unread)
1058     ((> score default-high)
1059      . gnus-summary-high-read)
1060     ((< score default-low)
1061      . gnus-summary-low-read)
1062     (t
1063      . gnus-summary-normal-read))
1064   "*Controls the highlighting of summary buffer lines.
1065
1066 A list of (FORM . FACE) pairs.  When deciding how a particular
1067 summary line should be displayed, each form is evaluated.  The content
1068 of the face field after the first true form is used.  You can change
1069 how those summary lines are displayed, by editing the face field.
1070
1071 You can use the following variables in the FORM field.
1072
1073 score:        The article's score.
1074 default:      The default article score.
1075 default-high: The default score for high scored articles.
1076 default-low:  The default score for low scored articles.
1077 below:        The score below which articles are automatically marked as read.
1078 mark:         The article's mark.
1079 uncached:     Non-nil if the article is uncached."
1080   :group 'gnus-summary-visual
1081   :type '(repeat (cons (sexp :tag "Form" nil)
1082                        face)))
1083
1084 (defcustom gnus-alter-header-function nil
1085   "Function called to allow alteration of article header structures.
1086 The function is called with one parameter, the article header vector,
1087 which it may alter in any way."
1088   :type '(choice (const :tag "None" nil)
1089                  function)
1090   :group 'gnus-summary)
1091
1092 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
1093   "Function used to decode a string with encoded words.")
1094
1095 (defvar gnus-decode-encoded-address-function
1096   'mail-decode-encoded-address-string
1097   "Function used to decode addresses with encoded words.")
1098
1099 (defcustom gnus-extra-headers '(To Newsgroups)
1100   "*Extra headers to parse."
1101   :version "21.1"
1102   :group 'gnus-summary
1103   :type '(repeat symbol))
1104
1105 (defcustom gnus-ignored-from-addresses
1106   (and user-mail-address
1107        (not (string= user-mail-address ""))
1108        (regexp-quote user-mail-address))
1109   "*From headers that may be suppressed in favor of To headers.
1110 This can be a regexp or a list of regexps."
1111   :version "21.1"
1112   :group 'gnus-summary
1113   :type '(choice regexp
1114                  (repeat :tag "Regexp List" regexp)))
1115
1116 (defsubst gnus-ignored-from-addresses ()
1117   (gmm-regexp-concat gnus-ignored-from-addresses))
1118
1119 (defcustom gnus-summary-to-prefix "-> "
1120   "*String prefixed to the To field in the summary line when
1121 using `gnus-ignored-from-addresses'."
1122   :version "22.1"
1123   :group 'gnus-summary
1124   :type 'string)
1125
1126 (defcustom gnus-summary-newsgroup-prefix "=> "
1127   "*String prefixed to the Newsgroup field in the summary
1128 line when using `gnus-ignored-from-addresses'."
1129   :version "22.1"
1130   :group 'gnus-summary
1131   :type 'string)
1132
1133 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1134   "List of charsets that should be ignored.
1135 When these charsets are used in the \"charset\" parameter, the
1136 default charset will be used instead."
1137   :version "21.1"
1138   :type '(repeat symbol)
1139   :group 'gnus-charset)
1140
1141 (defcustom gnus-newsgroup-maximum-articles nil
1142   "The maximum number of articles a newsgroup.
1143 If this is a number, old articles in a newsgroup exceeding this number
1144 are silently ignored.  If it is nil, no article is ignored.  Note that
1145 setting this variable to a number might prevent you from reading very
1146 old articles."
1147   :group 'gnus-group-select
1148   :version "22.2"
1149   :type '(choice (const :tag "No limit" nil)
1150                  integer))
1151
1152 (gnus-define-group-parameter
1153  ignored-charsets
1154  :type list
1155  :function-document
1156  "Return the ignored charsets of GROUP."
1157  :variable gnus-group-ignored-charsets-alist
1158  :variable-default
1159  '(("alt\\.chinese\\.text" iso-8859-1))
1160  :variable-document
1161  "Alist of regexps (to match group names) and charsets that should be ignored.
1162 When these charsets are used in the \"charset\" parameter, the
1163 default charset will be used instead."
1164  :variable-group gnus-charset
1165  :variable-type '(repeat (cons (regexp :tag "Group")
1166                                (repeat symbol)))
1167  :parameter-type '(choice :tag "Ignored charsets"
1168                           :value nil
1169                           (repeat (symbol)))
1170  :parameter-document       "\
1171 List of charsets that should be ignored.
1172
1173 When these charsets are used in the \"charset\" parameter, the
1174 default charset will be used instead.")
1175
1176 (defcustom gnus-group-highlight-words-alist nil
1177   "Alist of group regexps and highlight regexps.
1178 This variable uses the same syntax as `gnus-emphasis-alist'."
1179   :version "21.1"
1180   :type '(repeat (cons (regexp :tag "Group")
1181                        (repeat (list (regexp :tag "Highlight regexp")
1182                                      (number :tag "Group for entire word" 0)
1183                                      (number :tag "Group for displayed part" 0)
1184                                      (symbol :tag "Face"
1185                                              gnus-emphasis-highlight-words)))))
1186   :group 'gnus-summary-visual)
1187
1188 (defcustom gnus-summary-show-article-charset-alist
1189   nil
1190   "Alist of number and charset.
1191 The article will be shown with the charset corresponding to the
1192 numbered argument.
1193 For example: ((1 . cn-gb-2312) (2 . big5))."
1194   :version "21.1"
1195   :type '(repeat (cons (number :tag "Argument" 1)
1196                        (symbol :tag "Charset")))
1197   :group 'gnus-charset)
1198
1199 (defcustom gnus-preserve-marks t
1200   "Whether marks are preserved when moving, copying and respooling messages."
1201   :version "21.1"
1202   :type 'boolean
1203   :group 'gnus-summary-marks)
1204
1205 (defcustom gnus-alter-articles-to-read-function nil
1206   "Function to be called to alter the list of articles to be selected."
1207   :type '(choice (const nil) function)
1208   :group 'gnus-summary)
1209
1210 (defcustom gnus-orphan-score nil
1211   "*All orphans get this score added.  Set in the score file."
1212   :group 'gnus-score-default
1213   :type '(choice (const nil)
1214                  integer))
1215
1216 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1217   "*A regexp to match MIME parts when saving multiple parts of a
1218 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1219 This regexp will be used by default when prompting the user for which
1220 type of files to save."
1221   :group 'gnus-summary
1222   :type 'regexp)
1223
1224 (defcustom gnus-read-all-available-headers nil
1225   "Whether Gnus should parse all headers made available to it.
1226 This is mostly relevant for slow back ends where the user may
1227 wish to widen the summary buffer to include all headers
1228 that were fetched.  Say, for nnultimate groups."
1229   :version "22.1"
1230   :group 'gnus-summary
1231   :type '(choice boolean regexp))
1232
1233 (defcustom gnus-summary-muttprint-program "muttprint"
1234   "Command (and optional arguments) used to run Muttprint."
1235   :version "22.1"
1236   :group 'gnus-summary
1237   :type 'string)
1238
1239 (defcustom gnus-article-loose-mime t
1240   "If non-nil, don't require MIME-Version header.
1241 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1242 supply the MIME-Version header or deliberately strip it from the mail.
1243 If non-nil (the default), Gnus will treat some articles as MIME
1244 even if the MIME-Version header is missing."
1245   :version "22.1"
1246   :type 'boolean
1247   :group 'gnus-article-mime)
1248
1249 (defcustom gnus-article-emulate-mime t
1250   "If non-nil, use MIME emulation for uuencode and the like.
1251 This means that Gnus will search message bodies for text that look
1252 like uuencoded bits, yEncoded bits, and so on, and present that using
1253 the normal Gnus MIME machinery."
1254   :version "22.1"
1255   :type 'boolean
1256   :group 'gnus-article-mime)
1257
1258 ;;; Internal variables
1259
1260 (defvar gnus-summary-display-cache nil)
1261 (defvar gnus-article-mime-handles nil)
1262 (defvar gnus-article-decoded-p nil)
1263 (defvar gnus-article-charset nil)
1264 (defvar gnus-article-ignored-charsets nil)
1265 (defvar gnus-scores-exclude-files nil)
1266 (defvar gnus-page-broken nil)
1267
1268 (defvar gnus-original-article nil)
1269 (defvar gnus-article-internal-prepare-hook nil)
1270 (defvar gnus-newsgroup-process-stack nil)
1271
1272 (defvar gnus-thread-indent-array nil)
1273 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1274 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1275   "Function called to sort the articles within a thread after it has been gathered together.")
1276
1277 (defvar gnus-summary-save-parts-type-history nil)
1278 (defvar gnus-summary-save-parts-last-directory mm-default-directory)
1279
1280 ;; Avoid highlighting in kill files.
1281 (defvar gnus-summary-inhibit-highlight nil)
1282 (defvar gnus-newsgroup-selected-overlay nil)
1283 (defvar gnus-inhibit-limiting nil)
1284 (defvar gnus-newsgroup-adaptive-score-file nil)
1285 (defvar gnus-current-score-file nil)
1286 (defvar gnus-current-move-group nil)
1287 (defvar gnus-current-copy-group nil)
1288 (defvar gnus-current-crosspost-group nil)
1289 (defvar gnus-newsgroup-display nil)
1290
1291 (defvar gnus-newsgroup-dependencies nil)
1292 (defvar gnus-newsgroup-adaptive nil)
1293 (defvar gnus-summary-display-article-function nil)
1294 (defvar gnus-summary-highlight-line-function nil
1295   "Function called after highlighting a summary line.")
1296
1297 (defvar gnus-summary-line-format-alist
1298   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1299     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1300     (?s gnus-tmp-subject-or-nil ?s)
1301     (?n gnus-tmp-name ?s)
1302     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1303         ?s)
1304     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1305             gnus-tmp-from) ?s)
1306     (?F gnus-tmp-from ?s)
1307     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1308     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1309     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1310     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1311     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1312     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1313     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1314     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1315     (?L gnus-tmp-lines ?s)
1316     (?O gnus-tmp-downloaded ?c)
1317     (?I gnus-tmp-indentation ?s)
1318     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1319     (?R gnus-tmp-replied ?c)
1320     (?\[ gnus-tmp-opening-bracket ?c)
1321     (?\] gnus-tmp-closing-bracket ?c)
1322     (?\> (make-string gnus-tmp-level ? ) ?s)
1323     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1324     (?i gnus-tmp-score ?d)
1325     (?z gnus-tmp-score-char ?c)
1326     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1327     (?U gnus-tmp-unread ?c)
1328     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1329         ?s)
1330     (?t (gnus-summary-number-of-articles-in-thread
1331          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1332         ?d)
1333     (?e (gnus-summary-number-of-articles-in-thread
1334          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1335         ?c)
1336     (?u gnus-tmp-user-defined ?s)
1337     (?P (gnus-pick-line-number) ?d)
1338     (?B gnus-tmp-thread-tree-header-string ?s)
1339     (user-date (gnus-user-date
1340                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1341   "An alist of format specifications that can appear in summary lines.
1342 These are paired with what variables they correspond with, along with
1343 the type of the variable (string, integer, character, etc).")
1344
1345 (defvar gnus-summary-dummy-line-format-alist
1346   `((?S gnus-tmp-subject ?s)
1347     (?N gnus-tmp-number ?d)
1348     (?u gnus-tmp-user-defined ?s)))
1349
1350 (defvar gnus-summary-mode-line-format-alist
1351   `((?G gnus-tmp-group-name ?s)
1352     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1353     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1354     (?A gnus-tmp-article-number ?d)
1355     (?Z gnus-tmp-unread-and-unselected ?s)
1356     (?V gnus-version ?s)
1357     (?U gnus-tmp-unread-and-unticked ?d)
1358     (?S gnus-tmp-subject ?s)
1359     (?e gnus-tmp-unselected ?d)
1360     (?u gnus-tmp-user-defined ?s)
1361     (?d (length gnus-newsgroup-dormant) ?d)
1362     (?t (length gnus-newsgroup-marked) ?d)
1363     (?h (length gnus-newsgroup-spam-marked) ?d)
1364     (?r (length gnus-newsgroup-reads) ?d)
1365     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1366     (?E gnus-newsgroup-expunged-tally ?d)
1367     (?s (gnus-current-score-file-nondirectory) ?s)))
1368
1369 (defvar gnus-last-search-regexp nil
1370   "Default regexp for article search command.")
1371
1372 (defvar gnus-last-shell-command nil
1373   "Default shell command on article.")
1374
1375 (defvar gnus-newsgroup-agentized nil
1376   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1377 (defvar gnus-newsgroup-begin nil)
1378 (defvar gnus-newsgroup-end nil)
1379 (defvar gnus-newsgroup-last-rmail nil)
1380 (defvar gnus-newsgroup-last-mail nil)
1381 (defvar gnus-newsgroup-last-folder nil)
1382 (defvar gnus-newsgroup-last-file nil)
1383 (defvar gnus-newsgroup-last-directory nil)
1384 (defvar gnus-newsgroup-auto-expire nil)
1385 (defvar gnus-newsgroup-active nil)
1386
1387 (defvar gnus-newsgroup-data nil)
1388 (defvar gnus-newsgroup-data-reverse nil)
1389 (defvar gnus-newsgroup-limit nil)
1390 (defvar gnus-newsgroup-limits nil)
1391 (defvar gnus-summary-use-undownloaded-faces nil)
1392
1393 (defvar gnus-newsgroup-unreads nil
1394   "Sorted list of unread articles in the current newsgroup.")
1395
1396 (defvar gnus-newsgroup-unselected nil
1397   "Sorted list of unselected unread articles in the current newsgroup.")
1398
1399 (defvar gnus-newsgroup-reads nil
1400   "Alist of read articles and article marks in the current newsgroup.")
1401
1402 (defvar gnus-newsgroup-expunged-tally nil)
1403
1404 (defvar gnus-newsgroup-marked nil
1405   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1406
1407 (defvar gnus-newsgroup-spam-marked nil
1408   "List of ranges of articles that have been marked as spam.")
1409
1410 (defvar gnus-newsgroup-killed nil
1411   "List of ranges of articles that have been through the scoring process.")
1412
1413 (defvar gnus-newsgroup-cached nil
1414   "Sorted list of articles that come from the article cache.")
1415
1416 (defvar gnus-newsgroup-saved nil
1417   "List of articles that have been saved.")
1418
1419 (defvar gnus-newsgroup-kill-headers nil)
1420
1421 (defvar gnus-newsgroup-replied nil
1422   "List of articles that have been replied to in the current newsgroup.")
1423
1424 (defvar gnus-newsgroup-forwarded nil
1425   "List of articles that have been forwarded in the current newsgroup.")
1426
1427 (defvar gnus-newsgroup-recent nil
1428   "List of articles that have are recent in the current newsgroup.")
1429
1430 (defvar gnus-newsgroup-expirable nil
1431   "Sorted list of articles in the current newsgroup that can be expired.")
1432
1433 (defvar gnus-newsgroup-processable nil
1434   "List of articles in the current newsgroup that can be processed.")
1435
1436 (defvar gnus-newsgroup-downloadable nil
1437   "Sorted list of articles in the current newsgroup that can be processed.")
1438
1439 (defvar gnus-newsgroup-unfetched nil
1440   "Sorted list of articles in the current newsgroup whose headers have
1441 not been fetched into the agent.
1442
1443 This list will always be a subset of gnus-newsgroup-undownloaded.")
1444
1445 (defvar gnus-newsgroup-undownloaded nil
1446   "List of articles in the current newsgroup that haven't been downloaded.")
1447
1448 (defvar gnus-newsgroup-unsendable nil
1449   "List of articles in the current newsgroup that won't be sent.")
1450
1451 (defvar gnus-newsgroup-bookmarks nil
1452   "List of articles in the current newsgroup that have bookmarks.")
1453
1454 (defvar gnus-newsgroup-dormant nil
1455   "Sorted list of dormant articles in the current newsgroup.")
1456
1457 (defvar gnus-newsgroup-unseen nil
1458   "List of unseen articles in the current newsgroup.")
1459
1460 (defvar gnus-newsgroup-seen nil
1461   "Range of seen articles in the current newsgroup.")
1462
1463 (defvar gnus-newsgroup-articles nil
1464   "List of articles in the current newsgroup.")
1465
1466 (defvar gnus-newsgroup-scored nil
1467   "List of scored articles in the current newsgroup.")
1468
1469 (defvar gnus-newsgroup-headers nil
1470   "List of article headers in the current newsgroup.")
1471
1472 (defvar gnus-newsgroup-threads nil)
1473
1474 (defvar gnus-newsgroup-prepared nil
1475   "Whether the current group has been prepared properly.")
1476
1477 (defvar gnus-newsgroup-ancient nil
1478   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1479
1480 (defvar gnus-newsgroup-sparse nil)
1481
1482 (defvar gnus-current-article nil)
1483 (defvar gnus-article-current nil)
1484 (defvar gnus-current-headers nil)
1485 (defvar gnus-have-all-headers nil)
1486 (defvar gnus-last-article nil)
1487 (defvar gnus-newsgroup-history nil)
1488 (defvar gnus-newsgroup-charset nil)
1489 (defvar gnus-newsgroup-ephemeral-charset nil)
1490 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1491
1492 (defvar gnus-article-before-search nil)
1493
1494 (defvar gnus-summary-local-variables
1495   '(gnus-newsgroup-name
1496     gnus-newsgroup-begin gnus-newsgroup-end
1497     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1498     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1499     gnus-newsgroup-last-directory
1500     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1501     gnus-newsgroup-unselected gnus-newsgroup-marked
1502     gnus-newsgroup-spam-marked
1503     gnus-newsgroup-reads gnus-newsgroup-saved
1504     gnus-newsgroup-replied gnus-newsgroup-forwarded
1505     gnus-newsgroup-recent
1506     gnus-newsgroup-expirable
1507     gnus-newsgroup-processable gnus-newsgroup-killed
1508     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1509     gnus-newsgroup-unfetched
1510     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1511     gnus-newsgroup-seen gnus-newsgroup-articles
1512     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1513     gnus-newsgroup-headers gnus-newsgroup-threads
1514     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1515     gnus-current-article gnus-current-headers gnus-have-all-headers
1516     gnus-last-article gnus-article-internal-prepare-hook
1517     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1518     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1519     gnus-thread-expunge-below
1520     gnus-score-alist gnus-current-score-file
1521     (gnus-summary-expunge-below . global)
1522     (gnus-summary-mark-below . global)
1523     (gnus-orphan-score . global)
1524     gnus-newsgroup-active gnus-scores-exclude-files
1525     gnus-newsgroup-history gnus-newsgroup-ancient
1526     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1527     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1528     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1529     (gnus-newsgroup-expunged-tally . 0)
1530     gnus-cache-removable-articles gnus-newsgroup-cached
1531     gnus-newsgroup-data gnus-newsgroup-data-reverse
1532     gnus-newsgroup-limit gnus-newsgroup-limits
1533     gnus-newsgroup-charset gnus-newsgroup-display
1534     gnus-summary-use-undownloaded-faces)
1535   "Variables that are buffer-local to the summary buffers.")
1536
1537 (defvar gnus-newsgroup-variables nil
1538   "A list of variables that have separate values in different newsgroups.
1539 A list of newsgroup (summary buffer) local variables, or cons of
1540 variables and their default expressions to be evalled (when the default
1541 values are not nil), that should be made global while the summary buffer
1542 is active.
1543
1544 Note: The default expressions will be evaluated (using function `eval')
1545 before assignment to the local variable rather than just assigned to it.
1546 If the default expression is the symbol `global', that symbol will not
1547 be evaluated but the global value of the local variable will be used
1548 instead.
1549
1550 These variables can be used to set variables in the group parameters
1551 while still allowing them to affect operations done in other buffers.
1552 For example:
1553
1554 \(setq gnus-newsgroup-variables
1555      '(message-use-followup-to
1556        (gnus-visible-headers .
1557          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1558 ")
1559
1560 ;; Byte-compiler warning.
1561 (eval-when-compile
1562   ;; Bind features so that require will believe that gnus-sum has
1563   ;; already been loaded (avoids infinite recursion)
1564   (let ((features (cons 'gnus-sum features)))
1565     ;; Several of the declarations in gnus-sum are needed to load the
1566     ;; following files. Right now, these definitions have been
1567     ;; compiled but not defined (evaluated).  We could either do a
1568     ;; eval-and-compile about all of the declarations or evaluate the
1569     ;; source file.
1570     (if (boundp 'gnus-newsgroup-variables)
1571         nil
1572       (load "gnus-sum.el" t t t))
1573     (require 'gnus)
1574     (require 'gnus-art)))
1575
1576 ;; MIME stuff.
1577
1578 (defvar gnus-decode-encoded-word-methods
1579   '(mail-decode-encoded-word-string)
1580   "List of methods used to decode encoded words.
1581
1582 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1583 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1584 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1585 whose names match REGEXP.
1586
1587 For example:
1588 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1589  mail-decode-encoded-word-string
1590  (\"chinese\" . rfc1843-decode-string))")
1591
1592 (defvar gnus-decode-encoded-word-methods-cache nil)
1593
1594 (defun gnus-multi-decode-encoded-word-string (string)
1595   "Apply the functions from `gnus-encoded-word-methods' that match."
1596   (unless (and gnus-decode-encoded-word-methods-cache
1597                (eq gnus-newsgroup-name
1598                    (car gnus-decode-encoded-word-methods-cache)))
1599     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1600     (mapcar (lambda (x)
1601               (if (symbolp x)
1602                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1603                 (if (and gnus-newsgroup-name
1604                          (string-match (car x) gnus-newsgroup-name))
1605                     (nconc gnus-decode-encoded-word-methods-cache
1606                            (list (cdr x))))))
1607             gnus-decode-encoded-word-methods))
1608   (let ((xlist gnus-decode-encoded-word-methods-cache))
1609     (pop xlist)
1610     (while xlist
1611       (setq string (funcall (pop xlist) string))))
1612   string)
1613
1614 ;; Subject simplification.
1615
1616 (defun gnus-simplify-whitespace (str)
1617   "Remove excessive whitespace from STR."
1618   ;; Multiple spaces.
1619   (while (string-match "[ \t][ \t]+" str)
1620     (setq str (concat (substring str 0 (match-beginning 0))
1621                         " "
1622                         (substring str (match-end 0)))))
1623   ;; Leading spaces.
1624   (when (string-match "^[ \t]+" str)
1625     (setq str (substring str (match-end 0))))
1626   ;; Trailing spaces.
1627   (when (string-match "[ \t]+$" str)
1628     (setq str (substring str 0 (match-beginning 0))))
1629   str)
1630
1631 (defun gnus-simplify-all-whitespace (str)
1632   "Remove all whitespace from STR."
1633   (while (string-match "[ \t\n]+" str)
1634     (setq str (replace-match "" nil nil str)))
1635   str)
1636
1637 (defsubst gnus-simplify-subject-re (subject)
1638   "Remove \"Re:\" from subject lines."
1639   (if (string-match message-subject-re-regexp subject)
1640       (substring subject (match-end 0))
1641     subject))
1642
1643 (defun gnus-simplify-subject (subject &optional re-only)
1644   "Remove `Re:' and words in parentheses.
1645 If RE-ONLY is non-nil, strip leading `Re:'s only."
1646   (let ((case-fold-search t))           ;Ignore case.
1647     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1648     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1649       (setq subject (substring subject (match-end 0))))
1650     ;; Remove uninteresting prefixes.
1651     (when (and (not re-only)
1652                gnus-simplify-ignored-prefixes
1653                (string-match gnus-simplify-ignored-prefixes subject))
1654       (setq subject (substring subject (match-end 0))))
1655     ;; Remove words in parentheses from end.
1656     (unless re-only
1657       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1658         (setq subject (substring subject 0 (match-beginning 0)))))
1659     ;; Return subject string.
1660     subject))
1661
1662 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1663 ;; all whitespace.
1664 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1665   (goto-char (point-min))
1666   (while (re-search-forward regexp nil t)
1667     (replace-match (or newtext ""))))
1668
1669 (defun gnus-simplify-buffer-fuzzy ()
1670   "Simplify string in the buffer fuzzily.
1671 The string in the accessible portion of the current buffer is simplified.
1672 It is assumed to be a single-line subject.
1673 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1674 matter is removed.  Additional things can be deleted by setting
1675 `gnus-simplify-subject-fuzzy-regexp'."
1676   (let ((case-fold-search t)
1677         (modified-tick))
1678     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1679
1680     (while (not (eq modified-tick (buffer-modified-tick)))
1681       (setq modified-tick (buffer-modified-tick))
1682       (cond
1683        ((listp gnus-simplify-subject-fuzzy-regexp)
1684         (mapcar 'gnus-simplify-buffer-fuzzy-step
1685                 gnus-simplify-subject-fuzzy-regexp))
1686        (gnus-simplify-subject-fuzzy-regexp
1687         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1688       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1689       (gnus-simplify-buffer-fuzzy-step
1690        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1691       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1692
1693     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1694     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1695     (gnus-simplify-buffer-fuzzy-step " $")
1696     (gnus-simplify-buffer-fuzzy-step "^ +")))
1697
1698 (defun gnus-simplify-subject-fuzzy (subject)
1699   "Simplify a subject string fuzzily.
1700 See `gnus-simplify-buffer-fuzzy' for details."
1701   (save-excursion
1702     (gnus-set-work-buffer)
1703     (let ((case-fold-search t))
1704       ;; Remove uninteresting prefixes.
1705       (when (and gnus-simplify-ignored-prefixes
1706                  (string-match gnus-simplify-ignored-prefixes subject))
1707         (setq subject (substring subject (match-end 0))))
1708       (insert subject)
1709       (inline (gnus-simplify-buffer-fuzzy))
1710       (buffer-string))))
1711
1712 (defsubst gnus-simplify-subject-fully (subject)
1713   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1714   (cond
1715    (gnus-simplify-subject-functions
1716     (gnus-map-function gnus-simplify-subject-functions subject))
1717    ((null gnus-summary-gather-subject-limit)
1718     (gnus-simplify-subject-re subject))
1719    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1720     (gnus-simplify-subject-fuzzy subject))
1721    ((numberp gnus-summary-gather-subject-limit)
1722     (truncate-string-to-width (gnus-simplify-subject-re subject)
1723                               gnus-summary-gather-subject-limit))
1724    (t
1725     subject)))
1726
1727 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1728   "Check whether two subjects are equal.
1729 If optional argument SIMPLE-FIRST is t, first argument is already
1730 simplified."
1731   (cond
1732    ((null simple-first)
1733     (equal (gnus-simplify-subject-fully s1)
1734            (gnus-simplify-subject-fully s2)))
1735    (t
1736     (equal s1
1737            (gnus-simplify-subject-fully s2)))))
1738
1739 (defun gnus-summary-bubble-group ()
1740   "Increase the score of the current group.
1741 This is a handy function to add to `gnus-summary-exit-hook' to
1742 increase the score of each group you read."
1743   (gnus-group-add-score gnus-newsgroup-name))
1744
1745 \f
1746 ;;;
1747 ;;; Gnus summary mode
1748 ;;;
1749
1750 (put 'gnus-summary-mode 'mode-class 'special)
1751
1752 (defvar gnus-article-commands-menu)
1753
1754 ;; Non-orthogonal keys
1755
1756 (gnus-define-keys gnus-summary-mode-map
1757   " " gnus-summary-next-page
1758   "\177" gnus-summary-prev-page
1759   [delete] gnus-summary-prev-page
1760   [backspace] gnus-summary-prev-page
1761   "\r" gnus-summary-scroll-up
1762   "\M-\r" gnus-summary-scroll-down
1763   "n" gnus-summary-next-unread-article
1764   "p" gnus-summary-prev-unread-article
1765   "N" gnus-summary-next-article
1766   "P" gnus-summary-prev-article
1767   "\M-\C-n" gnus-summary-next-same-subject
1768   "\M-\C-p" gnus-summary-prev-same-subject
1769   "\M-n" gnus-summary-next-unread-subject
1770   "\M-p" gnus-summary-prev-unread-subject
1771   "." gnus-summary-first-unread-article
1772   "," gnus-summary-best-unread-article
1773   "\M-s" gnus-summary-search-article-forward
1774   "\M-r" gnus-summary-search-article-backward
1775   "\M-S" gnus-summary-repeat-search-article-forward
1776   "\M-R" gnus-summary-repeat-search-article-backward
1777   "<" gnus-summary-beginning-of-article
1778   ">" gnus-summary-end-of-article
1779   "j" gnus-summary-goto-article
1780   "^" gnus-summary-refer-parent-article
1781   "\M-^" gnus-summary-refer-article
1782   "u" gnus-summary-tick-article-forward
1783   "!" gnus-summary-tick-article-forward
1784   "U" gnus-summary-tick-article-backward
1785   "d" gnus-summary-mark-as-read-forward
1786   "D" gnus-summary-mark-as-read-backward
1787   "E" gnus-summary-mark-as-expirable
1788   "\M-u" gnus-summary-clear-mark-forward
1789   "\M-U" gnus-summary-clear-mark-backward
1790   "k" gnus-summary-kill-same-subject-and-select
1791   "\C-k" gnus-summary-kill-same-subject
1792   "\M-\C-k" gnus-summary-kill-thread
1793   "\M-\C-l" gnus-summary-lower-thread
1794   "e" gnus-summary-edit-article
1795   "#" gnus-summary-mark-as-processable
1796   "\M-#" gnus-summary-unmark-as-processable
1797   "\M-\C-t" gnus-summary-toggle-threads
1798   "\M-\C-s" gnus-summary-show-thread
1799   "\M-\C-h" gnus-summary-hide-thread
1800   "\M-\C-f" gnus-summary-next-thread
1801   "\M-\C-b" gnus-summary-prev-thread
1802   [(meta down)] gnus-summary-next-thread
1803   [(meta up)] gnus-summary-prev-thread
1804   "\M-\C-u" gnus-summary-up-thread
1805   "\M-\C-d" gnus-summary-down-thread
1806   "&" gnus-summary-execute-command
1807   "c" gnus-summary-catchup-and-exit
1808   "\C-w" gnus-summary-mark-region-as-read
1809   "\C-t" gnus-summary-toggle-truncation
1810   "?" gnus-summary-mark-as-dormant
1811   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1812   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1813   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1814   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1815   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1816   "\C-c\C-s\C-t" gnus-summary-sort-by-recipient
1817   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1818   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1819   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1820   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1821   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1822   "=" gnus-summary-expand-window
1823   "\C-x\C-s" gnus-summary-reselect-current-group
1824   "\M-g" gnus-summary-rescan-group
1825   "w" gnus-summary-stop-page-breaking
1826   "\C-c\C-r" gnus-summary-caesar-message
1827   "f" gnus-summary-followup
1828   "F" gnus-summary-followup-with-original
1829   "C" gnus-summary-cancel-article
1830   "r" gnus-summary-reply
1831   "R" gnus-summary-reply-with-original
1832   "\C-c\C-f" gnus-summary-mail-forward
1833   "o" gnus-summary-save-article
1834   "\C-o" gnus-summary-save-article-mail
1835   "|" gnus-summary-pipe-output
1836   "\M-k" gnus-summary-edit-local-kill
1837   "\M-K" gnus-summary-edit-global-kill
1838   ;; "V" gnus-version
1839   "\C-c\C-d" gnus-summary-describe-group
1840   "q" gnus-summary-exit
1841   "Q" gnus-summary-exit-no-update
1842   "\C-c\C-i" gnus-info-find-node
1843   gnus-mouse-2 gnus-mouse-pick-article
1844   [follow-link] mouse-face
1845   "m" gnus-summary-mail-other-window
1846   "a" gnus-summary-post-news
1847   "i" gnus-summary-news-other-window
1848   "x" gnus-summary-limit-to-unread
1849   "s" gnus-summary-isearch-article
1850   "t" gnus-summary-toggle-header
1851   "g" gnus-summary-show-article
1852   "l" gnus-summary-goto-last-article
1853   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1854   "\C-d" gnus-summary-enter-digest-group
1855   "\M-\C-d" gnus-summary-read-document
1856   "\M-\C-e" gnus-summary-edit-parameters
1857   "\M-\C-a" gnus-summary-customize-parameters
1858   "\C-c\C-b" gnus-bug
1859   "*" gnus-cache-enter-article
1860   "\M-*" gnus-cache-remove-article
1861   "\M-&" gnus-summary-universal-argument
1862   "\C-l" gnus-recenter
1863   "I" gnus-summary-increase-score
1864   "L" gnus-summary-lower-score
1865   "\M-i" gnus-symbolic-argument
1866   "h" gnus-summary-select-article-buffer
1867
1868   "b" gnus-article-view-part
1869   "\M-t" gnus-summary-toggle-display-buttonized
1870
1871   "V" gnus-summary-score-map
1872   "X" gnus-uu-extract-map
1873   "S" gnus-summary-send-map)
1874
1875 ;; Sort of orthogonal keymap
1876 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1877   "t" gnus-summary-tick-article-forward
1878   "!" gnus-summary-tick-article-forward
1879   "d" gnus-summary-mark-as-read-forward
1880   "r" gnus-summary-mark-as-read-forward
1881   "c" gnus-summary-clear-mark-forward
1882   " " gnus-summary-clear-mark-forward
1883   "e" gnus-summary-mark-as-expirable
1884   "x" gnus-summary-mark-as-expirable
1885   "?" gnus-summary-mark-as-dormant
1886   "b" gnus-summary-set-bookmark
1887   "B" gnus-summary-remove-bookmark
1888   "#" gnus-summary-mark-as-processable
1889   "\M-#" gnus-summary-unmark-as-processable
1890   "S" gnus-summary-limit-include-expunged
1891   "C" gnus-summary-catchup
1892   "H" gnus-summary-catchup-to-here
1893   "h" gnus-summary-catchup-from-here
1894   "\C-c" gnus-summary-catchup-all
1895   "k" gnus-summary-kill-same-subject-and-select
1896   "K" gnus-summary-kill-same-subject
1897   "P" gnus-uu-mark-map)
1898
1899 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1900   "c" gnus-summary-clear-above
1901   "u" gnus-summary-tick-above
1902   "m" gnus-summary-mark-above
1903   "k" gnus-summary-kill-below)
1904
1905 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1906   "/" gnus-summary-limit-to-subject
1907   "n" gnus-summary-limit-to-articles
1908   "b" gnus-summary-limit-to-bodies
1909   "h" gnus-summary-limit-to-headers
1910   "w" gnus-summary-pop-limit
1911   "s" gnus-summary-limit-to-subject
1912   "a" gnus-summary-limit-to-author
1913   "u" gnus-summary-limit-to-unread
1914   "m" gnus-summary-limit-to-marks
1915   "M" gnus-summary-limit-exclude-marks
1916   "v" gnus-summary-limit-to-score
1917   "*" gnus-summary-limit-include-cached
1918   "D" gnus-summary-limit-include-dormant
1919   "T" gnus-summary-limit-include-thread
1920   "d" gnus-summary-limit-exclude-dormant
1921   "t" gnus-summary-limit-to-age
1922   "." gnus-summary-limit-to-unseen
1923   "x" gnus-summary-limit-to-extra
1924   "p" gnus-summary-limit-to-display-predicate
1925   "E" gnus-summary-limit-include-expunged
1926   "c" gnus-summary-limit-exclude-childless-dormant
1927   "C" gnus-summary-limit-mark-excluded-as-read
1928   "o" gnus-summary-insert-old-articles
1929   "N" gnus-summary-insert-new-articles
1930   "S" gnus-summary-limit-to-singletons
1931   "r" gnus-summary-limit-to-replied
1932   "R" gnus-summary-limit-to-recipient
1933   "A" gnus-summary-limit-to-address)
1934
1935 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1936   "n" gnus-summary-next-unread-article
1937   "p" gnus-summary-prev-unread-article
1938   "N" gnus-summary-next-article
1939   "P" gnus-summary-prev-article
1940   "\C-n" gnus-summary-next-same-subject
1941   "\C-p" gnus-summary-prev-same-subject
1942   "\M-n" gnus-summary-next-unread-subject
1943   "\M-p" gnus-summary-prev-unread-subject
1944   "f" gnus-summary-first-unread-article
1945   "b" gnus-summary-best-unread-article
1946   "j" gnus-summary-goto-article
1947   "g" gnus-summary-goto-subject
1948   "l" gnus-summary-goto-last-article
1949   "o" gnus-summary-pop-article)
1950
1951 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1952   "k" gnus-summary-kill-thread
1953   "E" gnus-summary-expire-thread
1954   "l" gnus-summary-lower-thread
1955   "i" gnus-summary-raise-thread
1956   "T" gnus-summary-toggle-threads
1957   "t" gnus-summary-rethread-current
1958   "^" gnus-summary-reparent-thread
1959   "\M-^" gnus-summary-reparent-children
1960   "s" gnus-summary-show-thread
1961   "S" gnus-summary-show-all-threads
1962   "h" gnus-summary-hide-thread
1963   "H" gnus-summary-hide-all-threads
1964   "n" gnus-summary-next-thread
1965   "p" gnus-summary-prev-thread
1966   "u" gnus-summary-up-thread
1967   "o" gnus-summary-top-thread
1968   "d" gnus-summary-down-thread
1969   "#" gnus-uu-mark-thread
1970   "\M-#" gnus-uu-unmark-thread)
1971
1972 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1973   "g" gnus-summary-prepare
1974   "c" gnus-summary-insert-cached-articles
1975   "d" gnus-summary-insert-dormant-articles
1976   "t" gnus-summary-insert-ticked-articles)
1977
1978 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1979   "c" gnus-summary-catchup-and-exit
1980   "C" gnus-summary-catchup-all-and-exit
1981   "E" gnus-summary-exit-no-update
1982   "Q" gnus-summary-exit
1983   "Z" gnus-summary-exit
1984   "n" gnus-summary-catchup-and-goto-next-group
1985   "p" gnus-summary-catchup-and-goto-prev-group
1986   "R" gnus-summary-reselect-current-group
1987   "G" gnus-summary-rescan-group
1988   "N" gnus-summary-next-group
1989   "s" gnus-summary-save-newsrc
1990   "P" gnus-summary-prev-group)
1991
1992 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1993   " " gnus-summary-next-page
1994   "n" gnus-summary-next-page
1995   "\177" gnus-summary-prev-page
1996   [delete] gnus-summary-prev-page
1997   "p" gnus-summary-prev-page
1998   "\r" gnus-summary-scroll-up
1999   "\M-\r" gnus-summary-scroll-down
2000   "<" gnus-summary-beginning-of-article
2001   ">" gnus-summary-end-of-article
2002   "b" gnus-summary-beginning-of-article
2003   "e" gnus-summary-end-of-article
2004   "^" gnus-summary-refer-parent-article
2005   "r" gnus-summary-refer-parent-article
2006   "D" gnus-summary-enter-digest-group
2007   "R" gnus-summary-refer-references
2008   "T" gnus-summary-refer-thread
2009   "g" gnus-summary-show-article
2010   "s" gnus-summary-isearch-article
2011   "P" gnus-summary-print-article
2012   "S" gnus-sticky-article
2013   "M" gnus-mailing-list-insinuate
2014   "t" gnus-article-babel)
2015
2016 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
2017   "b" gnus-article-add-buttons
2018   "B" gnus-article-add-buttons-to-head
2019   "o" gnus-article-treat-overstrike
2020   "e" gnus-article-emphasize
2021   "w" gnus-article-fill-cited-article
2022   "Q" gnus-article-fill-long-lines
2023   "L" gnus-article-toggle-truncate-lines
2024   "C" gnus-article-capitalize-sentences
2025   "c" gnus-article-remove-cr
2026   "q" gnus-article-de-quoted-unreadable
2027   "6" gnus-article-de-base64-unreadable
2028   "Z" gnus-article-decode-HZ
2029   "A" gnus-article-treat-ansi-sequences
2030   "h" gnus-article-wash-html
2031   "u" gnus-article-unsplit-urls
2032   "s" gnus-summary-force-verify-and-decrypt
2033   "f" gnus-article-display-x-face
2034   "l" gnus-summary-stop-page-breaking
2035   "r" gnus-summary-caesar-message
2036   "m" gnus-summary-morse-message
2037   "t" gnus-summary-toggle-header
2038   "g" gnus-treat-smiley
2039   "v" gnus-summary-verbose-headers
2040   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
2041   "p" gnus-article-verify-x-pgp-sig
2042   "d" gnus-article-treat-dumbquotes
2043   "i" gnus-summary-idna-message)
2044
2045 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
2046   ;; mnemonic: deuglif*Y*
2047   "u" gnus-article-outlook-unwrap-lines
2048   "a" gnus-article-outlook-repair-attribution
2049   "c" gnus-article-outlook-rearrange-citation
2050   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
2051
2052 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
2053   "a" gnus-article-hide
2054   "h" gnus-article-hide-headers
2055   "b" gnus-article-hide-boring-headers
2056   "s" gnus-article-hide-signature
2057   "c" gnus-article-hide-citation
2058   "C" gnus-article-hide-citation-in-followups
2059   "l" gnus-article-hide-list-identifiers
2060   "B" gnus-article-strip-banner
2061   "P" gnus-article-hide-pem
2062   "\C-c" gnus-article-hide-citation-maybe)
2063
2064 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
2065   "a" gnus-article-highlight
2066   "h" gnus-article-highlight-headers
2067   "c" gnus-article-highlight-citation
2068   "s" gnus-article-highlight-signature)
2069
2070 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
2071   "f" gnus-article-treat-fold-headers
2072   "u" gnus-article-treat-unfold-headers
2073   "n" gnus-article-treat-fold-newsgroups)
2074
2075 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
2076   "x" gnus-article-display-x-face
2077   "d" gnus-article-display-face
2078   "s" gnus-treat-smiley
2079   "D" gnus-article-remove-images
2080   "f" gnus-treat-from-picon
2081   "m" gnus-treat-mail-picon
2082   "n" gnus-treat-newsgroups-picon)
2083
2084 (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
2085   "w" gnus-article-decode-mime-words
2086   "c" gnus-article-decode-charset
2087   "v" gnus-mime-view-all-parts
2088   "b" gnus-article-view-part)
2089
2090 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
2091   "z" gnus-article-date-ut
2092   "u" gnus-article-date-ut
2093   "l" gnus-article-date-local
2094   "p" gnus-article-date-english
2095   "e" gnus-article-date-lapsed
2096   "o" gnus-article-date-original
2097   "i" gnus-article-date-iso8601
2098   "s" gnus-article-date-user)
2099
2100 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2101   "t" gnus-article-remove-trailing-blank-lines
2102   "l" gnus-article-strip-leading-blank-lines
2103   "m" gnus-article-strip-multiple-blank-lines
2104   "a" gnus-article-strip-blank-lines
2105   "A" gnus-article-strip-all-blank-lines
2106   "s" gnus-article-strip-leading-space
2107   "e" gnus-article-strip-trailing-space
2108   "w" gnus-article-remove-leading-whitespace)
2109
2110 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2111   "v" gnus-version
2112   "f" gnus-summary-fetch-faq
2113   "d" gnus-summary-describe-group
2114   "h" gnus-summary-describe-briefly
2115   "i" gnus-info-find-node
2116   "c" gnus-group-fetch-charter
2117   "C" gnus-group-fetch-control)
2118
2119 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2120   "e" gnus-summary-expire-articles
2121   "\M-\C-e" gnus-summary-expire-articles-now
2122   "\177" gnus-summary-delete-article
2123   [delete] gnus-summary-delete-article
2124   [backspace] gnus-summary-delete-article
2125   "m" gnus-summary-move-article
2126   "r" gnus-summary-respool-article
2127   "w" gnus-summary-edit-article
2128   "c" gnus-summary-copy-article
2129   "B" gnus-summary-crosspost-article
2130   "q" gnus-summary-respool-query
2131   "t" gnus-summary-respool-trace
2132   "i" gnus-summary-import-article
2133   "I" gnus-summary-create-article
2134   "p" gnus-summary-article-posted-p)
2135
2136 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2137   "o" gnus-summary-save-article
2138   "m" gnus-summary-save-article-mail
2139   "F" gnus-summary-write-article-file
2140   "r" gnus-summary-save-article-rmail
2141   "f" gnus-summary-save-article-file
2142   "b" gnus-summary-save-article-body-file
2143   "B" gnus-summary-write-article-body-file
2144   "h" gnus-summary-save-article-folder
2145   "v" gnus-summary-save-article-vm
2146   "p" gnus-summary-pipe-output
2147   "P" gnus-summary-muttprint
2148   "s" gnus-soup-add-article)
2149
2150 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2151   "b" gnus-summary-display-buttonized
2152   "m" gnus-summary-repair-multipart
2153   "v" gnus-article-view-part
2154   "o" gnus-article-save-part
2155   "O" gnus-article-save-part-and-strip
2156   "r" gnus-article-replace-part
2157   "d" gnus-article-delete-part
2158   "t" gnus-article-view-part-as-type
2159   "j" gnus-article-jump-to-part
2160   "c" gnus-article-copy-part
2161   "C" gnus-article-view-part-as-charset
2162   "e" gnus-article-view-part-externally
2163   "H" gnus-article-browse-html-article
2164   "E" gnus-article-encrypt-body
2165   "i" gnus-article-inline-part
2166   "|" gnus-article-pipe-part)
2167
2168 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2169   "p" gnus-summary-mark-as-processable
2170   "u" gnus-summary-unmark-as-processable
2171   "U" gnus-summary-unmark-all-processable
2172   "v" gnus-uu-mark-over
2173   "s" gnus-uu-mark-series
2174   "r" gnus-uu-mark-region
2175   "g" gnus-uu-unmark-region
2176   "R" gnus-uu-mark-by-regexp
2177   "G" gnus-uu-unmark-by-regexp
2178   "t" gnus-uu-mark-thread
2179   "T" gnus-uu-unmark-thread
2180   "a" gnus-uu-mark-all
2181   "b" gnus-uu-mark-buffer
2182   "S" gnus-uu-mark-sparse
2183   "k" gnus-summary-kill-process-mark
2184   "y" gnus-summary-yank-process-mark
2185   "w" gnus-summary-save-process-mark
2186   "i" gnus-uu-invert-processable)
2187
2188 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2189   ;;"x" gnus-uu-extract-any
2190   "m" gnus-summary-save-parts
2191   "u" gnus-uu-decode-uu
2192   "U" gnus-uu-decode-uu-and-save
2193   "s" gnus-uu-decode-unshar
2194   "S" gnus-uu-decode-unshar-and-save
2195   "o" gnus-uu-decode-save
2196   "O" gnus-uu-decode-save
2197   "b" gnus-uu-decode-binhex
2198   "B" gnus-uu-decode-binhex
2199   "p" gnus-uu-decode-postscript
2200   "P" gnus-uu-decode-postscript-and-save)
2201
2202 (gnus-define-keys
2203     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2204   "u" gnus-uu-decode-uu-view
2205   "U" gnus-uu-decode-uu-and-save-view
2206   "s" gnus-uu-decode-unshar-view
2207   "S" gnus-uu-decode-unshar-and-save-view
2208   "o" gnus-uu-decode-save-view
2209   "O" gnus-uu-decode-save-view
2210   "b" gnus-uu-decode-binhex-view
2211   "B" gnus-uu-decode-binhex-view
2212   "p" gnus-uu-decode-postscript-view
2213   "P" gnus-uu-decode-postscript-and-save-view)
2214
2215 (defvar gnus-article-post-menu nil)
2216
2217 (defconst gnus-summary-menu-maxlen 20)
2218
2219 (defun gnus-summary-menu-split (menu)
2220   ;; If we have lots of elements, divide them into groups of 20
2221   ;; and make a pane (or submenu) for each one.
2222   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2223       (let ((menu menu) sublists next
2224             (i 1))
2225         (while menu
2226           ;; Pull off the next gnus-summary-menu-maxlen elements
2227           ;; and make them the next element of sublist.
2228           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2229           (if next
2230               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2231                       nil))
2232           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2233                                              (aref (car (last menu)) 0)) menu)
2234                                sublists))
2235           (setq i (1+ i))
2236           (setq menu next))
2237         (nreverse sublists))
2238     ;; Few elements--put them all in one pane.
2239     menu))
2240
2241 (defun gnus-summary-make-menu-bar ()
2242   (gnus-turn-off-edit-menu 'summary)
2243
2244   (unless (boundp 'gnus-summary-misc-menu)
2245
2246     (easy-menu-define
2247       gnus-summary-kill-menu gnus-summary-mode-map ""
2248       (cons
2249        "Score"
2250        (nconc
2251         (list
2252          ["Customize" gnus-score-customize t])
2253         (gnus-make-score-map 'increase)
2254         (gnus-make-score-map 'lower)
2255         '(("Mark"
2256            ["Kill below" gnus-summary-kill-below t]
2257            ["Mark above" gnus-summary-mark-above t]
2258            ["Tick above" gnus-summary-tick-above t]
2259            ["Clear above" gnus-summary-clear-above t])
2260           ["Current score" gnus-summary-current-score t]
2261           ["Set score" gnus-summary-set-score t]
2262           ["Switch current score file..." gnus-score-change-score-file t]
2263           ["Set mark below..." gnus-score-set-mark-below t]
2264           ["Set expunge below..." gnus-score-set-expunge-below t]
2265           ["Edit current score file" gnus-score-edit-current-scores t]
2266           ["Edit score file" gnus-score-edit-file t]
2267           ["Trace score" gnus-score-find-trace t]
2268           ["Find words" gnus-score-find-favourite-words t]
2269           ["Rescore buffer" gnus-summary-rescore t]
2270           ["Increase score..." gnus-summary-increase-score t]
2271           ["Lower score..." gnus-summary-lower-score t]))))
2272
2273     ;; Define both the Article menu in the summary buffer and the
2274     ;; equivalent Commands menu in the article buffer here for
2275     ;; consistency.
2276     (let ((innards
2277            `(("Hide"
2278               ["All" gnus-article-hide t]
2279               ["Headers" gnus-article-hide-headers t]
2280               ["Signature" gnus-article-hide-signature t]
2281               ["Citation" gnus-article-hide-citation t]
2282               ["List identifiers" gnus-article-hide-list-identifiers t]
2283               ["Banner" gnus-article-strip-banner t]
2284               ["Boring headers" gnus-article-hide-boring-headers t])
2285              ("Highlight"
2286               ["All" gnus-article-highlight t]
2287               ["Headers" gnus-article-highlight-headers t]
2288               ["Signature" gnus-article-highlight-signature t]
2289               ["Citation" gnus-article-highlight-citation t])
2290              ("MIME"
2291               ["Words" gnus-article-decode-mime-words t]
2292               ["Charset" gnus-article-decode-charset t]
2293               ["QP" gnus-article-de-quoted-unreadable t]
2294               ["Base64" gnus-article-de-base64-unreadable t]
2295               ["View MIME buttons" gnus-summary-display-buttonized t]
2296               ["View all" gnus-mime-view-all-parts t]
2297               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2298               ["Encrypt body" gnus-article-encrypt-body
2299                :active (not (gnus-group-read-only-p))
2300                ,@(if (featurep 'xemacs) nil
2301                    '(:help "Encrypt the message body on disk"))]
2302               ["Extract all parts..." gnus-summary-save-parts t]
2303               ("Multipart"
2304                ["Repair multipart" gnus-summary-repair-multipart t]
2305                ["Pipe part..." gnus-article-pipe-part t]
2306                ["Inline part" gnus-article-inline-part t]
2307                ["View part as type..." gnus-article-view-part-as-type t]
2308                ["Encrypt body" gnus-article-encrypt-body
2309                 :active (not (gnus-group-read-only-p))
2310                ,@(if (featurep 'xemacs) nil
2311                    '(:help "Encrypt the message body on disk"))]
2312                ["View part externally" gnus-article-view-part-externally t]
2313                ["View HTML parts in browser" gnus-article-browse-html-article t]
2314                ["View part with charset..." gnus-article-view-part-as-charset t]
2315                ["Copy part" gnus-article-copy-part t]
2316                ["Save part..." gnus-article-save-part t]
2317                ["View part" gnus-article-view-part t]))
2318              ("Date"
2319               ["Local" gnus-article-date-local t]
2320               ["ISO8601" gnus-article-date-iso8601 t]
2321               ["UT" gnus-article-date-ut t]
2322               ["Original" gnus-article-date-original t]
2323               ["Lapsed" gnus-article-date-lapsed t]
2324               ["User-defined" gnus-article-date-user t])
2325              ("Display"
2326               ["Remove images" gnus-article-remove-images t]
2327               ["Toggle smiley" gnus-treat-smiley t]
2328               ["Show X-Face" gnus-article-display-x-face t]
2329               ["Show picons in From" gnus-treat-from-picon t]
2330               ["Show picons in mail headers" gnus-treat-mail-picon t]
2331               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2332               ("View as different encoding"
2333                ,@(gnus-summary-menu-split
2334                   (mapcar
2335                    (lambda (cs)
2336                      ;; Since easymenu under Emacs doesn't allow
2337                      ;; lambda forms for menu commands, we should
2338                      ;; provide intern'ed function symbols.
2339                      (let ((command (intern (format "\
2340 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2341                        (fset command
2342                              `(lambda ()
2343                                 (interactive)
2344                                 (let ((gnus-summary-show-article-charset-alist
2345                                        '((1 . ,cs))))
2346                                   (gnus-summary-show-article 1))))
2347                        `[,(symbol-name cs) ,command t]))
2348                    (sort (if (fboundp 'coding-system-list)
2349                              (coding-system-list)
2350                            (mapcar 'car mm-mime-mule-charset-alist))
2351                          'string<)))))
2352              ("Washing"
2353               ("Remove Blanks"
2354                ["Leading" gnus-article-strip-leading-blank-lines t]
2355                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2356                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2357                ["All of the above" gnus-article-strip-blank-lines t]
2358                ["All" gnus-article-strip-all-blank-lines t]
2359                ["Leading space" gnus-article-strip-leading-space t]
2360                ["Trailing space" gnus-article-strip-trailing-space t]
2361                ["Leading space in headers"
2362                 gnus-article-remove-leading-whitespace t])
2363               ["Overstrike" gnus-article-treat-overstrike t]
2364               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2365               ["Emphasis" gnus-article-emphasize t]
2366               ["Word wrap" gnus-article-fill-cited-article t]
2367               ["Fill long lines" gnus-article-fill-long-lines t]
2368               ["Toggle truncate long lines" gnus-article-toggle-truncate-lines t]
2369               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2370               ["Remove CR" gnus-article-remove-cr t]
2371               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2372               ["Base64" gnus-article-de-base64-unreadable t]
2373               ["Rot 13" gnus-summary-caesar-message
2374                ,@(if (featurep 'xemacs) '(t)
2375                    '(:help "\"Caesar rotate\" article by 13"))]
2376               ["De-IDNA" gnus-summary-idna-message t]
2377               ["Morse decode" gnus-summary-morse-message t]
2378               ["Unix pipe..." gnus-summary-pipe-message t]
2379               ["Add buttons" gnus-article-add-buttons t]
2380               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2381               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2382               ["Verbose header" gnus-summary-verbose-headers t]
2383               ["Toggle header" gnus-summary-toggle-header t]
2384               ["Unfold headers" gnus-article-treat-unfold-headers t]
2385               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2386               ["Html" gnus-article-wash-html t]
2387               ["Unsplit URLs" gnus-article-unsplit-urls t]
2388               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2389               ["Decode HZ" gnus-article-decode-HZ t]
2390               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2391               ("(Outlook) Deuglify"
2392                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2393                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2394                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2395                ["Full (Outlook) deuglify"
2396                 gnus-article-outlook-deuglify-article t])
2397               )
2398              ("Output"
2399               ["Save in default format..." gnus-summary-save-article
2400                ,@(if (featurep 'xemacs) '(t)
2401                    '(:help "Save article using default method"))]
2402               ["Save in file..." gnus-summary-save-article-file
2403                ,@(if (featurep 'xemacs) '(t)
2404                    '(:help "Save article in file"))]
2405               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2406               ["Save in MH folder..." gnus-summary-save-article-folder t]
2407               ["Save in VM folder..." gnus-summary-save-article-vm t]
2408               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2409               ["Save body in file..." gnus-summary-save-article-body-file t]
2410               ["Pipe through a filter..." gnus-summary-pipe-output t]
2411               ["Add to SOUP packet" gnus-soup-add-article t]
2412               ["Print with Muttprint..." gnus-summary-muttprint t]
2413               ["Print" gnus-summary-print-article
2414                ,@(if (featurep 'xemacs) '(t)
2415                    '(:help "Generate and print a PostScript image"))])
2416              ("Copy, move,... (Backend)"
2417               ,@(if (featurep 'xemacs) nil
2418                   '(:help "Copying, moving, expiring articles..."))
2419               ["Respool article..." gnus-summary-respool-article t]
2420               ["Move article..." gnus-summary-move-article
2421                (gnus-check-backend-function
2422                 'request-move-article gnus-newsgroup-name)]
2423               ["Copy article..." gnus-summary-copy-article t]
2424               ["Crosspost article..." gnus-summary-crosspost-article
2425                (gnus-check-backend-function
2426                 'request-replace-article gnus-newsgroup-name)]
2427               ["Import file..." gnus-summary-import-article
2428                (gnus-check-backend-function
2429                 'request-accept-article gnus-newsgroup-name)]
2430               ["Create article..." gnus-summary-create-article
2431                (gnus-check-backend-function
2432                 'request-accept-article gnus-newsgroup-name)]
2433               ["Check if posted" gnus-summary-article-posted-p t]
2434               ["Edit article" gnus-summary-edit-article
2435                (not (gnus-group-read-only-p))]
2436               ["Delete article" gnus-summary-delete-article
2437                (gnus-check-backend-function
2438                 'request-expire-articles gnus-newsgroup-name)]
2439               ["Query respool" gnus-summary-respool-query t]
2440               ["Trace respool" gnus-summary-respool-trace t]
2441               ["Delete expirable articles" gnus-summary-expire-articles-now
2442                (gnus-check-backend-function
2443                 'request-expire-articles gnus-newsgroup-name)])
2444              ("Extract"
2445               ["Uudecode" gnus-uu-decode-uu
2446                ,@(if (featurep 'xemacs) '(t)
2447                    '(:help "Decode uuencoded article(s)"))]
2448               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2449               ["Unshar" gnus-uu-decode-unshar t]
2450               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2451               ["Save" gnus-uu-decode-save t]
2452               ["Binhex" gnus-uu-decode-binhex t]
2453               ["Postscript" gnus-uu-decode-postscript t]
2454               ["All MIME parts" gnus-summary-save-parts t])
2455              ("Cache"
2456               ["Enter article" gnus-cache-enter-article t]
2457               ["Remove article" gnus-cache-remove-article t])
2458              ["Translate" gnus-article-babel t]
2459              ["Select article buffer" gnus-summary-select-article-buffer t]
2460              ["Make article buffer sticky" gnus-sticky-article t]
2461              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2462              ["Isearch article..." gnus-summary-isearch-article t]
2463              ["Beginning of the article" gnus-summary-beginning-of-article t]
2464              ["End of the article" gnus-summary-end-of-article t]
2465              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2466              ["Fetch referenced articles" gnus-summary-refer-references t]
2467              ["Fetch current thread" gnus-summary-refer-thread t]
2468              ["Fetch article with id..." gnus-summary-refer-article t]
2469              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2470              ["Redisplay" gnus-summary-show-article t]
2471              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2472       (easy-menu-define
2473         gnus-summary-article-menu gnus-summary-mode-map ""
2474         (cons "Article" innards))
2475
2476       (if (not (keymapp gnus-summary-article-menu))
2477           (easy-menu-define
2478             gnus-article-commands-menu gnus-article-mode-map ""
2479             (cons "Commands" innards))
2480         ;; in Emacs, don't share menu.
2481         (setq gnus-article-commands-menu
2482               (copy-keymap gnus-summary-article-menu))
2483         (define-key gnus-article-mode-map [menu-bar commands]
2484           (cons "Commands" gnus-article-commands-menu))))
2485
2486     (easy-menu-define
2487       gnus-summary-thread-menu gnus-summary-mode-map ""
2488       '("Threads"
2489         ["Find all messages in thread" gnus-summary-refer-thread t]
2490         ["Toggle threading" gnus-summary-toggle-threads t]
2491         ["Hide threads" gnus-summary-hide-all-threads t]
2492         ["Show threads" gnus-summary-show-all-threads t]
2493         ["Hide thread" gnus-summary-hide-thread t]
2494         ["Show thread" gnus-summary-show-thread t]
2495         ["Go to next thread" gnus-summary-next-thread t]
2496         ["Go to previous thread" gnus-summary-prev-thread t]
2497         ["Go down thread" gnus-summary-down-thread t]
2498         ["Go up thread" gnus-summary-up-thread t]
2499         ["Top of thread" gnus-summary-top-thread t]
2500         ["Mark thread as read" gnus-summary-kill-thread t]
2501         ["Mark thread as expired" gnus-summary-expire-thread t]
2502         ["Lower thread score" gnus-summary-lower-thread t]
2503         ["Raise thread score" gnus-summary-raise-thread t]
2504         ["Rethread current" gnus-summary-rethread-current t]))
2505
2506     (easy-menu-define
2507       gnus-summary-post-menu gnus-summary-mode-map ""
2508       `("Post"
2509         ["Send a message (mail or news)" gnus-summary-post-news
2510          ,@(if (featurep 'xemacs) '(t)
2511              '(:help "Compose a new message (mail or news)"))]
2512         ["Followup" gnus-summary-followup
2513          ,@(if (featurep 'xemacs) '(t)
2514              '(:help "Post followup to this article"))]
2515         ["Followup and yank" gnus-summary-followup-with-original
2516          ,@(if (featurep 'xemacs) '(t)
2517              '(:help "Post followup to this article, quoting its contents"))]
2518         ["Supersede article" gnus-summary-supersede-article t]
2519         ["Cancel article" gnus-summary-cancel-article
2520          ,@(if (featurep 'xemacs) '(t)
2521              '(:help "Cancel an article you posted"))]
2522         ["Reply" gnus-summary-reply t]
2523         ["Reply and yank" gnus-summary-reply-with-original t]
2524         ["Wide reply" gnus-summary-wide-reply t]
2525         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2526          ,@(if (featurep 'xemacs) '(t)
2527              '(:help "Mail a reply, quoting this article"))]
2528         ["Very wide reply" gnus-summary-very-wide-reply t]
2529         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2530          ,@(if (featurep 'xemacs) '(t)
2531              '(:help "Mail a very wide reply, quoting this article"))]
2532         ["Mail forward" gnus-summary-mail-forward t]
2533         ["Post forward" gnus-summary-post-forward t]
2534         ["Digest and mail" gnus-uu-digest-mail-forward t]
2535         ["Digest and post" gnus-uu-digest-post-forward t]
2536         ["Resend message" gnus-summary-resend-message t]
2537         ["Resend message edit" gnus-summary-resend-message-edit t]
2538         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2539         ["Send a mail" gnus-summary-mail-other-window t]
2540         ["Create a local message" gnus-summary-news-other-window t]
2541         ["Uuencode and post" gnus-uu-post-news
2542          ,@(if (featurep 'xemacs) '(t)
2543              '(:help "Post a uuencoded article"))]
2544         ["Followup via news" gnus-summary-followup-to-mail t]
2545         ["Followup via news and yank"
2546          gnus-summary-followup-to-mail-with-original t]
2547         ;;("Draft"
2548         ;;["Send" gnus-summary-send-draft t]
2549         ;;["Send bounced" gnus-resend-bounced-mail t])
2550         ))
2551
2552     (cond
2553      ((not (keymapp gnus-summary-post-menu))
2554       (setq gnus-article-post-menu gnus-summary-post-menu))
2555      ((not gnus-article-post-menu)
2556       ;; Don't share post menu.
2557       (setq gnus-article-post-menu
2558             (copy-keymap gnus-summary-post-menu))))
2559     (define-key gnus-article-mode-map [menu-bar post]
2560       (cons "Post" gnus-article-post-menu))
2561
2562     (easy-menu-define
2563       gnus-summary-misc-menu gnus-summary-mode-map ""
2564       `("Gnus"
2565         ("Mark Read"
2566          ["Mark as read" gnus-summary-mark-as-read-forward t]
2567          ["Mark same subject and select"
2568           gnus-summary-kill-same-subject-and-select t]
2569          ["Mark same subject" gnus-summary-kill-same-subject t]
2570          ["Catchup" gnus-summary-catchup
2571           ,@(if (featurep 'xemacs) '(t)
2572               '(:help "Mark unread articles in this group as read"))]
2573          ["Catchup all" gnus-summary-catchup-all t]
2574          ["Catchup to here" gnus-summary-catchup-to-here t]
2575          ["Catchup from here" gnus-summary-catchup-from-here t]
2576          ["Catchup region" gnus-summary-mark-region-as-read
2577           (gnus-mark-active-p)]
2578          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2579         ("Mark Various"
2580          ["Tick" gnus-summary-tick-article-forward t]
2581          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2582          ["Remove marks" gnus-summary-clear-mark-forward t]
2583          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2584          ["Set bookmark" gnus-summary-set-bookmark t]
2585          ["Remove bookmark" gnus-summary-remove-bookmark t])
2586         ("Limit to"
2587          ["Marks..." gnus-summary-limit-to-marks t]
2588          ["Subject..." gnus-summary-limit-to-subject t]
2589          ["Author..." gnus-summary-limit-to-author t]
2590          ["Recipient..." gnus-summary-limit-to-recipient t]
2591          ["Address..." gnus-summary-limit-to-address t]
2592          ["Age..." gnus-summary-limit-to-age t]
2593          ["Extra..." gnus-summary-limit-to-extra t]
2594          ["Score..." gnus-summary-limit-to-score t]
2595          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2596          ["Unread" gnus-summary-limit-to-unread t]
2597          ["Unseen" gnus-summary-limit-to-unseen t]
2598          ["Singletons" gnus-summary-limit-to-singletons t]
2599          ["Replied" gnus-summary-limit-to-replied t]
2600          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2601          ["Next or process marked articles" gnus-summary-limit-to-articles t]
2602          ["Pop limit" gnus-summary-pop-limit t]
2603          ["Show dormant" gnus-summary-limit-include-dormant t]
2604          ["Hide childless dormant"
2605           gnus-summary-limit-exclude-childless-dormant t]
2606          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2607          ["Hide marked" gnus-summary-limit-exclude-marks t]
2608          ["Show expunged" gnus-summary-limit-include-expunged t])
2609         ("Process Mark"
2610          ["Set mark" gnus-summary-mark-as-processable t]
2611          ["Remove mark" gnus-summary-unmark-as-processable t]
2612          ["Remove all marks" gnus-summary-unmark-all-processable t]
2613          ["Invert marks" gnus-uu-invert-processable t]
2614          ["Mark above" gnus-uu-mark-over t]
2615          ["Mark series" gnus-uu-mark-series t]
2616          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2617          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2618          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2619          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2620          ["Mark all" gnus-uu-mark-all t]
2621          ["Mark buffer" gnus-uu-mark-buffer t]
2622          ["Mark sparse" gnus-uu-mark-sparse t]
2623          ["Mark thread" gnus-uu-mark-thread t]
2624          ["Unmark thread" gnus-uu-unmark-thread t]
2625          ("Process Mark Sets"
2626           ["Kill" gnus-summary-kill-process-mark t]
2627           ["Yank" gnus-summary-yank-process-mark
2628            gnus-newsgroup-process-stack]
2629           ["Save" gnus-summary-save-process-mark t]
2630           ["Run command on marked..." gnus-summary-universal-argument t]))
2631         ("Scroll article"
2632          ["Page forward" gnus-summary-next-page
2633           ,@(if (featurep 'xemacs) '(t)
2634               '(:help "Show next page of article"))]
2635          ["Page backward" gnus-summary-prev-page
2636           ,@(if (featurep 'xemacs) '(t)
2637               '(:help "Show previous page of article"))]
2638          ["Line forward" gnus-summary-scroll-up t])
2639         ("Move"
2640          ["Next unread article" gnus-summary-next-unread-article t]
2641          ["Previous unread article" gnus-summary-prev-unread-article t]
2642          ["Next article" gnus-summary-next-article t]
2643          ["Previous article" gnus-summary-prev-article t]
2644          ["Next unread subject" gnus-summary-next-unread-subject t]
2645          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2646          ["Next article same subject" gnus-summary-next-same-subject t]
2647          ["Previous article same subject" gnus-summary-prev-same-subject t]
2648          ["First unread article" gnus-summary-first-unread-article t]
2649          ["Best unread article" gnus-summary-best-unread-article t]
2650          ["Go to subject number..." gnus-summary-goto-subject t]
2651          ["Go to article number..." gnus-summary-goto-article t]
2652          ["Go to the last article" gnus-summary-goto-last-article t]
2653          ["Pop article off history" gnus-summary-pop-article t])
2654         ("Sort"
2655          ["Sort by number" gnus-summary-sort-by-number t]
2656          ["Sort by author" gnus-summary-sort-by-author t]
2657          ["Sort by recipient" gnus-summary-sort-by-recipient t]
2658          ["Sort by subject" gnus-summary-sort-by-subject t]
2659          ["Sort by date" gnus-summary-sort-by-date t]
2660          ["Sort by score" gnus-summary-sort-by-score t]
2661          ["Sort by lines" gnus-summary-sort-by-lines t]
2662          ["Sort by characters" gnus-summary-sort-by-chars t]
2663          ["Randomize" gnus-summary-sort-by-random t]
2664          ["Original sort" gnus-summary-sort-by-original t])
2665         ("Help"
2666          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2667          ["Describe group" gnus-summary-describe-group t]
2668          ["Fetch charter" gnus-group-fetch-charter
2669           ,@(if (featurep 'xemacs) nil
2670               '(:help "Display the charter of the current group"))]
2671          ["Fetch control message" gnus-group-fetch-control
2672           ,@(if (featurep 'xemacs) nil
2673               '(:help "Display the archived control message for the current group"))]
2674          ["Read manual" gnus-info-find-node t])
2675         ("Modes"
2676          ["Pick and read" gnus-pick-mode t]
2677          ["Binary" gnus-binary-mode t])
2678         ("Regeneration"
2679          ["Regenerate" gnus-summary-prepare t]
2680          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2681          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2682          ["Insert ticked articles" gnus-summary-insert-ticked-articles t]
2683          ["Toggle threading" gnus-summary-toggle-threads t])
2684         ["See old articles" gnus-summary-insert-old-articles t]
2685         ["See new articles" gnus-summary-insert-new-articles t]
2686         ["Filter articles..." gnus-summary-execute-command t]
2687         ["Run command on articles..." gnus-summary-universal-argument t]
2688         ["Search articles forward..." gnus-summary-search-article-forward t]
2689         ["Search articles backward..." gnus-summary-search-article-backward t]
2690         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2691         ["Expand window" gnus-summary-expand-window t]
2692         ["Expire expirable articles" gnus-summary-expire-articles
2693          (gnus-check-backend-function
2694           'request-expire-articles gnus-newsgroup-name)]
2695         ["Edit local kill file" gnus-summary-edit-local-kill t]
2696         ["Edit main kill file" gnus-summary-edit-global-kill t]
2697         ["Edit group parameters" gnus-summary-edit-parameters t]
2698         ["Customize group parameters" gnus-summary-customize-parameters t]
2699         ["Send a bug report" gnus-bug t]
2700         ("Exit"
2701          ["Catchup and exit" gnus-summary-catchup-and-exit
2702           ,@(if (featurep 'xemacs) '(t)
2703               '(:help "Mark unread articles in this group as read, then exit"))]
2704          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2705          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2706          ["Catchup and goto prev" gnus-summary-catchup-and-goto-prev-group t]
2707          ["Exit group" gnus-summary-exit
2708           ,@(if (featurep 'xemacs) '(t)
2709               '(:help "Exit current group, return to group selection mode"))]
2710          ["Exit group without updating" gnus-summary-exit-no-update t]
2711          ["Exit and goto next group" gnus-summary-next-group t]
2712          ["Exit and goto prev group" gnus-summary-prev-group t]
2713          ["Reselect group" gnus-summary-reselect-current-group t]
2714          ["Rescan group" gnus-summary-rescan-group t]
2715          ["Update dribble" gnus-summary-save-newsrc t])))
2716
2717     (gnus-run-hooks 'gnus-summary-menu-hook)))
2718
2719 (defvar gnus-summary-tool-bar-map nil)
2720
2721 ;; Note: The :set function in the `gnus-summary-tool-bar*' variables will only
2722 ;; affect _new_ message buffers.  We might add a function that walks thru all
2723 ;; summary-mode buffers and force the update.
2724 (defun gnus-summary-tool-bar-update (&optional symbol value)
2725   "Update summary mode toolbar.
2726 Setter function for custom variables."
2727   (setq-default gnus-summary-tool-bar-map nil)
2728   (when symbol
2729     ;; When used as ":set" function:
2730     (set-default symbol value))
2731   (when (gnus-buffer-live-p gnus-summary-buffer)
2732     (with-current-buffer gnus-summary-buffer
2733       (gnus-summary-make-tool-bar))))
2734
2735 (defcustom gnus-summary-tool-bar (if (eq gmm-tool-bar-style 'gnome)
2736                                      'gnus-summary-tool-bar-gnome
2737                                    'gnus-summary-tool-bar-retro)
2738   "Specifies the Gnus summary tool bar.
2739
2740 It can be either a list or a symbol refering to a list.  See
2741 `gmm-tool-bar-from-list' for the format of the list.  The
2742 default key map is `gnus-summary-mode-map'.
2743
2744 Pre-defined symbols include `gnus-summary-tool-bar-gnome' and
2745 `gnus-summary-tool-bar-retro'."
2746   :type '(choice (const :tag "GNOME style" gnus-summary-tool-bar-gnome)
2747                  (const :tag "Retro look"  gnus-summary-tool-bar-retro)
2748                  (repeat :tag "User defined list" gmm-tool-bar-item)
2749                  (symbol))
2750   :version "23.0" ;; No Gnus
2751   :initialize 'custom-initialize-default
2752   :set 'gnus-summary-tool-bar-update
2753   :group 'gnus-summary)
2754
2755 (defcustom gnus-summary-tool-bar-gnome
2756   '((gnus-summary-post-news "mail/compose" nil)
2757     (gnus-summary-insert-new-articles "mail/inbox" nil
2758                                       :visible (or (not gnus-agent)
2759                                                    gnus-plugged))
2760     (gnus-summary-reply-with-original "mail/reply")
2761     (gnus-summary-reply "mail/reply" nil :visible nil)
2762     (gnus-summary-followup-with-original "mail/reply-all")
2763     (gnus-summary-followup "mail/reply-all" nil :visible nil)
2764     (gnus-summary-mail-forward "mail/forward")
2765     (gnus-summary-save-article "mail/save")
2766     (gnus-summary-search-article-forward "search" nil :visible nil)
2767     (gnus-summary-print-article "print")
2768     (gnus-summary-tick-article-forward "flag-followup" nil :visible nil)
2769     ;; Some new commands that may need more suitable icons:
2770     (gnus-summary-save-newsrc "save" nil :visible nil)
2771     ;; (gnus-summary-show-article "stock_message-display" nil :visible nil)
2772     (gnus-summary-prev-article "left-arrow")
2773     (gnus-summary-next-article "right-arrow")
2774     (gnus-summary-next-page "next-page")
2775     ;; (gnus-summary-enter-digest-group "right_arrow" nil :visible nil)
2776     ;;
2777     ;; Maybe some sort-by-... could be added:
2778     ;; (gnus-summary-sort-by-author "sort-a-z" nil :visible nil)
2779     ;; (gnus-summary-sort-by-date "sort-1-9" nil :visible nil)
2780     (gnus-summary-mark-as-expirable
2781      "delete" nil
2782      :visible (gnus-check-backend-function 'request-expire-articles
2783                                            gnus-newsgroup-name))
2784     (gnus-summary-mark-as-spam
2785      "mail/spam" t
2786      :visible (and (fboundp 'spam-group-ham-contents-p)
2787                    (spam-group-ham-contents-p gnus-newsgroup-name))
2788      :help "Mark as spam")
2789     (gnus-summary-mark-as-read-forward
2790      "mail/not-spam" nil
2791      :visible (and (fboundp 'spam-group-spam-contents-p)
2792                    (spam-group-spam-contents-p gnus-newsgroup-name)))
2793     ;;
2794     (gnus-summary-exit "exit")
2795     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
2796     (gnus-info-find-node "help"))
2797   "List of functions for the summary tool bar (GNOME style).
2798
2799 See `gmm-tool-bar-from-list' for the format of the list."
2800   :type '(repeat gmm-tool-bar-item)
2801   :version "23.0" ;; No Gnus
2802   :initialize 'custom-initialize-default
2803   :set 'gnus-summary-tool-bar-update
2804   :group 'gnus-summary)
2805
2806 (defcustom gnus-summary-tool-bar-retro
2807   '((gnus-summary-prev-unread-article "gnus/prev-ur")
2808     (gnus-summary-next-unread-article "gnus/next-ur")
2809     (gnus-summary-post-news "gnus/post")
2810     (gnus-summary-followup-with-original "gnus/fuwo")
2811     (gnus-summary-followup "gnus/followup")
2812     (gnus-summary-reply-with-original "gnus/reply-wo")
2813     (gnus-summary-reply "gnus/reply")
2814     (gnus-summary-caesar-message "gnus/rot13")
2815     (gnus-uu-decode-uu "gnus/uu-decode")
2816     (gnus-summary-save-article-file "gnus/save-aif")
2817     (gnus-summary-save-article "gnus/save-art")
2818     (gnus-uu-post-news "gnus/uu-post")
2819     (gnus-summary-catchup "gnus/catchup")
2820     (gnus-summary-catchup-and-exit "gnus/cu-exit")
2821     (gnus-summary-exit "gnus/exit-summ")
2822     ;; Some new command that may need more suitable icons:
2823     (gnus-summary-print-article "gnus/print" nil :visible nil)
2824     (gnus-summary-mark-as-expirable "gnus/close" nil :visible nil)
2825     (gnus-summary-save-newsrc "gnus/save" nil :visible nil)
2826     ;; (gnus-summary-enter-digest-group "gnus/right_arrow" nil :visible nil)
2827     (gnus-summary-search-article-forward "gnus/search" nil :visible nil)
2828     ;; (gnus-summary-insert-new-articles "gnus/paste" nil :visible nil)
2829     ;; (gnus-summary-toggle-threads "gnus/open" nil :visible nil)
2830     ;;
2831     (gnus-info-find-node "gnus/help" nil :visible nil))
2832   "List of functions for the summary tool bar (retro look).
2833
2834 See `gmm-tool-bar-from-list' for the format of the list."
2835   :type '(repeat gmm-tool-bar-item)
2836   :version "23.0" ;; No Gnus
2837   :initialize 'custom-initialize-default
2838   :set 'gnus-summary-tool-bar-update
2839   :group 'gnus-summary)
2840
2841 (defcustom gnus-summary-tool-bar-zap-list t
2842   "List of icon items from the global tool bar.
2843 These items are not displayed in the Gnus summary mode tool bar.
2844
2845 See `gmm-tool-bar-from-list' for the format of the list."
2846   :type 'gmm-tool-bar-zap-list
2847   :version "23.0" ;; No Gnus
2848   :initialize 'custom-initialize-default
2849   :set 'gnus-summary-tool-bar-update
2850   :group 'gnus-summary)
2851
2852 (defvar image-load-path)
2853
2854 (defun gnus-summary-make-tool-bar (&optional force)
2855   "Make a summary mode tool bar from `gnus-summary-tool-bar'.
2856 When FORCE, rebuild the tool bar."
2857   (when (and (not (featurep 'xemacs))
2858              (boundp 'tool-bar-mode)
2859              tool-bar-mode
2860              (or (not gnus-summary-tool-bar-map) force))
2861     (let* ((load-path
2862             (gmm-image-load-path-for-library "gnus"
2863                                              "mail/save.xpm"
2864                                              nil t))
2865            (image-load-path (cons (car load-path)
2866                                   (when (boundp 'image-load-path)
2867                                     image-load-path)))
2868            (map (gmm-tool-bar-from-list gnus-summary-tool-bar
2869                                         gnus-summary-tool-bar-zap-list
2870                                         'gnus-summary-mode-map)))
2871       (when map
2872         ;; Need to set `gnus-summary-tool-bar-map' because `gnus-article-mode'
2873         ;; uses it's value.
2874         (setq gnus-summary-tool-bar-map map))))
2875   (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map))
2876
2877 (defun gnus-score-set-default (var value)
2878   "A version of set that updates the GNU Emacs menu-bar."
2879   (set var value)
2880   ;; It is the message that forces the active status to be updated.
2881   (message ""))
2882
2883 (defun gnus-make-score-map (type)
2884   "Make a summary score map of type TYPE."
2885   (if t
2886       nil
2887     (let ((headers '(("author" "from" string)
2888                      ("subject" "subject" string)
2889                      ("article body" "body" string)
2890                      ("article head" "head" string)
2891                      ("xref" "xref" string)
2892                      ("extra header" "extra" string)
2893                      ("lines" "lines" number)
2894                      ("followups to author" "followup" string)))
2895           (types '((number ("less than" <)
2896                            ("greater than" >)
2897                            ("equal" =))
2898                    (string ("substring" s)
2899                            ("exact string" e)
2900                            ("fuzzy string" f)
2901                            ("regexp" r))))
2902           (perms '(("temporary" (current-time-string))
2903                    ("permanent" nil)
2904                    ("immediate" now)))
2905           header)
2906       (list
2907        (apply
2908         'nconc
2909         (list
2910          (if (eq type 'lower)
2911              "Lower score"
2912            "Increase score"))
2913         (let (outh)
2914           (while headers
2915             (setq header (car headers))
2916             (setq outh
2917                   (cons
2918                    (apply
2919                     'nconc
2920                     (list (car header))
2921                     (let ((ts (cdr (assoc (nth 2 header) types)))
2922                           outt)
2923                       (while ts
2924                         (setq outt
2925                               (cons
2926                                (apply
2927                                 'nconc
2928                                 (list (caar ts))
2929                                 (let ((ps perms)
2930                                       outp)
2931                                   (while ps
2932                                     (setq outp
2933                                           (cons
2934                                            (vector
2935                                             (caar ps)
2936                                             (list
2937                                              'gnus-summary-score-entry
2938                                              (nth 1 header)
2939                                              (if (or (string= (nth 1 header)
2940                                                               "head")
2941                                                      (string= (nth 1 header)
2942                                                               "body"))
2943                                                  ""
2944                                                (list 'gnus-summary-header
2945                                                      (nth 1 header)))
2946                                              (list 'quote (nth 1 (car ts)))
2947                                              (list 'gnus-score-delta-default
2948                                                    nil)
2949                                              (nth 1 (car ps))
2950                                              t)
2951                                             t)
2952                                            outp))
2953                                     (setq ps (cdr ps)))
2954                                   (list (nreverse outp))))
2955                                outt))
2956                         (setq ts (cdr ts)))
2957                       (list (nreverse outt))))
2958                    outh))
2959             (setq headers (cdr headers)))
2960           (list (nreverse outh))))))))
2961
2962 \f
2963
2964 (defun gnus-summary-mode (&optional group)
2965   "Major mode for reading articles.
2966
2967 All normal editing commands are switched off.
2968 \\<gnus-summary-mode-map>
2969 Each line in this buffer represents one article.  To read an
2970 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2971 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2972 respectively.
2973
2974 You can also post articles and send mail from this buffer.  To
2975 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2976 of an article, type `\\[gnus-summary-reply]'.
2977
2978 There are approx. one gazillion commands you can execute in this
2979 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2980
2981 The following commands are available:
2982
2983 \\{gnus-summary-mode-map}"
2984   (interactive)
2985   (kill-all-local-variables)
2986   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2987     (gnus-summary-make-local-variables))
2988   (gnus-summary-make-local-variables)
2989   (setq gnus-newsgroup-name group)
2990   (when (gnus-visual-p 'summary-menu 'menu)
2991     (gnus-summary-make-menu-bar)
2992     (gnus-summary-make-tool-bar))
2993   (gnus-make-thread-indent-array)
2994   (gnus-simplify-mode-line)
2995   (setq major-mode 'gnus-summary-mode)
2996   (setq mode-name "Summary")
2997   (make-local-variable 'minor-mode-alist)
2998   (use-local-map gnus-summary-mode-map)
2999   (buffer-disable-undo)
3000   (setq buffer-read-only t              ;Disable modification
3001         show-trailing-whitespace nil)
3002   (setq truncate-lines t)
3003   (setq selective-display t)
3004   (setq selective-display-ellipses t)   ;Display `...'
3005   (gnus-summary-set-display-table)
3006   (gnus-set-default-directory)
3007   (make-local-variable 'gnus-summary-line-format)
3008   (make-local-variable 'gnus-summary-line-format-spec)
3009   (make-local-variable 'gnus-summary-dummy-line-format)
3010   (make-local-variable 'gnus-summary-dummy-line-format-spec)
3011   (make-local-variable 'gnus-summary-mark-positions)
3012   (gnus-make-local-hook 'pre-command-hook)
3013   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
3014   (gnus-run-mode-hooks 'gnus-summary-mode-hook)
3015   (turn-on-gnus-mailing-list-mode)
3016   (mm-enable-multibyte)
3017   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
3018   (gnus-update-summary-mark-positions))
3019
3020 (defun gnus-summary-make-local-variables ()
3021   "Make all the local summary buffer variables."
3022   (let (global)
3023     (dolist (local gnus-summary-local-variables)
3024       (if (consp local)
3025           (progn
3026             (if (eq (cdr local) 'global)
3027                 ;; Copy the global value of the variable.
3028                 (setq global (symbol-value (car local)))
3029               ;; Use the value from the list.
3030               (setq global (eval (cdr local))))
3031             (set (make-local-variable (car local)) global))
3032         ;; Simple nil-valued local variable.
3033         (set (make-local-variable local) nil)))))
3034
3035 (defun gnus-summary-clear-local-variables ()
3036   (let ((locals gnus-summary-local-variables))
3037     (while locals
3038       (if (consp (car locals))
3039           (and (symbolp (caar locals))
3040                (set (caar locals) nil))
3041         (and (symbolp (car locals))
3042              (set (car locals) nil)))
3043       (setq locals (cdr locals)))))
3044
3045 ;; Summary data functions.
3046
3047 (defmacro gnus-data-number (data)
3048   `(car ,data))
3049
3050 (defmacro gnus-data-set-number (data number)
3051   `(setcar ,data ,number))
3052
3053 (defmacro gnus-data-mark (data)
3054   `(nth 1 ,data))
3055
3056 (defmacro gnus-data-set-mark (data mark)
3057   `(setcar (nthcdr 1 ,data) ,mark))
3058
3059 (defmacro gnus-data-pos (data)
3060   `(nth 2 ,data))
3061
3062 (defmacro gnus-data-set-pos (data pos)
3063   `(setcar (nthcdr 2 ,data) ,pos))
3064
3065 (defmacro gnus-data-header (data)
3066   `(nth 3 ,data))
3067
3068 (defmacro gnus-data-set-header (data header)
3069   `(setf (nth 3 ,data) ,header))
3070
3071 (defmacro gnus-data-level (data)
3072   `(nth 4 ,data))
3073
3074 (defmacro gnus-data-unread-p (data)
3075   `(= (nth 1 ,data) gnus-unread-mark))
3076
3077 (defmacro gnus-data-read-p (data)
3078   `(/= (nth 1 ,data) gnus-unread-mark))
3079
3080 (defmacro gnus-data-pseudo-p (data)
3081   `(consp (nth 3 ,data)))
3082
3083 (defmacro gnus-data-find (number)
3084   `(assq ,number gnus-newsgroup-data))
3085
3086 (defmacro gnus-data-find-list (number &optional data)
3087   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
3088      (memq (assq ,number bdata)
3089            bdata)))
3090
3091 (defmacro gnus-data-make (number mark pos header level)
3092   `(list ,number ,mark ,pos ,header ,level))
3093
3094 (defun gnus-data-enter (after-article number mark pos header level offset)
3095   (let ((data (gnus-data-find-list after-article)))
3096     (unless data
3097       (error "No such article: %d" after-article))
3098     (setcdr data (cons (gnus-data-make number mark pos header level)
3099                        (cdr data)))
3100     (setq gnus-newsgroup-data-reverse nil)
3101     (gnus-data-update-list (cddr data) offset)))
3102
3103 (defun gnus-data-enter-list (after-article list &optional offset)
3104   (when list
3105     (let ((data (and after-article (gnus-data-find-list after-article)))
3106           (ilist list))
3107       (if (not (or data
3108                    after-article))
3109           (let ((odata gnus-newsgroup-data))
3110             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
3111             (when offset
3112               (gnus-data-update-list odata offset)))
3113         ;; Find the last element in the list to be spliced into the main
3114         ;; list.
3115         (setq list (last list))
3116         (if (not data)
3117             (progn
3118               (setcdr list gnus-newsgroup-data)
3119               (setq gnus-newsgroup-data ilist)
3120               (when offset
3121                 (gnus-data-update-list (cdr list) offset)))
3122           (setcdr list (cdr data))
3123           (setcdr data ilist)
3124           (when offset
3125             (gnus-data-update-list (cdr list) offset))))
3126       (setq gnus-newsgroup-data-reverse nil))))
3127
3128 (defun gnus-data-remove (article &optional offset)
3129   (let ((data gnus-newsgroup-data))
3130     (if (= (gnus-data-number (car data)) article)
3131         (progn
3132           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
3133                 gnus-newsgroup-data-reverse nil)
3134           (when offset
3135             (gnus-data-update-list gnus-newsgroup-data offset)))
3136       (while (cdr data)
3137         (when (= (gnus-data-number (cadr data)) article)
3138           (setcdr data (cddr data))
3139           (when offset
3140             (gnus-data-update-list (cdr data) offset))
3141           (setq data nil
3142                 gnus-newsgroup-data-reverse nil))
3143         (setq data (cdr data))))))
3144
3145 (defmacro gnus-data-list (backward)
3146   `(if ,backward
3147        (or gnus-newsgroup-data-reverse
3148            (setq gnus-newsgroup-data-reverse
3149                  (reverse gnus-newsgroup-data)))
3150      gnus-newsgroup-data))
3151
3152 (defun gnus-data-update-list (data offset)
3153   "Add OFFSET to the POS of all data entries in DATA."
3154   (setq gnus-newsgroup-data-reverse nil)
3155   (while data
3156     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
3157     (setq data (cdr data))))
3158
3159 (defun gnus-summary-article-pseudo-p (article)
3160   "Say whether this article is a pseudo article or not."
3161   (not (vectorp (gnus-data-header (gnus-data-find article)))))
3162
3163 (defmacro gnus-summary-article-sparse-p (article)
3164   "Say whether this article is a sparse article or not."
3165   `(memq ,article gnus-newsgroup-sparse))
3166
3167 (defmacro gnus-summary-article-ancient-p (article)
3168   "Say whether this article is a sparse article or not."
3169   `(memq ,article gnus-newsgroup-ancient))
3170
3171 (defun gnus-article-parent-p (number)
3172   "Say whether this article is a parent or not."
3173   (let ((data (gnus-data-find-list number)))
3174     (and (cdr data)              ; There has to be an article after...
3175          (< (gnus-data-level (car data)) ; And it has to have a higher level.
3176             (gnus-data-level (nth 1 data))))))
3177
3178 (defun gnus-article-children (number)
3179   "Return a list of all children to NUMBER."
3180   (let* ((data (gnus-data-find-list number))
3181          (level (gnus-data-level (car data)))
3182          children)
3183     (setq data (cdr data))
3184     (while (and data
3185                 (= (gnus-data-level (car data)) (1+ level)))
3186       (push (gnus-data-number (car data)) children)
3187       (setq data (cdr data)))
3188     children))
3189
3190 (defmacro gnus-summary-skip-intangible ()
3191   "If the current article is intangible, then jump to a different article."
3192   '(let ((to (get-text-property (point) 'gnus-intangible)))
3193      (and to (gnus-summary-goto-subject to))))
3194
3195 (defmacro gnus-summary-article-intangible-p ()
3196   "Say whether this article is intangible or not."
3197   '(get-text-property (point) 'gnus-intangible))
3198
3199 (defun gnus-article-read-p (article)
3200   "Say whether ARTICLE is read or not."
3201   (not (or (memq article gnus-newsgroup-marked)
3202            (memq article gnus-newsgroup-spam-marked)
3203            (memq article gnus-newsgroup-unreads)
3204            (memq article gnus-newsgroup-unselected)
3205            (memq article gnus-newsgroup-dormant))))
3206
3207 ;; Some summary mode macros.
3208
3209 (defmacro gnus-summary-article-number ()
3210   "The article number of the article on the current line.
3211 If there isn't an article number here, then we return the current
3212 article number."
3213   '(progn
3214      (gnus-summary-skip-intangible)
3215      (or (get-text-property (point) 'gnus-number)
3216          (gnus-summary-last-subject))))
3217
3218 (defmacro gnus-summary-article-header (&optional number)
3219   "Return the header of article NUMBER."
3220   `(gnus-data-header (gnus-data-find
3221                       ,(or number '(gnus-summary-article-number)))))
3222
3223 (defmacro gnus-summary-thread-level (&optional number)
3224   "Return the level of thread that starts with article NUMBER."
3225   `(if (and (eq gnus-summary-make-false-root 'dummy)
3226             (get-text-property (point) 'gnus-intangible))
3227        0
3228      (gnus-data-level (gnus-data-find
3229                        ,(or number '(gnus-summary-article-number))))))
3230
3231 (defmacro gnus-summary-article-mark (&optional number)
3232   "Return the mark of article NUMBER."
3233   `(gnus-data-mark (gnus-data-find
3234                     ,(or number '(gnus-summary-article-number)))))
3235
3236 (defmacro gnus-summary-article-pos (&optional number)
3237   "Return the position of the line of article NUMBER."
3238   `(gnus-data-pos (gnus-data-find
3239                    ,(or number '(gnus-summary-article-number)))))
3240
3241 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
3242 (defmacro gnus-summary-article-subject (&optional number)
3243   "Return current subject string or nil if nothing."
3244   `(let ((headers
3245           ,(if number
3246                `(gnus-data-header (assq ,number gnus-newsgroup-data))
3247              '(gnus-data-header (assq (gnus-summary-article-number)
3248                                       gnus-newsgroup-data)))))
3249      (and headers
3250           (vectorp headers)
3251           (mail-header-subject headers))))
3252
3253 (defmacro gnus-summary-article-score (&optional number)
3254   "Return current article score."
3255   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3256                   gnus-newsgroup-scored))
3257        gnus-summary-default-score 0))
3258
3259 (defun gnus-summary-article-children (&optional number)
3260   "Return a list of article numbers that are children of article NUMBER."
3261   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3262          (level (gnus-data-level (car data)))
3263          l children)
3264     (while (and (setq data (cdr data))
3265                 (> (setq l (gnus-data-level (car data))) level))
3266       (and (= (1+ level) l)
3267            (push (gnus-data-number (car data))
3268                  children)))
3269     (nreverse children)))
3270
3271 (defun gnus-summary-article-parent (&optional number)
3272   "Return the article number of the parent of article NUMBER."
3273   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3274                                     (gnus-data-list t)))
3275          (level (gnus-data-level (car data))))
3276     (if (zerop level)
3277         ()                              ; This is a root.
3278       ;; We search until we find an article with a level less than
3279       ;; this one.  That function has to be the parent.
3280       (while (and (setq data (cdr data))
3281                   (not (< (gnus-data-level (car data)) level))))
3282       (and data (gnus-data-number (car data))))))
3283
3284 (defun gnus-unread-mark-p (mark)
3285   "Say whether MARK is the unread mark."
3286   (= mark gnus-unread-mark))
3287
3288 (defun gnus-read-mark-p (mark)
3289   "Say whether MARK is one of the marks that mark as read.
3290 This is all marks except unread, ticked, dormant, and expirable."
3291   (not (or (= mark gnus-unread-mark)
3292            (= mark gnus-ticked-mark)
3293            (= mark gnus-spam-mark)
3294            (= mark gnus-dormant-mark)
3295            (= mark gnus-expirable-mark))))
3296
3297 (defmacro gnus-article-mark (number)
3298   "Return the MARK of article NUMBER.
3299 This macro should only be used when computing the mark the \"first\"
3300 time; i.e., when generating the summary lines.  After that,
3301 `gnus-summary-article-mark' should be used to examine the
3302 marks of articles."
3303   `(cond
3304     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3305     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3306     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3307     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3308     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3309     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3310     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3311     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3312            gnus-ancient-mark))))
3313
3314 ;; Saving hidden threads.
3315
3316 (defmacro gnus-save-hidden-threads (&rest forms)
3317   "Save hidden threads, eval FORMS, and restore the hidden threads."
3318   (let ((config (make-symbol "config")))
3319     `(let ((,config (gnus-hidden-threads-configuration)))
3320        (unwind-protect
3321            (save-excursion
3322              ,@forms)
3323          (gnus-restore-hidden-threads-configuration ,config)))))
3324 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3325 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3326
3327 (defun gnus-data-compute-positions ()
3328   "Compute the positions of all articles."
3329   (setq gnus-newsgroup-data-reverse nil)
3330   (let ((data gnus-newsgroup-data))
3331     (save-excursion
3332       (gnus-save-hidden-threads
3333         (gnus-summary-show-all-threads)
3334         (goto-char (point-min))
3335         (while data
3336           (while (get-text-property (point) 'gnus-intangible)
3337             (forward-line 1))
3338           (gnus-data-set-pos (car data) (+ (point) 3))
3339           (setq data (cdr data))
3340           (forward-line 1))))))
3341
3342 (defun gnus-hidden-threads-configuration ()
3343   "Return the current hidden threads configuration."
3344   (save-excursion
3345     (let (config)
3346       (goto-char (point-min))
3347       (while (search-forward "\r" nil t)
3348         (push (1- (point)) config))
3349       config)))
3350
3351 (defun gnus-restore-hidden-threads-configuration (config)
3352   "Restore hidden threads configuration from CONFIG."
3353   (save-excursion
3354     (let (point buffer-read-only)
3355       (while (setq point (pop config))
3356         (when (and (< point (point-max))
3357                    (goto-char point)
3358                    (eq (char-after) ?\n))
3359           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3360
3361 ;; Various summary mode internalish functions.
3362
3363 (defun gnus-mouse-pick-article (e)
3364   (interactive "e")
3365   (mouse-set-point e)
3366   (gnus-summary-next-page nil t))
3367
3368 (defun gnus-summary-set-display-table ()
3369   "Change the display table.
3370 Odd characters have a tendency to mess
3371 up nicely formatted displays - we make all possible glyphs
3372 display only a single character."
3373
3374   ;; We start from the standard display table, if any.
3375   (let ((table (or (copy-sequence standard-display-table)
3376                    (make-display-table)))
3377         (i 32))
3378     ;; Nix out all the control chars...
3379     (while (>= (setq i (1- i)) 0)
3380       (aset table i [??]))
3381    ;; ... but not newline and cr, of course.  (cr is necessary for the
3382     ;; selective display).
3383     (aset table ?\n nil)
3384     (aset table ?\r nil)
3385     ;; We keep TAB as well.
3386     (aset table ?\t nil)
3387     ;; We nix out any glyphs 127 through 255, or 127 through 159 in
3388     ;; Emacs 23 (unicode), that are not set already.
3389     (let ((i (if (ignore-errors (= (make-char 'latin-iso8859-1 160) 160))
3390                  160
3391                256)))
3392       (while (>= (setq i (1- i)) 127)
3393         ;; Only modify if the entry is nil.
3394         (unless (aref table i)
3395           (aset table i [??]))))
3396     (setq buffer-display-table table)))
3397
3398 (defun gnus-summary-set-article-display-arrow (pos)
3399   "Update the overlay arrow to point to line at position POS."
3400   (when (and gnus-summary-display-arrow
3401              (boundp 'overlay-arrow-position)
3402              (boundp 'overlay-arrow-string))
3403     (save-excursion
3404       (goto-char pos)
3405       (beginning-of-line)
3406       (unless overlay-arrow-position
3407         (setq overlay-arrow-position (make-marker)))
3408       (setq overlay-arrow-string "=>"
3409             overlay-arrow-position (set-marker overlay-arrow-position
3410                                                (point)
3411                                                (current-buffer))))))
3412
3413 (defun gnus-summary-setup-buffer (group)
3414   "Initialize summary buffer."
3415   (let ((buffer (gnus-summary-buffer-name group))
3416         (dead-name (concat "*Dead Summary "
3417                            (gnus-group-decoded-name group) "*")))
3418     ;; If a dead summary buffer exists, we kill it.
3419     (when (gnus-buffer-live-p dead-name)
3420       (gnus-kill-buffer dead-name))
3421     (if (get-buffer buffer)
3422         (progn
3423           (set-buffer buffer)
3424           (setq gnus-summary-buffer (current-buffer))
3425           (not gnus-newsgroup-prepared))
3426       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3427       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3428       (gnus-summary-mode group)
3429       (when gnus-carpal
3430         (gnus-carpal-setup-buffer 'summary))
3431       (when (gnus-group-quit-config group)
3432         (set (make-local-variable 'gnus-single-article-buffer) nil))
3433       (make-local-variable 'gnus-article-buffer)
3434       (make-local-variable 'gnus-article-current)
3435       (make-local-variable 'gnus-original-article-buffer)
3436       (setq gnus-newsgroup-name group)
3437       ;; Set any local variables in the group parameters.
3438       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3439       t)))
3440
3441 (defun gnus-set-global-variables ()
3442   "Set the global equivalents of the buffer-local variables.
3443 They are set to the latest values they had.  These reflect the summary
3444 buffer that was in action when the last article was fetched."
3445   (when (eq major-mode 'gnus-summary-mode)
3446     (setq gnus-summary-buffer (current-buffer))
3447     (let ((name gnus-newsgroup-name)
3448           (marked gnus-newsgroup-marked)
3449           (spam gnus-newsgroup-spam-marked)
3450           (unread gnus-newsgroup-unreads)
3451           (headers gnus-current-headers)
3452           (data gnus-newsgroup-data)
3453           (summary gnus-summary-buffer)
3454           (article-buffer gnus-article-buffer)
3455           (original gnus-original-article-buffer)
3456           (gac gnus-article-current)
3457           (reffed gnus-reffed-article-number)
3458           (score-file gnus-current-score-file)
3459           (default-charset gnus-newsgroup-charset)
3460           vlist)
3461       (let ((locals gnus-newsgroup-variables))
3462         (while locals
3463           (if (consp (car locals))
3464               (push (eval (caar locals)) vlist)
3465             (push (eval (car locals)) vlist))
3466           (setq locals (cdr locals)))
3467         (setq vlist (nreverse vlist)))
3468       (with-current-buffer gnus-group-buffer
3469         (setq gnus-newsgroup-name name
3470               gnus-newsgroup-marked marked
3471               gnus-newsgroup-spam-marked spam
3472               gnus-newsgroup-unreads unread
3473               gnus-current-headers headers
3474               gnus-newsgroup-data data
3475               gnus-article-current gac
3476               gnus-summary-buffer summary
3477               gnus-article-buffer article-buffer
3478               gnus-original-article-buffer original
3479               gnus-reffed-article-number reffed
3480               gnus-current-score-file score-file
3481               gnus-newsgroup-charset default-charset)
3482         (let ((locals gnus-newsgroup-variables))
3483           (while locals
3484             (if (consp (car locals))
3485                 (set (caar locals) (pop vlist))
3486               (set (car locals) (pop vlist)))
3487             (setq locals (cdr locals))))
3488         ;; The article buffer also has local variables.
3489         (when (gnus-buffer-live-p gnus-article-buffer)
3490           (set-buffer gnus-article-buffer)
3491           (setq gnus-summary-buffer summary))))))
3492
3493 (defun gnus-summary-article-unread-p (article)
3494   "Say whether ARTICLE is unread or not."
3495   (memq article gnus-newsgroup-unreads))
3496
3497 (defun gnus-summary-first-article-p (&optional article)
3498   "Return whether ARTICLE is the first article in the buffer."
3499   (if (not (setq article (or article (gnus-summary-article-number))))
3500       nil
3501     (eq article (caar gnus-newsgroup-data))))
3502
3503 (defun gnus-summary-last-article-p (&optional article)
3504   "Return whether ARTICLE is the last article in the buffer."
3505   (if (not (setq article (or article (gnus-summary-article-number))))
3506       ;; All non-existent numbers are the last article.  :-)
3507       t
3508     (not (cdr (gnus-data-find-list article)))))
3509
3510 (defun gnus-make-thread-indent-array (&optional n)
3511   (when (or n
3512             (progn (setq n 200) nil)
3513             (null gnus-thread-indent-array)
3514             (/= gnus-thread-indent-level gnus-thread-indent-array-level))
3515     (setq gnus-thread-indent-array (make-vector (1+ n) "")
3516           gnus-thread-indent-array-level gnus-thread-indent-level)
3517     (while (>= n 0)
3518       (aset gnus-thread-indent-array n
3519             (make-string (* n gnus-thread-indent-level) ? ))
3520       (setq n (1- n)))))
3521
3522 (defun gnus-update-summary-mark-positions ()
3523   "Compute where the summary marks are to go."
3524   (save-excursion
3525     (when (gnus-buffer-exists-p gnus-summary-buffer)
3526       (set-buffer gnus-summary-buffer))
3527     (let ((spec gnus-summary-line-format-spec)
3528           pos)
3529       (save-excursion
3530         (gnus-set-work-buffer)
3531         (let ((gnus-tmp-unread ?Z)
3532               (gnus-replied-mark ?Z)
3533               (gnus-score-below-mark ?Z)
3534               (gnus-score-over-mark ?Z)
3535               (gnus-undownloaded-mark ?Z)
3536               (gnus-summary-line-format-spec spec)
3537               (gnus-newsgroup-downloadable '(0))
3538               (header [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil])
3539               case-fold-search ignores)
3540           ;; Here, all marks are bound to Z.
3541           (gnus-summary-insert-line header
3542                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3543           (goto-char (point-min))
3544           ;; Memorize the positions of the same characters as dummy marks.
3545           (while (re-search-forward "[A-D]" nil t)
3546             (push (point) ignores))
3547           (erase-buffer)
3548           ;; We use A-D as dummy marks in order to know column positions
3549           ;; where marks should be inserted.
3550           (setq gnus-tmp-unread ?A
3551                 gnus-replied-mark ?B
3552                 gnus-score-below-mark ?C
3553                 gnus-score-over-mark ?C
3554                 gnus-undownloaded-mark ?D)
3555           (gnus-summary-insert-line header
3556                                     0 nil t gnus-tmp-unread t nil "" nil 1)
3557           ;; Ignore characters which aren't dummy marks.
3558           (dolist (p ignores)
3559             (delete-region (goto-char (1- p)) p)
3560             (insert ?Z))
3561           (goto-char (point-min))
3562           (setq pos (list (cons 'unread
3563                                 (and (search-forward "A" nil t)
3564                                      (- (point) (point-min) 1)))))
3565           (goto-char (point-min))
3566           (push (cons 'replied (and (search-forward "B" nil t)
3567                                     (- (point) (point-min) 1)))
3568                 pos)
3569           (goto-char (point-min))
3570           (push (cons 'score (and (search-forward "C" nil t)
3571                                   (- (point) (point-min) 1)))
3572                 pos)
3573           (goto-char (point-min))
3574           (push (cons 'download (and (search-forward "D" nil t)
3575                                      (- (point) (point-min) 1)))
3576                 pos)))
3577       (setq gnus-summary-mark-positions pos))))
3578
3579 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3580   "Insert a dummy root in the summary buffer."
3581   (beginning-of-line)
3582   (gnus-add-text-properties
3583    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3584    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3585
3586 (defun gnus-summary-extract-address-component (from)
3587   (or (car (funcall gnus-extract-address-components from))
3588       from))
3589
3590 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3591   (let ((mail-parse-charset gnus-newsgroup-charset)
3592         (ignored-from-addresses (gnus-ignored-from-addresses))
3593         ; Is it really necessary to do this next part for each summary line?
3594         ; Luckily, doesn't seem to slow things down much.
3595         (mail-parse-ignored-charsets
3596          (with-current-buffer gnus-summary-buffer
3597            gnus-newsgroup-ignored-charsets)))
3598     (or
3599      (and ignored-from-addresses
3600           (string-match ignored-from-addresses gnus-tmp-from)
3601           (let ((extra-headers (mail-header-extra header))
3602                 to
3603                 newsgroups)
3604             (cond
3605              ((setq to (cdr (assq 'To extra-headers)))
3606               (concat gnus-summary-to-prefix
3607                       (inline
3608                         (gnus-summary-extract-address-component
3609                          (funcall gnus-decode-encoded-address-function to)))))
3610              ((setq newsgroups
3611                     (or
3612                      (cdr (assq 'Newsgroups extra-headers))
3613                      (and
3614                       (memq 'Newsgroups gnus-extra-headers)
3615                       (eq (car (gnus-find-method-for-group
3616                                 gnus-newsgroup-name)) 'nntp)
3617                       (gnus-group-real-name gnus-newsgroup-name))))
3618               (concat gnus-summary-newsgroup-prefix newsgroups)))))
3619      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3620
3621 (defun gnus-summary-insert-line (gnus-tmp-header
3622                                  gnus-tmp-level gnus-tmp-current
3623                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3624                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3625                                  &optional gnus-tmp-dummy gnus-tmp-score
3626                                  gnus-tmp-process)
3627   (if (>= gnus-tmp-level (length gnus-thread-indent-array))
3628       (gnus-make-thread-indent-array (max (* 2 (length gnus-thread-indent-array))
3629                                           gnus-tmp-level)))
3630   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3631          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3632          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3633          (gnus-tmp-score-char
3634           (if (or (null gnus-summary-default-score)
3635                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3636                       gnus-summary-zcore-fuzz))
3637               ?                         ;Whitespace
3638             (if (< gnus-tmp-score gnus-summary-default-score)
3639                 gnus-score-below-mark gnus-score-over-mark)))
3640          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3641          (gnus-tmp-replied
3642           (cond (gnus-tmp-process gnus-process-mark)
3643                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3644                  gnus-cached-mark)
3645                 (gnus-tmp-replied gnus-replied-mark)
3646                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3647                  gnus-forwarded-mark)
3648                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3649                  gnus-saved-mark)
3650                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3651                  gnus-recent-mark)
3652                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3653                  gnus-unseen-mark)
3654                 (t gnus-no-mark)))
3655          (gnus-tmp-downloaded
3656           (cond (undownloaded
3657                  gnus-undownloaded-mark)
3658                 (gnus-newsgroup-agentized
3659                  gnus-downloaded-mark)
3660                 (t
3661                  gnus-no-mark)))
3662          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3663          (gnus-tmp-name
3664           (cond
3665            ((string-match "<[^>]+> *$" gnus-tmp-from)
3666             (let ((beg (match-beginning 0)))
3667               (or (and (string-match "^\".+\"" gnus-tmp-from)
3668                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3669                   (substring gnus-tmp-from 0 beg))))
3670            ((string-match "(.+)" gnus-tmp-from)
3671             (substring gnus-tmp-from
3672                        (1+ (match-beginning 0)) (1- (match-end 0))))
3673            (t gnus-tmp-from)))
3674          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3675          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3676          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3677          (buffer-read-only nil))
3678     (when (string= gnus-tmp-name "")
3679       (setq gnus-tmp-name gnus-tmp-from))
3680     (unless (numberp gnus-tmp-lines)
3681       (setq gnus-tmp-lines -1))
3682     (if (= gnus-tmp-lines -1)
3683         (setq gnus-tmp-lines "?")
3684       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3685       (gnus-put-text-property
3686      (point)
3687      (progn (eval gnus-summary-line-format-spec) (point))
3688        'gnus-number gnus-tmp-number)
3689     (when (gnus-visual-p 'summary-highlight 'highlight)
3690       (forward-line -1)
3691       (gnus-run-hooks 'gnus-summary-update-hook)
3692       (forward-line 1))))
3693
3694 (defun gnus-summary-update-line (&optional dont-update)
3695   "Update summary line after change."
3696   (when (and gnus-summary-default-score
3697              (not gnus-summary-inhibit-highlight))
3698     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3699            (article (gnus-summary-article-number))
3700            (score (gnus-summary-article-score article)))
3701       (unless dont-update
3702         (if (and gnus-summary-mark-below
3703                  (< (gnus-summary-article-score)
3704                     gnus-summary-mark-below))
3705             ;; This article has a low score, so we mark it as read.
3706             (when (memq article gnus-newsgroup-unreads)
3707               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3708           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3709             ;; This article was previously marked as read on account
3710             ;; of a low score, but now it has risen, so we mark it as
3711             ;; unread.
3712             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3713         (gnus-summary-update-mark
3714          (if (or (null gnus-summary-default-score)
3715                  (<= (abs (- score gnus-summary-default-score))
3716                      gnus-summary-zcore-fuzz))
3717              ?                          ;Whitespace
3718            (if (< score gnus-summary-default-score)
3719                gnus-score-below-mark gnus-score-over-mark))
3720          'score))
3721       ;; Do visual highlighting.
3722       (when (gnus-visual-p 'summary-highlight 'highlight)
3723         (gnus-run-hooks 'gnus-summary-update-hook)))))
3724
3725 (defvar gnus-tmp-new-adopts nil)
3726
3727 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3728   "Return the number of articles in THREAD.
3729 This may be 0 in some cases -- if none of the articles in
3730 the thread are to be displayed."
3731   (let* ((number
3732          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3733           (cond
3734            ((not (listp thread))
3735             1)
3736            ((and (consp thread) (cdr thread))
3737             (apply
3738              '+ 1 (mapcar
3739                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3740            ((null thread)
3741             1)
3742            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3743             1)
3744            (t 0))))
3745     (when (and level (zerop level) gnus-tmp-new-adopts)
3746       (incf number
3747             (apply '+ (mapcar
3748                        'gnus-summary-number-of-articles-in-thread
3749                        gnus-tmp-new-adopts))))
3750     (if char
3751         (if (> number 1) gnus-not-empty-thread-mark
3752           gnus-empty-thread-mark)
3753       number)))
3754
3755 (defsubst gnus-summary-line-message-size (head)
3756   "Return pretty-printed version of message size.
3757 This function is intended to be used in
3758 `gnus-summary-line-format-alist'."
3759   (let ((c (or (mail-header-chars head) -1)))
3760     (cond ((< c 0) "n/a")               ; chars not available
3761           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3762           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3763           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3764           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3765
3766
3767 (defun gnus-summary-set-local-parameters (group)
3768   "Go through the local params of GROUP and set all variable specs in that list."
3769   (let ((vars '(quit-config)))          ; Ignore quit-config.
3770     (dolist (elem (gnus-group-find-parameter group))
3771       (and (consp elem)                 ; Has to be a cons.
3772            (consp (cdr elem))           ; The cdr has to be a list.
3773            (symbolp (car elem))         ; Has to be a symbol in there.
3774            (not (memq (car elem) vars))
3775            (ignore-errors               ; So we set it.
3776              (push (car elem) vars)
3777              (make-local-variable (car elem))
3778              (set (car elem) (eval (nth 1 elem))))))))
3779
3780 (defun gnus-summary-read-group (group &optional show-all no-article
3781                                       kill-buffer no-display backward
3782                                       select-articles)
3783   "Start reading news in newsgroup GROUP.
3784 If SHOW-ALL is non-nil, already read articles are also listed.
3785 If NO-ARTICLE is non-nil, no article is selected initially.
3786 If NO-DISPLAY, don't generate a summary buffer."
3787   (let (result)
3788     (while (and group
3789                 (null (setq result
3790                             (let ((gnus-auto-select-next nil))
3791                               (or (gnus-summary-read-group-1
3792                                    group show-all no-article
3793                                    kill-buffer no-display
3794                                    select-articles)
3795                                   (setq show-all nil
3796                                         select-articles nil)))))
3797                 (eq gnus-auto-select-next 'quietly))
3798       (set-buffer gnus-group-buffer)
3799       ;; The entry function called above goes to the next
3800       ;; group automatically, so we go two groups back
3801       ;; if we are searching for the previous group.
3802       (when backward
3803         (gnus-group-prev-unread-group 2))
3804       (if (not (equal group (gnus-group-group-name)))
3805           (setq group (gnus-group-group-name))
3806         (setq group nil)))
3807     result))
3808
3809 (defun gnus-summary-read-group-1 (group show-all no-article
3810                                         kill-buffer no-display
3811                                         &optional select-articles)
3812   ;; Killed foreign groups can't be entered.
3813   ;;  (when (and (not (gnus-group-native-p group))
3814   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3815   ;;    (error "Dead non-native groups can't be entered"))
3816   (gnus-message 5 "Retrieving newsgroup: %s..."
3817                 (gnus-group-decoded-name group))
3818   (let* ((new-group (gnus-summary-setup-buffer group))
3819          (quit-config (gnus-group-quit-config group))
3820          (did-select (and new-group (gnus-select-newsgroup
3821                                      group show-all select-articles))))
3822     (cond
3823      ;; This summary buffer exists already, so we just select it.
3824      ((not new-group)
3825       (gnus-set-global-variables)
3826       (when kill-buffer
3827         (gnus-kill-or-deaden-summary kill-buffer))
3828       (gnus-configure-windows 'summary 'force)
3829       (gnus-set-mode-line 'summary)
3830       (gnus-summary-position-point)
3831       (message "")
3832       t)
3833      ;; We couldn't select this group.
3834      ((null did-select)
3835       (when (and (eq major-mode 'gnus-summary-mode)
3836                  (not (equal (current-buffer) kill-buffer)))
3837         (kill-buffer (current-buffer))
3838         (if (not quit-config)
3839             (progn
3840               ;; Update the info -- marks might need to be removed,
3841               ;; for instance.
3842               (gnus-summary-update-info)
3843               (set-buffer gnus-group-buffer)
3844               (gnus-group-jump-to-group group)
3845               (gnus-group-next-unread-group 1))
3846           (gnus-handle-ephemeral-exit quit-config)))
3847       (let ((grpinfo (gnus-get-info group)))
3848         (if (null (gnus-info-read grpinfo))
3849             (gnus-message 3 "Group %s contains no messages"
3850                           (gnus-group-decoded-name group))
3851           (gnus-message 3 "Can't select group")))
3852       nil)
3853      ;; The user did a `C-g' while prompting for number of articles,
3854      ;; so we exit this group.
3855      ((eq did-select 'quit)
3856       (and (eq major-mode 'gnus-summary-mode)
3857            (not (equal (current-buffer) kill-buffer))
3858            (kill-buffer (current-buffer)))
3859       (when kill-buffer
3860         (gnus-kill-or-deaden-summary kill-buffer))
3861       (if (not quit-config)
3862           (progn
3863             (set-buffer gnus-group-buffer)
3864             (gnus-group-jump-to-group group)
3865             (gnus-group-next-unread-group 1)
3866             (gnus-configure-windows 'group 'force))
3867         (gnus-handle-ephemeral-exit quit-config))
3868       ;; Finally signal the quit.
3869       (signal 'quit nil))
3870      ;; The group was successfully selected.
3871      (t
3872       (gnus-set-global-variables)
3873       ;; Save the active value in effect when the group was entered.
3874       (setq gnus-newsgroup-active
3875             (gnus-copy-sequence
3876              (gnus-active gnus-newsgroup-name)))
3877       ;; You can change the summary buffer in some way with this hook.
3878       (gnus-run-hooks 'gnus-select-group-hook)
3879       (when (memq 'summary (gnus-update-format-specifications
3880                             nil 'summary 'summary-mode 'summary-dummy))
3881         ;; The format specification for the summary line was updated,
3882         ;; so we need to update the mark positions as well.
3883         (gnus-update-summary-mark-positions))
3884       ;; Do score processing.
3885       (when gnus-use-scoring
3886         (gnus-possibly-score-headers))
3887       ;; Check whether to fill in the gaps in the threads.
3888       (when gnus-build-sparse-threads
3889         (gnus-build-sparse-threads))
3890       ;; Find the initial limit.
3891       (if show-all
3892           (let ((gnus-newsgroup-dormant nil))
3893             (gnus-summary-initial-limit show-all))
3894         (gnus-summary-initial-limit show-all))
3895       ;; Generate the summary buffer.
3896       (unless no-display
3897         (gnus-summary-prepare))
3898       (when gnus-use-trees
3899         (gnus-tree-open group)
3900         (setq gnus-summary-highlight-line-function
3901               'gnus-tree-highlight-article))
3902       ;; If the summary buffer is empty, but there are some low-scored
3903       ;; articles or some excluded dormants, we include these in the
3904       ;; buffer.
3905       (when (and (zerop (buffer-size))
3906                  (not no-display))
3907         (cond (gnus-newsgroup-dormant
3908                (gnus-summary-limit-include-dormant))
3909               ((and gnus-newsgroup-scored show-all)
3910                (gnus-summary-limit-include-expunged t))))
3911       ;; Function `gnus-apply-kill-file' must be called in this hook.
3912       (gnus-run-hooks 'gnus-apply-kill-hook)
3913       (if (and (zerop (buffer-size))
3914                (not no-display))
3915           (progn
3916             ;; This newsgroup is empty.
3917             (gnus-summary-catchup-and-exit nil t)
3918             (gnus-message 6 "No unread news")
3919             (when kill-buffer
3920               (gnus-kill-or-deaden-summary kill-buffer))
3921             ;; Return nil from this function.
3922             nil)
3923         ;; Hide conversation thread subtrees.  We cannot do this in
3924         ;; gnus-summary-prepare-hook since kill processing may not
3925         ;; work with hidden articles.
3926         (gnus-summary-maybe-hide-threads)
3927         (when kill-buffer
3928           (gnus-kill-or-deaden-summary kill-buffer))
3929         (gnus-summary-auto-select-subject)
3930         ;; Show first unread article if requested.
3931         (if (and (not no-article)
3932                  (not no-display)
3933                  gnus-newsgroup-unreads
3934                  gnus-auto-select-first)
3935             (progn
3936               (gnus-configure-windows 'summary)
3937               (let ((art (gnus-summary-article-number)))
3938                 (unless (and (not gnus-plugged)
3939                              (or (memq art gnus-newsgroup-undownloaded)
3940                                  (memq art gnus-newsgroup-downloadable)))
3941                   (gnus-summary-goto-article art))))
3942           ;; Don't select any articles.
3943           (gnus-summary-position-point)
3944           (gnus-configure-windows 'summary 'force)
3945           (gnus-set-mode-line 'summary))
3946         (when (and gnus-auto-center-group
3947                    (get-buffer-window gnus-group-buffer t))
3948           ;; Gotta use windows, because recenter does weird stuff if
3949           ;; the current buffer ain't the displayed window.
3950           (let ((owin (selected-window)))
3951             (select-window (get-buffer-window gnus-group-buffer t))
3952             (when (gnus-group-goto-group group)
3953               (recenter))
3954             (select-window owin)))
3955         ;; Mark this buffer as "prepared".
3956         (setq gnus-newsgroup-prepared t)
3957         (gnus-run-hooks 'gnus-summary-prepared-hook)
3958         (unless (gnus-ephemeral-group-p group)
3959           (gnus-group-update-group group))
3960         t)))))
3961
3962 (defun gnus-summary-auto-select-subject ()
3963   "Select the subject line on initial group entry."
3964   (goto-char (point-min))
3965   (cond
3966    ((eq gnus-auto-select-subject 'best)
3967     (gnus-summary-best-unread-subject))
3968    ((eq gnus-auto-select-subject 'unread)
3969     (gnus-summary-first-unread-subject))
3970    ((eq gnus-auto-select-subject 'unseen)
3971     (gnus-summary-first-unseen-subject))
3972    ((eq gnus-auto-select-subject 'unseen-or-unread)
3973     (gnus-summary-first-unseen-or-unread-subject))
3974    ((eq gnus-auto-select-subject 'first)
3975     ;; Do nothing.
3976     )
3977    ((functionp gnus-auto-select-subject)
3978     (funcall gnus-auto-select-subject))))
3979
3980 (defun gnus-summary-prepare ()
3981   "Generate the summary buffer."
3982   (interactive)
3983   (let ((buffer-read-only nil))
3984     (erase-buffer)
3985     (setq gnus-newsgroup-data nil
3986           gnus-newsgroup-data-reverse nil)
3987     (gnus-run-hooks 'gnus-summary-generate-hook)
3988     ;; Generate the buffer, either with threads or without.
3989     (when gnus-newsgroup-headers
3990       (gnus-summary-prepare-threads
3991        (if gnus-show-threads
3992            (gnus-sort-gathered-threads
3993             (funcall gnus-summary-thread-gathering-function
3994                      (gnus-sort-threads
3995                       (gnus-cut-threads (gnus-make-threads)))))
3996          ;; Unthreaded display.
3997          (gnus-sort-articles gnus-newsgroup-headers))))
3998     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3999     ;; Call hooks for modifying summary buffer.
4000     (goto-char (point-min))
4001     (gnus-run-hooks 'gnus-summary-prepare-hook)))
4002
4003 (defsubst gnus-general-simplify-subject (subject)
4004   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
4005   (setq subject
4006         (cond
4007          ;; Truncate the subject.
4008          (gnus-simplify-subject-functions
4009           (gnus-map-function gnus-simplify-subject-functions subject))
4010          ((numberp gnus-summary-gather-subject-limit)
4011           (setq subject (gnus-simplify-subject-re subject))
4012           (if (> (length subject) gnus-summary-gather-subject-limit)
4013               (substring subject 0 gnus-summary-gather-subject-limit)
4014             subject))
4015          ;; Fuzzily simplify it.
4016          ((eq 'fuzzy gnus-summary-gather-subject-limit)
4017           (gnus-simplify-subject-fuzzy subject))
4018          ;; Just remove the leading "Re:".
4019          (t
4020           (gnus-simplify-subject-re subject))))
4021
4022   (if (and gnus-summary-gather-exclude-subject
4023            (string-match gnus-summary-gather-exclude-subject subject))
4024       nil                         ; This article shouldn't be gathered
4025     subject))
4026
4027 (defun gnus-summary-simplify-subject-query ()
4028   "Query where the respool algorithm would put this article."
4029   (interactive)
4030   (gnus-summary-select-article)
4031   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
4032
4033 (defun gnus-gather-threads-by-subject (threads)
4034   "Gather threads by looking at Subject headers."
4035   (if (not gnus-summary-make-false-root)
4036       threads
4037     (let ((hashtb (gnus-make-hashtable 1024))
4038           (prev threads)
4039           (result threads)
4040           subject hthread whole-subject)
4041       (while threads
4042         (setq subject (gnus-general-simplify-subject
4043                        (setq whole-subject (mail-header-subject
4044                                             (caar threads)))))
4045         (when subject
4046           (if (setq hthread (gnus-gethash subject hashtb))
4047               (progn
4048                 ;; We enter a dummy root into the thread, if we
4049                 ;; haven't done that already.
4050                 (unless (stringp (caar hthread))
4051                   (setcar hthread (list whole-subject (car hthread))))
4052                 ;; We add this new gathered thread to this gathered
4053                 ;; thread.
4054                 (setcdr (car hthread)
4055                         (nconc (cdar hthread) (list (car threads))))
4056                 ;; Remove it from the list of threads.
4057                 (setcdr prev (cdr threads))
4058                 (setq threads prev))
4059             ;; Enter this thread into the hash table.
4060             (gnus-sethash subject
4061                           (if gnus-summary-make-false-root-always
4062                               (progn
4063                                 ;; If you want a dummy root above all
4064                                 ;; threads...
4065                                 (setcar threads (list whole-subject
4066                                                       (car threads)))
4067                                 threads)
4068                             threads)
4069                           hashtb)))
4070         (setq prev threads)
4071         (setq threads (cdr threads)))
4072       result)))
4073
4074 (defun gnus-gather-threads-by-references (threads)
4075   "Gather threads by looking at References headers."
4076   (let ((idhashtb (gnus-make-hashtable 1024))
4077         (thhashtb (gnus-make-hashtable 1024))
4078         (prev threads)
4079         (result threads)
4080         ids references id gthread gid entered ref)
4081     (while threads
4082       (when (setq references (mail-header-references (caar threads)))
4083         (setq id (mail-header-id (caar threads))
4084               ids (inline (gnus-split-references references))
4085               entered nil)
4086         (while (setq ref (pop ids))
4087           (setq ids (delete ref ids))
4088           (if (not (setq gid (gnus-gethash ref idhashtb)))
4089               (progn
4090                 (gnus-sethash ref id idhashtb)
4091                 (gnus-sethash id threads thhashtb))
4092             (setq gthread (gnus-gethash gid thhashtb))
4093             (unless entered
4094               ;; We enter a dummy root into the thread, if we
4095               ;; haven't done that already.
4096               (unless (stringp (caar gthread))
4097                 (setcar gthread (list (mail-header-subject (caar gthread))
4098                                       (car gthread))))
4099               ;; We add this new gathered thread to this gathered
4100               ;; thread.
4101               (setcdr (car gthread)
4102                       (nconc (cdar gthread) (list (car threads)))))
4103             ;; Add it into the thread hash table.
4104             (gnus-sethash id gthread thhashtb)
4105             (setq entered t)
4106             ;; Remove it from the list of threads.
4107             (setcdr prev (cdr threads))
4108             (setq threads prev))))
4109       (setq prev threads)
4110       (setq threads (cdr threads)))
4111     result))
4112
4113 (defun gnus-sort-gathered-threads (threads)
4114   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
4115   (let ((result threads))
4116     (while threads
4117       (when (stringp (caar threads))
4118         (setcdr (car threads)
4119                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
4120       (setq threads (cdr threads)))
4121     result))
4122
4123 (defun gnus-thread-loop-p (root thread)
4124   "Say whether ROOT is in THREAD."
4125   (let ((stack (list thread))
4126         (infloop 0)
4127         th)
4128     (while (setq thread (pop stack))
4129       (setq th (cdr thread))
4130       (while (and th
4131                   (not (eq (caar th) root)))
4132         (pop th))
4133       (if th
4134           ;; We have found a loop.
4135           (let (ref-dep)
4136             (setcdr thread (delq (car th) (cdr thread)))
4137             (if (boundp (setq ref-dep (intern "none"
4138                                               gnus-newsgroup-dependencies)))
4139                 (setcdr (symbol-value ref-dep)
4140                         (nconc (cdr (symbol-value ref-dep))
4141                                (list (car th))))
4142               (set ref-dep (list nil (car th))))
4143             (setq infloop 1
4144                   stack nil))
4145         ;; Push all the subthreads onto the stack.
4146         (push (cdr thread) stack)))
4147     infloop))
4148
4149 (defun gnus-make-threads ()
4150   "Go through the dependency hashtb and find the roots.  Return all threads."
4151   (let (threads)
4152     (while (catch 'infloop
4153              (mapatoms
4154               (lambda (refs)
4155                 ;; Deal with self-referencing References loops.
4156                 (when (and (car (symbol-value refs))
4157                            (not (zerop
4158                                  (apply
4159                                   '+
4160                                   (mapcar
4161                                    (lambda (thread)
4162                                      (gnus-thread-loop-p
4163                                       (car (symbol-value refs)) thread))
4164                                    (cdr (symbol-value refs)))))))
4165                   (setq threads nil)
4166                   (throw 'infloop t))
4167                 (unless (car (symbol-value refs))
4168                   ;; These threads do not refer back to any other
4169                   ;; articles, so they're roots.
4170                   (setq threads (append (cdr (symbol-value refs)) threads))))
4171               gnus-newsgroup-dependencies)))
4172     threads))
4173
4174 ;; Build the thread tree.
4175 (defsubst gnus-dependencies-add-header (header dependencies force-new)
4176   "Enter HEADER into the DEPENDENCIES table if it is not already there.
4177
4178 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
4179 if it was already present.
4180
4181 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
4182 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
4183 Message-IDs will be renamed to a unique Message-ID before being
4184 entered.
4185
4186 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
4187   (let* ((id (mail-header-id header))
4188          (id-dep (and id (intern id dependencies)))
4189          parent-id ref ref-dep ref-header replaced)
4190     ;; Enter this `header' in the `dependencies' table.
4191     (cond
4192      ((not id-dep)
4193       (setq header nil))
4194      ;; The first two cases do the normal part: enter a new `header'
4195      ;; in the `dependencies' table.
4196      ((not (boundp id-dep))
4197       (set id-dep (list header)))
4198      ((null (car (symbol-value id-dep)))
4199       (setcar (symbol-value id-dep) header))
4200
4201      ;; From here the `header' was already present in the
4202      ;; `dependencies' table.
4203      (force-new
4204       ;; Overrides an existing entry;
4205       ;; just set the header part of the entry.
4206       (setcar (symbol-value id-dep) header)
4207       (setq replaced t))
4208
4209      ;; Renames the existing `header' to a unique Message-ID.
4210      ((not gnus-summary-ignore-duplicates)
4211       ;; An article with this Message-ID has already been seen.
4212       ;; We rename the Message-ID.
4213       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
4214            (list header))
4215       (mail-header-set-id header id))
4216
4217      ;; The last case ignores an existing entry, except it adds any
4218      ;; additional Xrefs (in case the two articles came from different
4219      ;; servers.
4220      ;; Also sets `header' to `nil' meaning that the `dependencies'
4221      ;; table was *not* modified.
4222      (t
4223       (mail-header-set-xref
4224        (car (symbol-value id-dep))
4225        (concat (or (mail-header-xref (car (symbol-value id-dep)))
4226                    "")
4227                (or (mail-header-xref header) "")))
4228       (setq header nil)))
4229
4230     (when (and header (not replaced))
4231       ;; First check that we are not creating a References loop.
4232       (setq parent-id (gnus-parent-id (mail-header-references header)))
4233       (setq ref parent-id)
4234       (while (and ref
4235                   (setq ref-dep (intern-soft ref dependencies))
4236                   (boundp ref-dep)
4237                   (setq ref-header (car (symbol-value ref-dep))))
4238         (if (string= id ref)
4239             ;; Yuk!  This is a reference loop.  Make the article be a
4240             ;; root article.
4241             (progn
4242               (mail-header-set-references (car (symbol-value id-dep)) "none")
4243               (setq ref nil)
4244               (setq parent-id nil))
4245           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
4246       (setq ref-dep (intern (or parent-id "none") dependencies))
4247       (if (boundp ref-dep)
4248           (setcdr (symbol-value ref-dep)
4249                   (nconc (cdr (symbol-value ref-dep))
4250                          (list (symbol-value id-dep))))
4251         (set ref-dep (list nil (symbol-value id-dep)))))
4252     header))
4253
4254 (defun gnus-extract-message-id-from-in-reply-to (string)
4255   (if (string-match "<[^>]+>" string)
4256       (substring string (match-beginning 0) (match-end 0))
4257     nil))
4258
4259 (defun gnus-build-sparse-threads ()
4260   (let ((headers gnus-newsgroup-headers)
4261         (mail-parse-charset gnus-newsgroup-charset)
4262         (gnus-summary-ignore-duplicates t)
4263         header references generation relations
4264         subject child end new-child date)
4265     ;; First we create an alist of generations/relations, where
4266     ;; generations is how much we trust the relation, and the relation
4267     ;; is parent/child.
4268     (gnus-message 7 "Making sparse threads...")
4269     (save-excursion
4270       (nnheader-set-temp-buffer " *gnus sparse threads*")
4271       (while (setq header (pop headers))
4272         (when (and (setq references (mail-header-references header))
4273                    (not (string= references "")))
4274           (insert references)
4275           (setq child (mail-header-id header)
4276                 subject (mail-header-subject header)
4277                 date (mail-header-date header)
4278                 generation 0)
4279           (while (search-backward ">" nil t)
4280             (setq end (1+ (point)))
4281             (when (search-backward "<" nil t)
4282               (setq new-child (buffer-substring (point) end))
4283               (push (list (incf generation)
4284                           child (setq child new-child)
4285                           subject date)
4286                     relations)))
4287           (when child
4288             (push (list (1+ generation) child nil subject) relations))
4289           (erase-buffer)))
4290       (kill-buffer (current-buffer)))
4291     ;; Sort over trustworthiness.
4292     (mapcar
4293      (lambda (relation)
4294        (when (gnus-dependencies-add-header
4295               (make-full-mail-header
4296                gnus-reffed-article-number
4297                (nth 3 relation) "" (or (nth 4 relation) "")
4298                (nth 1 relation)
4299                (or (nth 2 relation) "") 0 0 "")
4300               gnus-newsgroup-dependencies nil)
4301          (push gnus-reffed-article-number gnus-newsgroup-limit)
4302          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4303          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4304                gnus-newsgroup-reads)
4305          (decf gnus-reffed-article-number)))
4306      (sort relations 'car-less-than-car))
4307     (gnus-message 7 "Making sparse threads...done")))
4308
4309 (defun gnus-build-old-threads ()
4310   ;; Look at all the articles that refer back to old articles, and
4311   ;; fetch the headers for the articles that aren't there.  This will
4312   ;; build complete threads - if the roots haven't been expired by the
4313   ;; server, that is.
4314   (let ((mail-parse-charset gnus-newsgroup-charset)
4315         id heads)
4316     (mapatoms
4317      (lambda (refs)
4318        (when (not (car (symbol-value refs)))
4319          (setq heads (cdr (symbol-value refs)))
4320          (while heads
4321            (if (memq (mail-header-number (caar heads))
4322                      gnus-newsgroup-dormant)
4323                (setq heads (cdr heads))
4324              (setq id (symbol-name refs))
4325              (while (and (setq id (gnus-build-get-header id))
4326                          (not (car (gnus-id-to-thread id)))))
4327              (setq heads nil)))))
4328      gnus-newsgroup-dependencies)))
4329
4330 (defsubst gnus-remove-odd-characters (string)
4331   "Translate STRING into something that doesn't contain weird characters."
4332   (mm-subst-char-in-string
4333    ?\r ?\-
4334    (mm-subst-char-in-string ?\n ?\- string t) t))
4335
4336 ;; This function has to be called with point after the article number
4337 ;; on the beginning of the line.
4338 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4339   (let ((eol (point-at-eol))
4340         (buffer (current-buffer))
4341         header references in-reply-to)
4342
4343     ;; overview: [num subject from date id refs chars lines misc]
4344     (unwind-protect
4345         (let (x)
4346           (narrow-to-region (point) eol)
4347           (unless (eobp)
4348             (forward-char))
4349
4350           (setq header
4351                 (make-full-mail-header
4352                  number                 ; number
4353                  (condition-case ()     ; subject
4354                      (gnus-remove-odd-characters
4355                       (funcall gnus-decode-encoded-word-function
4356                                (setq x (nnheader-nov-field))))
4357                    (error x))
4358                  (condition-case ()     ; from
4359                      (gnus-remove-odd-characters
4360                       (funcall gnus-decode-encoded-address-function
4361                                (setq x (nnheader-nov-field))))
4362                    (error x))
4363                  (nnheader-nov-field)   ; date
4364                  (nnheader-nov-read-message-id number)  ; id
4365                  (setq references (nnheader-nov-field)) ; refs
4366                  (nnheader-nov-read-integer) ; chars
4367                  (nnheader-nov-read-integer) ; lines
4368                  (unless (eobp)
4369                    (if (looking-at "Xref: ")
4370                        (goto-char (match-end 0)))
4371                    (nnheader-nov-field)) ; Xref
4372                  (nnheader-nov-parse-extra)))) ; extra
4373
4374       (widen))
4375
4376     (when (and (string= references "")
4377                (setq in-reply-to (mail-header-extra header))
4378                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4379       (mail-header-set-references
4380        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4381
4382     (when gnus-alter-header-function
4383       (funcall gnus-alter-header-function header))
4384     (gnus-dependencies-add-header header dependencies force-new)))
4385
4386 (defun gnus-build-get-header (id)
4387   "Look through the buffer of NOV lines and find the header to ID.
4388 Enter this line into the dependencies hash table, and return
4389 the id of the parent article (if any)."
4390   (let ((deps gnus-newsgroup-dependencies)
4391         found header)
4392     (prog1
4393         (save-excursion
4394           (set-buffer nntp-server-buffer)
4395           (let ((case-fold-search nil))
4396             (goto-char (point-min))
4397             (while (and (not found)
4398                         (search-forward id nil t))
4399               (beginning-of-line)
4400               (setq found (looking-at
4401                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4402                                    (regexp-quote id))))
4403               (or found (beginning-of-line 2)))
4404             (when found
4405               (beginning-of-line)
4406               (and
4407                (setq header (gnus-nov-parse-line
4408                              (read (current-buffer)) deps))
4409                (gnus-parent-id (mail-header-references header))))))
4410       (when header
4411         (let ((number (mail-header-number header)))
4412           (push number gnus-newsgroup-limit)
4413           (push header gnus-newsgroup-headers)
4414           (if (memq number gnus-newsgroup-unselected)
4415               (progn
4416                 (setq gnus-newsgroup-unreads
4417                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4418                                                number))
4419                 (setq gnus-newsgroup-unselected
4420                       (delq number gnus-newsgroup-unselected)))
4421             (push number gnus-newsgroup-ancient)))))))
4422
4423 (defun gnus-build-all-threads ()
4424   "Read all the headers."
4425   (let ((gnus-summary-ignore-duplicates t)
4426         (mail-parse-charset gnus-newsgroup-charset)
4427         (dependencies gnus-newsgroup-dependencies)
4428         header article)
4429     (save-excursion
4430       (set-buffer nntp-server-buffer)
4431       (let ((case-fold-search nil))
4432         (goto-char (point-min))
4433         (while (not (eobp))
4434           (ignore-errors
4435             (setq article (read (current-buffer))
4436                   header (gnus-nov-parse-line article dependencies)))
4437           (when header
4438             (with-current-buffer gnus-summary-buffer
4439               (push header gnus-newsgroup-headers)
4440               (if (memq (setq article (mail-header-number header))
4441                         gnus-newsgroup-unselected)
4442                   (progn
4443                     (setq gnus-newsgroup-unreads
4444                           (gnus-add-to-sorted-list
4445                            gnus-newsgroup-unreads article))
4446                     (setq gnus-newsgroup-unselected
4447                           (delq article gnus-newsgroup-unselected)))
4448                 (push article gnus-newsgroup-ancient)))
4449             (forward-line 1)))))))
4450
4451 (defun gnus-summary-update-article-line (article header)
4452   "Update the line for ARTICLE using HEADER."
4453   (let* ((id (mail-header-id header))
4454          (thread (gnus-id-to-thread id)))
4455     (unless thread
4456       (error "Article in no thread"))
4457     ;; Update the thread.
4458     (setcar thread header)
4459     (gnus-summary-goto-subject article)
4460     (let* ((datal (gnus-data-find-list article))
4461            (data (car datal))
4462            (buffer-read-only nil)
4463            (level (gnus-summary-thread-level)))
4464       (gnus-delete-line)
4465       (let ((inserted (- (point)
4466                          (progn
4467                            (gnus-summary-insert-line
4468                             header level nil
4469                             (memq article gnus-newsgroup-undownloaded)
4470                             (gnus-article-mark article)
4471                             (memq article gnus-newsgroup-replied)
4472                             (memq article gnus-newsgroup-expirable)
4473                             ;; Only insert the Subject string when it's different
4474                             ;; from the previous Subject string.
4475                             (if (and
4476                                  gnus-show-threads
4477                                  (gnus-subject-equal
4478                                   (condition-case ()
4479                                       (mail-header-subject
4480                                        (gnus-data-header
4481                                         (cadr
4482                                          (gnus-data-find-list
4483                                           article
4484                                           (gnus-data-list t)))))
4485                                     ;; Error on the side of excessive subjects.
4486                                     (error ""))
4487                                   (mail-header-subject header)))
4488                                 ""
4489                               (mail-header-subject header))
4490                             nil (cdr (assq article gnus-newsgroup-scored))
4491                             (memq article gnus-newsgroup-processable))
4492                            (point)))))
4493         (when (cdr datal)
4494           (gnus-data-update-list
4495            (cdr datal)
4496            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4497
4498 (defun gnus-summary-update-article (article &optional iheader)
4499   "Update ARTICLE in the summary buffer."
4500   (set-buffer gnus-summary-buffer)
4501   (let* ((header (gnus-summary-article-header article))
4502          (id (mail-header-id header))
4503          (data (gnus-data-find article))
4504          (thread (gnus-id-to-thread id))
4505          (references (mail-header-references header))
4506          (parent
4507           (gnus-id-to-thread
4508            (or (gnus-parent-id
4509                 (when (and references
4510                            (not (equal "" references)))
4511                   references))
4512                "none")))
4513          (buffer-read-only nil)
4514          (old (car thread)))
4515     (when thread
4516       (unless iheader
4517         (setcar thread nil)
4518         (when parent
4519           (delq thread parent)))
4520       (if (gnus-summary-insert-subject id header)
4521           ;; Set the (possibly) new article number in the data structure.
4522           (gnus-data-set-number data (gnus-id-to-article id))
4523         (setcar thread old)
4524         nil))))
4525
4526 (defun gnus-rebuild-thread (id &optional line)
4527   "Rebuild the thread containing ID.
4528 If LINE, insert the rebuilt thread starting on line LINE."
4529   (let ((buffer-read-only nil)
4530         old-pos current thread data)
4531     (if (not gnus-show-threads)
4532         (setq thread (list (car (gnus-id-to-thread id))))
4533       ;; Get the thread this article is part of.
4534       (setq thread (gnus-remove-thread id)))
4535     (setq old-pos (point-at-bol))
4536     (setq current (save-excursion
4537                     (and (re-search-backward "[\r\n]" nil t)
4538                          (gnus-summary-article-number))))
4539     ;; If this is a gathered thread, we have to go some re-gathering.
4540     (when (stringp (car thread))
4541       (let ((subject (car thread))
4542             roots thr)
4543         (setq thread (cdr thread))
4544         (while thread
4545           (unless (memq (setq thr (gnus-id-to-thread
4546                                    (gnus-root-id
4547                                     (mail-header-id (caar thread)))))
4548                         roots)
4549             (push thr roots))
4550           (setq thread (cdr thread)))
4551         ;; We now have all (unique) roots.
4552         (if (= (length roots) 1)
4553             ;; All the loose roots are now one solid root.
4554             (setq thread (car roots))
4555           (setq thread (cons subject (gnus-sort-threads roots))))))
4556     (let (threads)
4557       ;; We then insert this thread into the summary buffer.
4558       (when line
4559         (goto-char (point-min))
4560         (forward-line (1- line)))
4561       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4562         (if gnus-show-threads
4563             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4564           (gnus-summary-prepare-unthreaded thread))
4565         (setq data (nreverse gnus-newsgroup-data))
4566         (setq threads gnus-newsgroup-threads))
4567       ;; We splice the new data into the data structure.
4568       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4569       ;;!!! then we want to insert at the beginning of the buffer.
4570       ;;!!! That happens to be true with Gnus now, but that may
4571       ;;!!! change in the future.  Perhaps.
4572       (gnus-data-enter-list
4573        (if line nil current) data (- (point) old-pos))
4574       (setq gnus-newsgroup-threads
4575             (nconc threads gnus-newsgroup-threads))
4576       (gnus-data-compute-positions))))
4577
4578 (defun gnus-number-to-header (number)
4579   "Return the header for article NUMBER."
4580   (let ((headers gnus-newsgroup-headers))
4581     (while (and headers
4582                 (not (= number (mail-header-number (car headers)))))
4583       (pop headers))
4584     (when headers
4585       (car headers))))
4586
4587 (defun gnus-parent-headers (in-headers &optional generation)
4588   "Return the headers of the GENERATIONeth parent of HEADERS."
4589   (unless generation
4590     (setq generation 1))
4591   (let ((parent t)
4592         (headers in-headers)
4593         references)
4594     (while (and parent
4595                 (not (zerop generation))
4596                 (setq references (mail-header-references headers)))
4597       (setq headers (if (and references
4598                              (setq parent (gnus-parent-id references)))
4599                         (car (gnus-id-to-thread parent))
4600                       nil))
4601       (decf generation))
4602     (and (not (eq headers in-headers))
4603          headers)))
4604
4605 (defun gnus-id-to-thread (id)
4606   "Return the (sub-)thread where ID appears."
4607   (gnus-gethash id gnus-newsgroup-dependencies))
4608
4609 (defun gnus-id-to-article (id)
4610   "Return the article number of ID."
4611   (let ((thread (gnus-id-to-thread id)))
4612     (when (and thread
4613                (car thread))
4614       (mail-header-number (car thread)))))
4615
4616 (defun gnus-id-to-header (id)
4617   "Return the article headers of ID."
4618   (car (gnus-id-to-thread id)))
4619
4620 (defun gnus-article-displayed-root-p (article)
4621   "Say whether ARTICLE is a root(ish) article."
4622   (let ((level (gnus-summary-thread-level article))
4623         (refs (mail-header-references  (gnus-summary-article-header article)))
4624         particle)
4625     (cond
4626      ((null level) nil)
4627      ((zerop level) t)
4628      ((null refs) t)
4629      ((null (gnus-parent-id refs)) t)
4630      ((and (= 1 level)
4631            (null (setq particle (gnus-id-to-article
4632                                  (gnus-parent-id refs))))
4633            (null (gnus-summary-thread-level particle)))))))
4634
4635 (defun gnus-root-id (id)
4636   "Return the id of the root of the thread where ID appears."
4637   (let (last-id prev)
4638     (while (and id (setq prev (car (gnus-id-to-thread id))))
4639       (setq last-id id
4640             id (gnus-parent-id (mail-header-references prev))))
4641     last-id))
4642
4643 (defun gnus-articles-in-thread (thread)
4644   "Return the list of articles in THREAD."
4645   (cons (mail-header-number (car thread))
4646         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4647
4648 (defun gnus-remove-thread (id &optional dont-remove)
4649   "Remove the thread that has ID in it."
4650   (let (headers thread last-id)
4651     ;; First go up in this thread until we find the root.
4652     (setq last-id (gnus-root-id id)
4653           headers (message-flatten-list (gnus-id-to-thread last-id)))
4654     ;; We have now found the real root of this thread.  It might have
4655     ;; been gathered into some loose thread, so we have to search
4656     ;; through the threads to find the thread we wanted.
4657     (let ((threads gnus-newsgroup-threads)
4658           sub)
4659       (while threads
4660         (setq sub (car threads))
4661         (if (stringp (car sub))
4662             ;; This is a gathered thread, so we look at the roots
4663             ;; below it to find whether this article is in this
4664             ;; gathered root.
4665             (progn
4666               (setq sub (cdr sub))
4667               (while sub
4668                 (when (member (caar sub) headers)
4669                   (setq thread (car threads)
4670                         threads nil
4671                         sub nil))
4672                 (setq sub (cdr sub))))
4673           ;; It's an ordinary thread, so we check it.
4674           (when (eq (car sub) (car headers))
4675             (setq thread sub
4676                   threads nil)))
4677         (setq threads (cdr threads)))
4678       ;; If this article is in no thread, then it's a root.
4679       (if thread
4680           (unless dont-remove
4681             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4682         (setq thread (gnus-id-to-thread last-id)))
4683       (when thread
4684         (prog1
4685             thread                      ; We return this thread.
4686           (unless dont-remove
4687             (if (stringp (car thread))
4688                 (progn
4689                   ;; If we use dummy roots, then we have to remove the
4690                   ;; dummy root as well.
4691                   (when (eq gnus-summary-make-false-root 'dummy)
4692                     ;; We go to the dummy root by going to
4693                     ;; the first sub-"thread", and then one line up.
4694                     (gnus-summary-goto-article
4695                      (mail-header-number (caadr thread)))
4696                     (forward-line -1)
4697                     (gnus-delete-line)
4698                     (gnus-data-compute-positions))
4699                   (setq thread (cdr thread))
4700                   (while thread
4701                     (gnus-remove-thread-1 (car thread))
4702                     (setq thread (cdr thread))))
4703               (gnus-remove-thread-1 thread))))))))
4704
4705 (defun gnus-remove-thread-1 (thread)
4706   "Remove the thread THREAD recursively."
4707   (let ((number (mail-header-number (pop thread)))
4708         d)
4709     (setq thread (reverse thread))
4710     (while thread
4711       (gnus-remove-thread-1 (pop thread)))
4712     (when (setq d (gnus-data-find number))
4713       (goto-char (gnus-data-pos d))
4714       (gnus-summary-show-thread)
4715       (gnus-data-remove
4716        number
4717        (- (point-at-bol)
4718           (prog1
4719               (1+ (point-at-eol))
4720             (gnus-delete-line)))))))
4721
4722 (defun gnus-sort-threads-recursive (threads func)
4723   (sort (mapcar (lambda (thread)
4724                   (cons (car thread)
4725                         (and (cdr thread)
4726                              (gnus-sort-threads-recursive (cdr thread) func))))
4727                 threads) func))
4728
4729 (defun gnus-sort-threads-loop (threads func)
4730   (let* ((superthread (cons nil threads))
4731          (stack (list (cons superthread threads)))
4732          remaining-threads thread)
4733     (while stack
4734       (setq remaining-threads (cdr (car stack)))
4735       (if remaining-threads
4736           (progn (setq thread (car remaining-threads))
4737                  (setcdr (car stack) (cdr remaining-threads))
4738                  (if (cdr thread)
4739                      (push (cons thread (cdr thread)) stack)))
4740         (setq thread (caar stack))
4741         (setcdr thread (sort (cdr thread) func))
4742         (pop stack)))
4743     (cdr superthread)))
4744
4745 (defun gnus-sort-threads (threads)
4746   "Sort THREADS."
4747   (if (not gnus-thread-sort-functions)
4748       threads
4749     (gnus-message 8 "Sorting threads...")
4750     (prog1
4751         (condition-case nil
4752             (let ((max-lisp-eval-depth (max max-lisp-eval-depth 5000)))
4753               (gnus-sort-threads-recursive
4754                threads (gnus-make-sort-function gnus-thread-sort-functions)))
4755           ;; Even after binding max-lisp-eval-depth, the recursive
4756           ;; sorter might fail for very long threads.  In that case,
4757           ;; try using a (less well-tested) non-recursive sorter.
4758           (error (gnus-sort-threads-loop
4759                   threads (gnus-make-sort-function
4760                            gnus-thread-sort-functions))))
4761       (gnus-message 8 "Sorting threads...done"))))
4762
4763 (defun gnus-sort-articles (articles)
4764   "Sort ARTICLES."
4765   (when gnus-article-sort-functions
4766     (gnus-message 7 "Sorting articles...")
4767     (prog1
4768         (setq gnus-newsgroup-headers
4769               (sort articles (gnus-make-sort-function
4770                               gnus-article-sort-functions)))
4771       (gnus-message 7 "Sorting articles...done"))))
4772
4773 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4774 (defmacro gnus-thread-header (thread)
4775   "Return header of first article in THREAD.
4776 Note that THREAD must never, ever be anything else than a variable -
4777 using some other form will lead to serious barfage."
4778   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4779   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4780   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4781         (vector thread) 2))
4782
4783 (defsubst gnus-article-sort-by-number (h1 h2)
4784   "Sort articles by article number."
4785   (< (mail-header-number h1)
4786      (mail-header-number h2)))
4787
4788 (defun gnus-thread-sort-by-number (h1 h2)
4789   "Sort threads by root article number."
4790   (gnus-article-sort-by-number
4791    (gnus-thread-header h1) (gnus-thread-header h2)))
4792
4793 (defsubst gnus-article-sort-by-random (h1 h2)
4794   "Sort articles by article number."
4795   (zerop (random 2)))
4796
4797 (defun gnus-thread-sort-by-random (h1 h2)
4798   "Sort threads by root article number."
4799   (gnus-article-sort-by-random
4800    (gnus-thread-header h1) (gnus-thread-header h2)))
4801
4802 (defsubst gnus-article-sort-by-lines (h1 h2)
4803   "Sort articles by article Lines header."
4804   (< (mail-header-lines h1)
4805      (mail-header-lines h2)))
4806
4807 (defun gnus-thread-sort-by-lines (h1 h2)
4808   "Sort threads by root article Lines header."
4809   (gnus-article-sort-by-lines
4810    (gnus-thread-header h1) (gnus-thread-header h2)))
4811
4812 (defsubst gnus-article-sort-by-chars (h1 h2)
4813   "Sort articles by octet length."
4814   (< (mail-header-chars h1)
4815      (mail-header-chars h2)))
4816
4817 (defun gnus-thread-sort-by-chars (h1 h2)
4818   "Sort threads by root article octet length."
4819   (gnus-article-sort-by-chars
4820    (gnus-thread-header h1) (gnus-thread-header h2)))
4821
4822 (defsubst gnus-article-sort-by-author (h1 h2)
4823   "Sort articles by root author."
4824   (string-lessp
4825    (let ((extract (funcall
4826                    gnus-extract-address-components
4827                    (mail-header-from h1))))
4828      (or (car extract) (cadr extract) ""))
4829    (let ((extract (funcall
4830                    gnus-extract-address-components
4831                    (mail-header-from h2))))
4832      (or (car extract) (cadr extract) ""))))
4833
4834 (defun gnus-thread-sort-by-author (h1 h2)
4835   "Sort threads by root author."
4836   (gnus-article-sort-by-author
4837    (gnus-thread-header h1)  (gnus-thread-header h2)))
4838
4839 (defsubst gnus-article-sort-by-recipient (h1 h2)
4840   "Sort articles by recipient."
4841   (string-lessp
4842    (let ((extract (funcall
4843                    gnus-extract-address-components
4844                    (or (cdr (assq 'To (mail-header-extra h1))) ""))))
4845      (or (car extract) (cadr extract)))
4846    (let ((extract (funcall
4847                    gnus-extract-address-components
4848                    (or (cdr (assq 'To (mail-header-extra h2))) ""))))
4849      (or (car extract) (cadr extract)))))
4850
4851 (defun gnus-thread-sort-by-recipient (h1 h2)
4852   "Sort threads by root recipient."
4853   (gnus-article-sort-by-recipient
4854    (gnus-thread-header h1) (gnus-thread-header h2)))
4855
4856 (defsubst gnus-article-sort-by-subject (h1 h2)
4857   "Sort articles by root subject."
4858   (string-lessp
4859    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4860    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4861
4862 (defun gnus-thread-sort-by-subject (h1 h2)
4863   "Sort threads by root subject."
4864   (gnus-article-sort-by-subject
4865    (gnus-thread-header h1) (gnus-thread-header h2)))
4866
4867 (defsubst gnus-article-sort-by-date (h1 h2)
4868   "Sort articles by root article date."
4869   (time-less-p
4870    (gnus-date-get-time (mail-header-date h1))
4871    (gnus-date-get-time (mail-header-date h2))))
4872
4873 (defun gnus-thread-sort-by-date (h1 h2)
4874   "Sort threads by root article date."
4875   (gnus-article-sort-by-date
4876    (gnus-thread-header h1) (gnus-thread-header h2)))
4877
4878 (defsubst gnus-article-sort-by-score (h1 h2)
4879   "Sort articles by root article score.
4880 Unscored articles will be counted as having a score of zero."
4881   (> (or (cdr (assq (mail-header-number h1)
4882                     gnus-newsgroup-scored))
4883          gnus-summary-default-score 0)
4884      (or (cdr (assq (mail-header-number h2)
4885                     gnus-newsgroup-scored))
4886          gnus-summary-default-score 0)))
4887
4888 (defun gnus-thread-sort-by-score (h1 h2)
4889   "Sort threads by root article score."
4890   (gnus-article-sort-by-score
4891    (gnus-thread-header h1) (gnus-thread-header h2)))
4892
4893 (defun gnus-thread-sort-by-total-score (h1 h2)
4894   "Sort threads by the sum of all scores in the thread.
4895 Unscored articles will be counted as having a score of zero."
4896   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4897
4898 (defun gnus-thread-total-score (thread)
4899   ;; This function find the total score of THREAD.
4900   (cond
4901    ((null thread)
4902     0)
4903    ((consp thread)
4904     (if (stringp (car thread))
4905         (apply gnus-thread-score-function 0
4906                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4907       (gnus-thread-total-score-1 thread)))
4908    (t
4909     (gnus-thread-total-score-1 (list thread)))))
4910
4911 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4912   "Sort threads such that the thread with the most recently arrived article comes first."
4913   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4914
4915 (defun gnus-thread-highest-number (thread)
4916   "Return the highest article number in THREAD."
4917   (apply 'max (mapcar (lambda (header)
4918                         (mail-header-number header))
4919                       (message-flatten-list thread))))
4920
4921 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4922   "Sort threads such that the thread with the most recently dated article comes first."
4923   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4924
4925 (defun gnus-thread-latest-date (thread)
4926   "Return the highest article date in THREAD."
4927   (let ((previous-time 0))
4928     (apply 'max
4929            (mapcar
4930             (lambda (header)
4931               (setq previous-time
4932                     (condition-case ()
4933                         (time-to-seconds (mail-header-parse-date
4934                                           (mail-header-date header)))
4935                       (error previous-time))))
4936             (sort
4937              (message-flatten-list thread)
4938              (lambda (h1 h2)
4939                (< (mail-header-number h1)
4940                   (mail-header-number h2))))))))
4941
4942 (defun gnus-thread-total-score-1 (root)
4943   ;; This function find the total score of the thread below ROOT.
4944   (setq root (car root))
4945   (apply gnus-thread-score-function
4946          (or (append
4947               (mapcar 'gnus-thread-total-score
4948                       (cdr (gnus-id-to-thread (mail-header-id root))))
4949               (when (> (mail-header-number root) 0)
4950                 (list (or (cdr (assq (mail-header-number root)
4951                                      gnus-newsgroup-scored))
4952                           gnus-summary-default-score 0))))
4953              (list gnus-summary-default-score)
4954              '(0))))
4955
4956 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4957 (defvar gnus-tmp-prev-subject nil)
4958 (defvar gnus-tmp-false-parent nil)
4959 (defvar gnus-tmp-root-expunged nil)
4960 (defvar gnus-tmp-dummy-line nil)
4961
4962 (eval-when-compile (defvar gnus-tmp-header))
4963 (defun gnus-extra-header (type &optional header)
4964   "Return the extra header of TYPE."
4965   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4966       ""))
4967
4968 (defvar gnus-tmp-thread-tree-header-string "")
4969
4970 (defcustom gnus-sum-thread-tree-root "> "
4971   "With %B spec, used for the root of a thread.
4972 If nil, use subject instead."
4973   :version "22.1"
4974   :type '(radio (const :format "%v  " nil) string)
4975   :group 'gnus-thread)
4976
4977 (defcustom gnus-sum-thread-tree-false-root "> "
4978   "With %B spec, used for a false root of a thread.
4979 If nil, use subject instead."
4980   :version "22.1"
4981   :type '(radio (const :format "%v  " nil) string)
4982   :group 'gnus-thread)
4983
4984 (defcustom gnus-sum-thread-tree-single-indent ""
4985   "With %B spec, used for a thread with just one message.
4986 If nil, use subject instead."
4987   :version "22.1"
4988   :type '(radio (const :format "%v  " nil) string)
4989   :group 'gnus-thread)
4990
4991 (defcustom gnus-sum-thread-tree-vertical "| "
4992   "With %B spec, used for drawing a vertical line."
4993   :version "22.1"
4994   :type 'string
4995   :group 'gnus-thread)
4996
4997 (defcustom gnus-sum-thread-tree-indent "  "
4998   "With %B spec, used for indenting."
4999   :version "22.1"
5000   :type 'string
5001   :group 'gnus-thread)
5002
5003 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
5004   "With %B spec, used for a leaf with brothers."
5005   :version "22.1"
5006   :type 'string
5007   :group 'gnus-thread)
5008
5009 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
5010   "With %B spec, used for a leaf without brothers."
5011   :version "22.1"
5012   :type 'string
5013   :group 'gnus-thread)
5014
5015 (defun gnus-summary-prepare-threads (threads)
5016   "Prepare summary buffer from THREADS and indentation LEVEL.
5017 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
5018 or a straight list of headers."
5019   (gnus-message 7 "Generating summary...")
5020
5021   (setq gnus-newsgroup-threads threads)
5022   (beginning-of-line)
5023
5024   (let ((gnus-tmp-level 0)
5025         (default-score (or gnus-summary-default-score 0))
5026         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
5027         (building-line-count gnus-summary-display-while-building)
5028         (building-count (integerp gnus-summary-display-while-building))
5029         thread number subject stack state gnus-tmp-gathered beg-match
5030         new-roots gnus-tmp-new-adopts thread-end simp-subject
5031         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
5032         gnus-tmp-replied gnus-tmp-subject-or-nil
5033         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
5034         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
5035         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
5036         tree-stack)
5037
5038     (setq gnus-tmp-prev-subject nil
5039           gnus-tmp-thread-tree-header-string "")
5040
5041     (if (vectorp (car threads))
5042         ;; If this is a straight (sic) list of headers, then a
5043         ;; threaded summary display isn't required, so we just create
5044         ;; an unthreaded one.
5045         (gnus-summary-prepare-unthreaded threads)
5046
5047       ;; Do the threaded display.
5048
5049       (if gnus-summary-display-while-building
5050           (switch-to-buffer (buffer-name)))
5051       (while (or threads stack gnus-tmp-new-adopts new-roots)
5052
5053         (if (and (= gnus-tmp-level 0)
5054                  (or (not stack)
5055                      (= (caar stack) 0))
5056                  (not gnus-tmp-false-parent)
5057                  (or gnus-tmp-new-adopts new-roots))
5058             (if gnus-tmp-new-adopts
5059                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
5060                       thread (list (car gnus-tmp-new-adopts))
5061                       gnus-tmp-header (caar thread)
5062                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
5063               (when new-roots
5064                 (setq thread (list (car new-roots))
5065                       gnus-tmp-header (caar thread)
5066                       new-roots (cdr new-roots))))
5067
5068           (if threads
5069               ;; If there are some threads, we do them before the
5070               ;; threads on the stack.
5071               (setq thread threads
5072                     gnus-tmp-header (caar thread))
5073             ;; There were no current threads, so we pop something off
5074             ;; the stack.
5075             (setq state (car stack)
5076                   gnus-tmp-level (car state)
5077                   tree-stack (cadr state)
5078                   thread (caddr state)
5079                   stack (cdr stack)
5080                   gnus-tmp-header (caar thread))))
5081
5082         (setq gnus-tmp-false-parent nil)
5083         (setq gnus-tmp-root-expunged nil)
5084         (setq thread-end nil)
5085
5086         (if (stringp gnus-tmp-header)
5087             ;; The header is a dummy root.
5088             (cond
5089              ((eq gnus-summary-make-false-root 'adopt)
5090               ;; We let the first article adopt the rest.
5091               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
5092                                                (cddar thread)))
5093               (setq gnus-tmp-gathered
5094                     (nconc (mapcar
5095                             (lambda (h) (mail-header-number (car h)))
5096                             (cddar thread))
5097                            gnus-tmp-gathered))
5098               (setq thread (cons (list (caar thread)
5099                                        (cadar thread))
5100                                  (cdr thread)))
5101               (setq gnus-tmp-level -1
5102                     gnus-tmp-false-parent t))
5103              ((eq gnus-summary-make-false-root 'empty)
5104               ;; We print adopted articles with empty subject fields.
5105               (setq gnus-tmp-gathered
5106                     (nconc (mapcar
5107                             (lambda (h) (mail-header-number (car h)))
5108                             (cddar thread))
5109                            gnus-tmp-gathered))
5110               (setq gnus-tmp-level -1))
5111              ((eq gnus-summary-make-false-root 'dummy)
5112               ;; We remember that we probably want to output a dummy
5113               ;; root.
5114               (setq gnus-tmp-dummy-line gnus-tmp-header)
5115               (setq gnus-tmp-prev-subject gnus-tmp-header))
5116              (t
5117               ;; We do not make a root for the gathered
5118               ;; sub-threads at all.
5119               (setq gnus-tmp-level -1)))
5120
5121           (setq number (mail-header-number gnus-tmp-header)
5122                 subject (mail-header-subject gnus-tmp-header)
5123                 simp-subject (gnus-simplify-subject-fully subject))
5124
5125           (cond
5126            ;; If the thread has changed subject, we might want to make
5127            ;; this subthread into a root.
5128            ((and (null gnus-thread-ignore-subject)
5129                  (not (zerop gnus-tmp-level))
5130                  gnus-tmp-prev-subject
5131                  (not (string= gnus-tmp-prev-subject simp-subject)))
5132             (setq new-roots (nconc new-roots (list (car thread)))
5133                   thread-end t
5134                   gnus-tmp-header nil))
5135            ;; If the article lies outside the current limit,
5136            ;; then we do not display it.
5137            ((not (memq number gnus-newsgroup-limit))
5138             (setq gnus-tmp-gathered
5139                   (nconc (mapcar
5140                           (lambda (h) (mail-header-number (car h)))
5141                           (cdar thread))
5142                          gnus-tmp-gathered))
5143             (setq gnus-tmp-new-adopts (if (cdar thread)
5144                                           (append gnus-tmp-new-adopts
5145                                                   (cdar thread))
5146                                         gnus-tmp-new-adopts)
5147                   thread-end t
5148                   gnus-tmp-header nil)
5149             (when (zerop gnus-tmp-level)
5150               (setq gnus-tmp-root-expunged t)))
5151            ;; Perhaps this article is to be marked as read?
5152            ((and gnus-summary-mark-below
5153                  (< (or (cdr (assq number gnus-newsgroup-scored))
5154                         default-score)
5155                     gnus-summary-mark-below)
5156                  ;; Don't touch sparse articles.
5157                  (not (gnus-summary-article-sparse-p number))
5158                  (not (gnus-summary-article-ancient-p number)))
5159             (setq gnus-newsgroup-unreads
5160                   (delq number gnus-newsgroup-unreads))
5161             (if gnus-newsgroup-auto-expire
5162                 (setq gnus-newsgroup-expirable
5163                       (gnus-add-to-sorted-list
5164                        gnus-newsgroup-expirable number))
5165               (push (cons number gnus-low-score-mark)
5166                     gnus-newsgroup-reads))))
5167
5168           (when gnus-tmp-header
5169             ;; We may have an old dummy line to output before this
5170             ;; article.
5171             (when (and gnus-tmp-dummy-line
5172                        (gnus-subject-equal
5173                         gnus-tmp-dummy-line
5174                         (mail-header-subject gnus-tmp-header)))
5175               (gnus-summary-insert-dummy-line
5176                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
5177               (setq gnus-tmp-dummy-line nil))
5178
5179             ;; Compute the mark.
5180             (setq gnus-tmp-unread (gnus-article-mark number))
5181
5182             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
5183                                   gnus-tmp-header gnus-tmp-level)
5184                   gnus-newsgroup-data)
5185
5186             ;; Actually insert the line.
5187             (setq
5188              gnus-tmp-subject-or-nil
5189              (cond
5190               ((and gnus-thread-ignore-subject
5191                     gnus-tmp-prev-subject
5192                     (not (string= gnus-tmp-prev-subject simp-subject)))
5193                subject)
5194               ((zerop gnus-tmp-level)
5195                (if (and (eq gnus-summary-make-false-root 'empty)
5196                         (memq number gnus-tmp-gathered)
5197                         gnus-tmp-prev-subject
5198                         (string= gnus-tmp-prev-subject simp-subject))
5199                    gnus-summary-same-subject
5200                  subject))
5201               (t gnus-summary-same-subject)))
5202             (if (and (eq gnus-summary-make-false-root 'adopt)
5203                      (= gnus-tmp-level 1)
5204                      (memq number gnus-tmp-gathered))
5205                 (setq gnus-tmp-opening-bracket ?\<
5206                       gnus-tmp-closing-bracket ?\>)
5207               (setq gnus-tmp-opening-bracket ?\[
5208                     gnus-tmp-closing-bracket ?\]))
5209             (if (>= gnus-tmp-level (length gnus-thread-indent-array))
5210                 (gnus-make-thread-indent-array
5211                  (max (* 2 (length gnus-thread-indent-array))
5212                       gnus-tmp-level)))
5213             (setq
5214              gnus-tmp-indentation
5215              (aref gnus-thread-indent-array gnus-tmp-level)
5216              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
5217              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
5218                                 gnus-summary-default-score 0)
5219              gnus-tmp-score-char
5220              (if (or (null gnus-summary-default-score)
5221                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
5222                          gnus-summary-zcore-fuzz))
5223                  ?                      ;Whitespace
5224                (if (< gnus-tmp-score gnus-summary-default-score)
5225                    gnus-score-below-mark gnus-score-over-mark))
5226              gnus-tmp-replied
5227              (cond ((memq number gnus-newsgroup-processable)
5228                     gnus-process-mark)
5229                    ((memq number gnus-newsgroup-cached)
5230                     gnus-cached-mark)
5231                    ((memq number gnus-newsgroup-replied)
5232                     gnus-replied-mark)
5233                    ((memq number gnus-newsgroup-forwarded)
5234                     gnus-forwarded-mark)
5235                    ((memq number gnus-newsgroup-saved)
5236                     gnus-saved-mark)
5237                    ((memq number gnus-newsgroup-recent)
5238                     gnus-recent-mark)
5239                    ((memq number gnus-newsgroup-unseen)
5240                     gnus-unseen-mark)
5241                    (t gnus-no-mark))
5242              gnus-tmp-downloaded
5243              (cond ((memq number gnus-newsgroup-undownloaded)
5244                     gnus-undownloaded-mark)
5245                    (gnus-newsgroup-agentized
5246                     gnus-downloaded-mark)
5247                    (t
5248                     gnus-no-mark))
5249              gnus-tmp-from (mail-header-from gnus-tmp-header)
5250              gnus-tmp-name
5251              (cond
5252               ((string-match "<[^>]+> *$" gnus-tmp-from)
5253                (setq beg-match (match-beginning 0))
5254                (or (and (string-match "^\".+\"" gnus-tmp-from)
5255                         (substring gnus-tmp-from 1 (1- (match-end 0))))
5256                    (substring gnus-tmp-from 0 beg-match)))
5257               ((string-match "(.+)" gnus-tmp-from)
5258                (substring gnus-tmp-from
5259                           (1+ (match-beginning 0)) (1- (match-end 0))))
5260               (t gnus-tmp-from))
5261
5262              ;; Do the %B string
5263              gnus-tmp-thread-tree-header-string
5264              (cond
5265               ((not gnus-show-threads) "")
5266               ((zerop gnus-tmp-level)
5267                (cond ((cdar thread)
5268                       (or gnus-sum-thread-tree-root subject))
5269                      (gnus-tmp-new-adopts
5270                       (or gnus-sum-thread-tree-false-root subject))
5271                      (t
5272                       (or gnus-sum-thread-tree-single-indent subject))))
5273               (t
5274                (concat (apply 'concat
5275                               (mapcar (lambda (item)
5276                                         (if (= item 1)
5277                                             gnus-sum-thread-tree-vertical
5278                                           gnus-sum-thread-tree-indent))
5279                                       (cdr (reverse tree-stack))))
5280                        (if (nth 1 thread)
5281                            gnus-sum-thread-tree-leaf-with-other
5282                          gnus-sum-thread-tree-single-leaf)))))
5283             (when (string= gnus-tmp-name "")
5284               (setq gnus-tmp-name gnus-tmp-from))
5285             (unless (numberp gnus-tmp-lines)
5286               (setq gnus-tmp-lines -1))
5287             (if (= gnus-tmp-lines -1)
5288                 (setq gnus-tmp-lines "?")
5289               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
5290               (gnus-put-text-property
5291              (point)
5292              (progn (eval gnus-summary-line-format-spec) (point))
5293                'gnus-number number)
5294             (when gnus-visual-p
5295               (forward-line -1)
5296               (gnus-run-hooks 'gnus-summary-update-hook)
5297               (forward-line 1))
5298
5299             (setq gnus-tmp-prev-subject simp-subject)))
5300
5301         (when (nth 1 thread)
5302           (push (list (max 0 gnus-tmp-level)
5303                       (copy-sequence tree-stack)
5304                       (nthcdr 1 thread))
5305                 stack))
5306         (push (if (nth 1 thread) 1 0) tree-stack)
5307         (incf gnus-tmp-level)
5308         (setq threads (if thread-end nil (cdar thread)))
5309         (if gnus-summary-display-while-building
5310             (if building-count
5311                 (progn
5312                   ;; use a set frequency
5313                   (setq building-line-count (1- building-line-count))
5314                   (when (= building-line-count 0)
5315                     (sit-for 0)
5316                     (setq building-line-count
5317                           gnus-summary-display-while-building)))
5318               ;; always
5319               (sit-for 0)))
5320         (unless threads
5321           (setq gnus-tmp-level 0)))))
5322   (gnus-message 7 "Generating summary...done"))
5323
5324 (defun gnus-summary-prepare-unthreaded (headers)
5325   "Generate an unthreaded summary buffer based on HEADERS."
5326   (let (header number mark)
5327
5328     (beginning-of-line)
5329
5330     (while headers
5331       ;; We may have to root out some bad articles...
5332       (when (memq (setq number (mail-header-number
5333                                 (setq header (pop headers))))
5334                   gnus-newsgroup-limit)
5335         ;; Mark article as read when it has a low score.
5336         (when (and gnus-summary-mark-below
5337                    (< (or (cdr (assq number gnus-newsgroup-scored))
5338                           gnus-summary-default-score 0)
5339                       gnus-summary-mark-below)
5340                    (not (gnus-summary-article-ancient-p number)))
5341           (setq gnus-newsgroup-unreads
5342                 (delq number gnus-newsgroup-unreads))
5343           (if gnus-newsgroup-auto-expire
5344               (push number gnus-newsgroup-expirable)
5345             (push (cons number gnus-low-score-mark)
5346                   gnus-newsgroup-reads)))
5347
5348         (setq mark (gnus-article-mark number))
5349         (push (gnus-data-make number mark (1+ (point)) header 0)
5350               gnus-newsgroup-data)
5351         (gnus-summary-insert-line
5352          header 0 number
5353          (memq number gnus-newsgroup-undownloaded)
5354          mark (memq number gnus-newsgroup-replied)
5355          (memq number gnus-newsgroup-expirable)
5356          (mail-header-subject header) nil
5357          (cdr (assq number gnus-newsgroup-scored))
5358          (memq number gnus-newsgroup-processable))))))
5359
5360 (defun gnus-summary-remove-list-identifiers ()
5361   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5362   (let ((regexp (if (consp gnus-list-identifiers)
5363                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5364                   gnus-list-identifiers))
5365         changed subject)
5366     (when regexp
5367       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5368       (dolist (header gnus-newsgroup-headers)
5369         (setq subject (mail-header-subject header)
5370               changed nil)
5371         (while (string-match regexp subject)
5372           (setq subject
5373                 (concat (substring subject 0 (match-beginning 1))
5374                         (substring subject (match-end 0)))
5375                 changed t))
5376         (when changed
5377           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5378             (setq subject
5379                   (concat (substring subject 0 (match-beginning 1))
5380                           (substring subject (match-end 1)))))
5381           (mail-header-set-subject header subject))))))
5382
5383 (defun gnus-fetch-headers (articles)
5384   "Fetch headers of ARTICLES."
5385   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5386     (gnus-message 5 "Fetching headers for %s..." name)
5387     (prog1
5388         (if (eq 'nov
5389                 (setq gnus-headers-retrieved-by
5390                       (gnus-retrieve-headers
5391                        articles gnus-newsgroup-name
5392                        ;; We might want to fetch old headers, but
5393                        ;; not if there is only 1 article.
5394                        (and (or (and
5395                                  (not (eq gnus-fetch-old-headers 'some))
5396                                  (not (numberp gnus-fetch-old-headers)))
5397                                 (> (length articles) 1))
5398                             gnus-fetch-old-headers))))
5399             (gnus-get-newsgroup-headers-xover
5400              articles nil nil gnus-newsgroup-name t)
5401           (gnus-get-newsgroup-headers))
5402       (gnus-message 5 "Fetching headers for %s...done" name))))
5403
5404 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5405   "Select newsgroup GROUP.
5406 If READ-ALL is non-nil, all articles in the group are selected.
5407 If SELECT-ARTICLES, only select those articles from GROUP."
5408   (let* ((entry (gnus-group-entry group))
5409          ;;!!! Dirty hack; should be removed.
5410          (gnus-summary-ignore-duplicates
5411           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5412               t
5413             gnus-summary-ignore-duplicates))
5414          (info (nth 2 entry))
5415          charset articles fetched-articles cached)
5416
5417     (unless (gnus-check-server
5418              (set (make-local-variable 'gnus-current-select-method)
5419                   (gnus-find-method-for-group group)))
5420       (error "Couldn't open server"))
5421     (setq charset (gnus-group-name-charset gnus-current-select-method group))
5422
5423     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5424         (gnus-activate-group group)     ; Or we can activate it...
5425         (progn                          ; Or we bug out.
5426           (when (equal major-mode 'gnus-summary-mode)
5427             (gnus-kill-buffer (current-buffer)))
5428           (error
5429            "Couldn't activate group %s: %s"
5430            (mm-decode-coding-string group charset)
5431            (mm-decode-coding-string (gnus-status-message group) charset))))
5432
5433     (unless (gnus-request-group group t)
5434         (when (equal major-mode 'gnus-summary-mode)
5435           (gnus-kill-buffer (current-buffer)))
5436         (error "Couldn't request group %s: %s"
5437                (mm-decode-coding-string group charset)
5438                (mm-decode-coding-string (gnus-status-message group) charset)))
5439
5440     (when gnus-agent
5441       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5442
5443       (setq gnus-summary-use-undownloaded-faces
5444             (gnus-agent-find-parameter
5445              group
5446              'agent-enable-undownloaded-faces)))
5447
5448     (setq gnus-newsgroup-name group
5449           gnus-newsgroup-unselected nil
5450           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5451
5452     (let ((display (gnus-group-find-parameter group 'display)))
5453       (setq gnus-newsgroup-display
5454             (cond
5455              ((not (zerop (or (car-safe read-all) 0)))
5456               ;; The user entered the group with C-u SPC/RET, let's show
5457               ;; all articles.
5458               'gnus-not-ignore)
5459              ((eq display 'all)
5460               'gnus-not-ignore)
5461              ((arrayp display)
5462               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5463              ((numberp display)
5464               ;; The following is probably the "correct" solution, but
5465               ;; it makes Gnus fetch all headers and then limit the
5466               ;; articles (which is slow), so instead we hack the
5467               ;; select-articles parameter instead. -- Simon Josefsson
5468               ;; <jas@kth.se>
5469               ;;
5470               ;; (gnus-byte-compile
5471               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5472               ;;                         display)))))
5473               (setq select-articles
5474                     (gnus-uncompress-range
5475                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5476                              (if (> tmp 0)
5477                                  tmp
5478                                1))
5479                            (cdr (gnus-active group)))))
5480               nil)
5481              (t
5482               nil))))
5483
5484     (gnus-summary-setup-default-charset)
5485
5486     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5487     (when (gnus-virtual-group-p group)
5488       (setq cached gnus-newsgroup-cached))
5489
5490     (setq gnus-newsgroup-unreads
5491           (gnus-sorted-ndifference
5492            (gnus-sorted-ndifference gnus-newsgroup-unreads
5493                                     gnus-newsgroup-marked)
5494            gnus-newsgroup-dormant))
5495
5496     (setq gnus-newsgroup-processable nil)
5497
5498     (gnus-update-read-articles group gnus-newsgroup-unreads)
5499
5500     ;; Adjust and set lists of article marks.
5501     (when info
5502       (gnus-adjust-marked-articles info))
5503     (if (setq articles select-articles)
5504         (setq gnus-newsgroup-unselected
5505               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5506       (setq articles (gnus-articles-to-read group read-all)))
5507
5508     (cond
5509      ((null articles)
5510       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5511       'quit)
5512      ((eq articles 0) nil)
5513      (t
5514       ;; Init the dependencies hash table.
5515       (setq gnus-newsgroup-dependencies
5516             (gnus-make-hashtable (length articles)))
5517       (gnus-set-global-variables)
5518       ;; Retrieve the headers and read them in.
5519
5520       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5521
5522       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5523       (when cached
5524         (setq gnus-newsgroup-cached cached))
5525
5526       ;; Suppress duplicates?
5527       (when gnus-suppress-duplicates
5528         (gnus-dup-suppress-articles))
5529
5530       ;; Set the initial limit.
5531       (setq gnus-newsgroup-limit (copy-sequence articles))
5532       ;; Remove canceled articles from the list of unread articles.
5533       (setq fetched-articles
5534             (mapcar (lambda (headers) (mail-header-number headers))
5535                     gnus-newsgroup-headers))
5536       (setq gnus-newsgroup-articles fetched-articles)
5537       (setq gnus-newsgroup-unreads
5538             (gnus-sorted-nintersection
5539              gnus-newsgroup-unreads fetched-articles))
5540       (gnus-compute-unseen-list)
5541
5542       ;; Removed marked articles that do not exist.
5543       (gnus-update-missing-marks
5544        (gnus-sorted-difference articles fetched-articles))
5545       ;; We might want to build some more threads first.
5546       (when (and gnus-fetch-old-headers
5547                  (eq gnus-headers-retrieved-by 'nov))
5548         (if (eq gnus-fetch-old-headers 'invisible)
5549             (gnus-build-all-threads)
5550           (gnus-build-old-threads)))
5551       ;; Let the Gnus agent mark articles as read.
5552       (when gnus-agent
5553         (gnus-agent-get-undownloaded-list))
5554       ;; Remove list identifiers from subject
5555       (when gnus-list-identifiers
5556         (gnus-summary-remove-list-identifiers))
5557       ;; Check whether auto-expire is to be done in this group.
5558       (setq gnus-newsgroup-auto-expire
5559             (gnus-group-auto-expirable-p group))
5560       ;; Set up the article buffer now, if necessary.
5561       (unless (and gnus-single-article-buffer
5562                    (equal gnus-article-buffer "*Article*"))
5563         (gnus-article-setup-buffer))
5564       ;; First and last article in this newsgroup.
5565       (when gnus-newsgroup-headers
5566         (setq gnus-newsgroup-begin
5567               (mail-header-number (car gnus-newsgroup-headers))
5568               gnus-newsgroup-end
5569               (mail-header-number
5570                (gnus-last-element gnus-newsgroup-headers))))
5571       ;; GROUP is successfully selected.
5572       (or gnus-newsgroup-headers t)))))
5573
5574 (defun gnus-compute-unseen-list ()
5575   ;; The `seen' marks are treated specially.
5576   (if (not gnus-newsgroup-seen)
5577       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5578     (setq gnus-newsgroup-unseen
5579           (gnus-inverse-list-range-intersection
5580            gnus-newsgroup-articles gnus-newsgroup-seen))))
5581
5582 (defun gnus-summary-display-make-predicate (display)
5583   (require 'gnus-agent)
5584   (when (= (length display) 1)
5585     (setq display (car display)))
5586   (unless gnus-summary-display-cache
5587     (dolist (elem (append '((unread . unread)
5588                             (read . read)
5589                             (unseen . unseen))
5590                           gnus-article-mark-lists))
5591       (push (cons (cdr elem)
5592                   (gnus-byte-compile
5593                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5594             gnus-summary-display-cache)))
5595   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5596         (gnus-category-predicate-cache gnus-summary-display-cache))
5597     (gnus-get-predicate display)))
5598
5599 ;; Uses the dynamically bound `number' variable.
5600 (eval-when-compile
5601   (defvar number))
5602 (defun gnus-article-marked-p (type &optional article)
5603   (let ((article (or article number)))
5604     (cond
5605      ((eq type 'tick)
5606       (memq article gnus-newsgroup-marked))
5607      ((eq type 'spam)
5608       (memq article gnus-newsgroup-spam-marked))
5609      ((eq type 'unsend)
5610       (memq article gnus-newsgroup-unsendable))
5611      ((eq type 'undownload)
5612       (memq article gnus-newsgroup-undownloaded))
5613      ((eq type 'download)
5614       (memq article gnus-newsgroup-downloadable))
5615      ((eq type 'unread)
5616       (memq article gnus-newsgroup-unreads))
5617      ((eq type 'read)
5618       (memq article gnus-newsgroup-reads))
5619      ((eq type 'dormant)
5620       (memq article gnus-newsgroup-dormant) )
5621      ((eq type 'expire)
5622       (memq article gnus-newsgroup-expirable))
5623      ((eq type 'reply)
5624       (memq article gnus-newsgroup-replied))
5625      ((eq type 'killed)
5626       (memq article gnus-newsgroup-killed))
5627      ((eq type 'bookmark)
5628       (assq article gnus-newsgroup-bookmarks))
5629      ((eq type 'score)
5630       (assq article gnus-newsgroup-scored))
5631      ((eq type 'save)
5632       (memq article gnus-newsgroup-saved))
5633      ((eq type 'cache)
5634       (memq article gnus-newsgroup-cached))
5635      ((eq type 'forward)
5636       (memq article gnus-newsgroup-forwarded))
5637      ((eq type 'seen)
5638       (not (memq article gnus-newsgroup-unseen)))
5639      ((eq type 'recent)
5640       (memq article gnus-newsgroup-recent))
5641      (t t))))
5642
5643 (defun gnus-articles-to-read (group &optional read-all)
5644   "Find out what articles the user wants to read."
5645   (let* ((articles
5646           ;; Select all articles if `read-all' is non-nil, or if there
5647           ;; are no unread articles.
5648           (if (or read-all
5649                   (and (zerop (length gnus-newsgroup-marked))
5650                        (zerop (length gnus-newsgroup-unreads)))
5651                   ;; Fetch all if the predicate is non-nil.
5652                   gnus-newsgroup-display)
5653               ;; We want to select the headers for all the articles in
5654               ;; the group, so we select either all the active
5655               ;; articles in the group, or (if that's nil), the
5656               ;; articles in the cache.
5657               (or
5658                (if gnus-newsgroup-maximum-articles
5659                    (let ((active (gnus-active group)))
5660                      (gnus-uncompress-range
5661                       (cons (max (car active)
5662                                  (- (cdr active)
5663                                     gnus-newsgroup-maximum-articles
5664                                     -1))
5665                             (cdr active))))
5666                  (gnus-uncompress-range (gnus-active group)))
5667                (gnus-cache-articles-in-group group))
5668             ;; Select only the "normal" subset of articles.
5669             (gnus-sorted-nunion
5670              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5671              gnus-newsgroup-unreads)))
5672          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5673          (scored (length scored-list))
5674          (number (length articles))
5675          (marked (+ (length gnus-newsgroup-marked)
5676                     (length gnus-newsgroup-dormant)))
5677          (select
5678           (cond
5679            ((numberp read-all)
5680             read-all)
5681            ((numberp gnus-newsgroup-display)
5682             gnus-newsgroup-display)
5683            (t
5684             (condition-case ()
5685                 (cond
5686                  ((and (or (<= scored marked) (= scored number))
5687                        (numberp gnus-large-newsgroup)
5688                        (> number gnus-large-newsgroup))
5689                   (let* ((cursor-in-echo-area nil)
5690                          (initial (gnus-parameter-large-newsgroup-initial
5691                                    gnus-newsgroup-name))
5692                          (input
5693                           (read-string
5694                            (format
5695                             "How many articles from %s (%s %d): "
5696                             (gnus-group-decoded-name gnus-newsgroup-name)
5697                             (if initial "max" "default")
5698                             number)
5699                            (if initial
5700                                (cons (number-to-string initial)
5701                                      0)))))
5702                     (if (string-match "^[ \t]*$" input) number input)))
5703                  ((and (> scored marked) (< scored number)
5704                        (> (- scored number) 20))
5705                   (let ((input
5706                          (read-string
5707                           (format "%s %s (%d scored, %d total): "
5708                                   "How many articles from"
5709                                   (gnus-group-decoded-name group)
5710                                   scored number))))
5711                     (if (string-match "^[ \t]*$" input)
5712                         number input)))
5713                  (t number))
5714               (quit
5715                (message "Quit getting the articles to read")
5716                nil))))))
5717     (setq select (if (stringp select) (string-to-number select) select))
5718     (if (or (null select) (zerop select))
5719         select
5720       (if (and (not (zerop scored)) (<= (abs select) scored))
5721           (progn
5722             (setq articles (sort scored-list '<))
5723             (setq number (length articles)))
5724         (setq articles (copy-sequence articles)))
5725
5726       (when (< (abs select) number)
5727         (if (< select 0)
5728             ;; Select the N oldest articles.
5729             (setcdr (nthcdr (1- (abs select)) articles) nil)
5730           ;; Select the N most recent articles.
5731           (setq articles (nthcdr (- number select) articles))))
5732       (setq gnus-newsgroup-unselected
5733             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5734       (when gnus-alter-articles-to-read-function
5735         (setq articles
5736               (sort
5737                (funcall gnus-alter-articles-to-read-function
5738                         gnus-newsgroup-name articles)
5739                '<)))
5740       articles)))
5741
5742 (defun gnus-killed-articles (killed articles)
5743   (let (out)
5744     (while articles
5745       (when (inline (gnus-member-of-range (car articles) killed))
5746         (push (car articles) out))
5747       (setq articles (cdr articles)))
5748     out))
5749
5750 (defun gnus-uncompress-marks (marks)
5751   "Uncompress the mark ranges in MARKS."
5752   (let ((uncompressed '(score bookmark))
5753         out)
5754     (while marks
5755       (if (memq (caar marks) uncompressed)
5756           (push (car marks) out)
5757         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5758       (setq marks (cdr marks)))
5759     out))
5760
5761 (defun gnus-article-mark-to-type (mark)
5762   "Return the type of MARK."
5763   (or (cadr (assq mark gnus-article-special-mark-lists))
5764       'list))
5765
5766 (defun gnus-article-unpropagatable-p (mark)
5767   "Return whether MARK should be propagated to back end."
5768   (memq mark gnus-article-unpropagated-mark-lists))
5769
5770 (defun gnus-adjust-marked-articles (info)
5771   "Set all article lists and remove all marks that are no longer valid."
5772   (let* ((marked-lists (gnus-info-marks info))
5773          (active (gnus-active (gnus-info-group info)))
5774          (min (car active))
5775          (max (cdr active))
5776          (types gnus-article-mark-lists)
5777          marks var articles article mark mark-type
5778          bgn end)
5779
5780     (dolist (marks marked-lists)
5781       (setq mark (car marks)
5782             mark-type (gnus-article-mark-to-type mark)
5783             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5784
5785       ;; We set the variable according to the type of the marks list,
5786       ;; and then adjust the marks to a subset of the active articles.
5787       (cond
5788        ;; Adjust "simple" lists - compressed yet unsorted
5789        ((eq mark-type 'list)
5790         ;; Simultaneously uncompress and clip to active range
5791         ;; See gnus-uncompress-range for a description of possible marks
5792         (let (l lh)
5793           (if (not (cadr marks))
5794               (set var nil)
5795             (setq articles (if (numberp (cddr marks))
5796                                (list (cdr marks))
5797                              (cdr marks))
5798                   lh (cons nil nil)
5799                   l lh)
5800
5801             (while (setq article (pop articles))
5802               (cond ((consp article)
5803                      (setq bgn (max (car article) min)
5804                            end (min (cdr article) max))
5805                      (while (<= bgn end)
5806                        (setq l (setcdr l (cons bgn nil))
5807                              bgn (1+ bgn))))
5808                     ((and (<= min article)
5809                           (>= max article))
5810                      (setq l (setcdr l (cons article nil))))))
5811             (set var (cdr lh)))))
5812        ;; Adjust assocs.
5813        ((eq mark-type 'tuple)
5814         (set var (setq articles (cdr marks)))
5815         (when (not (listp (cdr (symbol-value var))))
5816           (set var (list (symbol-value var))))
5817         (when (not (listp (cdr articles)))
5818           (setq articles (list articles)))
5819         (while articles
5820           (when (or (not (consp (setq article (pop articles))))
5821                     (< (car article) min)
5822                     (> (car article) max))
5823             (set var (delq article (symbol-value var))))))
5824        ;; Adjust ranges (sloppily).
5825        ((eq mark-type 'range)
5826         (cond
5827          ((eq mark 'seen)
5828           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5829           ;; It should be (seen (NUM1 . NUM2)).
5830           (when (numberp (cddr marks))
5831             (setcdr marks (list (cdr marks))))
5832           (setq articles (cdr marks))
5833           (while (and articles
5834                       (or (and (consp (car articles))
5835                                (> min (cdar articles)))
5836                           (and (numberp (car articles))
5837                                (> min (car articles)))))
5838             (pop articles))
5839           (set var articles))))))))
5840
5841 (defun gnus-update-missing-marks (missing)
5842   "Go through the list of MISSING articles and remove them from the mark lists."
5843   (when missing
5844     (let (var m)
5845       ;; Go through all types.
5846       (dolist (elem gnus-article-mark-lists)
5847         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5848           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5849           (when (symbol-value var)
5850             ;; This list has articles.  So we delete all missing
5851             ;; articles from it.
5852             (setq m missing)
5853             (while m
5854               (set var (delq (pop m) (symbol-value var))))))))))
5855
5856 (defun gnus-update-marks ()
5857   "Enter the various lists of marked articles into the newsgroup info list."
5858   (let ((types gnus-article-mark-lists)
5859         (info (gnus-get-info gnus-newsgroup-name))
5860         type list newmarked symbol delta-marks)
5861     (when info
5862       ;; Add all marks lists to the list of marks lists.
5863       (while (setq type (pop types))
5864         (setq list (symbol-value
5865                     (setq symbol
5866                           (intern (format "gnus-newsgroup-%s" (car type))))))
5867
5868         (when list
5869           ;; Get rid of the entries of the articles that have the
5870           ;; default score.
5871           (when (and (eq (cdr type) 'score)
5872                      gnus-save-score
5873                      list)
5874             (let* ((arts list)
5875                    (prev (cons nil list))
5876                    (all prev))
5877               (while arts
5878                 (if (or (not (consp (car arts)))
5879                         (= (cdar arts) gnus-summary-default-score))
5880                     (setcdr prev (cdr arts))
5881                   (setq prev arts))
5882                 (setq arts (cdr arts)))
5883               (setq list (cdr all)))))
5884
5885         (when (eq (cdr type) 'seen)
5886           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5887
5888         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5889           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5890
5891         (when (and (gnus-check-backend-function
5892                     'request-set-mark gnus-newsgroup-name)
5893                    (not (gnus-article-unpropagatable-p (cdr type))))
5894           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5895                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5896                  (add (gnus-remove-from-range
5897                        (gnus-copy-sequence list) old)))
5898             (when add
5899               (push (list add 'add (list (cdr type))) delta-marks))
5900             (when del
5901               (push (list del 'del (list (cdr type))) delta-marks))))
5902
5903         (when list
5904           (push (cons (cdr type) list) newmarked)))
5905
5906       (when delta-marks
5907         (unless (gnus-check-group gnus-newsgroup-name)
5908           (error "Can't open server for %s" gnus-newsgroup-name))
5909         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5910
5911       ;; Enter these new marks into the info of the group.
5912       (if (nthcdr 3 info)
5913           (setcar (nthcdr 3 info) newmarked)
5914         ;; Add the marks lists to the end of the info.
5915         (when newmarked
5916           (setcdr (nthcdr 2 info) (list newmarked))))
5917
5918       ;; Cut off the end of the info if there's nothing else there.
5919       (let ((i 5))
5920         (while (and (> i 2)
5921                     (not (nth i info)))
5922           (when (nthcdr (decf i) info)
5923             (setcdr (nthcdr i info) nil)))))))
5924
5925 (defun gnus-set-mode-line (where)
5926   "Set the mode line of the article or summary buffers.
5927 If WHERE is `summary', the summary mode line format will be used."
5928   ;; Is this mode line one we keep updated?
5929   (when (and (memq where gnus-updated-mode-lines)
5930              (symbol-value
5931               (intern (format "gnus-%s-mode-line-format-spec" where))))
5932     (let (mode-string)
5933       (save-excursion
5934         ;; We evaluate this in the summary buffer since these
5935         ;; variables are buffer-local to that buffer.
5936         (set-buffer gnus-summary-buffer)
5937        ;; We bind all these variables that are used in the `eval' form
5938         ;; below.
5939         (let* ((mformat (symbol-value
5940                          (intern
5941                           (format "gnus-%s-mode-line-format-spec" where))))
5942                (gnus-tmp-group-name (gnus-mode-string-quote
5943                                      (gnus-group-decoded-name
5944                                       gnus-newsgroup-name)))
5945                (gnus-tmp-article-number (or gnus-current-article 0))
5946                (gnus-tmp-unread gnus-newsgroup-unreads)
5947                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5948                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5949                (gnus-tmp-unread-and-unselected
5950                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5951                             (zerop gnus-tmp-unselected))
5952                        "")
5953                       ((zerop gnus-tmp-unselected)
5954                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5955                       (t (format "{%d(+%d) more}"
5956                                  gnus-tmp-unread-and-unticked
5957                                  gnus-tmp-unselected))))
5958                (gnus-tmp-subject
5959                 (if (and gnus-current-headers
5960                          (vectorp gnus-current-headers))
5961                     (gnus-mode-string-quote
5962                      (mail-header-subject gnus-current-headers))
5963                   ""))
5964                bufname-length max-len
5965                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5966           (setq mode-string (eval mformat))
5967           (setq bufname-length (if (string-match "%b" mode-string)
5968                                    (- (length
5969                                        (buffer-name
5970                                         (if (eq where 'summary)
5971                                             nil
5972                                           (get-buffer gnus-article-buffer))))
5973                                       2)
5974                                  0))
5975           (setq max-len (max 4 (if gnus-mode-non-string-length
5976                                    (- (window-width)
5977                                       gnus-mode-non-string-length
5978                                       bufname-length)
5979                                  (length mode-string))))
5980           ;; We might have to chop a bit of the string off...
5981           (when (> (length mode-string) max-len)
5982             (setq mode-string
5983                   (concat (truncate-string-to-width mode-string (- max-len 3))
5984                           "...")))
5985           ;; Pad the mode string a bit.
5986           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5987       ;; Update the mode line.
5988       (setq mode-line-buffer-identification
5989             (gnus-mode-line-buffer-identification (list mode-string)))
5990       (set-buffer-modified-p t))))
5991
5992 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5993   "Go through the HEADERS list and add all Xrefs to a hash table.
5994 The resulting hash table is returned, or nil if no Xrefs were found."
5995   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5996          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5997          (xref-hashtb (gnus-make-hashtable))
5998          start group entry number xrefs header)
5999     (while headers
6000       (setq header (pop headers))
6001       (when (and (setq xrefs (mail-header-xref header))
6002                  (not (memq (setq number (mail-header-number header))
6003                             unreads)))
6004         (setq start 0)
6005         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
6006           (setq start (match-end 0))
6007           (setq group (if prefix
6008                           (concat prefix (substring xrefs (match-beginning 1)
6009                                                     (match-end 1)))
6010                         (substring xrefs (match-beginning 1) (match-end 1))))
6011           (setq number
6012                 (string-to-number (substring xrefs (match-beginning 2)
6013                                           (match-end 2))))
6014           (if (setq entry (gnus-gethash group xref-hashtb))
6015               (setcdr entry (cons number (cdr entry)))
6016             (gnus-sethash group (cons number nil) xref-hashtb)))))
6017     (and start xref-hashtb)))
6018
6019 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
6020   "Look through all the headers and mark the Xrefs as read."
6021   (let ((virtual (gnus-virtual-group-p from-newsgroup))
6022         name info xref-hashtb idlist method nth4)
6023     (save-excursion
6024       (set-buffer gnus-group-buffer)
6025       (when (setq xref-hashtb
6026                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
6027         (mapatoms
6028          (lambda (group)
6029            (unless (string= from-newsgroup (setq name (symbol-name group)))
6030              (setq idlist (symbol-value group))
6031              ;; Dead groups are not updated.
6032              (and (prog1
6033                       (setq info (gnus-get-info name))
6034                     (when (stringp (setq nth4 (gnus-info-method info)))
6035                       (setq nth4 (gnus-server-to-method nth4))))
6036                   ;; Only do the xrefs if the group has the same
6037                   ;; select method as the group we have just read.
6038                   (or (gnus-methods-equal-p
6039                        nth4 (gnus-find-method-for-group from-newsgroup))
6040                       virtual
6041                       (equal nth4 (setq method (gnus-find-method-for-group
6042                                                 from-newsgroup)))
6043                       (and (equal (car nth4) (car method))
6044                            (equal (nth 1 nth4) (nth 1 method))))
6045                   gnus-use-cross-reference
6046                   (or (not (eq gnus-use-cross-reference t))
6047                       virtual
6048                       ;; Only do cross-references on subscribed
6049                       ;; groups, if that is what is wanted.
6050                       (<= (gnus-info-level info) gnus-level-subscribed))
6051                   (gnus-group-make-articles-read name idlist))))
6052          xref-hashtb)))))
6053
6054 (defun gnus-compute-read-articles (group articles)
6055   (let* ((entry (gnus-group-entry group))
6056          (info (nth 2 entry))
6057          (active (gnus-active group))
6058          ninfo)
6059     (when entry
6060       ;; First peel off all invalid article numbers.
6061       (when active
6062         (let ((ids articles)
6063               id first)
6064           (while (setq id (pop ids))
6065             (when (and first (> id (cdr active)))
6066               ;; We'll end up in this situation in one particular
6067               ;; obscure situation.  If you re-scan a group and get
6068               ;; a new article that is cross-posted to a different
6069               ;; group that has not been re-scanned, you might get
6070               ;; crossposted article that has a higher number than
6071               ;; Gnus believes possible.  So we re-activate this
6072               ;; group as well.  This might mean doing the
6073               ;; crossposting thingy will *increase* the number
6074               ;; of articles in some groups.  Tsk, tsk.
6075               (setq active (or (gnus-activate-group group) active)))
6076             (when (or (> id (cdr active))
6077                       (< id (car active)))
6078               (setq articles (delq id articles))))))
6079       ;; If the read list is nil, we init it.
6080       (if (and active
6081                (null (gnus-info-read info))
6082                (> (car active) 1))
6083           (setq ninfo (cons 1 (1- (car active))))
6084         (setq ninfo (gnus-info-read info)))
6085       ;; Then we add the read articles to the range.
6086       (gnus-add-to-range
6087        ninfo (setq articles (sort articles '<))))))
6088
6089 (defun gnus-group-make-articles-read (group articles)
6090   "Update the info of GROUP to say that ARTICLES are read."
6091   (let* ((num 0)
6092          (entry (gnus-group-entry group))
6093          (info (nth 2 entry))
6094          (active (gnus-active group))
6095          range)
6096     (when entry
6097       (setq range (gnus-compute-read-articles group articles))
6098       (with-current-buffer gnus-group-buffer
6099         (gnus-undo-register
6100           `(progn
6101              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
6102              (gnus-info-set-read ',info ',(gnus-info-read info))
6103              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
6104              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
6105              (gnus-group-update-group ,group t))))
6106       ;; Add the read articles to the range.
6107       (gnus-info-set-read info range)
6108       (gnus-request-set-mark group (list (list range 'add '(read))))
6109       ;; Then we have to re-compute how many unread
6110       ;; articles there are in this group.
6111       (when active
6112         (cond
6113          ((not range)
6114           (setq num (- (1+ (cdr active)) (car active))))
6115          ((not (listp (cdr range)))
6116           (setq num (- (cdr active) (- (1+ (cdr range))
6117                                        (car range)))))
6118          (t
6119           (while range
6120             (if (numberp (car range))
6121                 (setq num (1+ num))
6122               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
6123             (setq range (cdr range)))
6124           (setq num (- (cdr active) num))))
6125         ;; Update the number of unread articles.
6126         (setcar entry num)
6127         ;; Update the group buffer.
6128         (unless (gnus-ephemeral-group-p group)
6129           (gnus-group-update-group group t))))))
6130
6131 (defvar gnus-newsgroup-none-id 0)
6132
6133 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
6134   (let ((cur nntp-server-buffer)
6135         (dependencies
6136          (or dependencies
6137              (with-current-buffer gnus-summary-buffer
6138                gnus-newsgroup-dependencies)))
6139         headers id end ref number
6140         (mail-parse-charset gnus-newsgroup-charset)
6141         (mail-parse-ignored-charsets
6142          (save-excursion (condition-case nil
6143                              (set-buffer gnus-summary-buffer)
6144                            (error))
6145                          gnus-newsgroup-ignored-charsets)))
6146     (save-excursion
6147       (set-buffer nntp-server-buffer)
6148       ;; Translate all TAB characters into SPACE characters.
6149       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
6150       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6151       (ietf-drums-unfold-fws)
6152       (gnus-run-hooks 'gnus-parse-headers-hook)
6153       (let ((case-fold-search t)
6154             in-reply-to header p lines chars)
6155         (goto-char (point-min))
6156         ;; Search to the beginning of the next header.  Error messages
6157         ;; do not begin with 2 or 3.
6158         (while (re-search-forward "^[23][0-9]+ " nil t)
6159           (setq id nil
6160                 ref nil)
6161           ;; This implementation of this function, with nine
6162           ;; search-forwards instead of the one re-search-forward and
6163           ;; a case (which basically was the old function) is actually
6164           ;; about twice as fast, even though it looks messier.  You
6165           ;; can't have everything, I guess.  Speed and elegance
6166           ;; doesn't always go hand in hand.
6167           (setq
6168            header
6169            (vector
6170             ;; Number.
6171             (prog1
6172                 (setq number (read cur))
6173               (end-of-line)
6174               (setq p (point))
6175               (narrow-to-region (point)
6176                                 (or (and (search-forward "\n.\n" nil t)
6177                                          (- (point) 2))
6178                                     (point))))
6179             ;; Subject.
6180             (progn
6181               (goto-char p)
6182               (if (search-forward "\nsubject:" nil t)
6183                   (funcall gnus-decode-encoded-word-function
6184                            (nnheader-header-value))
6185                 "(none)"))
6186             ;; From.
6187             (progn
6188               (goto-char p)
6189               (if (search-forward "\nfrom:" nil t)
6190                   (funcall gnus-decode-encoded-address-function
6191                            (nnheader-header-value))
6192                 "(nobody)"))
6193             ;; Date.
6194             (progn
6195               (goto-char p)
6196               (if (search-forward "\ndate:" nil t)
6197                   (nnheader-header-value) ""))
6198             ;; Message-ID.
6199             (progn
6200               (goto-char p)
6201               (setq id (if (re-search-forward
6202                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
6203                            ;; We do it this way to make sure the Message-ID
6204                            ;; is (somewhat) syntactically valid.
6205                            (buffer-substring (match-beginning 1)
6206                                              (match-end 1))
6207                          ;; If there was no message-id, we just fake one
6208                          ;; to make subsequent routines simpler.
6209                          (nnheader-generate-fake-message-id number))))
6210             ;; References.
6211             (progn
6212               (goto-char p)
6213               (if (search-forward "\nreferences:" nil t)
6214                   (progn
6215                     (setq end (point))
6216                     (prog1
6217                         (nnheader-header-value)
6218                       (setq ref
6219                             (buffer-substring
6220                              (progn
6221                                (end-of-line)
6222                                (search-backward ">" end t)
6223                                (1+ (point)))
6224                              (progn
6225                                (search-backward "<" end t)
6226                                (point))))))
6227                 ;; Get the references from the in-reply-to header if there
6228                 ;; were no references and the in-reply-to header looks
6229                 ;; promising.
6230                 (if (and (search-forward "\nin-reply-to:" nil t)
6231                          (setq in-reply-to (nnheader-header-value))
6232                          (string-match "<[^>]+>" in-reply-to))
6233                     (let (ref2)
6234                       (setq ref (substring in-reply-to (match-beginning 0)
6235                                            (match-end 0)))
6236                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
6237                         (setq ref2 (substring in-reply-to (match-beginning 0)
6238                                               (match-end 0)))
6239                         (when (> (length ref2) (length ref))
6240                           (setq ref ref2)))
6241                       ref)
6242                   (setq ref nil))))
6243             ;; Chars.
6244             (progn
6245               (goto-char p)
6246               (if (search-forward "\nchars: " nil t)
6247                   (if (numberp (setq chars (ignore-errors (read cur))))
6248                       chars -1)
6249                 -1))
6250             ;; Lines.
6251             (progn
6252               (goto-char p)
6253               (if (search-forward "\nlines: " nil t)
6254                   (if (numberp (setq lines (ignore-errors (read cur))))
6255                       lines -1)
6256                 -1))
6257             ;; Xref.
6258             (progn
6259               (goto-char p)
6260               (and (search-forward "\nxref:" nil t)
6261                    (nnheader-header-value)))
6262             ;; Extra.
6263             (when gnus-extra-headers
6264               (let ((extra gnus-extra-headers)
6265                     out)
6266                 (while extra
6267                   (goto-char p)
6268                   (when (search-forward
6269                          (concat "\n" (symbol-name (car extra)) ":") nil t)
6270                     (push (cons (car extra) (nnheader-header-value))
6271                           out))
6272                   (pop extra))
6273                 out))))
6274           (when (equal id ref)
6275             (setq ref nil))
6276
6277           (when gnus-alter-header-function
6278             (funcall gnus-alter-header-function header)
6279             (setq id (mail-header-id header)
6280                   ref (gnus-parent-id (mail-header-references header))))
6281
6282           (when (setq header
6283                       (gnus-dependencies-add-header
6284                        header dependencies force-new))
6285             (push header headers))
6286           (goto-char (point-max))
6287           (widen))
6288         (nreverse headers)))))
6289
6290 ;; Goes through the xover lines and returns a list of vectors
6291 (defun gnus-get-newsgroup-headers-xover (sequence &optional
6292                                                   force-new dependencies
6293                                                   group also-fetch-heads)
6294   "Parse the news overview data in the server buffer.
6295 Return a list of headers that match SEQUENCE (see
6296 `nntp-retrieve-headers')."
6297   ;; Get the Xref when the users reads the articles since most/some
6298   ;; NNTP servers do not include Xrefs when using XOVER.
6299   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
6300   (let ((mail-parse-charset gnus-newsgroup-charset)
6301         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
6302         (cur nntp-server-buffer)
6303         (dependencies (or dependencies gnus-newsgroup-dependencies))
6304         (allp (cond
6305                ((eq gnus-read-all-available-headers t)
6306                 t)
6307                ((and (stringp gnus-read-all-available-headers)
6308                      group)
6309                 (string-match gnus-read-all-available-headers group))
6310                (t
6311                 nil)))
6312         number headers header)
6313     (save-excursion
6314       (set-buffer nntp-server-buffer)
6315       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
6316       ;; Allow the user to mangle the headers before parsing them.
6317       (gnus-run-hooks 'gnus-parse-headers-hook)
6318       (goto-char (point-min))
6319       (gnus-parse-without-error
6320         (while (and (or sequence allp)
6321                     (not (eobp)))
6322           (setq number (read cur))
6323           (when (not allp)
6324             (while (and sequence
6325                         (< (car sequence) number))
6326               (setq sequence (cdr sequence))))
6327           (when (and (or allp
6328                          (and sequence
6329                               (eq number (car sequence))))
6330                      (progn
6331                        (setq sequence (cdr sequence))
6332                        (setq header (inline
6333                                       (gnus-nov-parse-line
6334                                        number dependencies force-new)))))
6335             (push header headers))
6336           (forward-line 1)))
6337       ;; A common bug in inn is that if you have posted an article and
6338       ;; then retrieves the active file, it will answer correctly --
6339       ;; the new article is included.  However, a NOV entry for the
6340       ;; article may not have been generated yet, so this may fail.
6341       ;; We work around this problem by retrieving the last few
6342       ;; headers using HEAD.
6343       (if (or (not also-fetch-heads)
6344               (not sequence))
6345           ;; We (probably) got all the headers.
6346           (nreverse headers)
6347         (let ((gnus-nov-is-evil t))
6348           (nconc
6349            (nreverse headers)
6350            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6351              (gnus-get-newsgroup-headers))))))))
6352
6353 (defun gnus-article-get-xrefs ()
6354   "Fill in the Xref value in `gnus-current-headers', if necessary.
6355 This is meant to be called in `gnus-article-internal-prepare-hook'."
6356   (let ((headers (with-current-buffer gnus-summary-buffer
6357                    gnus-current-headers)))
6358     (or (not gnus-use-cross-reference)
6359         (not headers)
6360         (and (mail-header-xref headers)
6361              (not (string= (mail-header-xref headers) "")))
6362         (let ((case-fold-search t)
6363               xref)
6364           (save-restriction
6365             (nnheader-narrow-to-headers)
6366             (goto-char (point-min))
6367             (when (or (and (not (eobp))
6368                            (eq (downcase (char-after)) ?x)
6369                            (looking-at "Xref:"))
6370                       (search-forward "\nXref:" nil t))
6371               (goto-char (1+ (match-end 0)))
6372               (setq xref (buffer-substring (point) (point-at-eol)))
6373               (mail-header-set-xref headers xref)))))))
6374
6375 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6376   "Find article ID and insert the summary line for that article.
6377 OLD-HEADER can either be a header or a line number to insert
6378 the subject line on."
6379   (let* ((line (and (numberp old-header) old-header))
6380          (old-header (and (vectorp old-header) old-header))
6381          (header (cond ((and old-header use-old-header)
6382                         old-header)
6383                        ((and (numberp id)
6384                              (gnus-number-to-header id))
6385                         (gnus-number-to-header id))
6386                        (t
6387                         (gnus-read-header id))))
6388          (number (and (numberp id) id))
6389          d)
6390     (when header
6391       ;; Rebuild the thread that this article is part of and go to the
6392       ;; article we have fetched.
6393       (when (and (not gnus-show-threads)
6394                  old-header)
6395         (when (and number
6396                    (setq d (gnus-data-find (mail-header-number old-header))))
6397           (goto-char (gnus-data-pos d))
6398           (gnus-data-remove
6399            number
6400            (- (point-at-bol)
6401               (prog1
6402                   (1+ (point-at-eol))
6403                 (gnus-delete-line))))))
6404       ;; Remove list identifiers from subject.
6405       (when gnus-list-identifiers
6406         (let ((gnus-newsgroup-headers (list header)))
6407           (gnus-summary-remove-list-identifiers)))
6408       (when old-header
6409         (mail-header-set-number header (mail-header-number old-header)))
6410       (setq gnus-newsgroup-sparse
6411             (delq (setq number (mail-header-number header))
6412                   gnus-newsgroup-sparse))
6413       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6414       (push number gnus-newsgroup-limit)
6415       (gnus-rebuild-thread (mail-header-id header) line)
6416       (gnus-summary-goto-subject number nil t))
6417     (when (and (numberp number)
6418                (> number 0))
6419       ;; We have to update the boundaries even if we can't fetch the
6420       ;; article if ID is a number -- so that the next `P' or `N'
6421       ;; command will fetch the previous (or next) article even
6422       ;; if the one we tried to fetch this time has been canceled.
6423       (when (> number gnus-newsgroup-end)
6424         (setq gnus-newsgroup-end number))
6425       (when (< number gnus-newsgroup-begin)
6426         (setq gnus-newsgroup-begin number))
6427       (setq gnus-newsgroup-unselected
6428             (delq number gnus-newsgroup-unselected)))
6429     ;; Report back a success?
6430     (and header (mail-header-number header))))
6431
6432 ;;; Process/prefix in the summary buffer
6433
6434 (defun gnus-summary-work-articles (n)
6435   "Return a list of articles to be worked upon.
6436 The prefix argument, the list of process marked articles, and the
6437 current article will be taken into consideration."
6438   (save-excursion
6439     (set-buffer gnus-summary-buffer)
6440     (cond
6441      (n
6442       ;; A numerical prefix has been given.
6443       (setq n (prefix-numeric-value n))
6444       (let ((backward (< n 0))
6445             (n (abs (prefix-numeric-value n)))
6446             articles article)
6447         (save-excursion
6448           (while
6449               (and (> n 0)
6450                    (push (setq article (gnus-summary-article-number))
6451                          articles)
6452                    (if backward
6453                        (gnus-summary-find-prev nil article)
6454                      (gnus-summary-find-next nil article)))
6455             (decf n)))
6456         (nreverse articles)))
6457      ((and (gnus-region-active-p) (mark))
6458       (message "region active")
6459       ;; Work on the region between point and mark.
6460       (let ((max (max (point) (mark)))
6461             articles article)
6462         (save-excursion
6463           (goto-char (min (point) (mark)))
6464           (while
6465               (and
6466                (push (setq article (gnus-summary-article-number)) articles)
6467                (gnus-summary-find-next nil article)
6468                (< (point) max)))
6469           (nreverse articles))))
6470      (gnus-newsgroup-processable
6471       ;; There are process-marked articles present.
6472       ;; Save current state.
6473       (gnus-summary-save-process-mark)
6474       ;; Return the list.
6475       (reverse gnus-newsgroup-processable))
6476      (t
6477       ;; Just return the current article.
6478       (list (gnus-summary-article-number))))))
6479
6480 (defmacro gnus-summary-iterate (arg &rest forms)
6481   "Iterate over the process/prefixed articles and do FORMS.
6482 ARG is the interactive prefix given to the command.  FORMS will be
6483 executed with point over the summary line of the articles."
6484   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6485     `(let ((,articles (gnus-summary-work-articles ,arg)))
6486        (while ,articles
6487          (gnus-summary-goto-subject (car ,articles))
6488          ,@forms
6489          (pop ,articles)))))
6490
6491 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6492 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6493
6494 (defun gnus-summary-save-process-mark ()
6495   "Push the current set of process marked articles on the stack."
6496   (interactive)
6497   (push (copy-sequence gnus-newsgroup-processable)
6498         gnus-newsgroup-process-stack))
6499
6500 (defun gnus-summary-kill-process-mark ()
6501   "Push the current set of process marked articles on the stack and unmark."
6502   (interactive)
6503   (gnus-summary-save-process-mark)
6504   (gnus-summary-unmark-all-processable))
6505
6506 (defun gnus-summary-yank-process-mark ()
6507   "Pop the last process mark state off the stack and restore it."
6508   (interactive)
6509   (unless gnus-newsgroup-process-stack
6510     (error "Empty mark stack"))
6511   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6512
6513 (defun gnus-summary-process-mark-set (set)
6514   "Make SET into the current process marked articles."
6515   (gnus-summary-unmark-all-processable)
6516   (mapc 'gnus-summary-set-process-mark set))
6517
6518 ;;; Searching and stuff
6519
6520 (defun gnus-summary-search-group (&optional backward use-level)
6521   "Search for next unread newsgroup.
6522 If optional argument BACKWARD is non-nil, search backward instead."
6523   (save-excursion
6524     (set-buffer gnus-group-buffer)
6525     (when (gnus-group-search-forward
6526            backward nil (if use-level (gnus-group-group-level) nil))
6527       (gnus-group-group-name))))
6528
6529 (defun gnus-summary-best-group (&optional exclude-group)
6530   "Find the name of the best unread group.
6531 If EXCLUDE-GROUP, do not go to this group."
6532   (with-current-buffer gnus-group-buffer
6533     (save-excursion
6534       (gnus-group-best-unread-group exclude-group))))
6535
6536 (defun gnus-summary-find-next (&optional unread article backward)
6537   (if backward
6538       (gnus-summary-find-prev unread article)
6539     (let* ((dummy (gnus-summary-article-intangible-p))
6540            (article (or article (gnus-summary-article-number)))
6541            (data (gnus-data-find-list article))
6542            result)
6543       (when (and (not dummy)
6544                  (or (not gnus-summary-check-current)
6545                      (not unread)
6546                      (not (gnus-data-unread-p (car data)))))
6547         (setq data (cdr data)))
6548       (when (setq result
6549                   (if unread
6550                       (progn
6551                         (while data
6552                           (unless (memq (gnus-data-number (car data))
6553                                         (cond
6554                                          ((eq gnus-auto-goto-ignores
6555                                               'always-undownloaded)
6556                                           gnus-newsgroup-undownloaded)
6557                                          (gnus-plugged
6558                                           nil)
6559                                          ((eq gnus-auto-goto-ignores
6560                                               'unfetched)
6561                                           gnus-newsgroup-unfetched)
6562                                          ((eq gnus-auto-goto-ignores
6563                                               'undownloaded)
6564                                           gnus-newsgroup-undownloaded)))
6565                             (when (gnus-data-unread-p (car data))
6566                               (setq result (car data)
6567                                     data nil)))
6568                           (setq data (cdr data)))
6569                         result)
6570                     (car data)))
6571         (goto-char (gnus-data-pos result))
6572         (gnus-data-number result)))))
6573
6574 (defun gnus-summary-find-prev (&optional unread article)
6575   (let* ((eobp (eobp))
6576          (article (or article (gnus-summary-article-number)))
6577          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6578          result)
6579     (when (and (not eobp)
6580                (or (not gnus-summary-check-current)
6581                    (not unread)
6582                    (not (gnus-data-unread-p (car data)))))
6583       (setq data (cdr data)))
6584     (when (setq result
6585                 (if unread
6586                     (progn
6587                       (while data
6588                         (unless (memq (gnus-data-number (car data))
6589                                       (cond
6590                                        ((eq gnus-auto-goto-ignores
6591                                             'always-undownloaded)
6592                                         gnus-newsgroup-undownloaded)
6593                                        (gnus-plugged
6594                                         nil)
6595                                        ((eq gnus-auto-goto-ignores
6596                                             'unfetched)
6597                                         gnus-newsgroup-unfetched)
6598                                        ((eq gnus-auto-goto-ignores
6599                                             'undownloaded)
6600                                         gnus-newsgroup-undownloaded)))
6601                           (when (gnus-data-unread-p (car data))
6602                             (setq result (car data)
6603                                   data nil)))
6604                         (setq data (cdr data)))
6605                       result)
6606                   (car data)))
6607       (goto-char (gnus-data-pos result))
6608       (gnus-data-number result))))
6609
6610 (defun gnus-summary-find-subject (subject &optional unread backward article)
6611   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6612          (article (or article (gnus-summary-article-number)))
6613          (articles (gnus-data-list backward))
6614          (arts (gnus-data-find-list article articles))
6615          result)
6616     (when (or (not gnus-summary-check-current)
6617               (not unread)
6618               (not (gnus-data-unread-p (car arts))))
6619       (setq arts (cdr arts)))
6620     (while arts
6621       (and (or (not unread)
6622                (gnus-data-unread-p (car arts)))
6623            (vectorp (gnus-data-header (car arts)))
6624            (gnus-subject-equal
6625             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6626            (setq result (car arts)
6627                  arts nil))
6628       (setq arts (cdr arts)))
6629     (and result
6630          (goto-char (gnus-data-pos result))
6631          (gnus-data-number result))))
6632
6633 (defun gnus-summary-search-forward (&optional unread subject backward)
6634   "Search forward for an article.
6635 If UNREAD, look for unread articles.  If SUBJECT, look for
6636 articles with that subject.  If BACKWARD, search backward instead."
6637   (cond (subject (gnus-summary-find-subject subject unread backward))
6638         (backward (gnus-summary-find-prev unread))
6639         (t (gnus-summary-find-next unread))))
6640
6641 (defun gnus-recenter (&optional n)
6642   "Center point in window and redisplay frame.
6643 Also do horizontal recentering."
6644   (interactive "P")
6645   (when (and gnus-auto-center-summary
6646              (not (eq gnus-auto-center-summary 'vertical)))
6647     (gnus-horizontal-recenter))
6648   (recenter n))
6649
6650 (defun gnus-summary-recenter ()
6651   "Center point in the summary window.
6652 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6653 displayed, no centering will be performed."
6654   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6655   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6656   (interactive)
6657   ;; The user has to want it.
6658   (when gnus-auto-center-summary
6659     (let* ((top (cond ((< (window-height) 4) 0)
6660                       ((< (window-height) 7) 1)
6661                       (t (if (numberp gnus-auto-center-summary)
6662                              gnus-auto-center-summary
6663                            (/ (1- (window-height)) 2)))))
6664            (height (1- (window-height)))
6665            (bottom (save-excursion (goto-char (point-max))
6666                                    (forward-line (- height))
6667                                    (point)))
6668            (window (get-buffer-window (current-buffer))))
6669       (when (get-buffer-window gnus-article-buffer)
6670         ;; Only do recentering when the article buffer is displayed,
6671         ;; Set the window start to either `bottom', which is the biggest
6672         ;; possible valid number, or the second line from the top,
6673         ;; whichever is the least.
6674         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6675           (if (> bottom top-pos)
6676               ;; Keep the second line from the top visible
6677               (set-window-start window top-pos)
6678             ;; Try to keep the bottom line visible; if it's partially
6679             ;; obscured, either scroll one more line to make it fully
6680             ;; visible, or revert to using TOP-POS.
6681             (save-excursion
6682               (goto-char (point-max))
6683               (forward-line -1)
6684               (let ((last-line-start (point)))
6685                 (goto-char bottom)
6686                 (set-window-start window (point) t)
6687                 (when (not (pos-visible-in-window-p last-line-start window))
6688                   (forward-line 1)
6689                   (set-window-start window (min (point) top-pos) t)))))))
6690       ;; Do horizontal recentering while we're at it.
6691       (when (and (get-buffer-window (current-buffer) t)
6692                  (not (eq gnus-auto-center-summary 'vertical)))
6693         (let ((selected (selected-window)))
6694           (select-window (get-buffer-window (current-buffer) t))
6695           (gnus-summary-position-point)
6696           (gnus-horizontal-recenter)
6697           (select-window selected))))))
6698
6699 (defun gnus-summary-jump-to-group (newsgroup)
6700   "Move point to NEWSGROUP in group mode buffer."
6701   ;; Keep update point of group mode buffer if visible.
6702   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6703       (save-window-excursion
6704         ;; Take care of tree window mode.
6705         (when (get-buffer-window gnus-group-buffer)
6706           (pop-to-buffer gnus-group-buffer))
6707         (gnus-group-jump-to-group newsgroup))
6708     (save-excursion
6709       ;; Take care of tree window mode.
6710       (if (get-buffer-window gnus-group-buffer)
6711           (pop-to-buffer gnus-group-buffer)
6712         (set-buffer gnus-group-buffer))
6713       (gnus-group-jump-to-group newsgroup))))
6714
6715 ;; This function returns a list of article numbers based on the
6716 ;; difference between the ranges of read articles in this group and
6717 ;; the range of active articles.
6718 (defun gnus-list-of-unread-articles (group)
6719   (let* ((read (gnus-info-read (gnus-get-info group)))
6720          (active (or (gnus-active group) (gnus-activate-group group)))
6721          (last (or (cdr active)
6722                    (error "Group %s couldn't be activated " group)))
6723          (bottom (if gnus-newsgroup-maximum-articles
6724                      (max (car active)
6725                           (- last gnus-newsgroup-maximum-articles -1))
6726                    (car active)))
6727          first nlast unread)
6728     ;; If none are read, then all are unread.
6729     (if (not read)
6730         (setq first bottom)
6731       ;; If the range of read articles is a single range, then the
6732       ;; first unread article is the article after the last read
6733       ;; article.  Sounds logical, doesn't it?
6734       (if (and (not (listp (cdr read)))
6735                (or (< (car read) bottom)
6736                    (progn (setq read (list read))
6737                           nil)))
6738           (setq first (max bottom (1+ (cdr read))))
6739         ;; `read' is a list of ranges.
6740         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6741                                   (caar read)))
6742                   1)
6743           (setq first bottom))
6744         (while read
6745           (when first
6746             (while (< first nlast)
6747               (setq unread (cons first unread)
6748                     first (1+ first))))
6749           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6750           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6751           (setq read (cdr read)))))
6752     ;; And add the last unread articles.
6753     (while (<= first last)
6754       (setq unread (cons first unread)
6755             first (1+ first)))
6756     ;; Return the list of unread articles.
6757     (delq 0 (nreverse unread))))
6758
6759 (defun gnus-list-of-read-articles (group)
6760   "Return a list of unread, unticked and non-dormant articles."
6761   (let* ((info (gnus-get-info group))
6762          (marked (gnus-info-marks info))
6763          (active (gnus-active group)))
6764     (and info active
6765          (gnus-list-range-difference
6766           (gnus-list-range-difference
6767            (gnus-sorted-complement
6768             (gnus-uncompress-range
6769              (if gnus-newsgroup-maximum-articles
6770                  (cons (max (car active)
6771                             (- (cdr active)
6772                                gnus-newsgroup-maximum-articles
6773                                -1))
6774                        (cdr active))
6775                active))
6776             (gnus-list-of-unread-articles group))
6777            (cdr (assq 'dormant marked)))
6778           (cdr (assq 'tick marked))))))
6779
6780 ;; This function returns a sequence of article numbers based on the
6781 ;; difference between the ranges of read articles in this group and
6782 ;; the range of active articles.
6783 (defun gnus-sequence-of-unread-articles (group)
6784   (let* ((read (gnus-info-read (gnus-get-info group)))
6785          (active (or (gnus-active group) (gnus-activate-group group)))
6786          (last (cdr active))
6787          (bottom (if gnus-newsgroup-maximum-articles
6788                      (max (car active)
6789                           (- last gnus-newsgroup-maximum-articles -1))
6790                    (car active)))
6791          first nlast unread)
6792     ;; If none are read, then all are unread.
6793     (if (not read)
6794         (setq first bottom)
6795       ;; If the range of read articles is a single range, then the
6796       ;; first unread article is the article after the last read
6797       ;; article.  Sounds logical, doesn't it?
6798       (if (and (not (listp (cdr read)))
6799                (or (< (car read) bottom)
6800                    (progn (setq read (list read))
6801                           nil)))
6802           (setq first (max bottom (1+ (cdr read))))
6803         ;; `read' is a list of ranges.
6804         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6805                                   (caar read)))
6806                   1)
6807           (setq first bottom))
6808         (while read
6809           (when first
6810             (push (cons first nlast) unread))
6811           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6812           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6813           (setq read (cdr read)))))
6814     ;; And add the last unread articles.
6815     (cond ((not (and first last))
6816            nil)
6817           ((< first last)
6818            (push (cons first last) unread))
6819           ((= first last)
6820            (push first unread)))
6821     ;; Return the sequence of unread articles.
6822     (delq 0 (nreverse unread))))
6823
6824 ;; Various summary commands
6825
6826 (defun gnus-summary-select-article-buffer ()
6827   "Reconfigure windows to show article buffer."
6828   (interactive)
6829   (if (not (gnus-buffer-live-p gnus-article-buffer))
6830       (error "There is no article buffer for this summary buffer")
6831     (gnus-configure-windows 'article)
6832     (select-window (get-buffer-window gnus-article-buffer))))
6833
6834 (defun gnus-summary-universal-argument (arg)
6835   "Perform any operation on all articles that are process/prefixed."
6836   (interactive "P")
6837   (let ((articles (gnus-summary-work-articles arg))
6838         func article)
6839     (if (eq
6840          (setq
6841           func
6842           (key-binding
6843            (read-key-sequence
6844             (substitute-command-keys
6845              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6846          'undefined)
6847         (gnus-error 1 "Undefined key")
6848       (save-excursion
6849         (while articles
6850           (gnus-summary-goto-subject (setq article (pop articles)))
6851           (let (gnus-newsgroup-processable)
6852             (command-execute func))
6853           (gnus-summary-remove-process-mark article)))))
6854   (gnus-summary-position-point))
6855
6856 (defun gnus-summary-toggle-truncation (&optional arg)
6857   "Toggle truncation of summary lines.
6858 With ARG, turn line truncation on if ARG is positive."
6859   (interactive "P")
6860   (setq truncate-lines
6861         (if (null arg) (not truncate-lines)
6862           (> (prefix-numeric-value arg) 0)))
6863   (redraw-display))
6864
6865 (defun gnus-summary-find-for-reselect ()
6866   "Return the number of an article to stay on across a reselect.
6867 The current article is considered, then following articles, then previous
6868 articles.  An article is sought which is not cancelled and isn't a temporary
6869 insertion from another group.  If there's no such then return a dummy 0."
6870   (let (found)
6871     (dolist (rev '(nil t))
6872       (unless found      ; don't demand the reverse list if we don't need it
6873         (let ((data (gnus-data-find-list
6874                      (gnus-summary-article-number) (gnus-data-list rev))))
6875           (while (and data (not found))
6876             (if (and (< 0 (gnus-data-number (car data)))
6877                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6878                 (setq found (gnus-data-number (car data))))
6879             (setq data (cdr data))))))
6880     (or found 0)))
6881
6882 (defun gnus-summary-reselect-current-group (&optional all rescan)
6883   "Exit and then reselect the current newsgroup.
6884 The prefix argument ALL means to select all articles."
6885   (interactive "P")
6886   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6887     (error "Ephemeral groups can't be reselected"))
6888   (let ((current-subject (gnus-summary-find-for-reselect))
6889         (group gnus-newsgroup-name))
6890     (setq gnus-newsgroup-begin nil)
6891     (gnus-summary-exit nil 'leave-hidden)
6892     ;; We have to adjust the point of group mode buffer because
6893     ;; point was moved to the next unread newsgroup by exiting.
6894     (gnus-summary-jump-to-group group)
6895     (when rescan
6896       (save-excursion
6897         (gnus-group-get-new-news-this-group 1)))
6898     (gnus-group-read-group all t)
6899     (gnus-summary-goto-subject current-subject nil t)))
6900
6901 (defun gnus-summary-rescan-group (&optional all)
6902   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6903   (interactive "P")
6904   (gnus-summary-reselect-current-group all t))
6905
6906 (defun gnus-summary-update-info (&optional non-destructive)
6907   (save-excursion
6908     (let ((group gnus-newsgroup-name))
6909       (when group
6910         (when gnus-newsgroup-kill-headers
6911           (setq gnus-newsgroup-killed
6912                 (gnus-compress-sequence
6913                  (gnus-sorted-union
6914                   (gnus-list-range-intersection
6915                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6916                   gnus-newsgroup-unreads)
6917                  t)))
6918         (unless (listp (cdr gnus-newsgroup-killed))
6919           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6920         (let ((headers gnus-newsgroup-headers))
6921           ;; Set the new ranges of read articles.
6922           (with-current-buffer gnus-group-buffer
6923             (gnus-undo-force-boundary))
6924           (gnus-update-read-articles
6925            group (gnus-sorted-union
6926                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6927           ;; Set the current article marks.
6928           (let ((gnus-newsgroup-scored
6929                  (if (and (not gnus-save-score)
6930                           (not non-destructive))
6931                      nil
6932                    gnus-newsgroup-scored)))
6933             (save-excursion
6934               (gnus-update-marks)))
6935           ;; Do the cross-ref thing.
6936           (when gnus-use-cross-reference
6937             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6938           ;; Do not switch windows but change the buffer to work.
6939           (set-buffer gnus-group-buffer)
6940           (unless (gnus-ephemeral-group-p group)
6941             (gnus-group-update-group group)))))))
6942
6943 (defun gnus-summary-save-newsrc (&optional force)
6944   "Save the current number of read/marked articles in the dribble buffer.
6945 The dribble buffer will then be saved.
6946 If FORCE (the prefix), also save the .newsrc file(s)."
6947   (interactive "P")
6948   (gnus-summary-update-info t)
6949   (if force
6950       (gnus-save-newsrc-file)
6951     (gnus-dribble-save)))
6952
6953 (defun gnus-summary-exit (&optional temporary leave-hidden)
6954   "Exit reading current newsgroup, and then return to group selection mode.
6955 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6956   (interactive)
6957   (gnus-set-global-variables)
6958   (when (gnus-buffer-live-p gnus-article-buffer)
6959     (save-excursion
6960       (set-buffer gnus-article-buffer)
6961       (mm-destroy-parts gnus-article-mime-handles)
6962       ;; Set it to nil for safety reason.
6963       (setq gnus-article-mime-handle-alist nil)
6964       (setq gnus-article-mime-handles nil)))
6965   (gnus-kill-save-kill-buffer)
6966   (gnus-async-halt-prefetch)
6967   (let* ((group gnus-newsgroup-name)
6968          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6969          (gnus-group-is-exiting-p t)
6970          (mode major-mode)
6971          (group-point nil)
6972          (buf (current-buffer)))
6973     (unless quit-config
6974       ;; Do adaptive scoring, and possibly save score files.
6975       (when gnus-newsgroup-adaptive
6976         (gnus-score-adaptive))
6977       (when gnus-use-scoring
6978         (gnus-score-save)))
6979     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6980     ;; If we have several article buffers, we kill them at exit.
6981     (unless gnus-single-article-buffer
6982       (when (gnus-buffer-live-p gnus-article-buffer)
6983         (with-current-buffer gnus-article-buffer
6984           ;; Don't kill sticky article buffers
6985           (unless (eq major-mode 'gnus-sticky-article-mode)
6986             (gnus-kill-buffer gnus-article-buffer)
6987             (setq gnus-article-current nil))))
6988       (gnus-kill-buffer gnus-original-article-buffer))
6989     (when gnus-use-cache
6990       (gnus-cache-possibly-remove-articles)
6991       (gnus-cache-save-buffers))
6992     (gnus-async-prefetch-remove-group group)
6993     (when gnus-suppress-duplicates
6994       (gnus-dup-enter-articles))
6995     (when gnus-use-trees
6996       (gnus-tree-close group))
6997     (when gnus-use-cache
6998       (gnus-cache-write-active))
6999     ;; Remove entries for this group.
7000     (nnmail-purge-split-history (gnus-group-real-name group))
7001     ;; Make all changes in this group permanent.
7002     (unless quit-config
7003       (gnus-run-hooks 'gnus-exit-group-hook)
7004       (gnus-summary-update-info))
7005     (gnus-close-group group)
7006     ;; Make sure where we were, and go to next newsgroup.
7007     (set-buffer gnus-group-buffer)
7008     (unless quit-config
7009       (gnus-group-jump-to-group group))
7010     (gnus-run-hooks 'gnus-summary-exit-hook)
7011     (unless (or quit-config
7012                 (not gnus-summary-next-group-on-exit)
7013                 ;; If this group has disappeared from the summary
7014                 ;; buffer, don't skip forwards.
7015                 (not (string= group (gnus-group-group-name))))
7016       (gnus-group-next-unread-group 1))
7017     (setq group-point (point))
7018     (if temporary
7019         nil                             ;Nothing to do.
7020       (set-buffer buf)
7021       (if (not gnus-kill-summary-on-exit)
7022           (progn
7023             (gnus-deaden-summary)
7024             (setq mode nil))
7025         ;; We set all buffer-local variables to nil.  It is unclear why
7026         ;; this is needed, but if we don't, buffer-local variables are
7027         ;; not garbage-collected, it seems.  This would the lead to en
7028         ;; ever-growing Emacs.
7029         (gnus-summary-clear-local-variables)
7030         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7031           (gnus-summary-clear-local-variables))
7032         (when (get-buffer gnus-article-buffer)
7033           (bury-buffer gnus-article-buffer))
7034         ;; Return to group mode buffer.
7035         (when (eq mode 'gnus-summary-mode)
7036           (gnus-kill-buffer buf)))
7037       (setq gnus-current-select-method gnus-select-method)
7038       (set-buffer gnus-group-buffer)
7039       (if quit-config
7040           (gnus-handle-ephemeral-exit quit-config)
7041         (goto-char group-point)
7042         ;; If gnus-group-buffer is already displayed, make sure we also move
7043         ;; the cursor in the window that displays it.
7044         (let ((win (get-buffer-window (current-buffer) 0)))
7045           (if win (set-window-point win (point))))
7046         (unless leave-hidden
7047           (gnus-configure-windows 'group 'force)))
7048       ;; Clear the current group name.
7049       (unless quit-config
7050         (setq gnus-newsgroup-name nil)))))
7051
7052 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
7053 (defun gnus-summary-exit-no-update (&optional no-questions)
7054   "Quit reading current newsgroup without updating read article info."
7055   (interactive)
7056   (let* ((group gnus-newsgroup-name)
7057          (gnus-group-is-exiting-p t)
7058          (gnus-group-is-exiting-without-update-p t)
7059          (quit-config (gnus-group-quit-config group)))
7060     (when (or no-questions
7061               gnus-expert-user
7062               (gnus-y-or-n-p "Discard changes to this group and exit? "))
7063       (gnus-async-halt-prefetch)
7064       (run-hooks 'gnus-summary-prepare-exit-hook)
7065       (when (gnus-buffer-live-p gnus-article-buffer)
7066         (save-excursion
7067           (set-buffer gnus-article-buffer)
7068           (mm-destroy-parts gnus-article-mime-handles)
7069           ;; Set it to nil for safety reason.
7070           (setq gnus-article-mime-handle-alist nil)
7071           (setq gnus-article-mime-handles nil)))
7072       ;; If we have several article buffers, we kill them at exit.
7073       (unless gnus-single-article-buffer
7074         (gnus-kill-buffer gnus-article-buffer)
7075         (gnus-kill-buffer gnus-original-article-buffer)
7076         (setq gnus-article-current nil))
7077       (if (not gnus-kill-summary-on-exit)
7078           (gnus-deaden-summary)
7079         (gnus-close-group group)
7080         (gnus-summary-clear-local-variables)
7081         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
7082           (gnus-summary-clear-local-variables))
7083         (gnus-kill-buffer gnus-summary-buffer))
7084       (unless gnus-single-article-buffer
7085         (setq gnus-article-current nil))
7086       (when gnus-use-trees
7087         (gnus-tree-close group))
7088       (gnus-async-prefetch-remove-group group)
7089       (when (get-buffer gnus-article-buffer)
7090         (bury-buffer gnus-article-buffer))
7091       ;; Return to the group buffer.
7092       (gnus-configure-windows 'group 'force)
7093       ;; Clear the current group name.
7094       (setq gnus-newsgroup-name nil)
7095       (unless (gnus-ephemeral-group-p group)
7096         (gnus-group-update-group group))
7097       (when (equal (gnus-group-group-name) group)
7098         (gnus-group-next-unread-group 1))
7099       (when quit-config
7100         (gnus-handle-ephemeral-exit quit-config)))))
7101
7102 (defun gnus-handle-ephemeral-exit (quit-config)
7103   "Handle movement when leaving an ephemeral group.
7104 The state which existed when entering the ephemeral is reset."
7105   (if (not (buffer-name (car quit-config)))
7106       (gnus-configure-windows 'group 'force)
7107     (set-buffer (car quit-config))
7108     (cond ((eq major-mode 'gnus-summary-mode)
7109            (gnus-set-global-variables))
7110           ((eq major-mode 'gnus-article-mode)
7111            (save-excursion
7112              ;; The `gnus-summary-buffer' variable may point
7113              ;; to the old summary buffer when using a single
7114              ;; article buffer.
7115              (unless (gnus-buffer-live-p gnus-summary-buffer)
7116                (set-buffer gnus-group-buffer))
7117              (set-buffer gnus-summary-buffer)
7118              (gnus-set-global-variables))))
7119     (if (or (eq (cdr quit-config) 'article)
7120             (eq (cdr quit-config) 'pick))
7121         (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
7122             (gnus-configure-windows 'pick 'force)
7123           (gnus-configure-windows (cdr quit-config) 'force))
7124       (gnus-configure-windows (cdr quit-config) 'force))
7125     (when (eq major-mode 'gnus-summary-mode)
7126       (if (memq gnus-auto-select-on-ephemeral-exit '(next-noselect
7127                                                      next-unread-noselect))
7128           (when (zerop (cond ((eq gnus-auto-select-on-ephemeral-exit
7129                                   'next-noselect)
7130                               (gnus-summary-next-subject 1 nil t))
7131                              ((eq gnus-auto-select-on-ephemeral-exit
7132                                   'next-unread-noselect)
7133                               (gnus-summary-next-subject 1 t t))))
7134             ;; Hide the article buffer which displays the article different
7135             ;; from the one that the cursor points to in the summary buffer.
7136             (gnus-configure-windows 'summary 'force))
7137         (cond ((eq gnus-auto-select-on-ephemeral-exit 'next)
7138                (gnus-summary-next-subject 1))
7139               ((eq gnus-auto-select-on-ephemeral-exit 'next-unread)
7140                (gnus-summary-next-subject 1 t))))
7141       (gnus-summary-recenter)
7142       (gnus-summary-position-point))))
7143
7144 ;;; Dead summaries.
7145
7146 (defvar gnus-dead-summary-mode-map nil)
7147
7148 (unless gnus-dead-summary-mode-map
7149   (setq gnus-dead-summary-mode-map (make-keymap))
7150   (suppress-keymap gnus-dead-summary-mode-map)
7151   (substitute-key-definition
7152    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
7153   (dolist (key '("\C-d" "\r" "\177" [delete]))
7154     (define-key gnus-dead-summary-mode-map
7155       key 'gnus-summary-wake-up-the-dead))
7156   (dolist (key '("q" "Q"))
7157     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
7158
7159 (defvar gnus-dead-summary-mode nil
7160   "Minor mode for Gnus summary buffers.")
7161
7162 (defun gnus-dead-summary-mode (&optional arg)
7163   "Minor mode for Gnus summary buffers."
7164   (interactive "P")
7165   (when (eq major-mode 'gnus-summary-mode)
7166     (make-local-variable 'gnus-dead-summary-mode)
7167     (setq gnus-dead-summary-mode
7168           (if (null arg) (not gnus-dead-summary-mode)
7169             (> (prefix-numeric-value arg) 0)))
7170     (when gnus-dead-summary-mode
7171       (add-minor-mode
7172        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
7173
7174 (defun gnus-deaden-summary ()
7175   "Make the current summary buffer into a dead summary buffer."
7176   ;; Kill any previous dead summary buffer.
7177   (when (and gnus-dead-summary
7178              (buffer-name gnus-dead-summary))
7179     (with-current-buffer gnus-dead-summary
7180       (when gnus-dead-summary-mode
7181         (kill-buffer (current-buffer)))))
7182   ;; Make this the current dead summary.
7183   (setq gnus-dead-summary (current-buffer))
7184   (gnus-dead-summary-mode 1)
7185   (let ((name (buffer-name)))
7186     (when (string-match "Summary" name)
7187       (rename-buffer
7188        (concat (substring name 0 (match-beginning 0)) "Dead "
7189                (substring name (match-beginning 0)))
7190        t)
7191       (bury-buffer))))
7192
7193 (defun gnus-kill-or-deaden-summary (buffer)
7194   "Kill or deaden the summary BUFFER."
7195   (save-excursion
7196     (when (and (buffer-name buffer)
7197                (not gnus-single-article-buffer))
7198       (with-current-buffer buffer
7199         (gnus-kill-buffer gnus-article-buffer)
7200         (gnus-kill-buffer gnus-original-article-buffer)))
7201     (cond
7202      ;; Kill the buffer.
7203      (gnus-kill-summary-on-exit
7204       (when (and gnus-use-trees
7205                  (gnus-buffer-exists-p buffer))
7206         (save-excursion
7207           (set-buffer buffer)
7208           (gnus-tree-close gnus-newsgroup-name)))
7209       (gnus-kill-buffer buffer))
7210      ;; Deaden the buffer.
7211      ((gnus-buffer-exists-p buffer)
7212       (save-excursion
7213         (set-buffer buffer)
7214         (gnus-deaden-summary))))))
7215
7216 (defun gnus-summary-wake-up-the-dead (&rest args)
7217   "Wake up the dead summary buffer."
7218   (interactive)
7219   (gnus-dead-summary-mode -1)
7220   (let ((name (buffer-name)))
7221     (when (string-match "Dead " name)
7222       (rename-buffer
7223        (concat (substring name 0 (match-beginning 0))
7224                (substring name (match-end 0)))
7225        t)))
7226   (gnus-message 3 "This dead summary is now alive again"))
7227
7228 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
7229 (defun gnus-summary-fetch-faq (&optional faq-dir)
7230   "Fetch the FAQ for the current group.
7231 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
7232 in."
7233   (interactive
7234    (list
7235     (when current-prefix-arg
7236       (completing-read
7237        "FAQ dir: " (and (listp gnus-group-faq-directory)
7238                         (mapcar 'list
7239                                 gnus-group-faq-directory))))))
7240   (let (gnus-faq-buffer)
7241     (when (setq gnus-faq-buffer
7242                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
7243       (gnus-configure-windows 'summary-faq))))
7244
7245 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
7246 (defun gnus-summary-describe-group (&optional force)
7247   "Describe the current newsgroup."
7248   (interactive "P")
7249   (gnus-group-describe-group force gnus-newsgroup-name))
7250
7251 (defun gnus-summary-describe-briefly ()
7252   "Describe summary mode commands briefly."
7253   (interactive)
7254   (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")))
7255
7256 ;; Walking around group mode buffer from summary mode.
7257
7258 (defun gnus-summary-next-group (&optional no-article target-group backward)
7259   "Exit current newsgroup and then select next unread newsgroup.
7260 If prefix argument NO-ARTICLE is non-nil, no article is selected
7261 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
7262 previous group instead."
7263   (interactive "P")
7264   ;; Stop pre-fetching.
7265   (gnus-async-halt-prefetch)
7266   (let ((current-group gnus-newsgroup-name)
7267         (current-buffer (current-buffer))
7268         entered)
7269     ;; First we semi-exit this group to update Xrefs and all variables.
7270     ;; We can't do a real exit, because the window conf must remain
7271     ;; the same in case the user is prompted for info, and we don't
7272     ;; want the window conf to change before that...
7273     (gnus-summary-exit t)
7274     (while (not entered)
7275       ;; Then we find what group we are supposed to enter.
7276       (set-buffer gnus-group-buffer)
7277       (gnus-group-jump-to-group current-group)
7278       (setq target-group
7279             (or target-group
7280                 (if (eq gnus-keep-same-level 'best)
7281                     (gnus-summary-best-group gnus-newsgroup-name)
7282                   (gnus-summary-search-group backward gnus-keep-same-level))))
7283       (if (not target-group)
7284           ;; There are no further groups, so we return to the group
7285           ;; buffer.
7286           (progn
7287             (gnus-message 5 "Returning to the group buffer")
7288             (setq entered t)
7289             (when (gnus-buffer-live-p current-buffer)
7290               (set-buffer current-buffer)
7291               (gnus-summary-exit))
7292             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
7293         ;; We try to enter the target group.
7294         (gnus-group-jump-to-group target-group)
7295         (let ((unreads (gnus-group-group-unread)))
7296           (if (and (or (eq t unreads)
7297                        (and unreads (not (zerop unreads))))
7298                    (gnus-summary-read-group
7299                     target-group nil no-article
7300                     (and (buffer-name current-buffer) current-buffer)
7301                     nil backward))
7302               (setq entered t)
7303             (setq current-group target-group
7304                   target-group nil)))))))
7305
7306 (defun gnus-summary-prev-group (&optional no-article)
7307   "Exit current newsgroup and then select previous unread newsgroup.
7308 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
7309   (interactive "P")
7310   (gnus-summary-next-group no-article nil t))
7311
7312 ;; Walking around summary lines.
7313
7314 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
7315   "Go to the first subject satisfying any non-nil constraint.
7316 If UNREAD is non-nil, the article should be unread.
7317 If UNDOWNLOADED is non-nil, the article should be undownloaded.
7318 If UNSEEN is non-nil, the article should be unseen.
7319 Returns the article selected or nil if there are no matching articles."
7320   (interactive "P")
7321   (cond
7322    ;; Empty summary.
7323    ((null gnus-newsgroup-data)
7324     (gnus-message 3 "No articles in the group")
7325     nil)
7326    ;; Pick the first article.
7327    ((not (or unread undownloaded unseen))
7328     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
7329     (gnus-data-number (car gnus-newsgroup-data)))
7330    ;; Find the first unread article.
7331    (t
7332     (let ((data gnus-newsgroup-data))
7333       (while (and data
7334                   (let ((num (gnus-data-number (car data))))
7335                     (or (memq num gnus-newsgroup-unfetched)
7336                         (not (or (and unread
7337                                       (memq num gnus-newsgroup-unreads))
7338                                  (and undownloaded
7339                                       (memq num gnus-newsgroup-undownloaded))
7340                                  (and unseen
7341                                       (memq num gnus-newsgroup-unseen)))))))
7342         (setq data (cdr data)))
7343       (prog1
7344           (if data
7345               (progn
7346                 (goto-char (gnus-data-pos (car data)))
7347                 (gnus-data-number (car data)))
7348             (gnus-message 3 "No more%s articles"
7349                           (let* ((r (when unread " unread"))
7350                                  (d (when undownloaded " undownloaded"))
7351                                  (s (when unseen " unseen"))
7352                                  (l (delq nil (list r d s))))
7353                             (cond ((= 3 (length l))
7354                                    (concat r "," d ", or" s))
7355                                   ((= 2 (length l))
7356                                    (concat (car l) ", or" (cadr l)))
7357                                   ((= 1 (length l))
7358                                    (car l))
7359                                   (t
7360                                    ""))))
7361             nil
7362             )
7363         (gnus-summary-position-point))))))
7364
7365 (defun gnus-summary-next-subject (n &optional unread dont-display)
7366   "Go to next N'th summary line.
7367 If N is negative, go to the previous N'th subject line.
7368 If UNREAD is non-nil, only unread articles are selected.
7369 The difference between N and the actual number of steps taken is
7370 returned."
7371   (interactive "p")
7372   (let ((backward (< n 0))
7373         (n (abs n)))
7374     (while (and (> n 0)
7375                 (if backward
7376                     (gnus-summary-find-prev unread)
7377                   (gnus-summary-find-next unread)))
7378       (unless (zerop (setq n (1- n)))
7379         (gnus-summary-show-thread)))
7380     (when (/= 0 n)
7381       (gnus-message 7 "No more%s articles"
7382                     (if unread " unread" "")))
7383     (unless dont-display
7384       (gnus-summary-recenter)
7385       (gnus-summary-position-point))
7386     n))
7387
7388 (defun gnus-summary-next-unread-subject (n)
7389   "Go to next N'th unread summary line."
7390   (interactive "p")
7391   (gnus-summary-next-subject n t))
7392
7393 (defun gnus-summary-prev-subject (n &optional unread)
7394   "Go to previous N'th summary line.
7395 If optional argument UNREAD is non-nil, only unread article is selected."
7396   (interactive "p")
7397   (gnus-summary-next-subject (- n) unread))
7398
7399 (defun gnus-summary-prev-unread-subject (n)
7400   "Go to previous N'th unread summary line."
7401   (interactive "p")
7402   (gnus-summary-next-subject (- n) t))
7403
7404 (defun gnus-summary-goto-subjects (articles)
7405   "Insert the subject header for ARTICLES in the current buffer."
7406   (save-excursion
7407     (dolist (article articles)
7408       (gnus-summary-goto-subject article t)))
7409   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7410   (gnus-summary-position-point))
7411
7412 (defun gnus-summary-goto-subject (article &optional force silent)
7413   "Go the subject line of ARTICLE.
7414 If FORCE, also allow jumping to articles not currently shown."
7415   (interactive "nArticle number: ")
7416   (unless (numberp article)
7417     (error "Article %s is not a number" article))
7418   (let ((b (point))
7419         (data (gnus-data-find article)))
7420     ;; We read in the article if we have to.
7421     (and (not data)
7422          force
7423          (gnus-summary-insert-subject
7424           article
7425           (if (or (numberp force) (vectorp force)) force)
7426           t)
7427          (setq data (gnus-data-find article)))
7428     (goto-char b)
7429     (if (not data)
7430         (progn
7431           (unless silent
7432             (gnus-message 3 "Can't find article %d" article))
7433           nil)
7434       (let ((pt (gnus-data-pos data)))
7435         (goto-char pt)
7436         (gnus-summary-set-article-display-arrow pt))
7437       (gnus-summary-position-point)
7438       article)))
7439
7440 ;; Walking around summary lines with displaying articles.
7441
7442 (defun gnus-summary-expand-window (&optional arg)
7443   "Make the summary buffer take up the entire Emacs frame.
7444 Given a prefix, will force an `article' buffer configuration."
7445   (interactive "P")
7446   (if arg
7447       (gnus-configure-windows 'article 'force)
7448     (gnus-configure-windows 'summary 'force)))
7449
7450 (defun gnus-summary-display-article (article &optional all-header)
7451   "Display ARTICLE in article buffer."
7452   (unless (and (gnus-buffer-live-p gnus-article-buffer)
7453                (with-current-buffer gnus-article-buffer
7454                  (eq major-mode 'gnus-article-mode)))
7455     (gnus-article-setup-buffer))
7456   (gnus-set-global-variables)
7457   (with-current-buffer gnus-article-buffer
7458     (setq gnus-article-charset gnus-newsgroup-charset)
7459     (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7460     (mm-enable-multibyte))
7461   (if (null article)
7462       nil
7463     (prog1
7464         (if gnus-summary-display-article-function
7465             (funcall gnus-summary-display-article-function article all-header)
7466           (gnus-article-prepare article all-header))
7467       (gnus-run-hooks 'gnus-select-article-hook)
7468       (when (and gnus-current-article
7469                  (not (zerop gnus-current-article)))
7470         (gnus-summary-goto-subject gnus-current-article))
7471       (gnus-summary-recenter)
7472       (when (and gnus-use-trees gnus-show-threads)
7473         (gnus-possibly-generate-tree article)
7474         (gnus-highlight-selected-tree article))
7475       ;; Successfully display article.
7476       (gnus-article-set-window-start
7477        (cdr (assq article gnus-newsgroup-bookmarks))))))
7478
7479 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7480   "Select the current article.
7481 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7482 non-nil, the article will be re-fetched even if it already present in
7483 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7484 be displayed."
7485   ;; Make sure we are in the summary buffer to work around bbdb bug.
7486   (unless (eq major-mode 'gnus-summary-mode)
7487     (set-buffer gnus-summary-buffer))
7488   (let ((article (or article (gnus-summary-article-number)))
7489         (all-headers (not (not all-headers))) ;Must be t or nil.
7490         gnus-summary-display-article-function)
7491     (and (not pseudo)
7492          (gnus-summary-article-pseudo-p article)
7493          (error "This is a pseudo-article"))
7494     (save-excursion
7495       (set-buffer gnus-summary-buffer)
7496       (if (or (and gnus-single-article-buffer
7497                    (or (null gnus-current-article)
7498                        (null gnus-article-current)
7499                        (null (get-buffer gnus-article-buffer))
7500                        (not (eq article (cdr gnus-article-current)))
7501                        (not (equal (car gnus-article-current)
7502                                    gnus-newsgroup-name))))
7503               (and (not gnus-single-article-buffer)
7504                    (or (null gnus-current-article)
7505                        (not (eq gnus-current-article article))))
7506               force)
7507           ;; The requested article is different from the current article.
7508           (progn
7509             (gnus-summary-display-article article all-headers)
7510             (when (gnus-buffer-live-p gnus-article-buffer)
7511               (with-current-buffer gnus-article-buffer
7512                 (if (not gnus-article-decoded-p) ;; a local variable
7513                     (mm-disable-multibyte))))
7514             (gnus-article-set-window-start
7515              (cdr (assq article gnus-newsgroup-bookmarks)))
7516             article)
7517         'old))))
7518
7519 (defun gnus-summary-force-verify-and-decrypt ()
7520   "Display buttons for signed/encrypted parts and verify/decrypt them."
7521   (interactive)
7522   (let ((mm-verify-option 'known)
7523         (mm-decrypt-option 'known)
7524         (gnus-article-emulate-mime t)
7525         (gnus-buttonized-mime-types (append (list "multipart/signed"
7526                                                   "multipart/encrypted")
7527                                             gnus-buttonized-mime-types)))
7528     (gnus-summary-select-article nil 'force)))
7529
7530 (defun gnus-summary-set-current-mark (&optional current-mark)
7531   "Obsolete function."
7532   nil)
7533
7534 (defun gnus-summary-next-article (&optional unread subject backward push)
7535   "Select the next article.
7536 If UNREAD, only unread articles are selected.
7537 If SUBJECT, only articles with SUBJECT are selected.
7538 If BACKWARD, the previous article is selected instead of the next."
7539   (interactive "P")
7540   ;; Make sure we are in the summary buffer.
7541   (unless (eq major-mode 'gnus-summary-mode)
7542     (set-buffer gnus-summary-buffer))
7543   (cond
7544    ;; Is there such an article?
7545    ((and (gnus-summary-search-forward unread subject backward)
7546          (or (gnus-summary-display-article (gnus-summary-article-number))
7547              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7548     (gnus-summary-position-point))
7549    ;; If not, we try the first unread, if that is wanted.
7550    ((and subject
7551          gnus-auto-select-same
7552          (gnus-summary-first-unread-article))
7553     (gnus-summary-position-point)
7554     (gnus-message 6 "Wrapped"))
7555    ;; Try to get next/previous article not displayed in this group.
7556    ((and gnus-auto-extend-newsgroup
7557          (not unread) (not subject))
7558     (gnus-summary-goto-article
7559      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7560      nil (count-lines (point-min) (point))))
7561    ;; Go to next/previous group.
7562    (t
7563     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7564       (gnus-summary-jump-to-group gnus-newsgroup-name))
7565     (let ((cmd last-command-char)
7566           (point
7567            (with-current-buffer gnus-group-buffer
7568              (point)))
7569           (group
7570            (if (eq gnus-keep-same-level 'best)
7571                (gnus-summary-best-group gnus-newsgroup-name)
7572              (gnus-summary-search-group backward gnus-keep-same-level))))
7573       ;; For some reason, the group window gets selected.  We change
7574       ;; it back.
7575       (select-window (get-buffer-window (current-buffer)))
7576       ;; Select next unread newsgroup automagically.
7577       (cond
7578        ((or (not gnus-auto-select-next)
7579             (not cmd))
7580         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7581        ((or (eq gnus-auto-select-next 'quietly)
7582             (and (eq gnus-auto-select-next 'slightly-quietly)
7583                  push)
7584             (and (eq gnus-auto-select-next 'almost-quietly)
7585                  (gnus-summary-last-article-p)))
7586         ;; Select quietly.
7587         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7588             (gnus-summary-exit)
7589           (gnus-message 7 "No more%s articles (%s)..."
7590                         (if unread " unread" "")
7591                         (if group (concat "selecting " group)
7592                           "exiting"))
7593           (gnus-summary-next-group nil group backward)))
7594        (t
7595         (when (gnus-key-press-event-p last-input-event)
7596           (gnus-summary-walk-group-buffer
7597            gnus-newsgroup-name cmd unread backward point))))))))
7598
7599 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7600   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7601                       (?\C-p (gnus-group-prev-unread-group 1))))
7602         (cursor-in-echo-area t)
7603         keve key group ended prompt)
7604     (save-excursion
7605       (set-buffer gnus-group-buffer)
7606       (goto-char start)
7607       (setq group
7608             (if (eq gnus-keep-same-level 'best)
7609                 (gnus-summary-best-group gnus-newsgroup-name)
7610               (gnus-summary-search-group backward gnus-keep-same-level))))
7611     (while (not ended)
7612       (setq prompt
7613             (format
7614              "No more%s articles%s " (if unread " unread" "")
7615              (if (and group
7616                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7617                  (format " (Type %s for %s [%s])"
7618                          (single-key-description cmd)
7619                          (gnus-group-decoded-name group)
7620                          (gnus-group-unread group))
7621                (format " (Type %s to exit %s)"
7622                        (single-key-description cmd)
7623                        (gnus-group-decoded-name gnus-newsgroup-name)))))
7624       ;; Confirm auto selection.
7625       (setq key (car (setq keve (gnus-read-event-char prompt)))
7626             ended t)
7627       (cond
7628        ((assq key keystrokes)
7629         (let ((obuf (current-buffer)))
7630           (switch-to-buffer gnus-group-buffer)
7631           (when group
7632             (gnus-group-jump-to-group group))
7633           (eval (cadr (assq key keystrokes)))
7634           (setq group (gnus-group-group-name))
7635           (switch-to-buffer obuf))
7636         (setq ended nil))
7637        ((equal key cmd)
7638         (if (or (not group)
7639                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7640             (gnus-summary-exit)
7641           (gnus-summary-next-group nil group backward)))
7642        (t
7643         (push (cdr keve) unread-command-events))))))
7644
7645 (defun gnus-summary-next-unread-article ()
7646   "Select unread article after current one."
7647   (interactive)
7648   (gnus-summary-next-article
7649    (or (not (eq gnus-summary-goto-unread 'never))
7650        (gnus-summary-last-article-p (gnus-summary-article-number)))
7651    (and gnus-auto-select-same
7652         (gnus-summary-article-subject))))
7653
7654 (defun gnus-summary-prev-article (&optional unread subject)
7655   "Select the article after the current one.
7656 If UNREAD is non-nil, only unread articles are selected."
7657   (interactive "P")
7658   (gnus-summary-next-article unread subject t))
7659
7660 (defun gnus-summary-prev-unread-article ()
7661   "Select unread article before current one."
7662   (interactive)
7663   (gnus-summary-prev-article
7664    (or (not (eq gnus-summary-goto-unread 'never))
7665        (gnus-summary-first-article-p (gnus-summary-article-number)))
7666    (and gnus-auto-select-same
7667         (gnus-summary-article-subject))))
7668
7669 (defun gnus-summary-next-page (&optional lines circular stop)
7670   "Show next page of the selected article.
7671 If at the end of the current article, select the next article.
7672 LINES says how many lines should be scrolled up.
7673
7674 If CIRCULAR is non-nil, go to the start of the article instead of
7675 selecting the next article when reaching the end of the current
7676 article.
7677
7678 If STOP is non-nil, just stop when reaching the end of the message.
7679
7680 Also see the variable `gnus-article-skip-boring'."
7681   (interactive "P")
7682   (setq gnus-summary-buffer (current-buffer))
7683   (gnus-set-global-variables)
7684   (let ((article (gnus-summary-article-number))
7685         (article-window (get-buffer-window gnus-article-buffer t))
7686         endp)
7687     ;; If the buffer is empty, we have no article.
7688     (unless article
7689       (error "No article to select"))
7690     (gnus-configure-windows 'article)
7691     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7692         (if (and (eq gnus-summary-goto-unread 'never)
7693                  (not (gnus-summary-last-article-p article)))
7694             (gnus-summary-next-article)
7695           (gnus-summary-next-unread-article))
7696       (if (or (null gnus-current-article)
7697               (null gnus-article-current)
7698               (/= article (cdr gnus-article-current))
7699               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7700           ;; Selected subject is different from current article's.
7701           (gnus-summary-display-article article)
7702         (when article-window
7703           (gnus-eval-in-buffer-window gnus-article-buffer
7704             (setq endp (or (gnus-article-next-page lines)
7705                            (gnus-article-only-boring-p))))
7706           (when endp
7707             (cond (stop
7708                    (gnus-message 3 "End of message"))
7709                   (circular
7710                    (gnus-summary-beginning-of-article))
7711                   (lines
7712                    (gnus-message 3 "End of message"))
7713                   ((null lines)
7714                    (if (and (eq gnus-summary-goto-unread 'never)
7715                             (not (gnus-summary-last-article-p article)))
7716                        (gnus-summary-next-article)
7717                      (gnus-summary-next-unread-article))))))))
7718     (gnus-summary-recenter)
7719     (gnus-summary-position-point)))
7720
7721 (defun gnus-summary-prev-page (&optional lines move)
7722   "Show previous page of selected article.
7723 Argument LINES specifies lines to be scrolled down.
7724 If MOVE, move to the previous unread article if point is at
7725 the beginning of the buffer."
7726   (interactive "P")
7727   (let ((article (gnus-summary-article-number))
7728         (article-window (get-buffer-window gnus-article-buffer t))
7729         endp)
7730     (gnus-configure-windows 'article)
7731     (if (or (null gnus-current-article)
7732             (null gnus-article-current)
7733             (/= article (cdr gnus-article-current))
7734             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7735         ;; Selected subject is different from current article's.
7736         (gnus-summary-display-article article)
7737       (gnus-summary-recenter)
7738       (when article-window
7739         (gnus-eval-in-buffer-window gnus-article-buffer
7740           (setq endp (gnus-article-prev-page lines)))
7741         (when (and move endp)
7742           (cond (lines
7743                  (gnus-message 3 "Beginning of message"))
7744                 ((null lines)
7745                  (if (and (eq gnus-summary-goto-unread 'never)
7746                           (not (gnus-summary-first-article-p article)))
7747                      (gnus-summary-prev-article)
7748                    (gnus-summary-prev-unread-article))))))))
7749   (gnus-summary-position-point))
7750
7751 (defun gnus-summary-prev-page-or-article (&optional lines)
7752   "Show previous page of selected article.
7753 Argument LINES specifies lines to be scrolled down.
7754 If at the beginning of the article, go to the next article."
7755   (interactive "P")
7756   (gnus-summary-prev-page lines t))
7757
7758 (defun gnus-summary-scroll-up (lines)
7759   "Scroll up (or down) one line current article.
7760 Argument LINES specifies lines to be scrolled up (or down if negative)."
7761   (interactive "p")
7762   (gnus-configure-windows 'article)
7763   (gnus-summary-show-thread)
7764   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7765     (gnus-eval-in-buffer-window gnus-article-buffer
7766       (cond ((> lines 0)
7767              (when (gnus-article-next-page lines)
7768                (gnus-message 3 "End of message")))
7769             ((< lines 0)
7770              (gnus-article-prev-page (- lines))))))
7771   (gnus-summary-recenter)
7772   (gnus-summary-position-point))
7773
7774 (defun gnus-summary-scroll-down (lines)
7775   "Scroll down (or up) one line current article.
7776 Argument LINES specifies lines to be scrolled down (or up if negative)."
7777   (interactive "p")
7778   (gnus-summary-scroll-up (- lines)))
7779
7780 (defun gnus-summary-next-same-subject ()
7781   "Select next article which has the same subject as current one."
7782   (interactive)
7783   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7784
7785 (defun gnus-summary-prev-same-subject ()
7786   "Select previous article which has the same subject as current one."
7787   (interactive)
7788   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7789
7790 (defun gnus-summary-next-unread-same-subject ()
7791   "Select next unread article which has the same subject as current one."
7792   (interactive)
7793   (gnus-summary-next-article t (gnus-summary-article-subject)))
7794
7795 (defun gnus-summary-prev-unread-same-subject ()
7796   "Select previous unread article which has the same subject as current one."
7797   (interactive)
7798   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7799
7800 (defun gnus-summary-first-unread-article ()
7801   "Select the first unread article.
7802 Return nil if there are no unread articles."
7803   (interactive)
7804   (prog1
7805       (when (gnus-summary-first-subject t)
7806         (gnus-summary-show-thread)
7807         (gnus-summary-first-subject t)
7808         (gnus-summary-display-article (gnus-summary-article-number)))
7809     (gnus-summary-position-point)))
7810
7811 (defun gnus-summary-first-unread-subject ()
7812   "Place the point on the subject line of the first unread article.
7813 Return nil if there are no unread articles."
7814   (interactive)
7815   (prog1
7816       (when (gnus-summary-first-subject t)
7817         (gnus-summary-show-thread)
7818         (gnus-summary-first-subject t))
7819     (gnus-summary-position-point)))
7820
7821 (defun gnus-summary-first-unseen-subject ()
7822   "Place the point on the subject line of the first unseen article.
7823 Return nil if there are no unseen articles."
7824   (interactive)
7825   (prog1
7826       (when (gnus-summary-first-subject nil nil t)
7827         (gnus-summary-show-thread)
7828         (gnus-summary-first-subject nil nil t))
7829     (gnus-summary-position-point)))
7830
7831 (defun gnus-summary-first-unseen-or-unread-subject ()
7832   "Place the point on the subject line of the first unseen article or,
7833 if all article have been seen, on the subject line of the first unread
7834 article."
7835   (interactive)
7836   (prog1
7837       (unless (when (gnus-summary-first-subject nil nil t)
7838                 (gnus-summary-show-thread)
7839                 (gnus-summary-first-subject nil nil t))
7840         (when (gnus-summary-first-subject t)
7841           (gnus-summary-show-thread)
7842           (gnus-summary-first-subject t)))
7843     (gnus-summary-position-point)))
7844
7845 (defun gnus-summary-first-article ()
7846   "Select the first article.
7847 Return nil if there are no articles."
7848   (interactive)
7849   (prog1
7850       (when (gnus-summary-first-subject)
7851         (gnus-summary-show-thread)
7852         (gnus-summary-first-subject)
7853         (gnus-summary-display-article (gnus-summary-article-number)))
7854     (gnus-summary-position-point)))
7855
7856 (defun gnus-summary-best-unread-article (&optional arg)
7857   "Select the unread article with the highest score.
7858 If given a prefix argument, select the next unread article that has a
7859 score higher than the default score."
7860   (interactive "P")
7861   (let ((article (if arg
7862                      (gnus-summary-better-unread-subject)
7863                    (gnus-summary-best-unread-subject))))
7864     (if article
7865         (gnus-summary-goto-article article)
7866       (error "No unread articles"))))
7867
7868 (defun gnus-summary-best-unread-subject ()
7869   "Select the unread subject with the highest score."
7870   (interactive)
7871   (let ((best -1000000)
7872         (data gnus-newsgroup-data)
7873         article score)
7874     (while data
7875       (and (gnus-data-unread-p (car data))
7876            (> (setq score
7877                     (gnus-summary-article-score (gnus-data-number (car data))))
7878               best)
7879            (setq best score
7880                  article (gnus-data-number (car data))))
7881       (setq data (cdr data)))
7882     (when article
7883       (gnus-summary-goto-subject article))
7884     (gnus-summary-position-point)
7885     article))
7886
7887 (defun gnus-summary-better-unread-subject ()
7888   "Select the first unread subject that has a score over the default score."
7889   (interactive)
7890   (let ((data gnus-newsgroup-data)
7891         article score)
7892     (while (and (setq article (gnus-data-number (car data)))
7893                 (or (gnus-data-read-p (car data))
7894                     (not (> (gnus-summary-article-score article)
7895                             gnus-summary-default-score))))
7896       (setq data (cdr data)))
7897     (when article
7898       (gnus-summary-goto-subject article))
7899     (gnus-summary-position-point)
7900     article))
7901
7902 (defun gnus-summary-last-subject ()
7903   "Go to the last displayed subject line in the group."
7904   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7905     (when article
7906       (gnus-summary-goto-subject article))))
7907
7908 (defun gnus-summary-goto-article (article &optional all-headers force)
7909   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7910 If ALL-HEADERS is non-nil, no header lines are hidden.
7911 If FORCE, go to the article even if it isn't displayed.  If FORCE
7912 is a number, it is the line the article is to be displayed on."
7913   (interactive
7914    (list
7915     (completing-read
7916      "Article number or Message-ID: "
7917      (mapcar (lambda (number) (list (int-to-string number)))
7918              gnus-newsgroup-limit))
7919     current-prefix-arg
7920     t))
7921   (prog1
7922       (if (and (stringp article)
7923                (string-match "@\\|%40" article))
7924           (gnus-summary-refer-article article)
7925         (when (stringp article)
7926           (setq article (string-to-number article)))
7927         (if (gnus-summary-goto-subject article force)
7928             (gnus-summary-display-article article all-headers)
7929           (gnus-message 4 "Couldn't go to article %s" article) nil))
7930     (gnus-summary-position-point)))
7931
7932 (defun gnus-summary-goto-last-article ()
7933   "Go to the previously read article."
7934   (interactive)
7935   (prog1
7936       (when gnus-last-article
7937         (gnus-summary-goto-article gnus-last-article nil t))
7938     (gnus-summary-position-point)))
7939
7940 (defun gnus-summary-pop-article (number)
7941   "Pop one article off the history and go to the previous.
7942 NUMBER articles will be popped off."
7943   (interactive "p")
7944   (let (to)
7945     (setq gnus-newsgroup-history
7946           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7947     (if to
7948         (gnus-summary-goto-article (car to) nil t)
7949       (error "Article history empty")))
7950   (gnus-summary-position-point))
7951
7952 ;; Summary commands and functions for limiting the summary buffer.
7953
7954 (defun gnus-summary-limit-to-articles (n)
7955   "Limit the summary buffer to the next N articles.
7956 If not given a prefix, use the process marked articles instead."
7957   (interactive "P")
7958   (prog1
7959       (let ((articles (gnus-summary-work-articles n)))
7960         (setq gnus-newsgroup-processable nil)
7961         (gnus-summary-limit articles))
7962     (gnus-summary-position-point)))
7963
7964 (defun gnus-summary-pop-limit (&optional total)
7965   "Restore the previous limit.
7966 If given a prefix, remove all limits."
7967   (interactive "P")
7968   (when total
7969     (setq gnus-newsgroup-limits
7970           (list (mapcar (lambda (h) (mail-header-number h))
7971                         gnus-newsgroup-headers))))
7972   (unless gnus-newsgroup-limits
7973     (error "No limit to pop"))
7974   (prog1
7975       (gnus-summary-limit nil 'pop)
7976     (gnus-summary-position-point)))
7977
7978 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7979   "Limit the summary buffer to articles that have subjects that match a regexp.
7980 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7981   (interactive
7982    (list (read-string (if current-prefix-arg
7983                           "Exclude subject (regexp): "
7984                         "Limit to subject (regexp): "))
7985          nil current-prefix-arg))
7986   (unless header
7987     (setq header "subject"))
7988   (when (not (equal "" subject))
7989     (prog1
7990         (let ((articles (gnus-summary-find-matching
7991                          (or header "subject") subject 'all nil nil
7992                          not-matching)))
7993           (unless articles
7994             (error "Found no matches for \"%s\"" subject))
7995           (gnus-summary-limit articles))
7996       (gnus-summary-position-point))))
7997
7998 (defun gnus-summary-limit-to-author (from &optional not-matching)
7999   "Limit the summary buffer to articles that have authors that match a regexp.
8000 If NOT-MATCHING, excluding articles that have authors that match a regexp."
8001   (interactive
8002    (list (read-string (if current-prefix-arg
8003                           "Exclude author (regexp): "
8004                         "Limit to author (regexp): "))
8005          current-prefix-arg))
8006   (gnus-summary-limit-to-subject from "from" not-matching))
8007
8008 (defun gnus-summary-limit-to-recipient (recipient &optional not-matching)
8009   "Limit the summary buffer to articles with the given RECIPIENT.
8010
8011 If NOT-MATCHING, exclude RECIPIENT.
8012
8013 To and Cc headers are checked.  You need to include them in
8014 `nnmail-extra-headers'."
8015   ;; Unlike `rmail-summary-by-recipients', doesn't include From.
8016   (interactive
8017    (list (read-string (format "%s recipient (regexp): "
8018                               (if current-prefix-arg "Exclude" "Limit to")))
8019          current-prefix-arg))
8020   (when (not (equal "" recipient))
8021     (prog1 (let* ((to
8022                    (if (memq 'To nnmail-extra-headers)
8023                        (gnus-summary-find-matching
8024                         (cons 'extra 'To) recipient 'all nil nil
8025                         not-matching)
8026                      (gnus-message
8027                       1 "`To' isn't present in `nnmail-extra-headers'")
8028                      (sit-for 1)
8029                      nil))
8030                   (cc
8031                    (if (memq 'Cc nnmail-extra-headers)
8032                        (gnus-summary-find-matching
8033                         (cons 'extra 'Cc) recipient 'all nil nil
8034                         not-matching)
8035                      (gnus-message
8036                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8037                      (sit-for 1)
8038                      nil))
8039                   (articles
8040                    (if not-matching
8041                        ;; We need the numbers that are in both lists:
8042                        (mapcar (lambda (a)
8043                                  (and (memq a to) a))
8044                                cc)
8045                      (nconc to cc))))
8046              (unless articles
8047                (error "Found no matches for \"%s\"" recipient))
8048              (gnus-summary-limit articles))
8049       (gnus-summary-position-point))))
8050
8051 (defun gnus-summary-limit-to-address (address &optional not-matching)
8052   "Limit the summary buffer to articles with the given ADDRESS.
8053
8054 If NOT-MATCHING, exclude ADDRESS.
8055
8056 To, Cc and From headers are checked.  You need to include `To' and `Cc'
8057 in `nnmail-extra-headers'."
8058   (interactive
8059    (list (read-string (format "%s address (regexp): "
8060                               (if current-prefix-arg "Exclude" "Limit to")))
8061          current-prefix-arg))
8062   (when (not (equal "" address))
8063     (prog1 (let* ((to
8064                    (if (memq 'To nnmail-extra-headers)
8065                        (gnus-summary-find-matching
8066                         (cons 'extra 'To) address 'all nil nil
8067                         not-matching)
8068                      (gnus-message
8069                       1 "`To' isn't present in `nnmail-extra-headers'")
8070                      (sit-for 1)
8071                      t))
8072                   (cc
8073                    (if (memq 'Cc nnmail-extra-headers)
8074                        (gnus-summary-find-matching
8075                         (cons 'extra 'Cc) address 'all nil nil
8076                         not-matching)
8077                      (gnus-message
8078                       1 "`Cc' isn't present in `nnmail-extra-headers'")
8079                      (sit-for 1)
8080                      t))
8081                   (from
8082                    (gnus-summary-find-matching "from" address
8083                                                'all nil nil not-matching))
8084                   (articles
8085                    (if not-matching
8086                        ;; We need the numbers that are in all lists:
8087                        (if (eq cc t)
8088                            (if (eq to t)
8089                                from
8090                              (mapcar (lambda (a) (car (memq a from))) to))
8091                          (if (eq to t)
8092                              (mapcar (lambda (a) (car (memq a from))) cc)
8093                            (mapcar (lambda (a) (car (memq a from)))
8094                                    (mapcar (lambda (a) (car (memq a to)))
8095                                            cc))))
8096                      (nconc (if (eq to t) nil to)
8097                             (if (eq cc t) nil cc)
8098                             from))))
8099              (unless articles
8100                (error "Found no matches for \"%s\"" address))
8101              (gnus-summary-limit articles))
8102       (gnus-summary-position-point))))
8103
8104 (defun gnus-summary-limit-strange-charsets-predicate (header)
8105   (let ((string (concat (mail-header-subject header)
8106                         (mail-header-from header)))
8107         charset found)
8108     (dotimes (i (1- (length string)))
8109       (setq charset (format "%s" (char-charset (aref string (1+ i)))))
8110       (when (string-match "unicode\\|big\\|japanese" charset)
8111         (setq found t)))
8112     found))
8113
8114 (defun gnus-summary-limit-to-predicate (predicate)
8115   "Limit to articles where PREDICATE returns non-nil.
8116 PREDICATE will be called with the header structures of the
8117 articles."
8118   (let ((articles nil)
8119         (case-fold-search t))
8120     (dolist (header gnus-newsgroup-headers)
8121       (when (funcall predicate header)
8122         (push (mail-header-number header) articles)))
8123     (gnus-summary-limit (nreverse articles))))
8124
8125 (defun gnus-summary-limit-to-age (age &optional younger-p)
8126   "Limit the summary buffer to articles that are older than (or equal) AGE days.
8127 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
8128 articles that are younger than AGE days."
8129   (interactive
8130    (let ((younger current-prefix-arg)
8131          (days-got nil)
8132          days)
8133      (while (not days-got)
8134        (setq days (if younger
8135                       (read-string "Limit to articles younger than (in days, older when negative): ")
8136                     (read-string
8137                      "Limit to articles older than (in days, younger when negative): ")))
8138        (when (> (length days) 0)
8139          (setq days (read days)))
8140        (if (numberp days)
8141            (progn
8142              (setq days-got t)
8143              (when (< days 0)
8144                (setq younger (not younger))
8145                (setq days (* days -1))))
8146          (message "Please enter a number.")
8147          (sleep-for 1)))
8148      (list days younger)))
8149   (prog1
8150       (let ((data gnus-newsgroup-data)
8151             (cutoff (days-to-time age))
8152             articles d date is-younger)
8153         (while (setq d (pop data))
8154           (when (and (vectorp (gnus-data-header d))
8155                      (setq date (mail-header-date (gnus-data-header d))))
8156             (setq is-younger (time-less-p
8157                               (time-since (condition-case ()
8158                                               (date-to-time date)
8159                                             (error '(0 0))))
8160                               cutoff))
8161             (when (if younger-p
8162                       is-younger
8163                     (not is-younger))
8164               (push (gnus-data-number d) articles))))
8165         (gnus-summary-limit (nreverse articles)))
8166     (gnus-summary-position-point)))
8167
8168 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
8169   "Limit the summary buffer to articles that match an 'extra' header."
8170   (interactive
8171    (let ((header
8172           (intern
8173            (gnus-completing-read-with-default
8174             (symbol-name (car gnus-extra-headers))
8175             (if current-prefix-arg
8176                 "Exclude extra header"
8177               "Limit extra header")
8178             (mapcar (lambda (x)
8179                       (cons (symbol-name x) x))
8180                     gnus-extra-headers)
8181             nil
8182             t))))
8183      (list header
8184            (read-string (format "%s header %s (regexp): "
8185                                 (if current-prefix-arg "Exclude" "Limit to")
8186                                 header))
8187            current-prefix-arg)))
8188   (when (not (equal "" regexp))
8189     (prog1
8190         (let ((articles (gnus-summary-find-matching
8191                          (cons 'extra header) regexp 'all nil nil
8192                          not-matching)))
8193           (unless articles
8194             (error "Found no matches for \"%s\"" regexp))
8195           (gnus-summary-limit articles))
8196       (gnus-summary-position-point))))
8197
8198 (defun gnus-summary-limit-to-display-predicate ()
8199   "Limit the summary buffer to the predicated in the `display' group parameter."
8200   (interactive)
8201   (unless gnus-newsgroup-display
8202     (error "There is no `display' group parameter"))
8203   (let (articles)
8204     (dolist (number gnus-newsgroup-articles)
8205       (when (funcall gnus-newsgroup-display)
8206         (push number articles)))
8207     (gnus-summary-limit articles))
8208   (gnus-summary-position-point))
8209
8210 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8211 (make-obsolete
8212  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
8213
8214 (defun gnus-summary-limit-to-unread (&optional all)
8215   "Limit the summary buffer to articles that are not marked as read.
8216 If ALL is non-nil, limit strictly to unread articles."
8217   (interactive "P")
8218   (if all
8219       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
8220     (gnus-summary-limit-to-marks
8221      ;; Concat all the marks that say that an article is read and have
8222      ;; those removed.
8223      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
8224            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
8225            gnus-low-score-mark gnus-expirable-mark
8226            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
8227            gnus-duplicate-mark gnus-souped-mark)
8228      'reverse)))
8229
8230 (defun gnus-summary-limit-to-headers (match &optional reverse)
8231   "Limit the summary buffer to articles that have headers that match MATCH.
8232 If REVERSE (the prefix), limit to articles that don't match."
8233   (interactive "sMatch headers (regexp): \nP")
8234   (gnus-summary-limit-to-bodies match reverse t))
8235
8236 (defun gnus-summary-limit-to-bodies (match &optional reverse headersp)
8237   "Limit the summary buffer to articles that have bodies that match MATCH.
8238 If REVERSE (the prefix), limit to articles that don't match."
8239   (interactive "sMatch body (regexp): \nP")
8240   (let ((articles nil)
8241         (gnus-select-article-hook nil)  ;Disable hook.
8242         (gnus-article-prepare-hook nil)
8243         (gnus-use-article-prefetch nil)
8244         (gnus-keep-backlog nil)
8245         (gnus-break-pages nil)
8246         (gnus-summary-display-arrow nil)
8247         (gnus-updated-mode-lines nil)
8248         (gnus-auto-center-summary nil)
8249         (gnus-display-mime-function nil))
8250     (dolist (data gnus-newsgroup-data)
8251       (let (gnus-mark-article-hook)
8252         (gnus-summary-select-article t t nil (gnus-data-number data)))
8253       (save-excursion
8254         (set-buffer gnus-article-buffer)
8255         (article-goto-body)
8256         (let* ((case-fold-search t)
8257                (found (if headersp
8258                           (re-search-backward match nil t)
8259                         (re-search-forward match nil t))))
8260           (when (or (and found
8261                          (not reverse))
8262                     (and (not found)
8263                          reverse))
8264             (push (gnus-data-number data) articles)))))
8265     (if (not articles)
8266         (message "No messages matched")
8267       (gnus-summary-limit articles)))
8268   (gnus-summary-position-point))
8269
8270 (defun gnus-summary-limit-to-singletons (&optional threadsp)
8271   "Limit the summary buffer to articles that aren't part on any thread.
8272 If THREADSP (the prefix), limit to articles that are in threads."
8273   (interactive "P")
8274   (let ((articles nil)
8275         thread-articles
8276         threads)
8277     (dolist (thread gnus-newsgroup-threads)
8278       (if (stringp (car thread))
8279           (dolist (thread (cdr thread))
8280             (push thread threads))
8281         (push thread threads)))
8282     (dolist (thread threads)
8283       (setq thread-articles (gnus-articles-in-thread thread))
8284       (when (or (and threadsp
8285                      (> (length thread-articles) 1))
8286                 (and (not threadsp)
8287                      (= (length thread-articles) 1)))
8288         (setq articles (nconc thread-articles articles))))
8289     (if (not articles)
8290         (message "No messages matched")
8291       (gnus-summary-limit articles))
8292     (gnus-summary-position-point)))
8293
8294 (defun gnus-summary-limit-to-replied (&optional unreplied)
8295   "Limit the summary buffer to replied articles.
8296 If UNREPLIED (the prefix), limit to unreplied articles."
8297   (interactive "P")
8298   (if unreplied
8299       (gnus-summary-limit
8300        (gnus-set-difference gnus-newsgroup-articles
8301         gnus-newsgroup-replied))
8302     (gnus-summary-limit gnus-newsgroup-replied))
8303   (gnus-summary-position-point))
8304
8305 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
8306 (make-obsolete 'gnus-summary-delete-marked-with
8307                'gnus-summary-limit-exclude-marks)
8308
8309 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
8310   "Exclude articles that are marked with MARKS (e.g. \"DK\").
8311 If REVERSE, limit the summary buffer to articles that are marked
8312 with MARKS.  MARKS can either be a string of marks or a list of marks.
8313 Returns how many articles were removed."
8314   (interactive "sMarks: ")
8315   (gnus-summary-limit-to-marks marks t))
8316
8317 (defun gnus-summary-limit-to-marks (marks &optional reverse)
8318   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
8319 If REVERSE (the prefix), limit the summary buffer to articles that are
8320 not marked with MARKS.  MARKS can either be a string of marks or a
8321 list of marks.
8322 Returns how many articles were removed."
8323   (interactive "sMarks: \nP")
8324   (prog1
8325       (let ((data gnus-newsgroup-data)
8326             (marks (if (listp marks) marks
8327                      (append marks nil))) ; Transform to list.
8328             articles)
8329         (while data
8330           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
8331                   (memq (gnus-data-mark (car data)) marks))
8332             (push (gnus-data-number (car data)) articles))
8333           (setq data (cdr data)))
8334         (gnus-summary-limit articles))
8335     (gnus-summary-position-point)))
8336
8337 (defun gnus-summary-limit-to-score (score)
8338   "Limit to articles with score at or above SCORE."
8339   (interactive "NLimit to articles with score of at least: ")
8340   (let ((data gnus-newsgroup-data)
8341         articles)
8342     (while data
8343       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
8344                 score)
8345         (push (gnus-data-number (car data)) articles))
8346       (setq data (cdr data)))
8347     (prog1
8348         (gnus-summary-limit articles)
8349       (gnus-summary-position-point))))
8350
8351 (defun gnus-summary-limit-to-unseen ()
8352   "Limit to unseen articles."
8353   (interactive)
8354   (prog1
8355       (gnus-summary-limit gnus-newsgroup-unseen)
8356     (gnus-summary-position-point)))
8357
8358 (defun gnus-summary-limit-include-thread (id)
8359   "Display all the hidden articles that is in the thread with ID in it.
8360 When called interactively, ID is the Message-ID of the current
8361 article."
8362   (interactive (list (mail-header-id (gnus-summary-article-header))))
8363   (let ((articles (gnus-articles-in-thread
8364                    (gnus-id-to-thread (gnus-root-id id)))))
8365     (prog1
8366         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8367       (gnus-summary-limit-include-matching-articles
8368        "subject"
8369        (regexp-quote (gnus-simplify-subject-re
8370                       (mail-header-subject (gnus-id-to-header id)))))
8371       (gnus-summary-position-point))))
8372
8373 (defun gnus-summary-limit-include-matching-articles (header regexp)
8374   "Display all the hidden articles that have HEADERs that match REGEXP."
8375   (interactive (list (read-string "Match on header: ")
8376                      (read-string "Regexp: ")))
8377   (let ((articles (gnus-find-matching-articles header regexp)))
8378     (prog1
8379         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
8380       (gnus-summary-position-point))))
8381
8382 (defun gnus-summary-insert-dormant-articles ()
8383   "Insert all the dormant articles for this group into the current buffer."
8384   (interactive)
8385   (let ((gnus-verbose (max 6 gnus-verbose)))
8386     (if (not gnus-newsgroup-dormant)
8387         (gnus-message 3 "No dormant articles for this group")
8388       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
8389
8390 (defun gnus-summary-insert-ticked-articles ()
8391   "Insert ticked articles for this group into the current buffer."
8392   (interactive)
8393   (let ((gnus-verbose (max 6 gnus-verbose)))
8394     (if (not gnus-newsgroup-marked)
8395         (gnus-message 3 "No ticked articles for this group")
8396       (gnus-summary-goto-subjects gnus-newsgroup-marked))))
8397
8398 (defun gnus-summary-limit-include-dormant ()
8399   "Display all the hidden articles that are marked as dormant.
8400 Note that this command only works on a subset of the articles currently
8401 fetched for this group."
8402   (interactive)
8403   (unless gnus-newsgroup-dormant
8404     (error "There are no dormant articles in this group"))
8405   (prog1
8406       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
8407     (gnus-summary-position-point)))
8408
8409 (defun gnus-summary-limit-exclude-dormant ()
8410   "Hide all dormant articles."
8411   (interactive)
8412   (prog1
8413       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
8414     (gnus-summary-position-point)))
8415
8416 (defun gnus-summary-limit-exclude-childless-dormant ()
8417   "Hide all dormant articles that have no children."
8418   (interactive)
8419   (let ((data (gnus-data-list t))
8420         articles d children)
8421     ;; Find all articles that are either not dormant or have
8422     ;; children.
8423     (while (setq d (pop data))
8424       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
8425                 (and (setq children
8426                            (gnus-article-children (gnus-data-number d)))
8427                      (let (found)
8428                        (while children
8429                          (when (memq (car children) articles)
8430                            (setq children nil
8431                                  found t))
8432                          (pop children))
8433                        found)))
8434         (push (gnus-data-number d) articles)))
8435     ;; Do the limiting.
8436     (prog1
8437         (gnus-summary-limit articles)
8438       (gnus-summary-position-point))))
8439
8440 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
8441   "Mark all unread excluded articles as read.
8442 If ALL, mark even excluded ticked and dormants as read."
8443   (interactive "P")
8444   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
8445   (let ((articles (gnus-sorted-ndifference
8446                    (sort
8447                     (mapcar (lambda (h) (mail-header-number h))
8448                             gnus-newsgroup-headers)
8449                     '<)
8450                    gnus-newsgroup-limit))
8451         article)
8452     (setq gnus-newsgroup-unreads
8453           (gnus-sorted-intersection gnus-newsgroup-unreads
8454                                     gnus-newsgroup-limit))
8455     (if all
8456         (setq gnus-newsgroup-dormant nil
8457               gnus-newsgroup-marked nil
8458               gnus-newsgroup-reads
8459               (nconc
8460                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
8461                gnus-newsgroup-reads))
8462       (while (setq article (pop articles))
8463         (unless (or (memq article gnus-newsgroup-dormant)
8464                     (memq article gnus-newsgroup-marked))
8465           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
8466
8467 (defun gnus-summary-limit (articles &optional pop)
8468   (if pop
8469       ;; We pop the previous limit off the stack and use that.
8470       (setq articles (car gnus-newsgroup-limits)
8471             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
8472     ;; We use the new limit, so we push the old limit on the stack.
8473     (push gnus-newsgroup-limit gnus-newsgroup-limits))
8474   ;; Set the limit.
8475   (setq gnus-newsgroup-limit articles)
8476   (let ((total (length gnus-newsgroup-data))
8477         (data (gnus-data-find-list (gnus-summary-article-number)))
8478         (gnus-summary-mark-below nil)   ; Inhibit this.
8479         found)
8480     ;; This will do all the work of generating the new summary buffer
8481     ;; according to the new limit.
8482     (gnus-summary-prepare)
8483     ;; Hide any threads, possibly.
8484     (gnus-summary-maybe-hide-threads)
8485     ;; Try to return to the article you were at, or one in the
8486     ;; neighborhood.
8487     (when data
8488       ;; We try to find some article after the current one.
8489       (while data
8490         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
8491           (setq data nil
8492                 found t))
8493         (setq data (cdr data))))
8494     (unless found
8495       ;; If there is no data, that means that we were after the last
8496       ;; article.  The same goes when we can't find any articles
8497       ;; after the current one.
8498       (goto-char (point-max))
8499       (gnus-summary-find-prev))
8500     (gnus-set-mode-line 'summary)
8501     ;; We return how many articles were removed from the summary
8502     ;; buffer as a result of the new limit.
8503     (- total (length gnus-newsgroup-data))))
8504
8505 (defsubst gnus-invisible-cut-children (threads)
8506   (let ((num 0))
8507     (while threads
8508       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
8509         (incf num))
8510       (pop threads))
8511     (< num 2)))
8512
8513 (defsubst gnus-cut-thread (thread)
8514   "Go forwards in the thread until we find an article that we want to display."
8515   (when (or (eq gnus-fetch-old-headers 'some)
8516             (eq gnus-fetch-old-headers 'invisible)
8517             (numberp gnus-fetch-old-headers)
8518             (eq gnus-build-sparse-threads 'some)
8519             (eq gnus-build-sparse-threads 'more))
8520     ;; Deal with old-fetched headers and sparse threads.
8521     (while (and
8522             thread
8523             (or
8524              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
8525              (gnus-summary-article-ancient-p
8526               (mail-header-number (car thread))))
8527             (if (or (<= (length (cdr thread)) 1)
8528                     (eq gnus-fetch-old-headers 'invisible))
8529                 (setq gnus-newsgroup-limit
8530                       (delq (mail-header-number (car thread))
8531                             gnus-newsgroup-limit)
8532                       thread (cadr thread))
8533               (when (gnus-invisible-cut-children (cdr thread))
8534                 (let ((th (cdr thread)))
8535                   (while th
8536                     (if (memq (mail-header-number (caar th))
8537                               gnus-newsgroup-limit)
8538                         (setq thread (car th)
8539                               th nil)
8540                       (setq th (cdr th))))))))))
8541   thread)
8542
8543 (defun gnus-cut-threads (threads)
8544   "Cut off all uninteresting articles from the beginning of THREADS."
8545   (when (or (eq gnus-fetch-old-headers 'some)
8546             (eq gnus-fetch-old-headers 'invisible)
8547             (numberp gnus-fetch-old-headers)
8548             (eq gnus-build-sparse-threads 'some)
8549             (eq gnus-build-sparse-threads 'more))
8550     (let ((th threads))
8551       (while th
8552         (setcar th (gnus-cut-thread (car th)))
8553         (setq th (cdr th)))))
8554   ;; Remove nixed out threads.
8555   (delq nil threads))
8556
8557 (defun gnus-summary-initial-limit (&optional show-if-empty)
8558   "Figure out what the initial limit is supposed to be on group entry.
8559 This entails weeding out unwanted dormants, low-scored articles,
8560 fetch-old-headers verbiage, and so on."
8561   ;; Most groups have nothing to remove.
8562   (if (or gnus-inhibit-limiting
8563           (and (null gnus-newsgroup-dormant)
8564                (eq gnus-newsgroup-display 'gnus-not-ignore)
8565                (not (eq gnus-fetch-old-headers 'some))
8566                (not (numberp gnus-fetch-old-headers))
8567                (not (eq gnus-fetch-old-headers 'invisible))
8568                (null gnus-summary-expunge-below)
8569                (not (eq gnus-build-sparse-threads 'some))
8570                (not (eq gnus-build-sparse-threads 'more))
8571                (null gnus-thread-expunge-below)
8572                (not gnus-use-nocem)))
8573       ()                                ; Do nothing.
8574     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8575     (setq gnus-newsgroup-limit nil)
8576     (mapatoms
8577      (lambda (node)
8578        (unless (car (symbol-value node))
8579          ;; These threads have no parents -- they are roots.
8580          (let ((nodes (cdr (symbol-value node)))
8581                thread)
8582            (while nodes
8583              (if (and gnus-thread-expunge-below
8584                       (< (gnus-thread-total-score (car nodes))
8585                          gnus-thread-expunge-below))
8586                  (gnus-expunge-thread (pop nodes))
8587                (setq thread (pop nodes))
8588                (gnus-summary-limit-children thread))))))
8589      gnus-newsgroup-dependencies)
8590     ;; If this limitation resulted in an empty group, we might
8591     ;; pop the previous limit and use it instead.
8592     (when (and (not gnus-newsgroup-limit)
8593                show-if-empty)
8594       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8595     gnus-newsgroup-limit))
8596
8597 (defun gnus-summary-limit-children (thread)
8598   "Return 1 if this subthread is visible and 0 if it is not."
8599   ;; First we get the number of visible children to this thread.  This
8600   ;; is done by recursing down the thread using this function, so this
8601   ;; will really go down to a leaf article first, before slowly
8602   ;; working its way up towards the root.
8603   (when thread
8604     (let* ((max-lisp-eval-depth (max 5000 max-lisp-eval-depth))
8605            (children
8606            (if (cdr thread)
8607                (apply '+ (mapcar 'gnus-summary-limit-children
8608                                  (cdr thread)))
8609              0))
8610           (number (mail-header-number (car thread)))
8611           score)
8612       (if (and
8613            (not (memq number gnus-newsgroup-marked))
8614            (or
8615             ;; If this article is dormant and has absolutely no visible
8616             ;; children, then this article isn't visible.
8617             (and (memq number gnus-newsgroup-dormant)
8618                  (zerop children))
8619             ;; If this is "fetch-old-headered" and there is no
8620             ;; visible children, then we don't want this article.
8621             (and (or (eq gnus-fetch-old-headers 'some)
8622                      (numberp gnus-fetch-old-headers))
8623                  (gnus-summary-article-ancient-p number)
8624                  (zerop children))
8625             ;; If this is "fetch-old-headered" and `invisible', then
8626             ;; we don't want this article.
8627             (and (eq gnus-fetch-old-headers 'invisible)
8628                  (gnus-summary-article-ancient-p number))
8629             ;; If this is a sparsely inserted article with no children,
8630             ;; we don't want it.
8631             (and (eq gnus-build-sparse-threads 'some)
8632                  (gnus-summary-article-sparse-p number)
8633                  (zerop children))
8634             ;; If we use expunging, and this article is really
8635             ;; low-scored, then we don't want this article.
8636             (when (and gnus-summary-expunge-below
8637                        (< (setq score
8638                                 (or (cdr (assq number gnus-newsgroup-scored))
8639                                     gnus-summary-default-score))
8640                           gnus-summary-expunge-below))
8641               ;; We increase the expunge-tally here, but that has
8642               ;; nothing to do with the limits, really.
8643               (incf gnus-newsgroup-expunged-tally)
8644               ;; We also mark as read here, if that's wanted.
8645               (when (and gnus-summary-mark-below
8646                          (< score gnus-summary-mark-below))
8647                 (setq gnus-newsgroup-unreads
8648                       (delq number gnus-newsgroup-unreads))
8649                 (if gnus-newsgroup-auto-expire
8650                     (push number gnus-newsgroup-expirable)
8651                   (push (cons number gnus-low-score-mark)
8652                         gnus-newsgroup-reads)))
8653               t)
8654             ;; Do the `display' group parameter.
8655             (and gnus-newsgroup-display
8656                  (not (funcall gnus-newsgroup-display)))
8657             ;; Check NoCeM things.
8658             (when (and gnus-use-nocem
8659                        (gnus-nocem-unwanted-article-p
8660                         (mail-header-id (car thread))))
8661               (setq gnus-newsgroup-unreads
8662                     (delq number gnus-newsgroup-unreads))
8663               t)))
8664           ;; Nope, invisible article.
8665           0
8666         ;; Ok, this article is to be visible, so we add it to the limit
8667         ;; and return 1.
8668         (push number gnus-newsgroup-limit)
8669         1))))
8670
8671 (defun gnus-expunge-thread (thread)
8672   "Mark all articles in THREAD as read."
8673   (let* ((number (mail-header-number (car thread))))
8674     (incf gnus-newsgroup-expunged-tally)
8675     ;; We also mark as read here, if that's wanted.
8676     (setq gnus-newsgroup-unreads
8677           (delq number gnus-newsgroup-unreads))
8678     (if gnus-newsgroup-auto-expire
8679         (push number gnus-newsgroup-expirable)
8680       (push (cons number gnus-low-score-mark)
8681             gnus-newsgroup-reads)))
8682   ;; Go recursively through all subthreads.
8683   (mapcar 'gnus-expunge-thread (cdr thread)))
8684
8685 ;; Summary article oriented commands
8686
8687 (defun gnus-summary-refer-parent-article (n)
8688   "Refer parent article N times.
8689 If N is negative, go to ancestor -N instead.
8690 The difference between N and the number of articles fetched is returned."
8691   (interactive "p")
8692   (let ((skip 1)
8693         error header ref)
8694     (when (not (natnump n))
8695       (setq skip (abs n)
8696             n 1))
8697     (while (and (> n 0)
8698                 (not error))
8699       (setq header (gnus-summary-article-header))
8700       (if (and (eq (mail-header-number header)
8701                    (cdr gnus-article-current))
8702                (equal gnus-newsgroup-name
8703                       (car gnus-article-current)))
8704           ;; If we try to find the parent of the currently
8705           ;; displayed article, then we take a look at the actual
8706           ;; References header, since this is slightly more
8707           ;; reliable than the References field we got from the
8708           ;; server.
8709           (save-excursion
8710             (set-buffer gnus-original-article-buffer)
8711             (nnheader-narrow-to-headers)
8712             (unless (setq ref (message-fetch-field "references"))
8713               (when (setq ref (message-fetch-field "in-reply-to"))
8714                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8715             (widen))
8716         (setq ref
8717               ;; It's not the current article, so we take a bet on
8718               ;; the value we got from the server.
8719               (mail-header-references header)))
8720       (if (and ref
8721                (not (equal ref "")))
8722           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8723             (gnus-message 1 "Couldn't find parent"))
8724         (gnus-message 1 "No references in article %d"
8725                       (gnus-summary-article-number))
8726         (setq error t))
8727       (decf n))
8728     (gnus-summary-position-point)
8729     n))
8730
8731 (defun gnus-summary-refer-references ()
8732   "Fetch all articles mentioned in the References header.
8733 Return the number of articles fetched."
8734   (interactive)
8735   (let ((ref (mail-header-references (gnus-summary-article-header)))
8736         (current (gnus-summary-article-number))
8737         (n 0))
8738     (if (or (not ref)
8739             (equal ref ""))
8740         (error "No References in the current article")
8741       ;; For each Message-ID in the References header...
8742       (while (string-match "<[^>]*>" ref)
8743         (incf n)
8744         ;; ... fetch that article.
8745         (gnus-summary-refer-article
8746          (prog1 (match-string 0 ref)
8747            (setq ref (substring ref (match-end 0))))))
8748       (gnus-summary-goto-subject current)
8749       (gnus-summary-position-point)
8750       n)))
8751
8752 (defun gnus-summary-refer-thread (&optional limit)
8753   "Fetch all articles in the current thread.
8754 If LIMIT (the numerical prefix), fetch that many old headers instead
8755 of what's specified by the `gnus-refer-thread-limit' variable."
8756   (interactive "P")
8757   (let ((id (mail-header-id (gnus-summary-article-header)))
8758         (limit (if limit (prefix-numeric-value limit)
8759                  gnus-refer-thread-limit)))
8760     (unless (eq gnus-fetch-old-headers 'invisible)
8761       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8762       ;; Retrieve the headers and read them in.
8763       (if (eq (if (numberp limit)
8764                   (gnus-retrieve-headers
8765                    (list (min
8766                           (+ (mail-header-number
8767                               (gnus-summary-article-header))
8768                              limit)
8769                           gnus-newsgroup-end))
8770                    gnus-newsgroup-name (* limit 2))
8771                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8772                 ;; headers.
8773                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8774                                        gnus-newsgroup-name limit))
8775               'nov)
8776           (gnus-build-all-threads)
8777         (error "Can't fetch thread from back ends that don't support NOV"))
8778       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8779     (gnus-summary-limit-include-thread id)))
8780
8781 (defun gnus-summary-refer-article (message-id)
8782   "Fetch an article specified by MESSAGE-ID."
8783   (interactive "sMessage-ID: ")
8784   (when (and (stringp message-id)
8785              (not (zerop (length message-id))))
8786     (setq message-id (gnus-replace-in-string message-id " " ""))
8787     ;; Construct the correct Message-ID if necessary.
8788     ;; Suggested by tale@pawl.rpi.edu.
8789     (unless (string-match "^<" message-id)
8790       (setq message-id (concat "<" message-id)))
8791     (unless (string-match ">$" message-id)
8792       (setq message-id (concat message-id ">")))
8793     ;; People often post MIDs from URLs, so unhex it:
8794     (unless (string-match "@" message-id)
8795       (setq message-id (gnus-url-unhex-string message-id)))
8796     (let* ((header (gnus-id-to-header message-id))
8797            (sparse (and header
8798                         (gnus-summary-article-sparse-p
8799                          (mail-header-number header))
8800                         (memq (mail-header-number header)
8801                               gnus-newsgroup-limit)))
8802            number)
8803       (cond
8804        ;; If the article is present in the buffer we just go to it.
8805        ((and header
8806              (or (not (gnus-summary-article-sparse-p
8807                        (mail-header-number header)))
8808                  sparse))
8809         (prog1
8810             (gnus-summary-goto-article
8811              (mail-header-number header) nil t)
8812           (when sparse
8813             (gnus-summary-update-article (mail-header-number header)))))
8814        (t
8815         ;; We fetch the article.
8816         (catch 'found
8817           (dolist (gnus-override-method (gnus-refer-article-methods))
8818             (when (and (gnus-check-server gnus-override-method)
8819                        ;; Fetch the header,
8820                        (setq number (gnus-summary-insert-subject message-id)))
8821               ;; and display the article.
8822               (gnus-summary-select-article nil nil nil number)
8823               (throw 'found t)))
8824           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8825
8826 (defun gnus-refer-article-methods ()
8827   "Return a list of referable methods."
8828   (cond
8829    ;; No method, so we default to current and native.
8830    ((null gnus-refer-article-method)
8831     (list gnus-current-select-method gnus-select-method))
8832    ;; Current.
8833    ((eq 'current gnus-refer-article-method)
8834     (list gnus-current-select-method))
8835    ;; List of select methods.
8836    ((not (and (symbolp (car gnus-refer-article-method))
8837               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8838     (let (out)
8839       (dolist (method gnus-refer-article-method)
8840         (push (if (eq 'current method)
8841                   gnus-current-select-method
8842                 method)
8843               out))
8844       (nreverse out)))
8845    ;; One single select method.
8846    (t
8847     (list gnus-refer-article-method))))
8848
8849 (defun gnus-summary-edit-parameters ()
8850   "Edit the group parameters of the current group."
8851   (interactive)
8852   (gnus-group-edit-group gnus-newsgroup-name 'params))
8853
8854 (defun gnus-summary-customize-parameters ()
8855   "Customize the group parameters of the current group."
8856   (interactive)
8857   (gnus-group-customize gnus-newsgroup-name))
8858
8859 (defun gnus-summary-enter-digest-group (&optional force)
8860   "Enter an nndoc group based on the current article.
8861 If FORCE, force a digest interpretation.  If not, try
8862 to guess what the document format is."
8863   (interactive "P")
8864   (let ((conf gnus-current-window-configuration))
8865     (save-window-excursion
8866       (save-excursion
8867         (let (gnus-article-prepare-hook
8868               gnus-display-mime-function
8869               gnus-break-pages)
8870           (gnus-summary-select-article))))
8871     (setq gnus-current-window-configuration conf)
8872     (let* ((name (format "%s-%d"
8873                          (gnus-group-prefixed-name
8874                           gnus-newsgroup-name (list 'nndoc ""))
8875                          (with-current-buffer gnus-summary-buffer
8876                            gnus-current-article)))
8877            (ogroup gnus-newsgroup-name)
8878            (params (append (gnus-info-params (gnus-get-info ogroup))
8879                            (list (cons 'to-group ogroup))
8880                            (list (cons 'parent-group ogroup))
8881                            (list (cons 'save-article-group ogroup))))
8882            (case-fold-search t)
8883            (buf (current-buffer))
8884            dig to-address)
8885       (save-excursion
8886         (set-buffer gnus-original-article-buffer)
8887         ;; Have the digest group inherit the main mail address of
8888         ;; the parent article.
8889         (when (setq to-address (or (gnus-fetch-field "reply-to")
8890                                    (gnus-fetch-field "from")))
8891           (setq params
8892                 (append
8893                  (list (cons 'to-address
8894                              (funcall gnus-decode-encoded-address-function
8895                                       to-address))))))
8896         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8897         (insert-buffer-substring gnus-original-article-buffer)
8898         ;; Remove lines that may lead nndoc to misinterpret the
8899         ;; document type.
8900         (narrow-to-region
8901          (goto-char (point-min))
8902          (or (search-forward "\n\n" nil t) (point)))
8903         (goto-char (point-min))
8904         (delete-matching-lines "^Path:\\|^From ")
8905         (widen))
8906       (unwind-protect
8907           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8908                     (gnus-newsgroup-ephemeral-ignored-charsets
8909                      gnus-newsgroup-ignored-charsets))
8910                 (gnus-group-read-ephemeral-group
8911                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8912                               (nndoc-article-type
8913                                ,(if force 'mbox 'guess)))
8914                  t nil nil nil
8915                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8916                                                         "ADAPT")))))
8917               ;; Make all postings to this group go to the parent group.
8918               (nconc (gnus-info-params (gnus-get-info name))
8919                      params)
8920             ;; Couldn't select this doc group.
8921             (switch-to-buffer buf)
8922             (gnus-set-global-variables)
8923             (gnus-configure-windows 'summary)
8924             (gnus-message 3 "Article couldn't be entered?"))
8925         (kill-buffer dig)))))
8926
8927 (defun gnus-summary-read-document (n)
8928   "Open a new group based on the current article(s).
8929 This will allow you to read digests and other similar
8930 documents as newsgroups.
8931 Obeys the standard process/prefix convention."
8932   (interactive "P")
8933   (let* ((ogroup gnus-newsgroup-name)
8934          (params (append (gnus-info-params (gnus-get-info ogroup))
8935                          (list (cons 'to-group ogroup))))
8936          group egroup groups vgroup)
8937     (dolist (article (gnus-summary-work-articles n))
8938       (setq group (format "%s-%d" gnus-newsgroup-name article))
8939       (gnus-summary-remove-process-mark article)
8940       (when (gnus-summary-display-article article)
8941         (save-excursion
8942           (with-temp-buffer
8943             (insert-buffer-substring gnus-original-article-buffer)
8944             ;; Remove some headers that may lead nndoc to make
8945             ;; the wrong guess.
8946             (message-narrow-to-head)
8947             (goto-char (point-min))
8948             (delete-matching-lines "^Path:\\|^From ")
8949             (widen)
8950             (if (setq egroup
8951                       (gnus-group-read-ephemeral-group
8952                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8953                                      (nndoc-article-type guess))
8954                        t nil t))
8955                 (progn
8956             ;; Make all postings to this group go to the parent group.
8957                   (nconc (gnus-info-params (gnus-get-info egroup))
8958                          params)
8959                   (push egroup groups))
8960               ;; Couldn't select this doc group.
8961               (gnus-error 3 "Article couldn't be entered"))))))
8962     ;; Now we have selected all the documents.
8963     (cond
8964      ((not groups)
8965       (error "None of the articles could be interpreted as documents"))
8966      ((gnus-group-read-ephemeral-group
8967        (setq vgroup (format
8968                      "nnvirtual:%s-%s" gnus-newsgroup-name
8969                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8970        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8971        t
8972        (cons (current-buffer) 'summary)))
8973      (t
8974       (error "Couldn't select virtual nndoc group")))))
8975
8976 (defun gnus-summary-isearch-article (&optional regexp-p)
8977   "Do incremental search forward on the current article.
8978 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8979   (interactive "P")
8980   (gnus-summary-select-article)
8981   (gnus-configure-windows 'article)
8982   (gnus-eval-in-buffer-window gnus-article-buffer
8983     (save-restriction
8984       (widen)
8985       (isearch-forward regexp-p))))
8986
8987 (defun gnus-summary-repeat-search-article-forward ()
8988   "Repeat the previous search forwards."
8989   (interactive)
8990   (unless gnus-last-search-regexp
8991     (error "No previous search"))
8992   (gnus-summary-search-article-forward gnus-last-search-regexp))
8993
8994 (defun gnus-summary-repeat-search-article-backward ()
8995   "Repeat the previous search backwards."
8996   (interactive)
8997   (unless gnus-last-search-regexp
8998     (error "No previous search"))
8999   (gnus-summary-search-article-forward gnus-last-search-regexp t))
9000
9001 (defun gnus-summary-search-article-forward (regexp &optional backward)
9002   "Search for an article containing REGEXP forward.
9003 If BACKWARD, search backward instead."
9004   (interactive
9005    (list (read-string
9006           (format "Search article %s (regexp%s): "
9007                   (if current-prefix-arg "backward" "forward")
9008                   (if gnus-last-search-regexp
9009                       (concat ", default " gnus-last-search-regexp)
9010                     "")))
9011          current-prefix-arg))
9012   (if (string-equal regexp "")
9013       (setq regexp (or gnus-last-search-regexp ""))
9014     (setq gnus-last-search-regexp regexp)
9015     (setq gnus-article-before-search gnus-current-article))
9016   ;; Intentionally set gnus-last-article.
9017   (setq gnus-last-article gnus-article-before-search)
9018   (let ((gnus-last-article gnus-last-article))
9019     (if (gnus-summary-search-article regexp backward)
9020         (gnus-summary-show-thread)
9021       (signal 'search-failed (list regexp)))))
9022
9023 (defun gnus-summary-search-article-backward (regexp)
9024   "Search for an article containing REGEXP backward."
9025   (interactive
9026    (list (read-string
9027           (format "Search article backward (regexp%s): "
9028                   (if gnus-last-search-regexp
9029                       (concat ", default " gnus-last-search-regexp)
9030                     "")))))
9031   (gnus-summary-search-article-forward regexp 'backward))
9032
9033 (defun gnus-summary-search-article (regexp &optional backward)
9034   "Search for an article containing REGEXP.
9035 Optional argument BACKWARD means do search for backward.
9036 `gnus-select-article-hook' is not called during the search."
9037   ;; We have to require this here to make sure that the following
9038   ;; dynamic binding isn't shadowed by autoloading.
9039   (require 'gnus-async)
9040   (require 'gnus-art)
9041   (let ((gnus-select-article-hook nil)  ;Disable hook.
9042         (gnus-article-prepare-hook nil)
9043         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
9044         (gnus-use-article-prefetch nil)
9045         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
9046         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
9047         (gnus-visual nil)
9048         (gnus-keep-backlog nil)
9049         (gnus-break-pages nil)
9050         (gnus-summary-display-arrow nil)
9051         (gnus-updated-mode-lines nil)
9052         (gnus-auto-center-summary nil)
9053         (sum (current-buffer))
9054         (gnus-display-mime-function nil)
9055         (found nil)
9056         point)
9057     (gnus-save-hidden-threads
9058       (gnus-summary-select-article)
9059       (set-buffer gnus-article-buffer)
9060       (goto-char (window-point (get-buffer-window (current-buffer))))
9061       (when backward
9062         (forward-line -1))
9063       (while (not found)
9064         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
9065         (if (if backward
9066                 (re-search-backward regexp nil t)
9067               (re-search-forward regexp nil t))
9068             ;; We found the regexp.
9069             (progn
9070               (setq found 'found)
9071               (beginning-of-line)
9072               (set-window-start
9073                (get-buffer-window (current-buffer))
9074                (point))
9075               (forward-line 1)
9076               (set-window-point
9077                (get-buffer-window (current-buffer))
9078                (point))
9079               (set-buffer sum)
9080               (setq point (point)))
9081           ;; We didn't find it, so we go to the next article.
9082           (set-buffer sum)
9083           (setq found 'not)
9084           (while (eq found 'not)
9085             (if (not (if backward (gnus-summary-find-prev)
9086                        (gnus-summary-find-next)))
9087                 ;; No more articles.
9088                 (setq found t)
9089               ;; Select the next article and adjust point.
9090               (unless (gnus-summary-article-sparse-p
9091                        (gnus-summary-article-number))
9092                 (setq found nil)
9093                 (gnus-summary-select-article)
9094                 (set-buffer gnus-article-buffer)
9095                 (widen)
9096                 (goto-char (if backward (point-max) (point-min))))))))
9097       (gnus-message 7 ""))
9098     ;; Return whether we found the regexp.
9099     (when (eq found 'found)
9100       (goto-char point)
9101       (gnus-summary-show-thread)
9102       (gnus-summary-goto-subject gnus-current-article)
9103       (gnus-summary-position-point)
9104       t)))
9105
9106 (defun gnus-find-matching-articles (header regexp)
9107   "Return a list of all articles that match REGEXP on HEADER.
9108 This search includes all articles in the current group that Gnus has
9109 fetched headers for, whether they are displayed or not."
9110   (let ((articles nil)
9111         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
9112         (case-fold-search t))
9113     (dolist (header gnus-newsgroup-headers)
9114       (when (string-match regexp (funcall func header))
9115         (push (mail-header-number header) articles)))
9116     (nreverse articles)))
9117
9118 (defun gnus-summary-find-matching (header regexp &optional backward unread
9119                                           not-case-fold not-matching)
9120   "Return a list of all articles that match REGEXP on HEADER.
9121 The search stars on the current article and goes forwards unless
9122 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
9123 If UNREAD is non-nil, only unread articles will
9124 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
9125 in the comparisons. If NOT-MATCHING, return a list of all articles that
9126 not match REGEXP on HEADER."
9127   (let ((case-fold-search (not not-case-fold))
9128         articles d func)
9129     (if (consp header)
9130         (if (eq (car header) 'extra)
9131             (setq func
9132                   `(lambda (h)
9133                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
9134                          "")))
9135           (error "%s is an invalid header" header))
9136       (unless (fboundp (intern (concat "mail-header-" header)))
9137         (error "%s is not a valid header" header))
9138       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
9139     (dolist (d (if (eq backward 'all)
9140                    gnus-newsgroup-data
9141                  (gnus-data-find-list
9142                   (gnus-summary-article-number)
9143                   (gnus-data-list backward))))
9144       (when (and (or (not unread)       ; We want all articles...
9145                      (gnus-data-unread-p d)) ; Or just unreads.
9146                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
9147                  (if not-matching
9148                      (not (string-match
9149                            regexp
9150                            (funcall func (gnus-data-header d))))
9151                    (string-match regexp
9152                                  (funcall func (gnus-data-header d)))))
9153         (push (gnus-data-number d) articles))) ; Success!
9154     (nreverse articles)))
9155
9156 (defun gnus-summary-execute-command (header regexp command &optional backward)
9157   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
9158 If HEADER is an empty string (or nil), the match is done on the entire
9159 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
9160   (interactive
9161    (list (let ((completion-ignore-case t))
9162            (completing-read
9163             "Header name: "
9164             (mapcar (lambda (header) (list (format "%s" header)))
9165                     (append
9166                      '("Number" "Subject" "From" "Lines" "Date"
9167                        "Message-ID" "Xref" "References" "Body")
9168                      gnus-extra-headers))
9169             nil 'require-match))
9170          (read-string "Regexp: ")
9171          (read-key-sequence "Command: ")
9172          current-prefix-arg))
9173   (when (equal header "Body")
9174     (setq header ""))
9175   ;; Hidden thread subtrees must be searched as well.
9176   (gnus-summary-show-all-threads)
9177   ;; We don't want to change current point nor window configuration.
9178   (save-excursion
9179     (save-window-excursion
9180       (let (gnus-visual
9181             gnus-treat-strip-trailing-blank-lines
9182             gnus-treat-strip-leading-blank-lines
9183             gnus-treat-strip-multiple-blank-lines
9184             gnus-treat-hide-boring-headers
9185             gnus-treat-fold-newsgroups
9186             gnus-article-prepare-hook)
9187         (gnus-message 6 "Executing %s..." (key-description command))
9188         ;; We'd like to execute COMMAND interactively so as to give arguments.
9189         (gnus-execute header regexp
9190                       `(call-interactively ',(key-binding command))
9191                       backward)
9192         (gnus-message 6 "Executing %s...done" (key-description command))))))
9193
9194 (defun gnus-summary-beginning-of-article ()
9195   "Scroll the article back to the beginning."
9196   (interactive)
9197   (gnus-summary-select-article)
9198   (gnus-configure-windows 'article)
9199   (gnus-eval-in-buffer-window gnus-article-buffer
9200     (widen)
9201     (goto-char (point-min))
9202     (when gnus-break-pages
9203       (gnus-narrow-to-page))))
9204
9205 (defun gnus-summary-end-of-article ()
9206   "Scroll to the end of the article."
9207   (interactive)
9208   (gnus-summary-select-article)
9209   (gnus-configure-windows 'article)
9210   (gnus-eval-in-buffer-window gnus-article-buffer
9211     (widen)
9212     (goto-char (point-max))
9213     (recenter -3)
9214     (when gnus-break-pages
9215       (when (re-search-backward page-delimiter nil t)
9216         (narrow-to-region (match-end 0) (point-max)))
9217       (gnus-narrow-to-page))))
9218
9219 (defun gnus-summary-print-truncate-and-quote (string &optional len)
9220   "Truncate to LEN and quote all \"(\"'s in STRING."
9221   (gnus-replace-in-string (if (and len (> (length string) len))
9222                               (substring string 0 len)
9223                             string)
9224                           "[()]" "\\\\\\&"))
9225
9226 (defun gnus-summary-print-article (&optional filename n)
9227   "Generate and print a PostScript image of the process-marked (mail) articles.
9228
9229 If used interactively, print the current article if none are
9230 process-marked.  With prefix arg, prompt the user for the name of the
9231 file to save in.
9232
9233 When used from Lisp, accept two optional args FILENAME and N.  N means
9234 to print the next N articles.  If N is negative, print the N previous
9235 articles.  If N is nil and articles have been marked with the process
9236 mark, print these instead.
9237
9238 If the optional first argument FILENAME is nil, send the image to the
9239 printer.  If FILENAME is a string, save the PostScript image in a file with
9240 that name.  If FILENAME is a number, prompt the user for the name of the file
9241 to save in."
9242   (interactive (list (ps-print-preprint current-prefix-arg)))
9243   (dolist (article (gnus-summary-work-articles n))
9244     (gnus-summary-select-article nil nil 'pseudo article)
9245     (gnus-eval-in-buffer-window gnus-article-buffer
9246       (gnus-print-buffer))
9247     (gnus-summary-remove-process-mark article))
9248   (ps-despool filename))
9249
9250 (defun gnus-print-buffer ()
9251   (let ((buffer (generate-new-buffer " *print*")))
9252     (unwind-protect
9253         (progn
9254           (copy-to-buffer buffer (point-min) (point-max))
9255           (set-buffer buffer)
9256           (gnus-remove-text-with-property 'gnus-decoration)
9257           (when (gnus-visual-p 'article-highlight 'highlight)
9258             ;; Copy-to-buffer doesn't copy overlay.  So redo
9259             ;; highlight.
9260             (let ((gnus-article-buffer buffer))
9261               (gnus-article-highlight-citation t)
9262               (gnus-article-highlight-signature)
9263               (gnus-article-emphasize)
9264               (gnus-article-delete-invisible-text)))
9265           (let ((ps-left-header
9266                  (list
9267                   (concat "("
9268                           (gnus-summary-print-truncate-and-quote
9269                            (mail-header-subject gnus-current-headers)
9270                            66) ")")
9271                   (concat "("
9272                           (gnus-summary-print-truncate-and-quote
9273                            (mail-header-from gnus-current-headers)
9274                            45) ")")))
9275                 (ps-right-header
9276                  (list
9277                   "/pagenumberstring load"
9278                   (concat "("
9279                           (mail-header-date gnus-current-headers) ")"))))
9280             (gnus-run-hooks 'gnus-ps-print-hook)
9281             (save-excursion
9282               (if window-system
9283                   (ps-spool-buffer-with-faces)
9284                 (ps-spool-buffer)))))
9285       (kill-buffer buffer))))
9286
9287 (defun gnus-summary-show-article (&optional arg)
9288   "Force redisplaying of the current article.
9289 If ARG (the prefix) is a number, show the article with the charset
9290 defined in `gnus-summary-show-article-charset-alist', or the charset
9291 input.
9292 If ARG (the prefix) is non-nil and not a number, show the raw article
9293 without any article massaging functions being run.  Normally, the key
9294 strokes are `C-u g'."
9295   (interactive "P")
9296   (cond
9297    ((numberp arg)
9298     (gnus-summary-show-article t)
9299     (let ((gnus-newsgroup-charset
9300            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
9301                (mm-read-coding-system
9302                 "View as charset: " ;; actually it is coding system.
9303                 (with-current-buffer gnus-article-buffer
9304                   (mm-detect-coding-region (point) (point-max))))))
9305           (gnus-newsgroup-ignored-charsets 'gnus-all))
9306       (gnus-summary-select-article nil 'force)
9307       (let ((deps gnus-newsgroup-dependencies)
9308             head header lines)
9309         (save-excursion
9310           (set-buffer gnus-original-article-buffer)
9311           (save-restriction
9312             (message-narrow-to-head)
9313             (setq head (buffer-string))
9314             (goto-char (point-min))
9315             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
9316               (goto-char (point-max))
9317               (widen)
9318               (setq lines (1- (count-lines (point) (point-max))))))
9319           (with-temp-buffer
9320             (insert (format "211 %d Article retrieved.\n"
9321                             (cdr gnus-article-current)))
9322             (insert head)
9323             (if lines (insert (format "Lines: %d\n" lines)))
9324             (insert ".\n")
9325             (let ((nntp-server-buffer (current-buffer)))
9326               (setq header (car (gnus-get-newsgroup-headers deps t))))))
9327         (gnus-data-set-header
9328          (gnus-data-find (cdr gnus-article-current))
9329          header)
9330         (gnus-summary-update-article-line
9331          (cdr gnus-article-current) header)
9332         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9333           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
9334    ((not arg)
9335     ;; Select the article the normal way.
9336     (gnus-summary-select-article nil 'force))
9337    (t
9338     ;; We have to require this here to make sure that the following
9339     ;; dynamic binding isn't shadowed by autoloading.
9340     (require 'gnus-async)
9341     (require 'gnus-art)
9342     ;; Bind the article treatment functions to nil.
9343     (let ((gnus-have-all-headers t)
9344           gnus-article-prepare-hook
9345           gnus-article-decode-hook
9346           gnus-display-mime-function
9347           gnus-break-pages)
9348       ;; Destroy any MIME parts.
9349       (when (gnus-buffer-live-p gnus-article-buffer)
9350         (save-excursion
9351           (set-buffer gnus-article-buffer)
9352           (mm-destroy-parts gnus-article-mime-handles)
9353           ;; Set it to nil for safety reason.
9354           (setq gnus-article-mime-handle-alist nil)
9355           (setq gnus-article-mime-handles nil)))
9356       (gnus-summary-select-article nil 'force))))
9357   (gnus-summary-goto-subject gnus-current-article)
9358   (gnus-summary-position-point))
9359
9360 (defun gnus-summary-show-raw-article ()
9361   "Show the raw article without any article massaging functions being run."
9362   (interactive)
9363   (gnus-summary-show-article t))
9364
9365 (defun gnus-summary-verbose-headers (&optional arg)
9366   "Toggle permanent full header display.
9367 If ARG is a positive number, turn header display on.
9368 If ARG is a negative number, turn header display off."
9369   (interactive "P")
9370   (setq gnus-show-all-headers
9371         (cond ((or (not (numberp arg))
9372                    (zerop arg))
9373                (not gnus-show-all-headers))
9374               ((natnump arg)
9375                t)))
9376   (gnus-summary-show-article))
9377
9378 (defun gnus-summary-toggle-header (&optional arg)
9379   "Show the headers if they are hidden, or hide them if they are shown.
9380 If ARG is a positive number, show the entire header.
9381 If ARG is a negative number, hide the unwanted header lines."
9382   (interactive "P")
9383   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
9384                      (get-buffer-window gnus-article-buffer t))))
9385     (with-current-buffer gnus-article-buffer
9386       (widen)
9387       (article-narrow-to-head)
9388       (let* ((buffer-read-only nil)
9389              (inhibit-point-motion-hooks t)
9390              (hidden (if (numberp arg)
9391                          (>= arg 0)
9392                        (or (not (looking-at "[^ \t\n]+:"))
9393                            (gnus-article-hidden-text-p 'headers))))
9394              s e)
9395         (delete-region (point-min) (point-max))
9396         (with-current-buffer gnus-original-article-buffer
9397           (goto-char (setq s (point-min)))
9398           (setq e (if (search-forward "\n\n" nil t)
9399                       (1- (point))
9400                     (point-max))))
9401         (insert-buffer-substring gnus-original-article-buffer s e)
9402         (run-hooks 'gnus-article-decode-hook)
9403         (if hidden
9404             (let ((gnus-treat-hide-headers nil)
9405                   (gnus-treat-hide-boring-headers nil))
9406               (gnus-delete-wash-type 'headers)
9407               (gnus-treat-article 'head))
9408           (gnus-treat-article 'head))
9409         (widen)
9410         (if window
9411             (set-window-start window (goto-char (point-min))))
9412         (if gnus-break-pages
9413             (gnus-narrow-to-page)
9414           (when (gnus-visual-p 'page-marker)
9415             (let ((buffer-read-only nil))
9416               (gnus-remove-text-with-property 'gnus-prev)
9417               (gnus-remove-text-with-property 'gnus-next))))
9418         (gnus-set-mode-line 'article)))))
9419
9420 (defun gnus-summary-show-all-headers ()
9421   "Make all header lines visible."
9422   (interactive)
9423   (gnus-summary-toggle-header 1))
9424
9425 (defun gnus-summary-caesar-message (&optional arg)
9426   "Caesar rotate the current article by 13.
9427 With a non-numerical prefix, also rotate headers.  A numerical
9428 prefix specifies how many places to rotate each letter forward."
9429   (interactive "P")
9430   (gnus-summary-select-article)
9431   (let ((mail-header-separator ""))
9432     (gnus-eval-in-buffer-window gnus-article-buffer
9433       (save-restriction
9434         (widen)
9435         (let ((start (window-start))
9436               buffer-read-only)
9437           (if (equal arg '(4))
9438               (message-caesar-buffer-body nil t)
9439             (message-caesar-buffer-body arg))
9440           (set-window-start (get-buffer-window (current-buffer)) start)))))
9441   ;; Create buttons and stuff...
9442   (gnus-treat-article nil))
9443
9444 (defun gnus-summary-idna-message (&optional arg)
9445   "Decode IDNA encoded domain names in the current articles.
9446 IDNA encoded domain names looks like `xn--bar'.  If a string
9447 remain unencoded after running this function, it is likely an
9448 invalid IDNA string (`xn--bar' is invalid).
9449
9450 You must have GNU Libidn (`http://www.gnu.org/software/libidn/')
9451 installed for this command to work."
9452   (interactive "P")
9453   (if (not (and (condition-case nil (require 'idna)
9454                   (file-error))
9455                 (mm-coding-system-p 'utf-8)
9456                 (executable-find (symbol-value 'idna-program))))
9457       (gnus-message
9458        5 "GNU Libidn not installed properly (`idn' or `idna.el' missing)")
9459     (gnus-summary-select-article)
9460     (let ((mail-header-separator ""))
9461       (gnus-eval-in-buffer-window gnus-article-buffer
9462         (save-restriction
9463           (widen)
9464           (let ((start (window-start))
9465                 buffer-read-only)
9466             (while (re-search-forward "\\(xn--[-0-9a-z]+\\)" nil t)
9467               (replace-match (idna-to-unicode (match-string 1))))
9468             (set-window-start (get-buffer-window (current-buffer)) start)))))))
9469
9470 (defun gnus-summary-morse-message (&optional arg)
9471   "Morse decode the current article."
9472   (interactive "P")
9473   (gnus-summary-select-article)
9474   (let ((mail-header-separator ""))
9475     (gnus-eval-in-buffer-window gnus-article-buffer
9476       (save-excursion
9477         (save-restriction
9478           (widen)
9479           (let ((pos (window-start))
9480                 buffer-read-only)
9481             (goto-char (point-min))
9482             (when (message-goto-body)
9483               (gnus-narrow-to-body))
9484             (goto-char (point-min))
9485             (while (search-forward "·" (point-max) t)
9486               (replace-match "."))
9487             (unmorse-region (point-min) (point-max))
9488             (widen)
9489             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9490
9491 (defun gnus-summary-stop-page-breaking ()
9492   "Stop page breaking in the current article."
9493   (interactive)
9494   (gnus-summary-select-article)
9495   (gnus-eval-in-buffer-window gnus-article-buffer
9496     (widen)
9497     (when (gnus-visual-p 'page-marker)
9498       (let ((buffer-read-only nil))
9499         (gnus-remove-text-with-property 'gnus-prev)
9500         (gnus-remove-text-with-property 'gnus-next))
9501       (setq gnus-page-broken nil))))
9502
9503 (defun gnus-summary-move-article (&optional n to-newsgroup
9504                                             select-method action)
9505   "Move the current article to a different newsgroup.
9506 If N is a positive number, move the N next articles.
9507 If N is a negative number, move the N previous articles.
9508 If N is nil and any articles have been marked with the process mark,
9509 move those articles instead.
9510 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9511 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9512 re-spool using this method.
9513
9514 When called interactively with TO-NEWSGROUP being nil, the value of
9515 the variable `gnus-move-split-methods' is used for finding a default
9516 for the target newsgroup.
9517
9518 For this function to work, both the current newsgroup and the
9519 newsgroup that you want to move to have to support the `request-move'
9520 and `request-accept' functions.
9521
9522 ACTION can be either `move' (the default), `crosspost' or `copy'."
9523   (interactive "P")
9524   (unless action
9525     (setq action 'move))
9526   ;; Check whether the source group supports the required functions.
9527   (cond ((and (eq action 'move)
9528               (not (gnus-check-backend-function
9529                     'request-move-article gnus-newsgroup-name)))
9530          (error "The current group does not support article moving"))
9531         ((and (eq action 'crosspost)
9532               (not (gnus-check-backend-function
9533                     'request-replace-article gnus-newsgroup-name)))
9534          (error "The current group does not support article editing")))
9535   (let ((articles (gnus-summary-work-articles n))
9536         (prefix (if (gnus-check-backend-function
9537                      'request-move-article gnus-newsgroup-name)
9538                     (funcall gnus-move-group-prefix-function
9539                              gnus-newsgroup-name)
9540                   ""))
9541         (names '((move "Move" "Moving")
9542                  (copy "Copy" "Copying")
9543                  (crosspost "Crosspost" "Crossposting")))
9544         (copy-buf (save-excursion
9545                     (nnheader-set-temp-buffer " *copy article*")))
9546         art-group to-method new-xref article to-groups
9547         articles-to-update-marks encoded)
9548     (unless (assq action names)
9549       (error "Unknown action %s" action))
9550     ;; Read the newsgroup name.
9551     (when (and (not to-newsgroup)
9552                (not select-method))
9553       (if (and gnus-move-split-methods
9554                (not
9555                 (and (memq gnus-current-article articles)
9556                      (gnus-buffer-live-p gnus-original-article-buffer))))
9557           ;; When `gnus-move-split-methods' is non-nil, we have to
9558           ;; select an article to give `gnus-read-move-group-name' an
9559           ;; opportunity to suggest an appropriate default.  However,
9560           ;; we needn't render or mark the article.
9561           (let ((gnus-display-mime-function nil)
9562                 (gnus-article-prepare-hook nil)
9563                 (gnus-mark-article-hook nil))
9564             (gnus-summary-select-article nil nil nil (car articles))))
9565       (setq to-newsgroup (gnus-read-move-group-name
9566                           (cadr (assq action names))
9567                           (symbol-value
9568                            (intern (format "gnus-current-%s-group" action)))
9569                           articles prefix)
9570             encoded to-newsgroup
9571             to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
9572       (set (intern (format "gnus-current-%s-group" action))
9573            (mm-decode-coding-string
9574             to-newsgroup
9575             (gnus-group-name-charset to-method to-newsgroup))))
9576     (unless to-method
9577       (setq to-method (or select-method
9578                           (gnus-server-to-method
9579                            (gnus-group-method to-newsgroup)))))
9580     (setq to-newsgroup
9581           (or encoded
9582               (and to-newsgroup
9583                    (mm-encode-coding-string
9584                     to-newsgroup
9585                     (gnus-group-name-charset to-method to-newsgroup)))))
9586     ;; Check the method we are to move this article to...
9587     (unless (gnus-check-backend-function
9588              'request-accept-article (car to-method))
9589       (error "%s does not support article copying" (car to-method)))
9590     (unless (gnus-check-server to-method)
9591       (error "Can't open server %s" (car to-method)))
9592     (gnus-message 6 "%s to %s: %s..."
9593                   (caddr (assq action names))
9594                   (or (car select-method)
9595                       (gnus-group-decoded-name to-newsgroup))
9596                   articles)
9597     (while articles
9598       (setq article (pop articles))
9599       (setq
9600        art-group
9601        (cond
9602         ;; Move the article.
9603         ((eq action 'move)
9604          ;; Remove this article from future suppression.
9605          (gnus-dup-unsuppress-article article)
9606          (let* ((from-method (gnus-find-method-for-group
9607                               gnus-newsgroup-name))
9608                 (to-method (or select-method
9609                                (gnus-find-method-for-group to-newsgroup)))
9610                 (move-is-internal (gnus-method-equal from-method to-method)))
9611            (gnus-request-move-article
9612             article                     ; Article to move
9613             gnus-newsgroup-name         ; From newsgroup
9614             (nth 1 (gnus-find-method-for-group
9615                     gnus-newsgroup-name)) ; Server
9616             (list 'gnus-request-accept-article
9617                   to-newsgroup (list 'quote select-method)
9618                   (not articles) t)     ; Accept form
9619             (not articles)              ; Only save nov last time
9620             move-is-internal)))         ; is this move internal?
9621         ;; Copy the article.
9622         ((eq action 'copy)
9623          (save-excursion
9624            (set-buffer copy-buf)
9625            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9626              (save-restriction
9627                (nnheader-narrow-to-headers)
9628                (dolist (hdr gnus-copy-article-ignored-headers)
9629                  (message-remove-header hdr t)))
9630              (gnus-request-accept-article
9631               to-newsgroup select-method (not articles) t))))
9632         ;; Crosspost the article.
9633         ((eq action 'crosspost)
9634          (let ((xref (message-tokenize-header
9635                       (mail-header-xref (gnus-summary-article-header article))
9636                       " ")))
9637            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9638                                   ":" (number-to-string article)))
9639            (unless xref
9640              (setq xref (list (system-name))))
9641            (setq new-xref
9642                  (concat
9643                   (mapconcat 'identity
9644                              (delete "Xref:" (delete new-xref xref))
9645                              " ")
9646                   " " new-xref))
9647            (save-excursion
9648              (set-buffer copy-buf)
9649              ;; First put the article in the destination group.
9650              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9651              (when (consp (setq art-group
9652                                 (gnus-request-accept-article
9653                                  to-newsgroup select-method (not articles) t)))
9654                (setq new-xref (concat new-xref " " (car art-group)
9655                                       ":"
9656                                       (number-to-string (cdr art-group))))
9657                ;; Now we have the new Xrefs header, so we insert
9658                ;; it and replace the new article.
9659                (nnheader-replace-header "Xref" new-xref)
9660                (gnus-request-replace-article
9661                 (cdr art-group) to-newsgroup (current-buffer) t)
9662                art-group))))))
9663       (cond
9664        ((not art-group)
9665         (gnus-message 1 "Couldn't %s article %s: %s"
9666                       (cadr (assq action names)) article
9667                       (nnheader-get-report (car to-method))))
9668        ((eq art-group 'junk)
9669         (when (eq action 'move)
9670           (gnus-summary-mark-article article gnus-canceled-mark)
9671           (gnus-message 4 "Deleted article %s" article)
9672           ;; run the delete hook
9673           (run-hook-with-args 'gnus-summary-article-delete-hook
9674                               action
9675                               (gnus-data-header
9676                                (assoc article (gnus-data-list nil)))
9677                               gnus-newsgroup-name nil
9678                               select-method)))
9679        (t
9680         (let* ((pto-group (gnus-group-prefixed-name
9681                            (car art-group) to-method))
9682                (info (gnus-get-info pto-group))
9683                (to-group (gnus-info-group info))
9684                to-marks)
9685           ;; Update the group that has been moved to.
9686           (when (and info
9687                      (memq action '(move copy)))
9688             (unless (member to-group to-groups)
9689               (push to-group to-groups))
9690
9691             (unless (memq article gnus-newsgroup-unreads)
9692               (push 'read to-marks)
9693               (gnus-info-set-read
9694                info (gnus-add-to-range (gnus-info-read info)
9695                                        (list (cdr art-group)))))
9696
9697             ;; See whether the article is to be put in the cache.
9698             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9699                              gnus-article-mark-lists
9700                            (delete '(expirable . expire)
9701                                    (copy-sequence gnus-article-mark-lists))))
9702                   (to-article (cdr art-group)))
9703
9704               ;; Enter the article into the cache in the new group,
9705               ;; if that is required.
9706               (when gnus-use-cache
9707                 (gnus-cache-possibly-enter-article
9708                  to-group to-article
9709                  (memq article gnus-newsgroup-marked)
9710                  (memq article gnus-newsgroup-dormant)
9711                  (memq article gnus-newsgroup-unreads)))
9712
9713               (when gnus-preserve-marks
9714                 ;; Copy any marks over to the new group.
9715                 (when (and (equal to-group gnus-newsgroup-name)
9716                            (not (memq article gnus-newsgroup-unreads)))
9717                   ;; Mark this article as read in this group.
9718                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9719                   (setcdr (gnus-active to-group) to-article)
9720                   (setcdr gnus-newsgroup-active to-article))
9721
9722                 (while marks
9723                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9724                     (when (memq article (symbol-value
9725                                          (intern (format "gnus-newsgroup-%s"
9726                                                          (caar marks)))))
9727                       (push (cdar marks) to-marks)
9728                       ;; If the other group is the same as this group,
9729                       ;; then we have to add the mark to the list.
9730                       (when (equal to-group gnus-newsgroup-name)
9731                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9732                              (cons to-article
9733                                    (symbol-value
9734                                     (intern (format "gnus-newsgroup-%s"
9735                                                     (caar marks)))))))
9736                       ;; Copy the marks to other group.
9737                       (gnus-add-marked-articles
9738                        to-group (cdar marks) (list to-article) info)))
9739                   (setq marks (cdr marks)))
9740
9741                 (gnus-request-set-mark
9742                  to-group (list (list (list to-article) 'add to-marks))))
9743
9744               (gnus-dribble-enter
9745                (concat "(gnus-group-set-info '"
9746                        (gnus-prin1-to-string (gnus-get-info to-group))
9747                        ")"))))
9748
9749           ;; Update the Xref header in this article to point to
9750           ;; the new crossposted article we have just created.
9751           (when (eq action 'crosspost)
9752             (save-excursion
9753               (set-buffer copy-buf)
9754               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9755               (nnheader-replace-header "Xref" new-xref)
9756               (gnus-request-replace-article
9757                article gnus-newsgroup-name (current-buffer) t)))
9758
9759           ;; run the move/copy/crosspost/respool hook
9760           (run-hook-with-args 'gnus-summary-article-move-hook
9761                               action
9762                               (gnus-data-header
9763                                (assoc article (gnus-data-list nil)))
9764                               gnus-newsgroup-name
9765                               to-newsgroup
9766                               select-method))
9767
9768         ;;;!!!Why is this necessary?
9769         (set-buffer gnus-summary-buffer)
9770
9771         (gnus-summary-goto-subject article)
9772         (when (eq action 'move)
9773           (gnus-summary-mark-article article gnus-canceled-mark))))
9774       (push article articles-to-update-marks))
9775
9776     (apply 'gnus-summary-remove-process-mark articles-to-update-marks)
9777     ;; Re-activate all groups that have been moved to.
9778     (save-excursion
9779       (set-buffer gnus-group-buffer)
9780       (let ((gnus-group-marked to-groups))
9781         (gnus-group-get-new-news-this-group nil t)))
9782
9783     (gnus-kill-buffer copy-buf)
9784     (gnus-summary-position-point)
9785     (gnus-set-mode-line 'summary)))
9786
9787 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9788   "Copy the current article to some other group.
9789 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9790 When called interactively, if TO-NEWSGROUP is nil, use the value of
9791 the variable `gnus-move-split-methods' for finding a default target
9792 newsgroup.
9793 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9794 re-spool using this method."
9795   (interactive "P")
9796   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9797
9798 (defun gnus-summary-crosspost-article (&optional n)
9799   "Crosspost the current article to some other group."
9800   (interactive "P")
9801   (gnus-summary-move-article n nil nil 'crosspost))
9802
9803 (defcustom gnus-summary-respool-default-method nil
9804   "Default method type for respooling an article.
9805 If nil, use to the current newsgroup method."
9806   :type 'symbol
9807   :group 'gnus-summary-mail)
9808
9809 (defcustom gnus-summary-display-while-building nil
9810   "If non-nil, show and update the summary buffer as it's being built.
9811 If the value is t, update the buffer after every line is inserted.  If
9812 the value is an integer (N), update the display every N lines."
9813   :version "22.1"
9814   :group 'gnus-thread
9815   :type '(choice (const :tag "off" nil)
9816                  number
9817                  (const :tag "frequently" t)))
9818
9819 (defun gnus-summary-respool-article (&optional n method)
9820   "Respool the current article.
9821 The article will be squeezed through the mail spooling process again,
9822 which means that it will be put in some mail newsgroup or other
9823 depending on `nnmail-split-methods'.
9824 If N is a positive number, respool the N next articles.
9825 If N is a negative number, respool the N previous articles.
9826 If N is nil and any articles have been marked with the process mark,
9827 respool those articles instead.
9828
9829 Respooling can be done both from mail groups and \"real\" newsgroups.
9830 In the former case, the articles in question will be moved from the
9831 current group into whatever groups they are destined to.  In the
9832 latter case, they will be copied into the relevant groups."
9833   (interactive
9834    (list current-prefix-arg
9835          (let* ((methods (gnus-methods-using 'respool))
9836                 (methname
9837                  (symbol-name (or gnus-summary-respool-default-method
9838                                   (car (gnus-find-method-for-group
9839                                         gnus-newsgroup-name)))))
9840                 (method
9841                  (gnus-completing-read-with-default
9842                   methname "Backend to use when respooling"
9843                   methods nil t nil 'gnus-mail-method-history))
9844                 ms)
9845            (cond
9846             ((zerop (length (setq ms (gnus-servers-using-backend
9847                                       (intern method)))))
9848              (list (intern method) ""))
9849             ((= 1 (length ms))
9850              (car ms))
9851             (t
9852              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9853                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9854                            ms-alist))))))))
9855   (unless method
9856     (error "No method given for respooling"))
9857   (if (assoc (symbol-name
9858               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9859              (gnus-methods-using 'respool))
9860       (gnus-summary-move-article n nil method)
9861     (gnus-summary-copy-article n nil method)))
9862
9863 (defun gnus-summary-import-article (file &optional edit)
9864   "Import an arbitrary file into a mail newsgroup."
9865   (interactive "fImport file: \nP")
9866   (let ((group gnus-newsgroup-name)
9867         (now (current-time))
9868         atts lines group-art)
9869     (unless (gnus-check-backend-function 'request-accept-article group)
9870       (error "%s does not support article importing" group))
9871     (or (file-readable-p file)
9872         (not (file-regular-p file))
9873         (error "Can't read %s" file))
9874     (save-excursion
9875       (set-buffer (gnus-get-buffer-create " *import file*"))
9876       (erase-buffer)
9877       (nnheader-insert-file-contents file)
9878       (goto-char (point-min))
9879       (if (nnheader-article-p)
9880           (save-restriction
9881             (goto-char (point-min))
9882             (search-forward "\n\n" nil t)
9883             (narrow-to-region (point-min) (1- (point)))
9884             (goto-char (point-min))
9885             (unless (re-search-forward "^date:" nil t)
9886               (goto-char (point-max))
9887               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9888        ;; This doesn't look like an article, so we fudge some headers.
9889         (setq atts (file-attributes file)
9890               lines (count-lines (point-min) (point-max)))
9891         (insert "From: " (read-string "From: ") "\n"
9892                 "Subject: " (read-string "Subject: ") "\n"
9893                 "Date: " (message-make-date (nth 5 atts)) "\n"
9894                 "Message-ID: " (message-make-message-id) "\n"
9895                 "Lines: " (int-to-string lines) "\n"
9896                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9897       (setq group-art (gnus-request-accept-article group nil t))
9898       (kill-buffer (current-buffer)))
9899     (setq gnus-newsgroup-active (gnus-activate-group group))
9900     (forward-line 1)
9901     (gnus-summary-goto-article (cdr group-art) nil t)
9902     (when edit
9903       (gnus-summary-edit-article))))
9904
9905 (defun gnus-summary-create-article ()
9906   "Create an article in a mail newsgroup."
9907   (interactive)
9908   (let ((group gnus-newsgroup-name)
9909         (now (current-time))
9910         group-art)
9911     (unless (gnus-check-backend-function 'request-accept-article group)
9912       (error "%s does not support article importing" group))
9913     (save-excursion
9914       (set-buffer (gnus-get-buffer-create " *import file*"))
9915       (erase-buffer)
9916       (goto-char (point-min))
9917       ;; This doesn't look like an article, so we fudge some headers.
9918       (insert "From: " (read-string "From: ") "\n"
9919               "Subject: " (read-string "Subject: ") "\n"
9920               "Date: " (message-make-date now) "\n"
9921               "Message-ID: " (message-make-message-id) "\n")
9922       (setq group-art (gnus-request-accept-article group nil t))
9923       (kill-buffer (current-buffer)))
9924     (setq gnus-newsgroup-active (gnus-activate-group group))
9925     (forward-line 1)
9926     (gnus-summary-goto-article (cdr group-art) nil t)
9927     (gnus-summary-edit-article)))
9928
9929 (defun gnus-summary-article-posted-p ()
9930   "Say whether the current (mail) article is available from news as well.
9931 This will be the case if the article has both been mailed and posted."
9932   (interactive)
9933   (let ((id (mail-header-references (gnus-summary-article-header)))
9934         (gnus-override-method (car (gnus-refer-article-methods))))
9935     (if (gnus-request-head id "")
9936         (gnus-message 2 "The current message was found on %s"
9937                       gnus-override-method)
9938       (gnus-message 2 "The current message couldn't be found on %s"
9939                     gnus-override-method)
9940       nil)))
9941
9942 (defun gnus-summary-expire-articles (&optional now)
9943   "Expire all articles that are marked as expirable in the current group."
9944   (interactive)
9945   (when (and (not gnus-group-is-exiting-without-update-p)
9946              (gnus-check-backend-function
9947               'request-expire-articles gnus-newsgroup-name))
9948     ;; This backend supports expiry.
9949     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9950            (expirable (if total
9951                           (progn
9952                             ;; We need to update the info for
9953                             ;; this group for `gnus-list-of-read-articles'
9954                             ;; to give us the right answer.
9955                             (gnus-run-hooks 'gnus-exit-group-hook)
9956                             (gnus-summary-update-info)
9957                             (gnus-list-of-read-articles gnus-newsgroup-name))
9958                         (setq gnus-newsgroup-expirable
9959                               (sort gnus-newsgroup-expirable '<))))
9960            (expiry-wait (if now 'immediate
9961                           (gnus-group-find-parameter
9962                            gnus-newsgroup-name 'expiry-wait)))
9963            (nnmail-expiry-target
9964             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9965                 nnmail-expiry-target))
9966            es)
9967       (when expirable
9968         ;; There are expirable articles in this group, so we run them
9969         ;; through the expiry process.
9970         (gnus-message 6 "Expiring articles...")
9971         (unless (gnus-check-group gnus-newsgroup-name)
9972           (error "Can't open server for %s" gnus-newsgroup-name))
9973         ;; The list of articles that weren't expired is returned.
9974         (save-excursion
9975           (if expiry-wait
9976               (let ((nnmail-expiry-wait-function nil)
9977                     (nnmail-expiry-wait expiry-wait))
9978                 (setq es (gnus-request-expire-articles
9979                           expirable gnus-newsgroup-name)))
9980             (setq es (gnus-request-expire-articles
9981                       expirable gnus-newsgroup-name)))
9982           (unless total
9983             (setq gnus-newsgroup-expirable es))
9984           ;; We go through the old list of expirable, and mark all
9985           ;; really expired articles as nonexistent.
9986           (unless (eq es expirable) ;If nothing was expired, we don't mark.
9987             (let ((gnus-use-cache nil))
9988               (dolist (article expirable)
9989                 (when (and (not (memq article es))
9990                            (gnus-data-find article))
9991                   (gnus-summary-mark-article article gnus-canceled-mark)
9992                   (run-hook-with-args 'gnus-summary-article-expire-hook
9993                                       'delete
9994                                       (gnus-data-header
9995                                        (assoc article (gnus-data-list nil)))
9996                                       gnus-newsgroup-name
9997                                       nil
9998                                       nil))))))
9999         (gnus-message 6 "Expiring articles...done")))))
10000
10001 (defun gnus-summary-expire-articles-now ()
10002   "Expunge all expirable articles in the current group.
10003 This means that *all* articles that are marked as expirable will be
10004 deleted forever, right now."
10005   (interactive)
10006   (or gnus-expert-user
10007       (gnus-yes-or-no-p
10008        "Are you really, really sure you want to delete all expirable messages? ")
10009       (error "Phew!"))
10010   (gnus-summary-expire-articles t))
10011
10012 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10013 (defun gnus-summary-delete-article (&optional n)
10014   "Delete the N next (mail) articles.
10015 This command actually deletes articles.  This is not a marking
10016 command.  The article will disappear forever from your life, never to
10017 return.
10018
10019 If N is negative, delete backwards.
10020 If N is nil and articles have been marked with the process mark,
10021 delete these instead.
10022
10023 If `gnus-novice-user' is non-nil you will be asked for
10024 confirmation before the articles are deleted."
10025   (interactive "P")
10026   (unless (gnus-check-backend-function 'request-expire-articles
10027                                        gnus-newsgroup-name)
10028     (error "The current newsgroup does not support article deletion"))
10029   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
10030     (error "Couldn't open server"))
10031   ;; Compute the list of articles to delete.
10032   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
10033         (nnmail-expiry-target 'delete)
10034         not-deleted)
10035     (if (and gnus-novice-user
10036              (not (gnus-yes-or-no-p
10037                    (format "Do you really want to delete %s forever? "
10038                            (if (> (length articles) 1)
10039                                (format "these %s articles" (length articles))
10040                              "this article")))))
10041         ()
10042       ;; Delete the articles.
10043       (setq not-deleted (gnus-request-expire-articles
10044                          articles gnus-newsgroup-name 'force))
10045       (while articles
10046         (gnus-summary-remove-process-mark (car articles))
10047         ;; The backend might not have been able to delete the article
10048         ;; after all.
10049         (unless (memq (car articles) not-deleted)
10050           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10051         (let* ((article (car articles))
10052                (ghead  (gnus-data-header
10053                                     (assoc article (gnus-data-list nil)))))
10054           (run-hook-with-args 'gnus-summary-article-delete-hook
10055                               'delete ghead gnus-newsgroup-name nil
10056                               nil))
10057         (setq articles (cdr articles)))
10058       (when not-deleted
10059         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
10060     (gnus-summary-position-point)
10061     (gnus-set-mode-line 'summary)
10062     not-deleted))
10063
10064 (defun gnus-summary-edit-article (&optional arg)
10065   "Edit the current article.
10066 This will have permanent effect only in mail groups.
10067 If ARG is nil, edit the decoded articles.
10068 If ARG is 1, edit the raw articles.
10069 If ARG is 2, edit the raw articles even in read-only groups.
10070 If ARG is 3, edit the articles with the current handles.
10071 Otherwise, allow editing of articles even in read-only
10072 groups."
10073   (interactive "P")
10074   (let (force raw current-handles)
10075     (cond
10076      ((null arg))
10077      ((eq arg 1)
10078       (setq raw t))
10079      ((eq arg 2)
10080       (setq raw t
10081             force t))
10082      ((eq arg 3)
10083       (setq current-handles
10084             (and (gnus-buffer-live-p gnus-article-buffer)
10085                  (with-current-buffer gnus-article-buffer
10086                    (prog1
10087                        gnus-article-mime-handles
10088                      (setq gnus-article-mime-handles nil))))))
10089      (t
10090       (setq force t)))
10091     (when (and raw (not force)
10092                (member gnus-newsgroup-name '("nndraft:delayed"
10093                                              "nndraft:drafts"
10094                                              "nndraft:queue")))
10095       (error "Can't edit the raw article in group %s"
10096              gnus-newsgroup-name))
10097     (save-excursion
10098       (set-buffer gnus-summary-buffer)
10099       (let ((mail-parse-charset gnus-newsgroup-charset)
10100             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
10101         (gnus-set-global-variables)
10102         (when (and (not force)
10103                    (gnus-group-read-only-p))
10104           (error "The current newsgroup does not support article editing"))
10105         (gnus-summary-show-article t)
10106         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
10107           (with-current-buffer gnus-article-buffer
10108             (mm-enable-multibyte)))
10109         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10110             (setq raw t))
10111         (gnus-article-edit-article
10112          (if raw 'ignore
10113            `(lambda ()
10114               (let ((mbl mml-buffer-list))
10115                 (setq mml-buffer-list nil)
10116                 (let ((rfc2047-quote-decoded-words-containing-tspecials t))
10117                   (mime-to-mml ,'current-handles))
10118                 (let ((mbl1 mml-buffer-list))
10119                   (setq mml-buffer-list mbl)
10120                   (set (make-local-variable 'mml-buffer-list) mbl1))
10121                 (gnus-make-local-hook 'kill-buffer-hook)
10122                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
10123          `(lambda (no-highlight)
10124             (let ((mail-parse-charset ',gnus-newsgroup-charset)
10125                   (message-options message-options)
10126                   (message-options-set-recipient)
10127                   (mail-parse-ignored-charsets
10128                    ',gnus-newsgroup-ignored-charsets)
10129                   (rfc2047-header-encoding-alist
10130                    ',(let ((charset (gnus-group-name-charset
10131                                      (gnus-find-method-for-group
10132                                       gnus-newsgroup-name)
10133                                      gnus-newsgroup-name)))
10134                        (append (list (cons "Newsgroups" charset)
10135                                      (cons "Followup-To" charset)
10136                                      (cons "Xref" charset))
10137                                rfc2047-header-encoding-alist))))
10138               ,(if (not raw) '(progn
10139                                 (mml-to-mime)
10140                                 (mml-destroy-buffers)
10141                                 (remove-hook 'kill-buffer-hook
10142                                              'mml-destroy-buffers t)
10143                                 (kill-local-variable 'mml-buffer-list)))
10144               (gnus-summary-edit-article-done
10145                ,(or (mail-header-references gnus-current-headers) "")
10146                ,(gnus-group-read-only-p)
10147                ,gnus-summary-buffer no-highlight))))))))
10148
10149 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
10150
10151 (defun gnus-summary-edit-article-done (&optional references read-only buffer
10152                                                  no-highlight)
10153   "Make edits to the current article permanent."
10154   (interactive)
10155   (save-excursion
10156    ;; The buffer restriction contains the entire article if it exists.
10157     (when (article-goto-body)
10158       (let ((lines (count-lines (point) (point-max)))
10159             (length (- (point-max) (point)))
10160             (case-fold-search t)
10161             (body (copy-marker (point))))
10162         (goto-char (point-min))
10163         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
10164           (delete-region (match-beginning 1) (match-end 1))
10165           (insert (number-to-string length)))
10166         (goto-char (point-min))
10167         (when (re-search-forward
10168                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
10169           (delete-region (match-beginning 1) (match-end 1))
10170           (insert (number-to-string length)))
10171         (goto-char (point-min))
10172         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
10173           (delete-region (match-beginning 1) (match-end 1))
10174           (insert (number-to-string lines))))))
10175   ;; Replace the article.
10176   (let ((buf (current-buffer)))
10177     (with-temp-buffer
10178       (insert-buffer-substring buf)
10179
10180       (if (and (not read-only)
10181                (not (gnus-request-replace-article
10182                      (cdr gnus-article-current) (car gnus-article-current)
10183                      (current-buffer) t)))
10184           (error "Couldn't replace article")
10185         ;; Update the summary buffer.
10186         (if (and references
10187                  (equal (message-tokenize-header references " ")
10188                         (message-tokenize-header
10189                          (or (message-fetch-field "references") "") " ")))
10190             ;; We only have to update this line.
10191             (save-excursion
10192               (save-restriction
10193                 (message-narrow-to-head)
10194                 (let ((head (buffer-substring-no-properties
10195                              (point-min) (point-max)))
10196                       header)
10197                   (with-temp-buffer
10198                     (insert (format "211 %d Article retrieved.\n"
10199                                     (cdr gnus-article-current)))
10200                     (insert head)
10201                     (insert ".\n")
10202                     (let ((nntp-server-buffer (current-buffer)))
10203                       (setq header (car (gnus-get-newsgroup-headers
10204                                          nil t))))
10205                     (save-excursion
10206                       (set-buffer gnus-summary-buffer)
10207                       (gnus-data-set-header
10208                        (gnus-data-find (cdr gnus-article-current))
10209                        header)
10210                       (gnus-summary-update-article-line
10211                        (cdr gnus-article-current) header)
10212                       (if (gnus-summary-goto-subject
10213                            (cdr gnus-article-current) nil t)
10214                           (gnus-summary-update-secondary-mark
10215                            (cdr gnus-article-current))))))))
10216           ;; Update threads.
10217           (set-buffer (or buffer gnus-summary-buffer))
10218           (gnus-summary-update-article (cdr gnus-article-current))
10219           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10220               (gnus-summary-update-secondary-mark
10221                (cdr gnus-article-current))))
10222         ;; Prettify the article buffer again.
10223         (unless no-highlight
10224           (save-excursion
10225             (set-buffer gnus-article-buffer)
10226             ;;;!!! Fix this -- article should be rehighlighted.
10227             ;;;(gnus-run-hooks 'gnus-article-display-hook)
10228             (set-buffer gnus-original-article-buffer)
10229             (gnus-request-article
10230              (cdr gnus-article-current)
10231              (car gnus-article-current) (current-buffer))))
10232         ;; Prettify the summary buffer line.
10233         (when (gnus-visual-p 'summary-highlight 'highlight)
10234           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
10235
10236 (defun gnus-summary-edit-wash (key)
10237   "Perform editing command KEY in the article buffer."
10238   (interactive
10239    (list
10240     (progn
10241       (message "%s" (concat (this-command-keys) "- "))
10242       (read-char))))
10243   (message "")
10244   (gnus-summary-edit-article)
10245   (execute-kbd-macro (concat (this-command-keys) key))
10246   (gnus-article-edit-done))
10247
10248 ;;; Respooling
10249
10250 (defun gnus-summary-respool-query (&optional silent trace)
10251   "Query where the respool algorithm would put this article."
10252   (interactive)
10253   (let (gnus-mark-article-hook)
10254     (gnus-summary-select-article)
10255     (save-excursion
10256       (set-buffer gnus-original-article-buffer)
10257       (let ((groups (nnmail-article-group 'identity trace)))
10258         (unless silent
10259           (if groups
10260               (message "This message would go to %s"
10261                        (mapconcat 'car groups ", "))
10262             (message "This message would go to no groups"))
10263           groups)))))
10264
10265 (defun gnus-summary-respool-trace ()
10266   "Trace where the respool algorithm would put this article.
10267 Display a buffer showing all fancy splitting patterns which matched."
10268   (interactive)
10269   (gnus-summary-respool-query nil t))
10270
10271 ;; Summary marking commands.
10272
10273 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10274   "Mark articles which has the same subject as read, and then select the next.
10275 If UNMARK is positive, remove any kind of mark.
10276 If UNMARK is negative, tick articles."
10277   (interactive "P")
10278   (when unmark
10279     (setq unmark (prefix-numeric-value unmark)))
10280   (let ((count
10281          (gnus-summary-mark-same-subject
10282           (gnus-summary-article-subject) unmark)))
10283     ;; Select next unread article.  If auto-select-same mode, should
10284     ;; select the first unread article.
10285     (gnus-summary-next-article t (and gnus-auto-select-same
10286                                       (gnus-summary-article-subject)))
10287     (gnus-message 7 "%d article%s marked as %s"
10288                   count (if (= count 1) " is" "s are")
10289                   (if unmark "unread" "read"))))
10290
10291 (defun gnus-summary-kill-same-subject (&optional unmark)
10292   "Mark articles which has the same subject as read.
10293 If UNMARK is positive, remove any kind of mark.
10294 If UNMARK is negative, tick articles."
10295   (interactive "P")
10296   (when unmark
10297     (setq unmark (prefix-numeric-value unmark)))
10298   (let ((count
10299          (gnus-summary-mark-same-subject
10300           (gnus-summary-article-subject) unmark)))
10301     ;; If marked as read, go to next unread subject.
10302     (when (null unmark)
10303       ;; Go to next unread subject.
10304       (gnus-summary-next-subject 1 t))
10305     (gnus-message 7 "%d articles are marked as %s"
10306                   count (if unmark "unread" "read"))))
10307
10308 (defun gnus-summary-mark-same-subject (subject &optional unmark)
10309   "Mark articles with same SUBJECT as read, and return marked number.
10310 If optional argument UNMARK is positive, remove any kinds of marks.
10311 If optional argument UNMARK is negative, mark articles as unread instead."
10312   (let ((count 1))
10313     (save-excursion
10314       (cond
10315        ((null unmark)                   ; Mark as read.
10316         (while (and
10317                 (progn
10318                   (gnus-summary-mark-article-as-read gnus-killed-mark)
10319                   (gnus-summary-show-thread) t)
10320                 (gnus-summary-find-subject subject))
10321           (setq count (1+ count))))
10322        ((> unmark 0)                    ; Tick.
10323         (while (and
10324                 (progn
10325                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
10326                   (gnus-summary-show-thread) t)
10327                 (gnus-summary-find-subject subject))
10328           (setq count (1+ count))))
10329        (t                               ; Mark as unread.
10330         (while (and
10331                 (progn
10332                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
10333                   (gnus-summary-show-thread) t)
10334                 (gnus-summary-find-subject subject))
10335           (setq count (1+ count)))))
10336       (gnus-set-mode-line 'summary)
10337       ;; Return the number of marked articles.
10338       count)))
10339
10340 (defun gnus-summary-mark-as-processable (n &optional unmark)
10341   "Set the process mark on the next N articles.
10342 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
10343 the process mark instead.  The difference between N and the actual
10344 number of articles marked is returned."
10345   (interactive "P")
10346   (if (and (null n) (gnus-region-active-p))
10347       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
10348     (setq n (prefix-numeric-value n))
10349     (let ((backward (< n 0))
10350           (n (abs n)))
10351       (while (and
10352               (> n 0)
10353               (if unmark
10354                   (gnus-summary-remove-process-mark
10355                    (gnus-summary-article-number))
10356                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
10357               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
10358         (setq n (1- n)))
10359       (when (/= 0 n)
10360         (gnus-message 7 "No more articles"))
10361       (gnus-summary-recenter)
10362       (gnus-summary-position-point)
10363       n)))
10364
10365 (defun gnus-summary-unmark-as-processable (n)
10366   "Remove the process mark from the next N articles.
10367 If N is negative, unmark backward instead.  The difference between N and
10368 the actual number of articles unmarked is returned."
10369   (interactive "P")
10370   (gnus-summary-mark-as-processable n t))
10371
10372 (defun gnus-summary-unmark-all-processable ()
10373   "Remove the process mark from all articles."
10374   (interactive)
10375   (save-excursion
10376     (while gnus-newsgroup-processable
10377       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
10378   (gnus-summary-position-point))
10379
10380 (defun gnus-summary-add-mark (article type)
10381   "Mark ARTICLE with a mark of TYPE."
10382   (let ((vtype (car (assq type gnus-article-mark-lists)))
10383         var)
10384     (if (not vtype)
10385         (error "No such mark type: %s" type)
10386       (setq var (intern (format "gnus-newsgroup-%s" type)))
10387       (set var (cons article (symbol-value var)))
10388       (if (memq type '(processable cached replied forwarded recent saved))
10389           (gnus-summary-update-secondary-mark article)
10390         ;;; !!! This is bogus.  We should find out what primary
10391         ;;; !!! mark we want to set.
10392         (gnus-summary-update-mark gnus-del-mark 'unread)))))
10393
10394 (defun gnus-summary-mark-as-expirable (n)
10395   "Mark N articles forward as expirable.
10396 If N is negative, mark backward instead.  The difference between N and
10397 the actual number of articles marked is returned."
10398   (interactive "p")
10399   (gnus-summary-mark-forward n gnus-expirable-mark))
10400
10401 (defun gnus-summary-mark-as-spam (n)
10402   "Mark N articles forward as spam.
10403 If N is negative, mark backward instead.  The difference between N and
10404 the actual number of articles marked is returned."
10405   (interactive "p")
10406   (gnus-summary-mark-forward n gnus-spam-mark))
10407
10408 (defun gnus-summary-mark-article-as-replied (article)
10409   "Mark ARTICLE as replied to and update the summary line.
10410 ARTICLE can also be a list of articles."
10411   (interactive (list (gnus-summary-article-number)))
10412   (let ((articles (if (listp article) article (list article))))
10413     (dolist (article articles)
10414       (unless (numberp article)
10415         (error "%s is not a number" article))
10416       (push article gnus-newsgroup-replied)
10417       (let ((buffer-read-only nil))
10418         (when (gnus-summary-goto-subject article nil t)
10419           (gnus-summary-update-secondary-mark article))))))
10420
10421 (defun gnus-summary-mark-article-as-forwarded (article)
10422   "Mark ARTICLE as forwarded and update the summary line.
10423 ARTICLE can also be a list of articles."
10424   (let ((articles (if (listp article) article (list article))))
10425     (dolist (article articles)
10426       (push article gnus-newsgroup-forwarded)
10427       (let ((buffer-read-only nil))
10428         (when (gnus-summary-goto-subject article nil t)
10429           (gnus-summary-update-secondary-mark article))))))
10430
10431 (defun gnus-summary-set-bookmark (article)
10432   "Set a bookmark in current article."
10433   (interactive (list (gnus-summary-article-number)))
10434   (when (or (not (get-buffer gnus-article-buffer))
10435             (not gnus-current-article)
10436             (not gnus-article-current)
10437             (not (equal gnus-newsgroup-name (car gnus-article-current))))
10438     (error "No current article selected"))
10439   ;; Remove old bookmark, if one exists.
10440   (gnus-pull article gnus-newsgroup-bookmarks)
10441   ;; Set the new bookmark, which is on the form
10442   ;; (article-number . line-number-in-body).
10443   (push
10444    (cons article
10445          (with-current-buffer gnus-article-buffer
10446            (count-lines
10447             (min (point)
10448                  (save-excursion
10449                    (article-goto-body)
10450                    (point)))
10451             (point))))
10452    gnus-newsgroup-bookmarks)
10453   (gnus-message 6 "A bookmark has been added to the current article."))
10454
10455 (defun gnus-summary-remove-bookmark (article)
10456   "Remove the bookmark from the current article."
10457   (interactive (list (gnus-summary-article-number)))
10458   ;; Remove old bookmark, if one exists.
10459   (if (not (assq article gnus-newsgroup-bookmarks))
10460       (gnus-message 6 "No bookmark in current article.")
10461     (gnus-pull article gnus-newsgroup-bookmarks)
10462     (gnus-message 6 "Removed bookmark.")))
10463
10464 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10465 (defun gnus-summary-mark-as-dormant (n)
10466   "Mark N articles forward as dormant.
10467 If N is negative, mark backward instead.  The difference between N and
10468 the actual number of articles marked is returned."
10469   (interactive "p")
10470   (gnus-summary-mark-forward n gnus-dormant-mark))
10471
10472 (defun gnus-summary-set-process-mark (article)
10473   "Set the process mark on ARTICLE and update the summary line."
10474   (setq gnus-newsgroup-processable
10475         (cons article
10476               (delq article gnus-newsgroup-processable)))
10477   (when (gnus-summary-goto-subject article)
10478     (gnus-summary-show-thread)
10479     (gnus-summary-goto-subject article)
10480     (gnus-summary-update-secondary-mark article)))
10481
10482 (defun gnus-summary-remove-process-mark (&rest articles)
10483   "Remove the process mark from ARTICLES and update the summary line."
10484   (dolist (article articles)
10485     (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
10486     (when (gnus-summary-goto-subject article)
10487       (gnus-summary-show-thread)
10488       (gnus-summary-goto-subject article)
10489       (gnus-summary-update-secondary-mark article)))
10490   t)
10491
10492 (defun gnus-summary-set-saved-mark (article)
10493   "Set the process mark on ARTICLE and update the summary line."
10494   (push article gnus-newsgroup-saved)
10495   (when (gnus-summary-goto-subject article)
10496     (gnus-summary-update-secondary-mark article)))
10497
10498 (defun gnus-summary-mark-forward (n &optional mark no-expire)
10499   "Mark N articles as read forwards.
10500 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
10501 The difference between N and the actual number of articles marked is
10502 returned.
10503 If NO-EXPIRE, auto-expiry will be inhibited."
10504   (interactive "p")
10505   (gnus-summary-show-thread)
10506   (let ((backward (< n 0))
10507         (gnus-summary-goto-unread
10508          (and gnus-summary-goto-unread
10509               (not (eq gnus-summary-goto-unread 'never))
10510               (not (memq mark (list gnus-unread-mark gnus-spam-mark
10511                                     gnus-ticked-mark gnus-dormant-mark)))))
10512         (n (abs n))
10513         (mark (or mark gnus-del-mark)))
10514     (while (and (> n 0)
10515                 (gnus-summary-mark-article nil mark no-expire)
10516                 (zerop (gnus-summary-next-subject
10517                         (if backward -1 1)
10518                         (and gnus-summary-goto-unread
10519                              (not (eq gnus-summary-goto-unread 'never)))
10520                         t)))
10521       (setq n (1- n)))
10522     (when (/= 0 n)
10523       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10524     (gnus-summary-recenter)
10525     (gnus-summary-position-point)
10526     (gnus-set-mode-line 'summary)
10527     n))
10528
10529 (defun gnus-summary-mark-article-as-read (mark)
10530   "Mark the current article quickly as read with MARK."
10531   (let ((article (gnus-summary-article-number)))
10532     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10533     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10534     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10535     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10536     (push (cons article mark) gnus-newsgroup-reads)
10537     ;; Possibly remove from cache, if that is used.
10538     (when gnus-use-cache
10539       (gnus-cache-enter-remove-article article))
10540     ;; Allow the backend to change the mark.
10541     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10542     ;; Check for auto-expiry.
10543     (when (and gnus-newsgroup-auto-expire
10544                (memq mark gnus-auto-expirable-marks))
10545       (setq mark gnus-expirable-mark)
10546       ;; Let the backend know about the mark change.
10547       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10548       (push article gnus-newsgroup-expirable))
10549     ;; Set the mark in the buffer.
10550     (gnus-summary-update-mark mark 'unread)
10551     t))
10552
10553 (defun gnus-summary-mark-article-as-unread (mark)
10554   "Mark the current article quickly as unread with MARK."
10555   (let* ((article (gnus-summary-article-number))
10556          (old-mark (gnus-summary-article-mark article)))
10557     ;; Allow the backend to change the mark.
10558     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10559     (if (eq mark old-mark)
10560         t
10561       (if (<= article 0)
10562           (progn
10563             (gnus-error 1 "Can't mark negative article numbers")
10564             nil)
10565         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10566         (setq gnus-newsgroup-spam-marked
10567               (delq article gnus-newsgroup-spam-marked))
10568         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10569         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10570         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10571         (cond ((= mark gnus-ticked-mark)
10572                (setq gnus-newsgroup-marked
10573                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10574                                               article)))
10575               ((= mark gnus-spam-mark)
10576                (setq gnus-newsgroup-spam-marked
10577                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10578                                               article)))
10579               ((= mark gnus-dormant-mark)
10580                (setq gnus-newsgroup-dormant
10581                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10582                                               article)))
10583               (t
10584                (setq gnus-newsgroup-unreads
10585                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10586                                               article))))
10587         (gnus-pull article gnus-newsgroup-reads)
10588
10589         ;; See whether the article is to be put in the cache.
10590         (and gnus-use-cache
10591              (vectorp (gnus-summary-article-header article))
10592              (save-excursion
10593                (gnus-cache-possibly-enter-article
10594                 gnus-newsgroup-name article
10595                 (= mark gnus-ticked-mark)
10596                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10597
10598         ;; Fix the mark.
10599         (gnus-summary-update-mark mark 'unread)
10600         t))))
10601
10602 (defun gnus-summary-mark-article (&optional article mark no-expire)
10603   "Mark ARTICLE with MARK.  MARK can be any character.
10604 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10605 `??' (dormant) and `?E' (expirable).
10606 If MARK is nil, then the default character `?r' is used.
10607 If ARTICLE is nil, then the article on the current line will be
10608 marked.
10609 If NO-EXPIRE, auto-expiry will be inhibited."
10610   ;; The mark might be a string.
10611   (when (stringp mark)
10612     (setq mark (aref mark 0)))
10613   ;; If no mark is given, then we check auto-expiring.
10614   (when (null mark)
10615     (setq mark gnus-del-mark))
10616   (when (and (not no-expire)
10617              gnus-newsgroup-auto-expire
10618              (memq mark gnus-auto-expirable-marks))
10619     (setq mark gnus-expirable-mark))
10620   (let ((article (or article (gnus-summary-article-number)))
10621         (old-mark (gnus-summary-article-mark article)))
10622     ;; Allow the backend to change the mark.
10623     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10624     (if (eq mark old-mark)
10625         t
10626       (unless article
10627         (error "No article on current line"))
10628       (if (not (if (or (= mark gnus-unread-mark)
10629                        (= mark gnus-ticked-mark)
10630                        (= mark gnus-spam-mark)
10631                        (= mark gnus-dormant-mark))
10632                    (gnus-mark-article-as-unread article mark)
10633                  (gnus-mark-article-as-read article mark)))
10634           t
10635         ;; See whether the article is to be put in the cache.
10636         (and gnus-use-cache
10637              (not (= mark gnus-canceled-mark))
10638              (vectorp (gnus-summary-article-header article))
10639              (save-excursion
10640                (gnus-cache-possibly-enter-article
10641                 gnus-newsgroup-name article
10642                 (= mark gnus-ticked-mark)
10643                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10644
10645         (when (gnus-summary-goto-subject article nil t)
10646           (let ((buffer-read-only nil))
10647             (gnus-summary-show-thread)
10648             ;; Fix the mark.
10649             (gnus-summary-update-mark mark 'unread)
10650             t))))))
10651
10652 (defun gnus-summary-update-secondary-mark (article)
10653   "Update the secondary (read, process, cache) mark."
10654   (gnus-summary-update-mark
10655    (cond ((memq article gnus-newsgroup-processable)
10656           gnus-process-mark)
10657          ((memq article gnus-newsgroup-cached)
10658           gnus-cached-mark)
10659          ((memq article gnus-newsgroup-replied)
10660           gnus-replied-mark)
10661          ((memq article gnus-newsgroup-forwarded)
10662           gnus-forwarded-mark)
10663          ((memq article gnus-newsgroup-saved)
10664           gnus-saved-mark)
10665          ((memq article gnus-newsgroup-recent)
10666           gnus-recent-mark)
10667          ((memq article gnus-newsgroup-unseen)
10668           gnus-unseen-mark)
10669          (t gnus-no-mark))
10670    'replied)
10671   (when (gnus-visual-p 'summary-highlight 'highlight)
10672     (gnus-run-hooks 'gnus-summary-update-hook))
10673   t)
10674
10675 (defun gnus-summary-update-download-mark (article)
10676   "Update the download mark."
10677   (gnus-summary-update-mark
10678    (cond ((memq article gnus-newsgroup-undownloaded)
10679           gnus-undownloaded-mark)
10680          (gnus-newsgroup-agentized
10681           gnus-downloaded-mark)
10682          (t
10683           gnus-no-mark))
10684    'download)
10685   (gnus-summary-update-line t)
10686   t)
10687
10688 (defun gnus-summary-update-mark (mark type)
10689   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10690         (buffer-read-only nil))
10691     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10692     (when forward
10693       (when (looking-at "\r")
10694         (incf forward))
10695       (when (<= (+ forward (point)) (point-max))
10696         ;; Go to the right position on the line.
10697         (goto-char (+ forward (point)))
10698         ;; Replace the old mark with the new mark.
10699         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10700         ;; Optionally update the marks by some user rule.
10701         (when (eq type 'unread)
10702           (gnus-data-set-mark
10703            (gnus-data-find (gnus-summary-article-number)) mark)
10704           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10705
10706 (defun gnus-mark-article-as-read (article &optional mark)
10707   "Enter ARTICLE in the pertinent lists and remove it from others."
10708   ;; Make the article expirable.
10709   (let ((mark (or mark gnus-del-mark)))
10710     (setq gnus-newsgroup-expirable
10711           (if (= mark gnus-expirable-mark)
10712               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10713             (delq article gnus-newsgroup-expirable)))
10714     ;; Remove from unread and marked lists.
10715     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10716     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10717     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10718     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10719     (push (cons article mark) gnus-newsgroup-reads)
10720     ;; Possibly remove from cache, if that is used.
10721     (when gnus-use-cache
10722       (gnus-cache-enter-remove-article article))
10723     t))
10724
10725 (defun gnus-mark-article-as-unread (article &optional mark)
10726   "Enter ARTICLE in the pertinent lists and remove it from others."
10727   (let ((mark (or mark gnus-ticked-mark)))
10728     (if (<= article 0)
10729         (progn
10730           (gnus-error 1 "Can't mark negative article numbers")
10731           nil)
10732       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10733             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10734             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10735             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10736             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10737
10738       ;; Unsuppress duplicates?
10739       (when gnus-suppress-duplicates
10740         (gnus-dup-unsuppress-article article))
10741
10742       (cond ((= mark gnus-ticked-mark)
10743              (setq gnus-newsgroup-marked
10744                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10745             ((= mark gnus-spam-mark)
10746              (setq gnus-newsgroup-spam-marked
10747                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10748                                             article)))
10749             ((= mark gnus-dormant-mark)
10750              (setq gnus-newsgroup-dormant
10751                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10752             (t
10753              (setq gnus-newsgroup-unreads
10754                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10755       (gnus-pull article gnus-newsgroup-reads)
10756       t)))
10757
10758 (defalias 'gnus-summary-mark-as-unread-forward
10759   'gnus-summary-tick-article-forward)
10760 (make-obsolete 'gnus-summary-mark-as-unread-forward
10761                'gnus-summary-tick-article-forward)
10762 (defun gnus-summary-tick-article-forward (n)
10763   "Tick N articles forwards.
10764 If N is negative, tick backwards instead.
10765 The difference between N and the number of articles ticked is returned."
10766   (interactive "p")
10767   (gnus-summary-mark-forward n gnus-ticked-mark))
10768
10769 (defalias 'gnus-summary-mark-as-unread-backward
10770   'gnus-summary-tick-article-backward)
10771 (make-obsolete 'gnus-summary-mark-as-unread-backward
10772                'gnus-summary-tick-article-backward)
10773 (defun gnus-summary-tick-article-backward (n)
10774   "Tick N articles backwards.
10775 The difference between N and the number of articles ticked is returned."
10776   (interactive "p")
10777   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10778
10779 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10780 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10781 (defun gnus-summary-tick-article (&optional article clear-mark)
10782   "Mark current article as unread.
10783 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10784 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10785   (interactive)
10786   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10787                                        gnus-ticked-mark)))
10788
10789 (defun gnus-summary-mark-as-read-forward (n)
10790   "Mark N articles as read forwards.
10791 If N is negative, mark backwards instead.
10792 The difference between N and the actual number of articles marked is
10793 returned."
10794   (interactive "p")
10795   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10796
10797 (defun gnus-summary-mark-as-read-backward (n)
10798   "Mark the N articles as read backwards.
10799 The difference between N and the actual number of articles marked is
10800 returned."
10801   (interactive "p")
10802   (gnus-summary-mark-forward
10803    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10804
10805 (defun gnus-summary-mark-as-read (&optional article mark)
10806   "Mark current article as read.
10807 ARTICLE specifies the article to be marked as read.
10808 MARK specifies a string to be inserted at the beginning of the line."
10809   (gnus-summary-mark-article article mark))
10810
10811 (defun gnus-summary-clear-mark-forward (n)
10812   "Clear marks from N articles forward.
10813 If N is negative, clear backward instead.
10814 The difference between N and the number of marks cleared is returned."
10815   (interactive "p")
10816   (gnus-summary-mark-forward n gnus-unread-mark))
10817
10818 (defun gnus-summary-clear-mark-backward (n)
10819   "Clear marks from N articles backward.
10820 The difference between N and the number of marks cleared is returned."
10821   (interactive "p")
10822   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10823
10824 (defun gnus-summary-mark-unread-as-read ()
10825   "Intended to be used by `gnus-summary-mark-article-hook'."
10826   (when (memq gnus-current-article gnus-newsgroup-unreads)
10827     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10828
10829 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10830   "Intended to be used by `gnus-summary-mark-article-hook'."
10831   (let ((mark (gnus-summary-article-mark)))
10832     (when (or (gnus-unread-mark-p mark)
10833               (gnus-read-mark-p mark))
10834       (gnus-summary-mark-article gnus-current-article
10835                                  (or new-mark gnus-read-mark)))))
10836
10837 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10838   "Intended to be used by `gnus-summary-mark-article-hook'."
10839   (let ((mark (gnus-summary-article-mark)))
10840     (when (or (gnus-unread-mark-p mark)
10841               (gnus-read-mark-p mark))
10842       (gnus-summary-mark-article (gnus-summary-article-number)
10843                                  (or new-mark gnus-read-mark)))))
10844
10845 (defun gnus-summary-mark-unread-as-ticked ()
10846   "Intended to be used by `gnus-summary-mark-article-hook'."
10847   (when (memq gnus-current-article gnus-newsgroup-unreads)
10848     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10849
10850 (defun gnus-summary-mark-region-as-read (point mark all)
10851   "Mark all unread articles between point and mark as read.
10852 If given a prefix, mark all articles between point and mark as read,
10853 even ticked and dormant ones."
10854   (interactive "r\nP")
10855   (save-excursion
10856     (let (article)
10857       (goto-char point)
10858       (beginning-of-line)
10859       (while (and
10860               (< (point) mark)
10861               (progn
10862                 (when (or all
10863                           (memq (setq article (gnus-summary-article-number))
10864                                 gnus-newsgroup-unreads))
10865                   (gnus-summary-mark-article article gnus-del-mark))
10866                 t)
10867               (gnus-summary-find-next))))))
10868
10869 (defun gnus-summary-mark-below (score mark)
10870   "Mark articles with score less than SCORE with MARK."
10871   (interactive "P\ncMark: ")
10872   (setq score (if score
10873                   (prefix-numeric-value score)
10874                 (or gnus-summary-default-score 0)))
10875   (save-excursion
10876     (set-buffer gnus-summary-buffer)
10877     (goto-char (point-min))
10878     (while
10879         (progn
10880           (and (< (gnus-summary-article-score) score)
10881                (gnus-summary-mark-article nil mark))
10882           (gnus-summary-find-next)))))
10883
10884 (defun gnus-summary-kill-below (&optional score)
10885   "Mark articles with score below SCORE as read."
10886   (interactive "P")
10887   (gnus-summary-mark-below score gnus-killed-mark))
10888
10889 (defun gnus-summary-clear-above (&optional score)
10890   "Clear all marks from articles with score above SCORE."
10891   (interactive "P")
10892   (gnus-summary-mark-above score gnus-unread-mark))
10893
10894 (defun gnus-summary-tick-above (&optional score)
10895   "Tick all articles with score above SCORE."
10896   (interactive "P")
10897   (gnus-summary-mark-above score gnus-ticked-mark))
10898
10899 (defun gnus-summary-mark-above (score mark)
10900   "Mark articles with score over SCORE with MARK."
10901   (interactive "P\ncMark: ")
10902   (setq score (if score
10903                   (prefix-numeric-value score)
10904                 (or gnus-summary-default-score 0)))
10905   (save-excursion
10906     (set-buffer gnus-summary-buffer)
10907     (goto-char (point-min))
10908     (while (and (progn
10909                   (when (> (gnus-summary-article-score) score)
10910                     (gnus-summary-mark-article nil mark))
10911                   t)
10912                 (gnus-summary-find-next)))))
10913
10914 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10915 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10916 (defun gnus-summary-limit-include-expunged (&optional no-error)
10917   "Display all the hidden articles that were expunged for low scores."
10918   (interactive)
10919   (let ((buffer-read-only nil))
10920     (let ((scored gnus-newsgroup-scored)
10921           headers h)
10922       (while scored
10923         (unless (gnus-summary-article-header (caar scored))
10924           (and (setq h (gnus-number-to-header (caar scored)))
10925                (< (cdar scored) gnus-summary-expunge-below)
10926                (push h headers)))
10927         (setq scored (cdr scored)))
10928       (if (not headers)
10929           (when (not no-error)
10930             (error "No expunged articles hidden"))
10931         (goto-char (point-min))
10932         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10933         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10934         (mapcar (lambda (x) (push (mail-header-number x)
10935                                   gnus-newsgroup-limit))
10936                 headers)
10937         (gnus-summary-prepare-unthreaded (nreverse headers))
10938         (goto-char (point-min))
10939         (gnus-summary-position-point)
10940         t))))
10941
10942 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10943   "Mark all unread articles in this newsgroup as read.
10944 If prefix argument ALL is non-nil, ticked and dormant articles will
10945 also be marked as read.
10946 If QUIETLY is non-nil, no questions will be asked.
10947
10948 If TO-HERE is non-nil, it should be a point in the buffer.  All
10949 articles before (after, if REVERSE is set) this point will be marked
10950 as read.
10951
10952 Note that this function will only catch up the unread article
10953 in the current summary buffer limitation.
10954
10955 The number of articles marked as read is returned."
10956   (interactive "P")
10957   (prog1
10958       (save-excursion
10959         (when (or quietly
10960                   (not gnus-interactive-catchup) ;Without confirmation?
10961                   gnus-expert-user
10962                   (gnus-y-or-n-p
10963                    (if all
10964                        "Mark absolutely all articles as read? "
10965                      "Mark all unread articles as read? ")))
10966           (if (and not-mark
10967                    (not gnus-newsgroup-adaptive)
10968                    (not gnus-newsgroup-auto-expire)
10969                    (not gnus-suppress-duplicates)
10970                    (or (not gnus-use-cache)
10971                        (eq gnus-use-cache 'passive)))
10972               (progn
10973                 (when all
10974                   (setq gnus-newsgroup-marked nil
10975                         gnus-newsgroup-spam-marked nil
10976                         gnus-newsgroup-dormant nil))
10977                 (setq gnus-newsgroup-unreads
10978                       (gnus-sorted-nunion
10979                        (gnus-sorted-intersection gnus-newsgroup-unreads
10980                                                  gnus-newsgroup-downloadable)
10981                        (gnus-sorted-difference gnus-newsgroup-unfetched
10982                                                gnus-newsgroup-cached))))
10983             ;; We actually mark all articles as canceled, which we
10984             ;; have to do when using auto-expiry or adaptive scoring.
10985             (gnus-summary-show-all-threads)
10986             (if (and to-here reverse)
10987                 (progn
10988                   (goto-char to-here)
10989                   (gnus-summary-mark-current-read-and-unread-as-read
10990                    gnus-catchup-mark)
10991                   (while (gnus-summary-find-next (not all))
10992                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10993               (when (gnus-summary-first-subject (not all))
10994                 (while (and
10995                         (if to-here (< (point) to-here) t)
10996                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10997                         (gnus-summary-find-next (not all))))))
10998             (gnus-set-mode-line 'summary))
10999           t))
11000     (gnus-summary-position-point)))
11001
11002 (defun gnus-summary-catchup-to-here (&optional all)
11003   "Mark all unticked articles before the current one as read.
11004 If ALL is non-nil, also mark ticked and dormant articles as read."
11005   (interactive "P")
11006   (save-excursion
11007     (gnus-save-hidden-threads
11008       (let ((beg (point)))
11009         ;; We check that there are unread articles.
11010         (when (or all (gnus-summary-find-prev))
11011           (gnus-summary-catchup all t beg)))))
11012   (gnus-summary-position-point))
11013
11014 (defun gnus-summary-catchup-from-here (&optional all)
11015   "Mark all unticked articles after (and including) the current one as read.
11016 If ALL is non-nil, also mark ticked and dormant articles as read."
11017   (interactive "P")
11018   (save-excursion
11019     (gnus-save-hidden-threads
11020       (let ((beg (point)))
11021         ;; We check that there are unread articles.
11022         (when (or all (gnus-summary-find-next))
11023           (gnus-summary-catchup all t beg nil t)))))
11024   (gnus-summary-position-point))
11025
11026 (defun gnus-summary-catchup-all (&optional quietly)
11027   "Mark all articles in this newsgroup as read.
11028 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
11029 instead, which marks only unread articles as read."
11030   (interactive "P")
11031   (gnus-summary-catchup t quietly))
11032
11033 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11034   "Mark all unread articles in this group as read, then exit.
11035 If prefix argument ALL is non-nil, all articles are marked as read.
11036 If QUIETLY is non-nil, no questions will be asked."
11037   (interactive "P")
11038   (when (gnus-summary-catchup all quietly nil 'fast)
11039     ;; Select next newsgroup or exit.
11040     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
11041              (eq gnus-auto-select-next 'quietly))
11042         (gnus-summary-next-group nil)
11043       (gnus-summary-exit))))
11044
11045 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11046   "Mark all articles in this newsgroup as read, and then exit.
11047 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
11048 instead, which marks only unread articles as read."
11049   (interactive "P")
11050   (gnus-summary-catchup-and-exit t quietly))
11051
11052 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11053   "Mark all articles in this group as read and select the next group.
11054 If given a prefix, mark all articles, unread as well as ticked, as
11055 read."
11056   (interactive "P")
11057   (save-excursion
11058     (gnus-summary-catchup all))
11059   (gnus-summary-next-group))
11060
11061 (defun gnus-summary-catchup-and-goto-prev-group (&optional all)
11062   "Mark all articles in this group as read and select the previous group.
11063 If given a prefix, mark all articles, unread as well as ticked, as
11064 read."
11065   (interactive "P")
11066   (save-excursion
11067     (gnus-summary-catchup all))
11068   (gnus-summary-next-group nil nil t))
11069
11070 ;;;
11071 ;;; with article
11072 ;;;
11073
11074 (defmacro gnus-with-article (article &rest forms)
11075   "Select ARTICLE and perform FORMS in the original article buffer.
11076 Then replace the article with the result."
11077   `(progn
11078      ;; We don't want the article to be marked as read.
11079      (let (gnus-mark-article-hook)
11080        (gnus-summary-select-article t t nil ,article))
11081      (set-buffer gnus-original-article-buffer)
11082      ,@forms
11083      (if (not (gnus-check-backend-function
11084                'request-replace-article (car gnus-article-current)))
11085          (gnus-message 5 "Read-only group; not replacing")
11086        (unless (gnus-request-replace-article
11087                 ,article (car gnus-article-current)
11088                 (current-buffer) t)
11089          (error "Couldn't replace article")))
11090      ;; The cache and backlog have to be flushed somewhat.
11091      (when gnus-keep-backlog
11092        (gnus-backlog-remove-article
11093         (car gnus-article-current) (cdr gnus-article-current)))
11094      (when gnus-use-cache
11095        (gnus-cache-update-article
11096         (car gnus-article-current) (cdr gnus-article-current)))))
11097
11098 (put 'gnus-with-article 'lisp-indent-function 1)
11099 (put 'gnus-with-article 'edebug-form-spec '(form body))
11100
11101 ;; Thread-based commands.
11102
11103 (defun gnus-summary-articles-in-thread (&optional article)
11104   "Return a list of all articles in the current thread.
11105 If ARTICLE is non-nil, return all articles in the thread that starts
11106 with that article."
11107   (let* ((article (or article (gnus-summary-article-number)))
11108          (data (gnus-data-find-list article))
11109          (top-level (gnus-data-level (car data)))
11110          (top-subject
11111           (cond ((null gnus-thread-operation-ignore-subject)
11112                  (gnus-simplify-subject-re
11113                   (mail-header-subject (gnus-data-header (car data)))))
11114                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11115                  (gnus-simplify-subject-fuzzy
11116                   (mail-header-subject (gnus-data-header (car data)))))
11117                 (t nil)))
11118          (end-point (save-excursion
11119                       (if (gnus-summary-go-to-next-thread)
11120                           (point) (point-max))))
11121          articles)
11122     (while (and data
11123                 (< (gnus-data-pos (car data)) end-point))
11124       (when (or (not top-subject)
11125                 (string= top-subject
11126                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11127                              (gnus-simplify-subject-fuzzy
11128                               (mail-header-subject
11129                                (gnus-data-header (car data))))
11130                            (gnus-simplify-subject-re
11131                             (mail-header-subject
11132                              (gnus-data-header (car data)))))))
11133         (push (gnus-data-number (car data)) articles))
11134       (unless (and (setq data (cdr data))
11135                    (> (gnus-data-level (car data)) top-level))
11136         (setq data nil)))
11137     ;; Return the list of articles.
11138     (nreverse articles)))
11139
11140 (defun gnus-summary-rethread-current ()
11141   "Rethread the thread the current article is part of."
11142   (interactive)
11143   (let* ((gnus-show-threads t)
11144          (article (gnus-summary-article-number))
11145          (id (mail-header-id (gnus-summary-article-header)))
11146          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
11147     (unless id
11148       (error "No article on the current line"))
11149     (gnus-rebuild-thread id)
11150     (gnus-summary-goto-subject article)))
11151
11152 (defun gnus-summary-reparent-thread ()
11153   "Make the current article child of the marked (or previous) article.
11154
11155 Note that the re-threading will only work if `gnus-thread-ignore-subject'
11156 is non-nil or the Subject: of both articles are the same."
11157   (interactive)
11158   (unless (not (gnus-group-read-only-p))
11159     (error "The current newsgroup does not support article editing"))
11160   (unless (<= (length gnus-newsgroup-processable) 1)
11161     (error "No more than one article may be marked"))
11162   (let ((child (gnus-summary-article-number))
11163         ;; First grab the marked article, otherwise one line up.
11164         (parent (if (not (null gnus-newsgroup-processable))
11165                     (car gnus-newsgroup-processable)
11166                   (save-excursion
11167                     (if (eq (forward-line -1) 0)
11168                         (gnus-summary-article-number)
11169                       (error "Beginning of summary buffer"))))))
11170     (gnus-summary-reparent-children parent (list child))))
11171
11172 (defun gnus-summary-reparent-children (parent children)
11173   "Make PARENT the parent of CHILDREN.
11174 When called interactively, PARENT is the current article and CHILDREN
11175 are the process-marked articles."
11176   (interactive
11177    (list (gnus-summary-article-number)
11178          (gnus-summary-work-articles nil)))
11179   (dolist (child children)
11180     (save-window-excursion
11181       (let ((gnus-article-buffer " *reparent*"))
11182         (unless (not (eq parent child))
11183           (error "An article may not be self-referential"))
11184         (let ((message-id (mail-header-id
11185                            (gnus-summary-article-header parent))))
11186           (unless (and message-id (not (equal message-id "")))
11187             (error "No message-id in desired parent"))
11188           (gnus-with-article child
11189             (save-restriction
11190               (goto-char (point-min))
11191               (message-narrow-to-head)
11192               (if (re-search-forward "^References: " nil t)
11193                   (progn
11194                     (re-search-forward "^[^ \t]" nil t)
11195                     (forward-line -1)
11196                     (end-of-line)
11197                     (insert " " message-id))
11198                 (insert "References: " message-id "\n"))))
11199           (set-buffer gnus-summary-buffer)
11200           (gnus-summary-unmark-all-processable)
11201           (gnus-summary-update-article child)
11202           (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
11203             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
11204           (gnus-summary-rethread-current)
11205           (gnus-message 3 "Article %d is now the child of article %d"
11206                         child parent))))))
11207
11208 (defun gnus-summary-toggle-threads (&optional arg)
11209   "Toggle showing conversation threads.
11210 If ARG is positive number, turn showing conversation threads on."
11211   (interactive "P")
11212   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11213     (setq gnus-show-threads
11214           (if (null arg) (not gnus-show-threads)
11215             (> (prefix-numeric-value arg) 0)))
11216     (gnus-summary-prepare)
11217     (gnus-summary-goto-subject current)
11218     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
11219     (gnus-summary-position-point)))
11220
11221 (defun gnus-summary-show-all-threads ()
11222   "Show all threads."
11223   (interactive)
11224   (save-excursion
11225     (let ((buffer-read-only nil))
11226       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11227   (gnus-summary-position-point))
11228
11229 (defun gnus-summary-show-thread ()
11230   "Show thread subtrees.
11231 Returns nil if no thread was there to be shown."
11232   (interactive)
11233   (let ((buffer-read-only nil)
11234         (orig (point))
11235         (end (point-at-eol))
11236         ;; Leave point at bol
11237         (beg (progn (beginning-of-line) (point))))
11238     (prog1
11239         ;; Any hidden lines here?
11240         (search-forward "\r" end t)
11241       (subst-char-in-region beg end ?\^M ?\n t)
11242       (goto-char orig)
11243       (gnus-summary-position-point))))
11244
11245 (defun gnus-summary-maybe-hide-threads ()
11246   "If requested, hide the threads that should be hidden."
11247   (when (and gnus-show-threads
11248              gnus-thread-hide-subtree)
11249     (gnus-summary-hide-all-threads
11250      (if (or (consp gnus-thread-hide-subtree)
11251              (functionp gnus-thread-hide-subtree))
11252          (gnus-make-predicate gnus-thread-hide-subtree)
11253        nil))))
11254
11255 ;;; Hiding predicates.
11256
11257 (defun gnus-article-unread-p (header)
11258   (memq (mail-header-number header) gnus-newsgroup-unreads))
11259
11260 (defun gnus-article-unseen-p (header)
11261   (memq (mail-header-number header) gnus-newsgroup-unseen))
11262
11263 (defun gnus-map-articles (predicate articles)
11264   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
11265   (apply 'gnus-or (mapcar predicate
11266                           (mapcar (lambda (number)
11267                                     (gnus-summary-article-header number))
11268                                   articles))))
11269
11270 (defun gnus-summary-hide-all-threads (&optional predicate)
11271   "Hide all thread subtrees.
11272 If PREDICATE is supplied, threads that satisfy this predicate
11273 will not be hidden."
11274   (interactive)
11275   (save-excursion
11276     (goto-char (point-min))
11277     (let ((end nil))
11278       (while (not end)
11279         (when (or (not predicate)
11280                   (gnus-map-articles
11281                    predicate (gnus-summary-article-children)))
11282             (gnus-summary-hide-thread))
11283         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
11284   (gnus-summary-position-point))
11285
11286 (defun gnus-summary-hide-thread ()
11287   "Hide thread subtrees.
11288 If PREDICATE is supplied, threads that satisfy this predicate
11289 will not be hidden.
11290 Returns nil if no threads were there to be hidden."
11291   (interactive)
11292   (let ((buffer-read-only nil)
11293         (start (point))
11294         (article (gnus-summary-article-number)))
11295     (goto-char start)
11296     ;; Go forward until either the buffer ends or the subthread
11297     ;; ends.
11298     (when (and (not (eobp))
11299                (or (zerop (gnus-summary-next-thread 1 t))
11300                    (goto-char (point-max))))
11301       (prog1
11302           (if (and (> (point) start)
11303                    (search-backward "\n" start t))
11304               (progn
11305                 (subst-char-in-region start (point) ?\n ?\^M)
11306                 (gnus-summary-goto-subject article))
11307             (goto-char start)
11308             nil)))))
11309
11310 (defun gnus-summary-go-to-next-thread (&optional previous)
11311   "Go to the same level (or less) next thread.
11312 If PREVIOUS is non-nil, go to previous thread instead.
11313 Return the article number moved to, or nil if moving was impossible."
11314   (let ((level (gnus-summary-thread-level))
11315         (way (if previous -1 1))
11316         (beg (point)))
11317     (forward-line way)
11318     (while (and (not (eobp))
11319                 (< level (gnus-summary-thread-level)))
11320       (forward-line way))
11321     (if (eobp)
11322         (progn
11323           (goto-char beg)
11324           nil)
11325       (setq beg (point))
11326       (prog1
11327           (gnus-summary-article-number)
11328         (goto-char beg)))))
11329
11330 (defun gnus-summary-next-thread (n &optional silent)
11331   "Go to the same level next N'th thread.
11332 If N is negative, search backward instead.
11333 Returns the difference between N and the number of skips actually
11334 done.
11335
11336 If SILENT, don't output messages."
11337   (interactive "p")
11338   (let ((backward (< n 0))
11339         (n (abs n)))
11340     (while (and (> n 0)
11341                 (gnus-summary-go-to-next-thread backward))
11342       (decf n))
11343     (unless silent
11344       (gnus-summary-position-point))
11345     (when (and (not silent) (/= 0 n))
11346       (gnus-message 7 "No more threads"))
11347     n))
11348
11349 (defun gnus-summary-prev-thread (n)
11350   "Go to the same level previous N'th thread.
11351 Returns the difference between N and the number of skips actually
11352 done."
11353   (interactive "p")
11354   (gnus-summary-next-thread (- n)))
11355
11356 (defun gnus-summary-go-down-thread ()
11357   "Go down one level in the current thread."
11358   (let ((children (gnus-summary-article-children)))
11359     (when children
11360       (gnus-summary-goto-subject (car children)))))
11361
11362 (defun gnus-summary-go-up-thread ()
11363   "Go up one level in the current thread."
11364   (let ((parent (gnus-summary-article-parent)))
11365     (when parent
11366       (gnus-summary-goto-subject parent))))
11367
11368 (defun gnus-summary-down-thread (n)
11369   "Go down thread N steps.
11370 If N is negative, go up instead.
11371 Returns the difference between N and how many steps down that were
11372 taken."
11373   (interactive "p")
11374   (let ((up (< n 0))
11375         (n (abs n)))
11376     (while (and (> n 0)
11377                 (if up (gnus-summary-go-up-thread)
11378                   (gnus-summary-go-down-thread)))
11379       (setq n (1- n)))
11380     (gnus-summary-position-point)
11381     (when (/= 0 n)
11382       (gnus-message 7 "Can't go further"))
11383     n))
11384
11385 (defun gnus-summary-up-thread (n)
11386   "Go up thread N steps.
11387 If N is negative, go down instead.
11388 Returns the difference between N and how many steps down that were
11389 taken."
11390   (interactive "p")
11391   (gnus-summary-down-thread (- n)))
11392
11393 (defun gnus-summary-top-thread ()
11394   "Go to the top of the thread."
11395   (interactive)
11396   (while (gnus-summary-go-up-thread))
11397   (gnus-summary-article-number))
11398
11399 (defun gnus-summary-expire-thread ()
11400   "Mark articles under current thread as expired."
11401   (interactive)
11402   (gnus-summary-kill-thread 0))
11403
11404 (defun gnus-summary-kill-thread (&optional unmark)
11405   "Mark articles under current thread as read.
11406 If the prefix argument is positive, remove any kinds of marks.
11407 If the prefix argument is zero, mark thread as expired.
11408 If the prefix argument is negative, tick articles instead."
11409   (interactive "P")
11410   (when unmark
11411     (setq unmark (prefix-numeric-value unmark)))
11412   (let ((articles (gnus-summary-articles-in-thread))
11413         (hide (or (null unmark) (= unmark 0))))
11414     (save-excursion
11415       ;; Expand the thread.
11416       (gnus-summary-show-thread)
11417       ;; Mark all the articles.
11418       (while articles
11419         (gnus-summary-goto-subject (car articles))
11420         (cond ((null unmark)
11421                (gnus-summary-mark-article-as-read gnus-killed-mark))
11422               ((> unmark 0)
11423                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11424               ((= unmark 0)
11425                (gnus-summary-mark-article-as-unread gnus-expirable-mark))
11426               (t
11427                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11428         (setq articles (cdr articles))))
11429     ;; Hide killed subtrees when hide is true.
11430     (and hide
11431          gnus-thread-hide-killed
11432          (gnus-summary-hide-thread))
11433     ;; If hide is t, go to next unread subject.
11434     (when hide
11435       ;; Go to next unread subject.
11436       (gnus-summary-next-subject 1 t)))
11437   (gnus-set-mode-line 'summary))
11438
11439 ;; Summary sorting commands
11440
11441 (defun gnus-summary-sort-by-number (&optional reverse)
11442   "Sort the summary buffer by article number.
11443 Argument REVERSE means reverse order."
11444   (interactive "P")
11445   (gnus-summary-sort 'number reverse))
11446
11447 (defun gnus-summary-sort-by-random (&optional reverse)
11448   "Randomize the order in the summary buffer.
11449 Argument REVERSE means to randomize in reverse order."
11450   (interactive "P")
11451   (gnus-summary-sort 'random reverse))
11452
11453 (defun gnus-summary-sort-by-author (&optional reverse)
11454   "Sort the summary buffer by author name alphabetically.
11455 If `case-fold-search' is non-nil, case of letters is ignored.
11456 Argument REVERSE means reverse order."
11457   (interactive "P")
11458   (gnus-summary-sort 'author reverse))
11459
11460 (defun gnus-summary-sort-by-recipient (&optional reverse)
11461   "Sort the summary buffer by recipient name alphabetically.
11462 If `case-fold-search' is non-nil, case of letters is ignored.
11463 Argument REVERSE means reverse order."
11464   (interactive "P")
11465   (gnus-summary-sort 'recipient reverse))
11466
11467 (defun gnus-summary-sort-by-subject (&optional reverse)
11468   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
11469 If `case-fold-search' is non-nil, case of letters is ignored.
11470 Argument REVERSE means reverse order."
11471   (interactive "P")
11472   (gnus-summary-sort 'subject reverse))
11473
11474 (defun gnus-summary-sort-by-date (&optional reverse)
11475   "Sort the summary buffer by date.
11476 Argument REVERSE means reverse order."
11477   (interactive "P")
11478   (gnus-summary-sort 'date reverse))
11479
11480 (defun gnus-summary-sort-by-score (&optional reverse)
11481   "Sort the summary buffer by score.
11482 Argument REVERSE means reverse order."
11483   (interactive "P")
11484   (gnus-summary-sort 'score reverse))
11485
11486 (defun gnus-summary-sort-by-lines (&optional reverse)
11487   "Sort the summary buffer by the number of lines.
11488 Argument REVERSE means reverse order."
11489   (interactive "P")
11490   (gnus-summary-sort 'lines reverse))
11491
11492 (defun gnus-summary-sort-by-chars (&optional reverse)
11493   "Sort the summary buffer by article length.
11494 Argument REVERSE means reverse order."
11495   (interactive "P")
11496   (gnus-summary-sort 'chars reverse))
11497
11498 (defun gnus-summary-sort-by-original (&optional reverse)
11499   "Sort the summary buffer using the default sorting method.
11500 Argument REVERSE means reverse order."
11501   (interactive "P")
11502   (let* ((buffer-read-only)
11503          (gnus-summary-prepare-hook nil))
11504     ;; We do the sorting by regenerating the threads.
11505     (gnus-summary-prepare)
11506     ;; Hide subthreads if needed.
11507     (gnus-summary-maybe-hide-threads)))
11508
11509 (defun gnus-summary-sort (predicate reverse)
11510   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11511   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
11512          (article (intern (format "gnus-article-sort-by-%s" predicate)))
11513          (gnus-thread-sort-functions
11514           (if (not reverse)
11515               thread
11516             `(lambda (t1 t2)
11517                (,thread t2 t1))))
11518          (gnus-sort-gathered-threads-function
11519           gnus-thread-sort-functions)
11520          (gnus-article-sort-functions
11521           (if (not reverse)
11522               article
11523             `(lambda (t1 t2)
11524                (,article t2 t1))))
11525          (buffer-read-only)
11526          (gnus-summary-prepare-hook nil))
11527     ;; We do the sorting by regenerating the threads.
11528     (gnus-summary-prepare)
11529     ;; Hide subthreads if needed.
11530     (gnus-summary-maybe-hide-threads)))
11531
11532 ;; Summary saving commands.
11533
11534 (defun gnus-summary-save-article (&optional n not-saved)
11535   "Save the current article using the default saver function.
11536 If N is a positive number, save the N next articles.
11537 If N is a negative number, save the N previous articles.
11538 If N is nil and any articles have been marked with the process mark,
11539 save those articles instead.
11540 The variable `gnus-default-article-saver' specifies the saver function.
11541
11542 If the optional second argument NOT-SAVED is non-nil, articles saved
11543 will not be marked as saved."
11544   (interactive "P")
11545   (require 'gnus-art)
11546   (let* ((articles (gnus-summary-work-articles n))
11547          (save-buffer (save-excursion
11548                         (nnheader-set-temp-buffer " *Gnus Save*")))
11549          (num (length articles))
11550          ;; Whether to save decoded articles or raw articles.
11551          (decode (when gnus-article-save-coding-system
11552                    (get gnus-default-article-saver :decode)))
11553          ;; When saving many articles in a single file, use the other
11554          ;; function to save articles other than the first one.
11555          (saver2 (get gnus-default-article-saver :function))
11556          (gnus-prompt-before-saving (if saver2
11557                                         t
11558                                       gnus-prompt-before-saving))
11559          (gnus-default-article-saver gnus-default-article-saver)
11560          header file)
11561     (dolist (article articles)
11562       (setq header (gnus-summary-article-header article))
11563       (if (not (vectorp header))
11564           ;; This is a pseudo-article.
11565           (if (assq 'name header)
11566               (gnus-copy-file (cdr (assq 'name header)))
11567             (gnus-message 1 "Article %d is unsaveable" article))
11568         ;; This is a real article.
11569         (save-window-excursion
11570           (let ((gnus-display-mime-function (when decode
11571                                               gnus-display-mime-function))
11572                 (gnus-article-prepare-hook (when decode
11573                                              gnus-article-prepare-hook)))
11574             (gnus-summary-select-article t nil nil article)
11575             (gnus-summary-goto-subject article)))
11576         (save-excursion
11577           (set-buffer save-buffer)
11578           (erase-buffer)
11579           (insert-buffer-substring (if decode
11580                                        gnus-article-buffer
11581                                      gnus-original-article-buffer)))
11582         (setq file (gnus-article-save save-buffer file num))
11583         (gnus-summary-remove-process-mark article)
11584         (unless not-saved
11585           (gnus-summary-set-saved-mark article)))
11586       (when saver2
11587         (setq gnus-default-article-saver saver2
11588               saver2 nil)))
11589     (gnus-kill-buffer save-buffer)
11590     (gnus-summary-position-point)
11591     (gnus-set-mode-line 'summary)
11592     n))
11593
11594 (defun gnus-summary-pipe-output (&optional arg headers)
11595   "Pipe the current article to a subprocess.
11596 If N is a positive number, pipe the N next articles.
11597 If N is a negative number, pipe the N previous articles.
11598 If N is nil and any articles have been marked with the process mark,
11599 pipe those articles instead.
11600 If HEADERS (the symbolic prefix), include the headers, too."
11601   (interactive (gnus-interactive "P\ny"))
11602   (require 'gnus-art)
11603   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11604         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11605     (gnus-summary-save-article arg t))
11606   (let ((buffer (get-buffer "*Shell Command Output*")))
11607     (when (and buffer
11608                (not (zerop (buffer-size buffer))))
11609       (gnus-configure-windows 'pipe))))
11610
11611 (defun gnus-summary-save-article-mail (&optional arg)
11612   "Append the current article to a Unix mail box file.
11613 If N is a positive number, save the N next articles.
11614 If N is a negative number, save the N previous articles.
11615 If N is nil and any articles have been marked with the process mark,
11616 save those articles instead."
11617   (interactive "P")
11618   (require 'gnus-art)
11619   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11620     (gnus-summary-save-article arg)))
11621
11622 (defun gnus-summary-save-article-rmail (&optional arg)
11623   "Append the current article to an rmail file.
11624 If N is a positive number, save the N next articles.
11625 If N is a negative number, save the N previous articles.
11626 If N is nil and any articles have been marked with the process mark,
11627 save those articles instead."
11628   (interactive "P")
11629   (require 'gnus-art)
11630   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11631     (gnus-summary-save-article arg)))
11632
11633 (defun gnus-summary-save-article-file (&optional arg)
11634   "Append the current article to a file.
11635 If N is a positive number, save the N next articles.
11636 If N is a negative number, save the N previous articles.
11637 If N is nil and any articles have been marked with the process mark,
11638 save those articles instead."
11639   (interactive "P")
11640   (require 'gnus-art)
11641   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11642     (gnus-summary-save-article arg)))
11643
11644 (defun gnus-summary-write-article-file (&optional arg)
11645   "Write the current article to a file, deleting the previous file.
11646 If N is a positive number, save the N next articles.
11647 If N is a negative number, save the N previous articles.
11648 If N is nil and any articles have been marked with the process mark,
11649 save those articles instead."
11650   (interactive "P")
11651   (require 'gnus-art)
11652   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11653     (gnus-summary-save-article arg)))
11654
11655 (defun gnus-summary-save-article-body-file (&optional arg)
11656   "Append the current article body to a file.
11657 If N is a positive number, save the N next articles.
11658 If N is a negative number, save the N previous articles.
11659 If N is nil and any articles have been marked with the process mark,
11660 save those articles instead."
11661   (interactive "P")
11662   (require 'gnus-art)
11663   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11664     (gnus-summary-save-article arg)))
11665
11666 (defun gnus-summary-write-article-body-file (&optional arg)
11667   "Write the current article body to a file, deleting the previous file.
11668 If N is a positive number, save the N next articles.
11669 If N is a negative number, save the N previous articles.
11670 If N is nil and any articles have been marked with the process mark,
11671 save those articles instead."
11672   (interactive "P")
11673   (require 'gnus-art)
11674   (let ((gnus-default-article-saver 'gnus-summary-write-body-to-file))
11675     (gnus-summary-save-article arg)))
11676
11677 (defun gnus-summary-muttprint (&optional arg)
11678   "Print the current article using Muttprint.
11679 If N is a positive number, save the N next articles.
11680 If N is a negative number, save the N previous articles.
11681 If N is nil and any articles have been marked with the process mark,
11682 save those articles instead."
11683   (interactive "P")
11684   (require 'gnus-art)
11685   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11686     (gnus-summary-save-article arg t)))
11687
11688 (defun gnus-summary-pipe-message (program)
11689   "Pipe the current article through PROGRAM."
11690   (interactive "sProgram: ")
11691   (gnus-summary-select-article)
11692   (let ((mail-header-separator ""))
11693     (gnus-eval-in-buffer-window gnus-article-buffer
11694       (save-restriction
11695         (widen)
11696         (let ((start (window-start))
11697               buffer-read-only)
11698           (message-pipe-buffer-body program)
11699           (set-window-start (get-buffer-window (current-buffer)) start))))))
11700
11701 (defun gnus-get-split-value (methods)
11702   "Return a value based on the split METHODS."
11703   (let (split-name method result match)
11704     (when methods
11705       (save-excursion
11706         (set-buffer gnus-original-article-buffer)
11707         (save-restriction
11708           (nnheader-narrow-to-headers)
11709           (while (and methods (not split-name))
11710             (goto-char (point-min))
11711             (setq method (pop methods))
11712             (setq match (car method))
11713             (when (cond
11714                    ((stringp match)
11715                     ;; Regular expression.
11716                     (ignore-errors
11717                       (re-search-forward match nil t)))
11718                    ((functionp match)
11719                     ;; Function.
11720                     (save-restriction
11721                       (widen)
11722                       (setq result (funcall match gnus-newsgroup-name))))
11723                    ((consp match)
11724                     ;; Form.
11725                     (save-restriction
11726                       (widen)
11727                       (setq result (eval match)))))
11728               (setq split-name (cdr method))
11729               (cond ((stringp result)
11730                      (push (expand-file-name
11731                             result gnus-article-save-directory)
11732                            split-name))
11733                     ((consp result)
11734                      (setq split-name (append result split-name)))))))))
11735     (nreverse split-name)))
11736
11737 (defun gnus-valid-move-group-p (group)
11738   (and (boundp group)
11739        (symbol-name group)
11740        (symbol-value group)
11741        (gnus-get-function (gnus-find-method-for-group
11742                            (symbol-name group)) 'request-accept-article t)))
11743
11744 (defun gnus-read-move-group-name (prompt default articles prefix)
11745   "Read a group name."
11746   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11747          (minibuffer-confirm-incomplete nil) ; XEmacs
11748          (prom
11749           (format "%s %s to"
11750                   prompt
11751                   (if (> (length articles) 1)
11752                       (format "these %d articles" (length articles))
11753                     "this article")))
11754          (to-newsgroup
11755           (let (active group)
11756             (when (or (null split-name) (= 1 (length split-name)))
11757               (setq active (gnus-make-hashtable (length gnus-active-hashtb)))
11758               (mapatoms (lambda (symbol)
11759                           (setq group (symbol-name symbol))
11760                           (when (string-match "[^\000-\177]" group)
11761                             (setq group (gnus-group-decoded-name group)))
11762                           (set (intern group active) group))
11763                         gnus-active-hashtb))
11764             (cond
11765              ((null split-name)
11766               (gnus-completing-read-with-default
11767                default prom active 'gnus-valid-move-group-p nil prefix
11768                'gnus-group-history))
11769              ((= 1 (length split-name))
11770               (gnus-completing-read-with-default
11771                (car split-name) prom active 'gnus-valid-move-group-p nil nil
11772                'gnus-group-history))
11773              (t
11774               (gnus-completing-read-with-default
11775                nil prom (mapcar 'list (nreverse split-name)) nil nil nil
11776                'gnus-group-history)))))
11777          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup)))
11778          encoded)
11779     (when to-newsgroup
11780       (if (or (string= to-newsgroup "")
11781               (string= to-newsgroup prefix))
11782           (setq to-newsgroup default))
11783       (unless to-newsgroup
11784         (error "No group name entered"))
11785       (setq encoded (mm-encode-coding-string
11786                      to-newsgroup
11787                      (gnus-group-name-charset to-method to-newsgroup)))
11788       (or (gnus-active encoded)
11789           (gnus-activate-group encoded nil nil to-method)
11790           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11791                                      to-newsgroup))
11792               (or (and (gnus-request-create-group encoded to-method)
11793                        (gnus-activate-group encoded nil nil to-method)
11794                        (gnus-subscribe-group encoded))
11795                   (error "Couldn't create group %s" to-newsgroup)))
11796           (error "No such group: %s" to-newsgroup))
11797       encoded)))
11798
11799 (defvar gnus-summary-save-parts-counter)
11800
11801 (defun gnus-summary-save-parts (type dir n &optional reverse)
11802   "Save parts matching TYPE to DIR.
11803 If REVERSE, save parts that do not match TYPE."
11804   (interactive
11805    (list (read-string "Save parts of type: "
11806                       (or (car gnus-summary-save-parts-type-history)
11807                           gnus-summary-save-parts-default-mime)
11808                       'gnus-summary-save-parts-type-history)
11809          (setq gnus-summary-save-parts-last-directory
11810                (read-file-name "Save to directory: "
11811                                gnus-summary-save-parts-last-directory
11812                                nil t))
11813          current-prefix-arg))
11814   (gnus-summary-iterate n
11815     (let ((gnus-display-mime-function nil)
11816           (gnus-inhibit-treatment t))
11817       (gnus-summary-select-article))
11818     (save-excursion
11819       (set-buffer gnus-article-buffer)
11820       (let ((handles (or gnus-article-mime-handles
11821                          (mm-dissect-buffer nil gnus-article-loose-mime)
11822                          (and gnus-article-emulate-mime
11823                               (mm-uu-dissect))))
11824             (gnus-summary-save-parts-counter 1))
11825         (when handles
11826           (gnus-summary-save-parts-1 type dir handles reverse)
11827           (unless gnus-article-mime-handles ;; Don't destroy this case.
11828             (mm-destroy-parts handles)))))))
11829
11830 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11831   (if (stringp (car handle))
11832       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11833               (cdr handle))
11834     (when (if reverse
11835               (not (string-match type (mm-handle-media-type handle)))
11836             (string-match type (mm-handle-media-type handle)))
11837       (let ((file (expand-file-name
11838                    (gnus-map-function
11839                     mm-file-name-rewrite-functions
11840                     (file-name-nondirectory
11841                      (or
11842                       (mail-content-type-get
11843                        (mm-handle-disposition handle) 'filename)
11844                       (mail-content-type-get
11845                        (mm-handle-type handle) 'name)
11846                       (format "%s.%d.%d" gnus-newsgroup-name
11847                               (cdr gnus-article-current)
11848                               gnus-summary-save-parts-counter))))
11849                    dir)))
11850         (incf gnus-summary-save-parts-counter)
11851         (unless (file-exists-p file)
11852           (mm-save-part-to-file handle file))))))
11853
11854 ;; Summary extract commands
11855
11856 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11857   (let ((buffer-read-only nil)
11858         (article (gnus-summary-article-number))
11859         after-article b e)
11860     (unless (gnus-summary-goto-subject article)
11861       (error "No such article: %d" article))
11862     (gnus-summary-position-point)
11863     ;; If all commands are to be bunched up on one line, we collect
11864     ;; them here.
11865     (unless gnus-view-pseudos-separately
11866       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11867             files action)
11868         (while ps
11869           (setq action (cdr (assq 'action (car ps))))
11870           (setq files (list (cdr (assq 'name (car ps)))))
11871           (while (and ps (cdr ps)
11872                       (string= (or action "1")
11873                                (or (cdr (assq 'action (cadr ps))) "2")))
11874             (push (cdr (assq 'name (cadr ps))) files)
11875             (setcdr ps (cddr ps)))
11876           (when files
11877             (when (not (string-match "%s" action))
11878               (push " " files))
11879             (push " " files)
11880             (when (assq 'execute (car ps))
11881               (setcdr (assq 'execute (car ps))
11882                       (funcall (if (string-match "%s" action)
11883                                    'format 'concat)
11884                                action
11885                                (mapconcat
11886                                 (lambda (f)
11887                                   (if (equal f " ")
11888                                       f
11889                                     (shell-quote-argument f)))
11890                                 files " ")))))
11891           (setq ps (cdr ps)))))
11892     (if (and gnus-view-pseudos (not not-view))
11893         (while pslist
11894           (when (assq 'execute (car pslist))
11895             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11896                                   (eq gnus-view-pseudos 'not-confirm)))
11897           (setq pslist (cdr pslist)))
11898       (save-excursion
11899         (while pslist
11900           (setq after-article (or (cdr (assq 'article (car pslist)))
11901                                   (gnus-summary-article-number)))
11902           (gnus-summary-goto-subject after-article)
11903           (forward-line 1)
11904           (setq b (point))
11905           (insert "    " (file-name-nondirectory
11906                           (cdr (assq 'name (car pslist))))
11907                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11908           (setq e (point))
11909           (forward-line -1)             ; back to `b'
11910           (gnus-add-text-properties
11911            b (1- e) (list 'gnus-number gnus-reffed-article-number
11912                           gnus-mouse-face-prop gnus-mouse-face))
11913           (gnus-data-enter
11914            after-article gnus-reffed-article-number
11915            gnus-unread-mark b (car pslist) 0 (- e b))
11916           (setq gnus-newsgroup-unreads
11917                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11918                                          gnus-reffed-article-number))
11919           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11920           (setq pslist (cdr pslist)))))))
11921
11922 (defun gnus-pseudos< (p1 p2)
11923   (let ((c1 (cdr (assq 'action p1)))
11924         (c2 (cdr (assq 'action p2))))
11925     (and c1 c2 (string< c1 c2))))
11926
11927 (defun gnus-request-pseudo-article (props)
11928   (cond ((assq 'execute props)
11929          (gnus-execute-command (cdr (assq 'execute props)))))
11930   (let ((gnus-current-article (gnus-summary-article-number)))
11931     (gnus-run-hooks 'gnus-mark-article-hook)))
11932
11933 (defun gnus-execute-command (command &optional automatic)
11934   (save-excursion
11935     (gnus-article-setup-buffer)
11936     (set-buffer gnus-article-buffer)
11937     (setq buffer-read-only nil)
11938     (let ((command (if automatic command
11939                      (read-string "Command: " (cons command 0)))))
11940       (erase-buffer)
11941       (insert "$ " command "\n\n")
11942       (if gnus-view-pseudo-asynchronously
11943           (start-process "gnus-execute" (current-buffer) shell-file-name
11944                          shell-command-switch command)
11945         (call-process shell-file-name nil t nil
11946                       shell-command-switch command)))))
11947
11948 ;; Summary kill commands.
11949
11950 (defun gnus-summary-edit-global-kill (article)
11951   "Edit the \"global\" kill file."
11952   (interactive (list (gnus-summary-article-number)))
11953   (gnus-group-edit-global-kill article))
11954
11955 (defun gnus-summary-edit-local-kill ()
11956   "Edit a local kill file applied to the current newsgroup."
11957   (interactive)
11958   (setq gnus-current-headers (gnus-summary-article-header))
11959   (gnus-group-edit-local-kill
11960    (gnus-summary-article-number) gnus-newsgroup-name))
11961
11962 ;;; Header reading.
11963
11964 (defun gnus-read-header (id &optional header)
11965   "Read the headers of article ID and enter them into the Gnus system."
11966   (let ((group gnus-newsgroup-name)
11967         (gnus-override-method
11968          (or
11969           gnus-override-method
11970           (and (gnus-news-group-p gnus-newsgroup-name)
11971                (car (gnus-refer-article-methods)))))
11972         where)
11973     ;; First we check to see whether the header in question is already
11974     ;; fetched.
11975     (if (stringp id)
11976         ;; This is a Message-ID.
11977         (setq header (or header (gnus-id-to-header id)))
11978       ;; This is an article number.
11979       (setq header (or header (gnus-summary-article-header id))))
11980     (if (and header
11981              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11982         ;; We have found the header.
11983         header
11984       ;; We have to really fetch the header to this article.
11985       (save-excursion
11986         (set-buffer nntp-server-buffer)
11987         (when (setq where (gnus-request-head id group))
11988           (nnheader-fold-continuation-lines)
11989           (goto-char (point-max))
11990           (insert ".\n")
11991           (goto-char (point-min))
11992           (insert "211 ")
11993           (princ (cond
11994                   ((numberp id) id)
11995                   ((cdr where) (cdr where))
11996                   (header (mail-header-number header))
11997                   (t gnus-reffed-article-number))
11998                  (current-buffer))
11999           (insert " Article retrieved.\n"))
12000         (if (or (not where)
12001                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
12002             ()                          ; Malformed head.
12003           (unless (gnus-summary-article-sparse-p (mail-header-number header))
12004             (when (and (stringp id)
12005                        (or
12006                         (not (string= (gnus-group-real-name group)
12007                                       (car where)))
12008                         (not (gnus-server-equal gnus-override-method
12009                                                 (gnus-group-method group)))))
12010               ;; If we fetched by Message-ID and the article came from
12011               ;; a different group (or server), we fudge some bogus
12012               ;; article numbers for this article.
12013               (mail-header-set-number header gnus-reffed-article-number))
12014             (save-excursion
12015               (set-buffer gnus-summary-buffer)
12016               (decf gnus-reffed-article-number)
12017               (gnus-remove-header (mail-header-number header))
12018               (push header gnus-newsgroup-headers)
12019               (setq gnus-current-headers header)
12020               (push (mail-header-number header) gnus-newsgroup-limit)))
12021           header)))))
12022
12023 (defun gnus-remove-header (number)
12024   "Remove header NUMBER from `gnus-newsgroup-headers'."
12025   (if (and gnus-newsgroup-headers
12026            (= number (mail-header-number (car gnus-newsgroup-headers))))
12027       (pop gnus-newsgroup-headers)
12028     (let ((headers gnus-newsgroup-headers))
12029       (while (and (cdr headers)
12030                   (not (= number (mail-header-number (cadr headers)))))
12031         (pop headers))
12032       (when (cdr headers)
12033         (setcdr headers (cddr headers))))))
12034
12035 ;;;
12036 ;;; summary highlights
12037 ;;;
12038
12039 (defun gnus-highlight-selected-summary ()
12040   "Highlight selected article in summary buffer."
12041   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
12042   (when gnus-summary-selected-face
12043     (save-excursion
12044       (let* ((beg (point-at-bol))
12045              (end (point-at-eol))
12046              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
12047              (from (if (get-text-property beg gnus-mouse-face-prop)
12048                        beg
12049                      (or (next-single-property-change
12050                           beg gnus-mouse-face-prop nil end)
12051                          beg)))
12052              (to
12053               (if (= from end)
12054                   (- from 2)
12055                 (or (next-single-property-change
12056                      from gnus-mouse-face-prop nil end)
12057                     end))))
12058         ;; If no mouse-face prop on line we will have to = from = end,
12059         ;; so we highlight the entire line instead.
12060         (when (= (+ to 2) from)
12061           (setq from beg)
12062           (setq to end))
12063         (if gnus-newsgroup-selected-overlay
12064             ;; Move old overlay.
12065             (gnus-move-overlay
12066              gnus-newsgroup-selected-overlay from to (current-buffer))
12067           ;; Create new overlay.
12068           (gnus-overlay-put
12069            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
12070            'face gnus-summary-selected-face))))))
12071
12072 (defvar gnus-summary-highlight-line-cached nil)
12073 (defvar gnus-summary-highlight-line-trigger nil)
12074
12075 (defun gnus-summary-highlight-line-0 ()
12076   (if (and (eq gnus-summary-highlight-line-trigger
12077                gnus-summary-highlight)
12078            gnus-summary-highlight-line-cached)
12079       gnus-summary-highlight-line-cached
12080     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
12081           gnus-summary-highlight-line-cached
12082           (let* ((cond (list 'cond))
12083                  (c cond)
12084                  (list gnus-summary-highlight))
12085             (while list
12086               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
12087                               nil))
12088               (setq c (cdr c)
12089                     list (cdr list)))
12090             (gnus-byte-compile (list 'lambda nil cond))))))
12091
12092 (defun gnus-summary-highlight-line ()
12093   "Highlight current line according to `gnus-summary-highlight'."
12094   (let* ((beg (point-at-bol))
12095          (article (or (gnus-summary-article-number) gnus-current-article))
12096          (score (or (cdr (assq article
12097                                gnus-newsgroup-scored))
12098                     gnus-summary-default-score 0))
12099          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
12100          (inhibit-read-only t)
12101          (default gnus-summary-default-score)
12102          (default-high gnus-summary-default-high-score)
12103          (default-low gnus-summary-default-low-score)
12104          (uncached (and gnus-summary-use-undownloaded-faces
12105                         (memq article gnus-newsgroup-undownloaded)
12106                         (not (memq article gnus-newsgroup-cached)))))
12107     (let ((face (funcall (gnus-summary-highlight-line-0))))
12108       (unless (eq face (get-text-property beg 'face))
12109         (gnus-put-text-property-excluding-characters-with-faces
12110          beg (point-at-eol) 'face
12111          (setq face (if (boundp face) (symbol-value face) face)))
12112         (when gnus-summary-highlight-line-function
12113           (funcall gnus-summary-highlight-line-function article face))))))
12114
12115 (defun gnus-update-read-articles (group unread &optional compute)
12116   "Update the list of read articles in GROUP.
12117 UNREAD is a sorted list."
12118   (let ((active (or gnus-newsgroup-active (gnus-active group)))
12119         (info (gnus-get-info group))
12120         (prev 1)
12121         read)
12122     (if (or (not info) (not active))
12123         ;; There is no info on this group if it was, in fact,
12124         ;; killed.  Gnus stores no information on killed groups, so
12125         ;; there's nothing to be done.
12126         ;; One could store the information somewhere temporarily,
12127         ;; perhaps...  Hmmm...
12128         ()
12129       ;; Remove any negative articles numbers.
12130       (while (and unread (< (car unread) 0))
12131         (setq unread (cdr unread)))
12132       ;; Remove any expired article numbers
12133       (while (and unread (< (car unread) (car active)))
12134         (setq unread (cdr unread)))
12135       ;; Compute the ranges of read articles by looking at the list of
12136       ;; unread articles.
12137       (while unread
12138         (when (/= (car unread) prev)
12139           (push (if (= prev (1- (car unread))) prev
12140                   (cons prev (1- (car unread))))
12141                 read))
12142         (setq prev (1+ (car unread)))
12143         (setq unread (cdr unread)))
12144       (when (<= prev (cdr active))
12145         (push (cons prev (cdr active)) read))
12146       (setq read (if (> (length read) 1) (nreverse read) read))
12147       (if compute
12148           read
12149         (save-excursion
12150           (let (setmarkundo)
12151             ;; Propagate the read marks to the backend.
12152             (when (gnus-check-backend-function 'request-set-mark group)
12153               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
12154                     (add (gnus-remove-from-range read (gnus-info-read info))))
12155                 (when (or add del)
12156                   (unless (gnus-check-group group)
12157                     (error "Can't open server for %s" group))
12158                   (gnus-request-set-mark
12159                    group (delq nil (list (if add (list add 'add '(read)))
12160                                          (if del (list del 'del '(read))))))
12161                   (setq setmarkundo
12162                         `(gnus-request-set-mark
12163                           ,group
12164                           ',(delq nil (list
12165                                        (if del (list del 'add '(read)))
12166                                        (if add (list add 'del '(read))))))))))
12167             (set-buffer gnus-group-buffer)
12168             (gnus-undo-register
12169               `(progn
12170                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
12171                  (gnus-info-set-read ',info ',(gnus-info-read info))
12172                  (gnus-get-unread-articles-in-group ',info
12173                                                     (gnus-active ,group))
12174                  (gnus-group-update-group ,group t)
12175                  ,setmarkundo))))
12176         ;; Enter this list into the group info.
12177         (gnus-info-set-read info read)
12178         ;; Set the number of unread articles in gnus-newsrc-hashtb.
12179         (gnus-get-unread-articles-in-group info (gnus-active group))
12180         t))))
12181
12182 (defun gnus-offer-save-summaries ()
12183   "Offer to save all active summary buffers."
12184   (let (buffers)
12185     ;; Go through all buffers and find all summaries.
12186     (dolist (buffer (buffer-list))
12187       (when (and (setq buffer (buffer-name buffer))
12188                  (string-match "Summary" buffer)
12189                  (with-current-buffer buffer
12190                    ;; We check that this is, indeed, a summary buffer.
12191                    (and (eq major-mode 'gnus-summary-mode)
12192                         ;; Also make sure this isn't bogus.
12193                         gnus-newsgroup-prepared
12194                         ;; Also make sure that this isn't a
12195                         ;; dead summary buffer.
12196                         (not gnus-dead-summary-mode))))
12197         (push buffer buffers)))
12198     ;; Go through all these summary buffers and offer to save them.
12199     (when buffers
12200       (save-excursion
12201         (map-y-or-n-p
12202          "Update summary buffer %s? "
12203          (lambda (buf)
12204            (switch-to-buffer buf)
12205            (gnus-summary-exit))
12206          buffers)))))
12207
12208 (defun gnus-summary-setup-default-charset ()
12209   "Setup newsgroup default charset."
12210   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
12211       (setq gnus-newsgroup-charset nil)
12212     (let* ((ignored-charsets
12213             (or gnus-newsgroup-ephemeral-ignored-charsets
12214                 (append
12215                  (and gnus-newsgroup-name
12216                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
12217                  gnus-newsgroup-ignored-charsets))))
12218       (setq gnus-newsgroup-charset
12219             (or gnus-newsgroup-ephemeral-charset
12220                 (and gnus-newsgroup-name
12221                      (gnus-parameter-charset gnus-newsgroup-name))
12222                 gnus-default-charset))
12223       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
12224            ignored-charsets))))
12225
12226 ;;;
12227 ;;; Mime Commands
12228 ;;;
12229
12230 (defun gnus-summary-display-buttonized (&optional show-all-parts)
12231   "Display the current article buffer fully MIME-buttonized.
12232 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
12233 treated as multipart/mixed."
12234   (interactive "P")
12235   (require 'gnus-art)
12236   (let ((gnus-unbuttonized-mime-types nil)
12237         (gnus-mime-display-multipart-as-mixed show-all-parts))
12238     (gnus-summary-show-article)))
12239
12240 (defun gnus-summary-repair-multipart (article)
12241   "Add a Content-Type header to a multipart article without one."
12242   (interactive (list (gnus-summary-article-number)))
12243   (gnus-with-article article
12244     (message-narrow-to-head)
12245     (message-remove-header "Mime-Version")
12246     (goto-char (point-max))
12247     (insert "Mime-Version: 1.0\n")
12248     (widen)
12249     (when (search-forward "\n--" nil t)
12250       (let ((separator (buffer-substring (point) (point-at-eol))))
12251         (message-narrow-to-head)
12252         (message-remove-header "Content-Type")
12253         (goto-char (point-max))
12254         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
12255                         separator))
12256         (widen))))
12257   (let (gnus-mark-article-hook)
12258     (gnus-summary-select-article t t nil article)))
12259
12260 (defun gnus-summary-toggle-display-buttonized ()
12261   "Toggle the buttonizing of the article buffer."
12262   (interactive)
12263   (require 'gnus-art)
12264   (if (setq gnus-inhibit-mime-unbuttonizing
12265             (not gnus-inhibit-mime-unbuttonizing))
12266       (let ((gnus-unbuttonized-mime-types nil))
12267         (gnus-summary-show-article))
12268     (gnus-summary-show-article)))
12269
12270 ;;;
12271 ;;; Generic summary marking commands
12272 ;;;
12273
12274 (defvar gnus-summary-marking-alist
12275   '((read gnus-del-mark "d")
12276     (unread gnus-unread-mark "u")
12277     (ticked gnus-ticked-mark "!")
12278     (dormant gnus-dormant-mark "?")
12279     (expirable gnus-expirable-mark "e"))
12280   "An alist of names/marks/keystrokes.")
12281
12282 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
12283 (defvar gnus-summary-mark-map)
12284
12285 (defun gnus-summary-make-all-marking-commands ()
12286   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
12287   (dolist (elem gnus-summary-marking-alist)
12288     (apply 'gnus-summary-make-marking-command elem)))
12289
12290 (defun gnus-summary-make-marking-command (name mark keystroke)
12291   (let ((map (make-sparse-keymap)))
12292     (define-key gnus-summary-generic-mark-map keystroke map)
12293     (dolist (lway `((next "next" next nil "n")
12294                     (next-unread "next unread" next t "N")
12295                     (prev "previous" prev nil "p")
12296                     (prev-unread "previous unread" prev t "P")
12297                     (nomove "" nil nil ,keystroke)))
12298       (let ((func (gnus-summary-make-marking-command-1
12299                    mark (car lway) lway name)))
12300         (setq func (eval func))
12301         (define-key map (nth 4 lway) func)))))
12302
12303 (defun gnus-summary-make-marking-command-1 (mark way lway name)
12304   `(defun ,(intern
12305             (format "gnus-summary-put-mark-as-%s%s"
12306                     name (if (eq way 'nomove)
12307                              ""
12308                            (concat "-" (symbol-name way)))))
12309      (n)
12310      ,(format
12311        "Mark the current article as %s%s.
12312 If N, the prefix, then repeat N times.
12313 If N is negative, move in reverse order.
12314 The difference between N and the actual number of articles marked is
12315 returned."
12316        name (cadr lway))
12317      (interactive "p")
12318      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
12319
12320 (defun gnus-summary-generic-mark (n mark move unread)
12321   "Mark N articles with MARK."
12322   (unless (eq major-mode 'gnus-summary-mode)
12323     (error "This command can only be used in the summary buffer"))
12324   (gnus-summary-show-thread)
12325   (let ((nummove
12326          (cond
12327           ((eq move 'next) 1)
12328           ((eq move 'prev) -1)
12329           (t 0))))
12330     (if (zerop nummove)
12331         (setq n 1)
12332       (when (< n 0)
12333         (setq n (abs n)
12334               nummove (* -1 nummove))))
12335     (while (and (> n 0)
12336                 (gnus-summary-mark-article nil mark)
12337                 (zerop (gnus-summary-next-subject nummove unread t)))
12338       (setq n (1- n)))
12339     (when (/= 0 n)
12340       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12341     (gnus-summary-recenter)
12342     (gnus-summary-position-point)
12343     (gnus-set-mode-line 'summary)
12344     n))
12345
12346 (defun gnus-summary-insert-articles (articles)
12347   (when (setq articles
12348               (gnus-sorted-difference articles
12349                                       (mapcar (lambda (h)
12350                                                 (mail-header-number h))
12351                                               gnus-newsgroup-headers)))
12352     (setq gnus-newsgroup-headers
12353           (gnus-merge 'list
12354                       gnus-newsgroup-headers
12355                       (gnus-fetch-headers articles)
12356                       'gnus-article-sort-by-number))
12357     ;; Suppress duplicates?
12358     (when gnus-suppress-duplicates
12359       (gnus-dup-suppress-articles))
12360
12361     (if (and gnus-fetch-old-headers
12362              (eq gnus-headers-retrieved-by 'nov))
12363         ;; We might want to build some more threads first.
12364         (if (eq gnus-fetch-old-headers 'invisible)
12365             (gnus-build-all-threads)
12366           (gnus-build-old-threads))
12367       ;; Mark the inserted articles that are unread as unread.
12368       (setq gnus-newsgroup-unreads
12369             (gnus-sorted-nunion
12370              gnus-newsgroup-unreads
12371              (gnus-sorted-nintersection
12372               (gnus-list-of-unread-articles gnus-newsgroup-name)
12373               articles)))
12374       ;; Mark the inserted articles as selected so that the information
12375       ;; of the marks having been changed by a user may be updated when
12376       ;; exiting this group.  See `gnus-summary-update-info'.
12377       (dolist (art articles)
12378         (setq gnus-newsgroup-unselected (delq art gnus-newsgroup-unselected))))
12379     ;; Let the Gnus agent mark articles as read.
12380     (when gnus-agent
12381       (gnus-agent-get-undownloaded-list))
12382     ;; Remove list identifiers from subject
12383     (when gnus-list-identifiers
12384       (gnus-summary-remove-list-identifiers))
12385     ;; First and last article in this newsgroup.
12386     (when gnus-newsgroup-headers
12387       (setq gnus-newsgroup-begin
12388             (mail-header-number (car gnus-newsgroup-headers))
12389             gnus-newsgroup-end
12390             (mail-header-number
12391              (gnus-last-element gnus-newsgroup-headers))))
12392     (when gnus-use-scoring
12393       (gnus-possibly-score-headers))))
12394
12395 (defun gnus-summary-insert-old-articles (&optional all)
12396   "Insert all old articles in this group.
12397 If ALL is non-nil, already read articles become readable.
12398 If ALL is a number, fetch this number of articles."
12399   (interactive "P")
12400   (prog1
12401       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12402             older len)
12403         (setq older
12404               ;; Some nntp servers lie about their active range.  When
12405               ;; this happens, the active range can be in the millions.
12406               ;; Use a compressed range to avoid creating a huge list.
12407               (gnus-range-difference (list gnus-newsgroup-active) old))
12408         (setq len (gnus-range-length older))
12409         (cond
12410          ((null older) nil)
12411          ((numberp all)
12412           (if (< all len)
12413               (let ((older-range (nreverse older)))
12414                 (setq older nil)
12415
12416                 (while (> all 0)
12417                   (let* ((r (pop older-range))
12418                          (min (if (numberp r) r (car r)))
12419                          (max (if (numberp r) r (cdr r))))
12420                     (while (and (<= min max)
12421                                 (> all 0))
12422                       (push max older)
12423                       (setq all (1- all)
12424                             max (1- max))))))
12425             (setq older (gnus-uncompress-range older))))
12426          (all
12427           (setq older (gnus-uncompress-range older)))
12428          (t
12429           (when (and (numberp gnus-large-newsgroup)
12430                    (> len gnus-large-newsgroup))
12431               (let* ((cursor-in-echo-area nil)
12432                      (initial (gnus-parameter-large-newsgroup-initial
12433                                gnus-newsgroup-name))
12434                      (input
12435                       (read-string
12436                        (format
12437                         "How many articles from %s (%s %d): "
12438                         (gnus-group-decoded-name gnus-newsgroup-name)
12439                         (if initial "max" "default")
12440                         len)
12441                        (if initial
12442                            (cons (number-to-string initial)
12443                                  0)))))
12444                 (unless (string-match "^[ \t]*$" input)
12445                   (setq all (string-to-number input))
12446                   (if (< all len)
12447                       (let ((older-range (nreverse older)))
12448                         (setq older nil)
12449
12450                         (while (> all 0)
12451                           (let* ((r (pop older-range))
12452                                  (min (if (numberp r) r (car r)))
12453                                  (max (if (numberp r) r (cdr r))))
12454                             (while (and (<= min max)
12455                                         (> all 0))
12456                               (push max older)
12457                               (setq all (1- all)
12458                                     max (1- max))))))))))
12459           (setq older (gnus-uncompress-range older))))
12460         (if (not older)
12461             (message "No old news.")
12462           (gnus-summary-insert-articles older)
12463           (gnus-summary-limit (gnus-sorted-nunion old older))))
12464     (gnus-summary-position-point)))
12465
12466 (defun gnus-summary-insert-new-articles ()
12467   "Insert all new articles in this group."
12468   (interactive)
12469   (prog1
12470       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12471             (old-active gnus-newsgroup-active)
12472             (nnmail-fetched-sources (list t))
12473             i new)
12474         (setq gnus-newsgroup-active
12475               (gnus-activate-group gnus-newsgroup-name 'scan))
12476         (setq i (cdr gnus-newsgroup-active))
12477         (while (> i (cdr old-active))
12478           (push i new)
12479           (decf i))
12480         (if (not new)
12481             (message "No gnus is bad news")
12482           (gnus-summary-insert-articles new)
12483           (setq gnus-newsgroup-unreads
12484                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12485           (gnus-summary-limit (gnus-sorted-nunion old new))))
12486     (gnus-summary-position-point)))
12487
12488 (gnus-summary-make-all-marking-commands)
12489
12490 (gnus-ems-redefine)
12491
12492 (provide 'gnus-sum)
12493
12494 (run-hooks 'gnus-sum-load-hook)
12495
12496 ;; Local Variables:
12497 ;; coding: iso-8859-1
12498 ;; End:
12499
12500 ;; arch-tag: 17c6748f-6d00-4d36-bf01-835c42f31235
12501 ;;; gnus-sum.el ends here