55a449b122d25a219e6c9e03d26f66a02fb1b136
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32   (require 'cl)
33   (defvar tool-bar-map))
34
35 (require 'gnus)
36 (require 'gnus-group)
37 (require 'gnus-spec)
38 (require 'gnus-range)
39 (require 'gnus-int)
40 (require 'gnus-undo)
41 (require 'gnus-util)
42 (require 'nnoo)
43
44 (eval-when-compile
45   (require 'mime-play)
46   (require 'static))
47
48 (eval-and-compile
49   (autoload 'pgg-decrypt-region "pgg" nil t)
50   (autoload 'pgg-verify-region "pgg" nil t))
51
52 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
53 (autoload 'gnus-cache-write-active "gnus-cache")
54 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
55 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
56 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
57 (autoload 'mm-uu-dissect "mm-uu")
58 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
59   "Deuglify broken Outlook (Express) articles and redisplay."
60   t)
61 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
62 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
63 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-fetch-old-headers nil
73   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
74 If an unread article in the group refers to an older, already read (or
75 just marked as read) article, the old article will not normally be
76 displayed in the Summary buffer.  If this variable is t, Gnus
77 will attempt to grab the headers to the old articles, and thereby
78 build complete threads.  If it has the value `some', only enough
79 headers to connect otherwise loose threads will be displayed.  This
80 variable can also be a number.  In that case, no more than that number
81 of old headers will be fetched.  If it has the value `invisible', all
82 old headers will be fetched, but none will be displayed.
83
84 The server has to support NOV for any of this to work."
85   :group 'gnus-thread
86   :type '(choice (const :tag "off" nil)
87                  (const :tag "on" t)
88                  (const some)
89                  (const invisible)
90                  number
91                  (sexp :menu-tag "other" t)))
92
93 (defcustom gnus-refer-thread-limit 200
94   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
95 If t, fetch all the available old headers."
96   :group 'gnus-thread
97   :type '(choice number
98                  (sexp :menu-tag "other" t)))
99
100 (defcustom gnus-summary-make-false-root 'adopt
101   "*nil means that Gnus won't gather loose threads.
102 If the root of a thread has expired or been read in a previous
103 session, the information necessary to build a complete thread has been
104 lost.  Instead of having many small sub-threads from this original thread
105 scattered all over the summary buffer, Gnus can gather them.
106
107 If non-nil, Gnus will try to gather all loose sub-threads from an
108 original thread into one large thread.
109
110 If this variable is non-nil, it should be one of `none', `adopt',
111 `dummy' or `empty'.
112
113 If this variable is `none', Gnus will not make a false root, but just
114 present the sub-threads after another.
115 If this variable is `dummy', Gnus will create a dummy root that will
116 have all the sub-threads as children.
117 If this variable is `adopt', Gnus will make one of the \"children\"
118 the parent and mark all the step-children as such.
119 If this variable is `empty', the \"children\" are printed with empty
120 subject fields.  (Or rather, they will be printed with a string
121 given by the `gnus-summary-same-subject' variable.)"
122   :group 'gnus-thread
123   :type '(choice (const :tag "off" nil)
124                  (const none)
125                  (const dummy)
126                  (const adopt)
127                  (const empty)))
128
129 (defcustom gnus-summary-make-false-root-always nil
130   "Always make a false dummy root."
131   :version "21.4"
132   :group 'gnus-thread
133   :type 'boolean)
134
135 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
136   "*A regexp to match subjects to be excluded from loose thread gathering.
137 As loose thread gathering is done on subjects only, that means that
138 there can be many false gatherings performed.  By rooting out certain
139 common subjects, gathering might become saner."
140   :group 'gnus-thread
141   :type 'regexp)
142
143 (defcustom gnus-summary-gather-subject-limit nil
144   "*Maximum length of subject comparisons when gathering loose threads.
145 Use nil to compare full subjects.  Setting this variable to a low
146 number will help gather threads that have been corrupted by
147 newsreaders chopping off subject lines, but it might also mean that
148 unrelated articles that have subject that happen to begin with the
149 same few characters will be incorrectly gathered.
150
151 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
152 comparing subjects."
153   :group 'gnus-thread
154   :type '(choice (const :tag "off" nil)
155                  (const fuzzy)
156                  (sexp :menu-tag "on" t)))
157
158 (defcustom gnus-simplify-subject-functions nil
159   "List of functions taking a string argument that simplify subjects.
160 The functions are applied recursively.
161
162 Useful functions to put in this list include:
163 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
164 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
165   :group 'gnus-thread
166   :type '(repeat function))
167
168 (defcustom gnus-simplify-ignored-prefixes nil
169   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
170   :group 'gnus-thread
171   :type '(choice (const :tag "off" nil)
172                  regexp))
173
174 (defcustom gnus-build-sparse-threads nil
175   "*If non-nil, fill in the gaps in threads.
176 If `some', only fill in the gaps that are needed to tie loose threads
177 together.  If `more', fill in all leaf nodes that Gnus can find.  If
178 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
179   :group 'gnus-thread
180   :type '(choice (const :tag "off" nil)
181                  (const some)
182                  (const more)
183                  (sexp :menu-tag "all" t)))
184
185 (defcustom gnus-summary-thread-gathering-function
186   'gnus-gather-threads-by-subject
187   "*Function used for gathering loose threads.
188 There are two pre-defined functions: `gnus-gather-threads-by-subject',
189 which only takes Subjects into consideration; and
190 `gnus-gather-threads-by-references', which compared the References
191 headers of the articles to find matches."
192   :group 'gnus-thread
193   :type '(radio (function-item gnus-gather-threads-by-subject)
194                 (function-item gnus-gather-threads-by-references)
195                 (function :tag "other")))
196
197 (defcustom gnus-summary-same-subject ""
198   "*String indicating that the current article has the same subject as the previous.
199 This variable will only be used if the value of
200 `gnus-summary-make-false-root' is `empty'."
201   :group 'gnus-summary-format
202   :type 'string)
203
204 (defcustom gnus-summary-goto-unread t
205   "*If t, many commands will go to the next unread article.
206 This applies to marking commands as well as other commands that
207 \"naturally\" select the next article, like, for instance, `SPC' at
208 the end of an article.
209
210 If nil, the marking commands do NOT go to the next unread article
211 \(they go to the next article instead).  If `never', commands that
212 usually go to the next unread article, will go to the next article,
213 whether it is read or not."
214   :group 'gnus-summary-marks
215   :link '(custom-manual "(gnus)Setting Marks")
216   :type '(choice (const :tag "off" nil)
217                  (const never)
218                  (sexp :menu-tag "on" t)))
219
220 (defcustom gnus-summary-default-score 0
221   "*Default article score level.
222 All scores generated by the score files will be added to this score.
223 If this variable is nil, scoring will be disabled."
224   :group 'gnus-score-default
225   :type '(choice (const :tag "disable")
226                  integer))
227
228 (defcustom gnus-summary-default-high-score 0
229   "*Default threshold for a high scored article.
230 An article will be highlighted as high scored if its score is greater
231 than this score."
232   :version "21.4"
233   :group 'gnus-score-default
234   :type 'integer)
235
236 (defcustom gnus-summary-default-low-score 0
237   "*Default threshold for a low scored article.
238 An article will be highlighted as low scored if its score is smaller
239 than this score."
240   :version "21.4"
241   :group 'gnus-score-default
242   :type 'integer)
243
244 (defcustom gnus-summary-zcore-fuzz 0
245   "*Fuzziness factor for the zcore in the summary buffer.
246 Articles with scores closer than this to `gnus-summary-default-score'
247 will not be marked."
248   :group 'gnus-summary-format
249   :type 'integer)
250
251 (defcustom gnus-simplify-subject-fuzzy-regexp nil
252   "*Strings to be removed when doing fuzzy matches.
253 This can either be a regular expression or list of regular expressions
254 that will be removed from subject strings if fuzzy subject
255 simplification is selected."
256   :group 'gnus-thread
257   :type '(repeat regexp))
258
259 (defcustom gnus-show-threads t
260   "*If non-nil, display threads in summary mode."
261   :group 'gnus-thread
262   :type 'boolean)
263
264 (defcustom gnus-thread-hide-subtree nil
265   "*If non-nil, hide all threads initially.
266 This can be a predicate specifier which says which threads to hide.
267 If threads are hidden, you have to run the command
268 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
269 to expose hidden threads."
270   :group 'gnus-thread
271   :type '(radio (sexp :format "Non-nil\n"
272                       :match (lambda (widget value)
273                                (not (or (consp value) (functionp value))))
274                       :value t)
275                 (const nil)
276                 (sexp :tag "Predicate specifier" :size 0)))
277
278 (defcustom gnus-thread-hide-killed t
279   "*If non-nil, hide killed threads automatically."
280   :group 'gnus-thread
281   :type 'boolean)
282
283 (defcustom gnus-thread-ignore-subject t
284   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
285 If nil, articles that have different subjects from their parents will
286 start separate threads."
287   :group 'gnus-thread
288   :type 'boolean)
289
290 (defcustom gnus-thread-operation-ignore-subject t
291   "*If non-nil, subjects will be ignored when doing thread commands.
292 This affects commands like `gnus-summary-kill-thread' and
293 `gnus-summary-lower-thread'.
294
295 If this variable is nil, articles in the same thread with different
296 subjects will not be included in the operation in question.  If this
297 variable is `fuzzy', only articles that have subjects that are fuzzily
298 equal will be included."
299   :group 'gnus-thread
300   :type '(choice (const :tag "off" nil)
301                  (const fuzzy)
302                  (sexp :tag "on" t)))
303
304 (defcustom gnus-thread-indent-level 4
305   "*Number that says how much each sub-thread should be indented."
306   :group 'gnus-thread
307   :type 'integer)
308
309 (defcustom gnus-auto-extend-newsgroup t
310   "*If non-nil, extend newsgroup forward and backward when requested."
311   :group 'gnus-summary-choose
312   :type 'boolean)
313
314 (defcustom gnus-auto-select-first t
315   "*If non-nil, select the article under point.
316 Which article this is is controlled by the `gnus-auto-select-subject'
317 variable.
318
319 If you want to prevent automatic selection of articles in some
320 newsgroups, set the variable to nil in `gnus-select-group-hook'."
321   :group 'gnus-group-select
322   :type '(choice (const :tag "none" nil)
323                  (sexp :menu-tag "first" t)))
324
325 (defcustom gnus-auto-select-subject 'unread
326   "*Says what subject to place under point when entering a group.
327
328 This variable can either be the symbols `first' (place point on the
329 first subject), `unread' (place point on the subject line of the first
330 unread article), `best' (place point on the subject line of the
331 higest-scored article), `unseen' (place point on the subject line of
332 the first unseen article), 'unseen-or-unread' (place point on the subject
333 line of the first unseen article or, if all article have been seen, on the
334 subject line of the first unread article), or a function to be called to
335 place point on some subject line."
336   :version "21.4"
337   :group 'gnus-group-select
338   :type '(choice (const best)
339                  (const unread)
340                  (const first)
341                  (const unseen)
342                  (const unseen-or-unread)))
343
344 (defcustom gnus-dont-select-after-jump-to-other-group nil
345   "If non-nil, don't select the first unread article after entering the
346 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
347 it is depend on the value of `gnus-auto-select-first' whether to select
348 or not."
349   :group 'gnus-group-select
350   :type 'boolean)
351
352 (defcustom gnus-auto-select-next t
353   "*If non-nil, offer to go to the next group from the end of the previous.
354 If the value is t and the next newsgroup is empty, Gnus will exit
355 summary mode and go back to group mode.  If the value is neither nil
356 nor t, Gnus will select the following unread newsgroup.  In
357 particular, if the value is the symbol `quietly', the next unread
358 newsgroup will be selected without any confirmation, and if it is
359 `almost-quietly', the next group will be selected without any
360 confirmation if you are located on the last article in the group.
361 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
362 will go to the next group without confirmation."
363   :group 'gnus-summary-maneuvering
364   :type '(choice (const :tag "off" nil)
365                  (const quietly)
366                  (const almost-quietly)
367                  (const slightly-quietly)
368                  (sexp :menu-tag "on" t)))
369
370 (defcustom gnus-auto-select-same nil
371   "*If non-nil, select the next article with the same subject.
372 If there are no more articles with the same subject, go to
373 the first unread article."
374   :group 'gnus-summary-maneuvering
375   :type 'boolean)
376
377 (defcustom gnus-auto-goto-ignores 'unfetched
378   "*Says how to handle unfetched articles when maneuvering.
379
380 This variable can either be the symbols nil (maneuver to any
381 article), `undownloaded' (maneuvering while unplugged ignores articles
382 that have not been fetched), `always-undownloaded' (maneuvering always
383 ignores articles that have not been fetched), `unfetched' (maneuvering
384 ignores articles whose headers have not been fetched).
385
386 NOTE: The list of unfetched articles will always be nil when plugged
387 and, when unplugged, a subset of the undownloaded article list."
388   :version "21.4"
389   :group 'gnus-summary-maneuvering
390   :type '(choice (const :tag "None" nil)
391                  (const :tag "Undownloaded when unplugged" undownloaded)
392                  (const :tag "Undownloaded" always-undownloaded)
393                  (const :tag "Unfetched" unfetched)))
394
395 (defcustom gnus-summary-check-current nil
396   "*If non-nil, consider the current article when moving.
397 The \"unread\" movement commands will stay on the same line if the
398 current article is unread."
399   :group 'gnus-summary-maneuvering
400   :type 'boolean)
401
402 (defcustom gnus-auto-center-summary 2
403   "*If non-nil, always center the current summary buffer.
404 In particular, if `vertical' do only vertical recentering.  If non-nil
405 and non-`vertical', do both horizontal and vertical recentering."
406   :group 'gnus-summary-maneuvering
407   :type '(choice (const :tag "none" nil)
408                  (const vertical)
409                  (integer :tag "height")
410                  (sexp :menu-tag "both" t)))
411
412 (defvar gnus-auto-center-group t
413   "*If non-nil, always center the group buffer.")
414
415 (defcustom gnus-show-all-headers nil
416   "*If non-nil, don't hide any headers."
417   :group 'gnus-article-hiding
418   :group 'gnus-article-headers
419   :type 'boolean)
420
421 (defcustom gnus-summary-ignore-duplicates nil
422   "*If non-nil, ignore articles with identical Message-ID headers."
423   :group 'gnus-summary
424   :type 'boolean)
425
426 (defcustom gnus-single-article-buffer t
427   "*If non-nil, display all articles in the same buffer.
428 If nil, each group will get its own article buffer."
429   :group 'gnus-article-various
430   :type 'boolean)
431
432 (defcustom gnus-break-pages t
433   "*If non-nil, do page breaking on articles.
434 The page delimiter is specified by the `gnus-page-delimiter'
435 variable."
436   :group 'gnus-article-various
437   :type 'boolean)
438
439 (defcustom gnus-show-mime t
440   "*If non-nil, do mime processing of articles.
441 The articles will simply be fed to the function given by
442 `gnus-article-display-method-for-mime'."
443   :group 'gnus-article-mime
444   :type 'boolean)
445
446 (defcustom gnus-move-split-methods nil
447   "*Variable used to suggest where articles are to be moved to.
448 It uses the same syntax as the `gnus-split-methods' variable.
449 However, whereas `gnus-split-methods' specifies file names as targets,
450 this variable specifies group names."
451   :group 'gnus-summary-mail
452   :type '(repeat (choice (list :value (fun) function)
453                          (cons :value ("" "") regexp (repeat string))
454                          (sexp :value nil))))
455
456 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
457   "Function used to compute default prefix for article move/copy/etc prompts.
458 The function should take one argument, a group name, and return a
459 string with the suggested prefix."
460   :group 'gnus-summary-mail
461   :type 'function)
462
463 ;; FIXME: Although the custom type is `character' for the following variables,
464 ;; using multibyte characters (Latin-1, UTF-8) doesn't work.  -- rs
465
466 (defcustom gnus-unread-mark ?\ ;;;Whitespace
467   "*Mark used for unread articles."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-ticked-mark ?!
472   "*Mark used for ticked articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-dormant-mark ??
477   "*Mark used for dormant articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-del-mark ?r
482   "*Mark used for del'd articles."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-read-mark ?R
487   "*Mark used for read articles."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-expirable-mark ?E
492   "*Mark used for expirable articles."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-killed-mark ?K
497   "*Mark used for killed articles."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-spam-mark ?$
502   "*Mark used for spam articles."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-souped-mark ?F
507   "*Mark used for souped articles."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-kill-file-mark ?X
512   "*Mark used for articles killed by kill files."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-low-score-mark ?Y
517   "*Mark used for articles with a low score."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-catchup-mark ?C
522   "*Mark used for articles that are caught up."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-replied-mark ?A
527   "*Mark used for articles that have been replied to."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-forwarded-mark ?F
532   "*Mark used for articles that have been forwarded."
533   :version "21.4"
534   :group 'gnus-summary-marks
535   :type 'character)
536
537 (defcustom gnus-recent-mark ?N
538   "*Mark used for articles that are recent."
539   :group 'gnus-summary-marks
540   :type 'character)
541
542 (defcustom gnus-cached-mark ?*
543   "*Mark used for articles that are in the cache."
544   :group 'gnus-summary-marks
545   :type 'character)
546
547 (defcustom gnus-saved-mark ?S
548   "*Mark used for articles that have been saved."
549   :group 'gnus-summary-marks
550   :type 'character)
551
552 (defcustom gnus-unseen-mark ?.
553   "*Mark used for articles that haven't been seen."
554   :version "21.4"
555   :group 'gnus-summary-marks
556   :type 'character)
557
558 (defcustom gnus-no-mark ?\ ;;;Whitespace
559   "*Mark used for articles that have no other secondary mark."
560   :version "21.4"
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-ancient-mark ?O
565   "*Mark used for ancient articles."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-sparse-mark ?Q
570   "*Mark used for sparsely reffed articles."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-canceled-mark ?G
575   "*Mark used for canceled articles."
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-duplicate-mark ?M
580   "*Mark used for duplicate articles."
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-undownloaded-mark ?-
585   "*Mark used for articles that weren't downloaded."
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-downloaded-mark ?+
590   "*Mark used for articles that were downloaded."
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-downloadable-mark ?%
595   "*Mark used for articles that are to be downloaded."
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-unsendable-mark ?=
600   "*Mark used for articles that won't be sent."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-score-over-mark ?+
605   "*Score mark used for articles with high scores."
606   :group 'gnus-summary-marks
607   :type 'character)
608
609 (defcustom gnus-score-below-mark ?-
610   "*Score mark used for articles with low scores."
611   :group 'gnus-summary-marks
612   :type 'character)
613
614 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
615   "*There is no thread under the article."
616   :group 'gnus-summary-marks
617   :type 'character)
618
619 (defcustom gnus-not-empty-thread-mark ?=
620   "*There is a thread under the article."
621   :group 'gnus-summary-marks
622   :type 'character)
623
624 (defcustom gnus-view-pseudo-asynchronously nil
625   "*If non-nil, Gnus will view pseudo-articles asynchronously."
626   :group 'gnus-extract-view
627   :type 'boolean)
628
629 (defcustom gnus-auto-expirable-marks
630   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
631         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
632         gnus-souped-mark gnus-duplicate-mark)
633   "*The list of marks converted into expiration if a group is auto-expirable."
634   :version "21.1"
635   :group 'gnus-summary
636   :type '(repeat character))
637
638 (defcustom gnus-inhibit-user-auto-expire t
639   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
640   :version "21.1"
641   :group 'gnus-summary
642   :type 'boolean)
643
644 (defcustom gnus-view-pseudos nil
645   "*If `automatic', pseudo-articles will be viewed automatically.
646 If `not-confirm', pseudos will be viewed automatically, and the user
647 will not be asked to confirm the command."
648   :group 'gnus-extract-view
649   :type '(choice (const :tag "off" nil)
650                  (const automatic)
651                  (const not-confirm)))
652
653 (defcustom gnus-view-pseudos-separately t
654   "*If non-nil, one pseudo-article will be created for each file to be viewed.
655 If nil, all files that use the same viewing command will be given as a
656 list of parameters to that command."
657   :group 'gnus-extract-view
658   :type 'boolean)
659
660 (defcustom gnus-insert-pseudo-articles t
661   "*If non-nil, insert pseudo-articles when decoding articles."
662   :group 'gnus-extract-view
663   :type 'boolean)
664
665 (defcustom gnus-summary-dummy-line-format
666   "   %(:                             :%) %S\n"
667   "*The format specification for the dummy roots in the summary buffer.
668 It works along the same lines as a normal formatting string,
669 with some simple extensions.
670
671 %S  The subject
672
673 General format specifiers can also be used.
674 See `(gnus)Formatting Variables'."
675   :link '(custom-manual "(gnus)Formatting Variables")
676   :group 'gnus-threading
677   :type 'string)
678
679 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
680   "*The format specification for the summary mode line.
681 It works along the same lines as a normal formatting string,
682 with some simple extensions:
683
684 %G  Group name
685 %p  Unprefixed group name
686 %A  Current article number
687 %z  Current article score
688 %V  Gnus version
689 %U  Number of unread articles in the group
690 %e  Number of unselected articles in the group
691 %Z  A string with unread/unselected article counts
692 %g  Shortish group name
693 %S  Subject of the current article
694 %u  User-defined spec
695 %s  Current score file name
696 %d  Number of dormant articles
697 %r  Number of articles that have been marked as read in this session
698 %E  Number of articles expunged by the score files"
699   :group 'gnus-summary-format
700   :type 'string)
701
702 (defcustom gnus-list-identifiers nil
703   "Regexp that matches list identifiers to be removed from subject.
704 This can also be a list of regexps."
705   :version "21.1"
706   :group 'gnus-summary-format
707   :group 'gnus-article-hiding
708   :type '(choice (const :tag "none" nil)
709                  (regexp :value ".*")
710                  (repeat :value (".*") regexp)))
711
712 (defcustom gnus-summary-mark-below 0
713   "*Mark all articles with a score below this variable as read.
714 This variable is local to each summary buffer and usually set by the
715 score file."
716   :group 'gnus-score-default
717   :type 'integer)
718
719 (defun gnus-widget-reversible-match (widget value)
720   "Ignoring WIDGET, convert VALUE to internal form.
721 VALUE should have the form `FOO' or `(not FOO)', where FOO is an symbol."
722   ;; (debug value)
723   (or (symbolp value)
724       (and (listp value)
725            (eq (length value) 2)
726            (eq (nth 0 value) 'not)
727            (symbolp (nth 1 value)))))
728
729 (defun gnus-widget-reversible-to-internal (widget value)
730   "Ignoring WIDGET, convert VALUE to internal form.
731 VALUE should have the form `FOO' or `(not FOO)', where FOO is an atom.
732 FOO is converted to (FOO nil) and (not FOO) is converted to (FOO t)."
733   ;; (debug value)
734   (if (atom value)
735       (list value nil)
736     (list (nth 1 value) t)))
737
738 (defun gnus-widget-reversible-to-external (widget value)
739   "Ignoring WIDGET, convert VALUE to external form.
740 VALUE should have the form `(FOO nil)' or `(FOO t)', where FOO is an atom.
741 \(FOO  nil) is converted to FOO and (FOO t) is converted to (not FOO)."
742   ;; (debug value)
743   (if (nth 1 value)
744       (list 'not (nth 0 value))
745     (nth 0 value)))
746
747 (define-widget 'gnus-widget-reversible 'group
748   "A `group' that convert values."
749   :match 'gnus-widget-reversible-match
750   :value-to-internal 'gnus-widget-reversible-to-internal
751   :value-to-external 'gnus-widget-reversible-to-external)
752
753 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
754   "*List of functions used for sorting articles in the summary buffer.
755
756 Each function takes two articles and returns non-nil if the first
757 article should be sorted before the other.  If you use more than one
758 function, the primary sort function should be the last.  You should
759 probably always include `gnus-article-sort-by-number' in the list of
760 sorting functions -- preferably first.  Also note that sorting by date
761 is often much slower than sorting by number, and the sorting order is
762 very similar.  (Sorting by date means sorting by the time the message
763 was sent, sorting by number means sorting by arrival time.)
764
765 Each item can also be a list `(not F)' where F is a function;
766 this reverses the sort order.
767
768 Ready-made functions include `gnus-article-sort-by-number',
769 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
770 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
771 and `gnus-article-sort-by-score'.
772
773 When threading is turned on, the variable `gnus-thread-sort-functions'
774 controls how articles are sorted."
775   :group 'gnus-summary-sort
776   :type '(repeat (gnus-widget-reversible
777                   (choice (function-item gnus-article-sort-by-number)
778                           (function-item gnus-article-sort-by-author)
779                           (function-item gnus-article-sort-by-subject)
780                           (function-item gnus-article-sort-by-date)
781                           (function-item gnus-article-sort-by-score)
782                           (function-item gnus-article-sort-by-random)
783                           (function :tag "other"))
784                   (boolean :tag "Reverse order"))))
785
786
787 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
788   "*List of functions used for sorting threads in the summary buffer.
789 By default, threads are sorted by article number.
790
791 Each function takes two threads and returns non-nil if the first
792 thread should be sorted before the other.  If you use more than one
793 function, the primary sort function should be the last.  You should
794 probably always include `gnus-thread-sort-by-number' in the list of
795 sorting functions -- preferably first.  Also note that sorting by date
796 is often much slower than sorting by number, and the sorting order is
797 very similar.  (Sorting by date means sorting by the time the message
798 was sent, sorting by number means sorting by arrival time.)
799
800 Each list item can also be a list `(not F)' where F is a
801 function; this specifies reversed sort order.
802
803 Ready-made functions include `gnus-thread-sort-by-number',
804 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
805 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
806 `gnus-thread-sort-by-most-recent-number',
807 `gnus-thread-sort-by-most-recent-date',
808 `gnus-thread-sort-by-random', and
809 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
810
811 When threading is turned off, the variable
812 `gnus-article-sort-functions' controls how articles are sorted."
813   :group 'gnus-summary-sort
814   :type '(repeat 
815           (gnus-widget-reversible
816            (choice (function-item gnus-thread-sort-by-number)
817                    (function-item gnus-thread-sort-by-author)
818                    (function-item gnus-thread-sort-by-subject)
819                    (function-item gnus-thread-sort-by-date)
820                    (function-item gnus-thread-sort-by-score)
821                    (function-item gnus-thread-sort-by-most-recent-number)
822                    (function-item gnus-thread-sort-by-most-recent-date)
823                    (function-item gnus-thread-sort-by-random)
824                    (function-item gnus-thread-sort-by-total-score)
825                    (function :tag "other"))
826            (boolean :tag "Reverse order"))))
827
828 (defcustom gnus-thread-score-function '+
829   "*Function used for calculating the total score of a thread.
830
831 The function is called with the scores of the article and each
832 subthread and should then return the score of the thread.
833
834 Some functions you can use are `+', `max', or `min'."
835   :group 'gnus-summary-sort
836   :type 'function)
837
838 (defcustom gnus-summary-expunge-below nil
839   "All articles that have a score less than this variable will be expunged.
840 This variable is local to the summary buffers."
841   :group 'gnus-score-default
842   :type '(choice (const :tag "off" nil)
843                  integer))
844
845 (defcustom gnus-thread-expunge-below nil
846   "All threads that have a total score less than this variable will be expunged.
847 See `gnus-thread-score-function' for en explanation of what a
848 \"thread score\" is.
849
850 This variable is local to the summary buffers."
851   :group 'gnus-threading
852   :group 'gnus-score-default
853   :type '(choice (const :tag "off" nil)
854                  integer))
855
856 (defcustom gnus-summary-mode-hook nil
857   "*A hook for Gnus summary mode.
858 This hook is run before any variables are set in the summary buffer."
859   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
860   :group 'gnus-summary-various
861   :type 'hook)
862
863 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
864 (when (featurep 'xemacs)
865   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
866   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
867   (add-hook 'gnus-summary-mode-hook
868             'gnus-xmas-switch-horizontal-scrollbar-off))
869
870 (defcustom gnus-summary-menu-hook nil
871   "*Hook run after the creation of the summary mode menu."
872   :group 'gnus-summary-visual
873   :type 'hook)
874
875 (defcustom gnus-summary-exit-hook nil
876   "*A hook called on exit from the summary buffer.
877 It will be called with point in the group buffer."
878   :group 'gnus-summary-exit
879   :type 'hook)
880
881 (defcustom gnus-summary-prepare-hook nil
882   "*A hook called after the summary buffer has been generated.
883 If you want to modify the summary buffer, you can use this hook."
884   :group 'gnus-summary-various
885   :type 'hook)
886
887 (defcustom gnus-summary-prepared-hook nil
888   "*A hook called as the last thing after the summary buffer has been generated."
889   :group 'gnus-summary-various
890   :type 'hook)
891
892 (defcustom gnus-summary-generate-hook nil
893   "*A hook run just before generating the summary buffer.
894 This hook is commonly used to customize threading variables and the
895 like."
896   :group 'gnus-summary-various
897   :type 'hook)
898
899 (defcustom gnus-select-group-hook nil
900   "*A hook called when a newsgroup is selected.
901
902 If you'd like to simplify subjects like the
903 `gnus-summary-next-same-subject' command does, you can use the
904 following hook:
905
906  (add-hook gnus-select-group-hook
907            (lambda ()
908              (mapcar (lambda (header)
909                        (mail-header-set-subject
910                         header
911                         (gnus-simplify-subject
912                          (mail-header-subject header) 're-only)))
913                      gnus-newsgroup-headers)))"
914   :group 'gnus-group-select
915   :type 'hook)
916
917 (defcustom gnus-select-article-hook nil
918   "*A hook called when an article is selected."
919   :group 'gnus-summary-choose
920   :options '(gnus-agent-fetch-selected-article)
921   :type 'hook)
922
923 (defcustom gnus-visual-mark-article-hook
924   (list 'gnus-highlight-selected-summary)
925   "*Hook run after selecting an article in the summary buffer.
926 It is meant to be used for highlighting the article in some way.  It
927 is not run if `gnus-visual' is nil."
928   :group 'gnus-summary-visual
929   :type 'hook)
930
931 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
932   "*A hook called before parsing the headers."
933   :group 'gnus-various
934   :type 'hook)
935
936 (defcustom gnus-exit-group-hook nil
937   "*A hook called when exiting summary mode.
938 This hook is not called from the non-updating exit commands like `Q'."
939   :group 'gnus-various
940   :type 'hook)
941
942 (defcustom gnus-summary-update-hook
943   (list 'gnus-summary-highlight-line)
944   "*A hook called when a summary line is changed.
945 The hook will not be called if `gnus-visual' is nil.
946
947 The default function `gnus-summary-highlight-line' will
948 highlight the line according to the `gnus-summary-highlight'
949 variable."
950   :group 'gnus-summary-visual
951   :type 'hook)
952
953 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
954   "*A hook called when an article is selected for the first time.
955 The hook is intended to mark an article as read (or unread)
956 automatically when it is selected."
957   :group 'gnus-summary-choose
958   :type 'hook)
959
960 (defcustom gnus-group-no-more-groups-hook nil
961   "*A hook run when returning to group mode having no more (unread) groups."
962   :group 'gnus-group-select
963   :type 'hook)
964
965 (defcustom gnus-ps-print-hook nil
966   "*A hook run before ps-printing something from Gnus."
967   :group 'gnus-summary
968   :type 'hook)
969
970 (defcustom gnus-summary-article-move-hook nil
971   "*A hook called after an article is moved, copied, respooled, or crossposted."
972   :group 'gnus-summary
973   :type 'hook)
974
975 (defcustom gnus-summary-article-delete-hook nil
976   "*A hook called after an article is deleted."
977   :group 'gnus-summary
978   :type 'hook)
979
980 (defcustom gnus-summary-article-expire-hook nil
981   "*A hook called after an article is expired."
982   :group 'gnus-summary
983   :type 'hook)
984
985 (defcustom gnus-summary-display-arrow
986   (and (fboundp 'display-graphic-p)
987        (display-graphic-p))
988   "*If non-nil, display an arrow highlighting the current article."
989   :version "21.4"
990   :group 'gnus-summary
991   :type 'boolean)
992
993 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
994   "Face used for highlighting the current article in the summary buffer."
995   :group 'gnus-summary-visual
996   :type 'face)
997
998 (defvar gnus-tmp-downloaded nil)
999
1000 (defcustom gnus-summary-highlight
1001   '(((eq mark gnus-canceled-mark)
1002      . gnus-summary-cancelled-face)
1003     ((and uncached (> score default-high))
1004      . gnus-summary-high-undownloaded-face)
1005     ((and uncached (< score default-low))
1006      . gnus-summary-low-undownloaded-face)
1007     (uncached
1008      . gnus-summary-normal-undownloaded-face)
1009     ((and (> score default-high)
1010           (or (eq mark gnus-dormant-mark)
1011               (eq mark gnus-ticked-mark)))
1012      . gnus-summary-high-ticked-face)
1013     ((and (< score default-low)
1014           (or (eq mark gnus-dormant-mark)
1015               (eq mark gnus-ticked-mark)))
1016      . gnus-summary-low-ticked-face)
1017     ((or (eq mark gnus-dormant-mark)
1018          (eq mark gnus-ticked-mark))
1019      . gnus-summary-normal-ticked-face)
1020     ((and (> score default-high) (eq mark gnus-ancient-mark))
1021      . gnus-summary-high-ancient-face)
1022     ((and (< score default-low) (eq mark gnus-ancient-mark))
1023      . gnus-summary-low-ancient-face)
1024     ((eq mark gnus-ancient-mark)
1025      . gnus-summary-normal-ancient-face)
1026     ((and (> score default-high) (eq mark gnus-unread-mark))
1027      . gnus-summary-high-unread-face)
1028     ((and (< score default-low) (eq mark gnus-unread-mark))
1029      . gnus-summary-low-unread-face)
1030     ((eq mark gnus-unread-mark)
1031      . gnus-summary-normal-unread-face)
1032     ((> score default-high)
1033      . gnus-summary-high-read-face)
1034     ((< score default-low)
1035      . gnus-summary-low-read-face)
1036     (t
1037      . gnus-summary-normal-read-face))
1038   "*Controls the highlighting of summary buffer lines.
1039
1040 A list of (FORM . FACE) pairs.  When deciding how a a particular
1041 summary line should be displayed, each form is evaluated.  The content
1042 of the face field after the first true form is used.  You can change
1043 how those summary lines are displayed, by editing the face field.
1044
1045 You can use the following variables in the FORM field.
1046
1047 score:        The article's score
1048 default:      The default article score.
1049 default-high: The default score for high scored articles.
1050 default-low:  The default score for low scored articles.
1051 below:        The score below which articles are automatically marked as read.
1052 mark:         The article's mark.
1053 uncached:     Non-nil if the article is uncached."
1054   :group 'gnus-summary-visual
1055   :type '(repeat (cons (sexp :tag "Form" nil)
1056                        face)))
1057
1058 (defcustom gnus-alter-header-function nil
1059   "Function called to allow alteration of article header structures.
1060 The function is called with one parameter, the article header vector,
1061 which it may alter in any way."
1062   :type '(choice (const :tag "None" nil)
1063                  function)
1064   :group 'gnus-summary)
1065
1066 (defvar gnus-decode-encoded-word-function
1067   (mime-find-field-decoder 'From 'nov)
1068   "Variable that says which function should be used to decode a string with encoded words.")
1069
1070 (defcustom gnus-extra-headers '(To Newsgroups)
1071   "*Extra headers to parse."
1072   :version "21.1"
1073   :group 'gnus-summary
1074   :type '(repeat symbol))
1075
1076 (defcustom gnus-ignored-from-addresses
1077   (and user-mail-address (regexp-quote user-mail-address))
1078   "*Regexp of From headers that may be suppressed in favor of To headers."
1079   :version "21.1"
1080   :group 'gnus-summary
1081   :type 'regexp)
1082
1083 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1084   "List of charsets that should be ignored.
1085 When these charsets are used in the \"charset\" parameter, the
1086 default charset will be used instead."
1087   :version "21.1"
1088   :type '(repeat symbol)
1089   :group 'gnus-charset)
1090
1091 (gnus-define-group-parameter
1092  ignored-charsets
1093  :type list
1094  :function-document
1095  "Return the ignored charsets of GROUP."
1096  :variable gnus-group-ignored-charsets-alist
1097  :variable-default
1098  '(("alt\\.chinese\\.text" iso-8859-1))
1099  :variable-document
1100  "Alist of regexps (to match group names) and charsets that should be ignored.
1101 When these charsets are used in the \"charset\" parameter, the
1102 default charset will be used instead."
1103  :variable-group gnus-charset
1104  :variable-type '(repeat (cons (regexp :tag "Group")
1105                                (repeat symbol)))
1106  :parameter-type '(choice :tag "Ignored charsets"
1107                           :value nil
1108                           (repeat (symbol)))
1109  :parameter-document       "\
1110 List of charsets that should be ignored.
1111
1112 When these charsets are used in the \"charset\" parameter, the
1113 default charset will be used instead.")
1114
1115 (defcustom gnus-group-highlight-words-alist nil
1116   "Alist of group regexps and highlight regexps.
1117 This variable uses the same syntax as `gnus-emphasis-alist'."
1118   :version "21.1"
1119   :type '(repeat (cons (regexp :tag "Group")
1120                        (repeat (list (regexp :tag "Highlight regexp")
1121                                      (number :tag "Group for entire word" 0)
1122                                      (number :tag "Group for displayed part" 0)
1123                                      (symbol :tag "Face"
1124                                              gnus-emphasis-highlight-words)))))
1125   :group 'gnus-summary-visual)
1126
1127 (defcustom gnus-use-wheel nil
1128   "Use Intelli-mouse on summary movement"
1129   :type 'boolean
1130   :group 'gnus-summary-maneuvering)
1131
1132 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1133   "Amount to scroll messages by spinning the mouse wheel.
1134 This is actually a cons cell, where the first item is the amount to scroll
1135 on a normal wheel event, and the second is the amount to scroll when the
1136 wheel is moved with the shift key depressed."
1137   :type '(cons (integer :tag "Shift") integer)
1138   :group 'gnus-summary-maneuvering)
1139
1140 (defcustom gnus-wheel-edge-resistance 2
1141   "How hard it should be to change the current article
1142 by moving the mouse over the edge of the article window."
1143   :type 'integer
1144   :group 'gnus-summary-maneuvering)
1145
1146 (defcustom gnus-summary-show-article-charset-alist
1147   nil
1148   "Alist of number and charset.
1149 The article will be shown with the charset corresponding to the
1150 numbered argument.
1151 For example: ((1 . cn-gb-2312) (2 . big5))."
1152   :version "21.1"
1153   :type '(repeat (cons (number :tag "Argument" 1)
1154                        (symbol :tag "Charset")))
1155   :group 'gnus-charset)
1156
1157 (defcustom gnus-preserve-marks t
1158   "Whether marks are preserved when moving, copying and respooling messages."
1159   :version "21.1"
1160   :type 'boolean
1161   :group 'gnus-summary-marks)
1162
1163 (defcustom gnus-alter-articles-to-read-function nil
1164   "Function to be called to alter the list of articles to be selected."
1165   :type '(choice (const nil) function)
1166   :group 'gnus-summary)
1167
1168 (defcustom gnus-orphan-score nil
1169   "*All orphans get this score added.  Set in the score file."
1170   :group 'gnus-score-default
1171   :type '(choice (const nil)
1172                  integer))
1173
1174 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1175   "*A regexp to match MIME parts when saving multiple parts of a
1176 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1177 This regexp will be used by default when prompting the user for which
1178 type of files to save."
1179   :group 'gnus-summary
1180   :type 'regexp)
1181
1182 (defcustom gnus-read-all-available-headers nil
1183   "Whether Gnus should parse all headers made available to it.
1184 This is mostly relevant for slow back ends where the user may
1185 wish to widen the summary buffer to include all headers
1186 that were fetched.  Say, for nnultimate groups."
1187   :version "21.4"
1188   :group 'gnus-summary
1189   :type '(choice boolean regexp))
1190
1191 (defcustom gnus-summary-muttprint-program "muttprint"
1192   "Command (and optional arguments) used to run Muttprint."
1193   :version "21.4"
1194   :group 'gnus-summary
1195   :type 'string)
1196
1197 (defcustom gnus-article-loose-mime t
1198   "If non-nil, don't require MIME-Version header.
1199 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1200 supply the MIME-Version header or deliberately strip it from the mail.
1201 If non-nil (the default), Gnus will treat some articles as MIME
1202 even if the MIME-Version header is missing."
1203   :version "21.4"
1204   :type 'boolean
1205   :group 'gnus-article-mime)
1206
1207 (defcustom gnus-article-emulate-mime t
1208   "If non-nil, use MIME emulation for uuencode and the like.
1209 This means that Gnus will search message bodies for text that look
1210 like uuencoded bits, yEncoded bits, and so on, and present that using
1211 the normal Gnus MIME machinery."
1212   :version "21.4"
1213   :type 'boolean
1214   :group 'gnus-article-mime)
1215
1216 ;;; Internal variables
1217
1218 (defvar gnus-summary-display-cache nil)
1219 (defvar gnus-article-mime-handles nil)
1220 (defvar gnus-article-decoded-p nil)
1221 (defvar gnus-article-charset nil)
1222 (defvar gnus-article-ignored-charsets nil)
1223 (defvar gnus-scores-exclude-files nil)
1224 (defvar gnus-page-broken nil)
1225
1226 (defvar gnus-original-article nil)
1227 (defvar gnus-article-internal-prepare-hook nil)
1228 (defvar gnus-newsgroup-process-stack nil)
1229
1230 (defvar gnus-thread-indent-array nil)
1231 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1232 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1233   "Function called to sort the articles within a thread after it has been gathered together.")
1234
1235 (defvar gnus-summary-save-parts-type-history nil)
1236 (defvar gnus-summary-save-parts-last-directory nil)
1237
1238 ;; Avoid highlighting in kill files.
1239 (defvar gnus-summary-inhibit-highlight nil)
1240 (defvar gnus-newsgroup-selected-overlay nil)
1241 (defvar gnus-inhibit-limiting nil)
1242 (defvar gnus-newsgroup-adaptive-score-file nil)
1243 (defvar gnus-current-score-file nil)
1244 (defvar gnus-current-move-group nil)
1245 (defvar gnus-current-copy-group nil)
1246 (defvar gnus-current-crosspost-group nil)
1247 (defvar gnus-newsgroup-display nil)
1248
1249 (defvar gnus-newsgroup-dependencies nil)
1250 (defvar gnus-newsgroup-adaptive nil)
1251 (defvar gnus-summary-display-article-function nil)
1252 (defvar gnus-summary-highlight-line-function nil
1253   "Function called after highlighting a summary line.")
1254
1255 (defvar gnus-summary-line-format-alist
1256   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1257     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1258     (?s gnus-tmp-subject-or-nil ?s)
1259     (?n gnus-tmp-name ?s)
1260     (?A (std11-address-string
1261          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1262     (?a (or (std11-full-name-string
1263              (car (mime-entity-read-field gnus-tmp-header 'From)))
1264             gnus-tmp-from) ?s)
1265     (?F gnus-tmp-from ?s)
1266     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1267     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1268     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1269     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1270     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1271     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1272     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1273     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1274     (?L gnus-tmp-lines ?s)
1275     (?O gnus-tmp-downloaded ?c)
1276     (?I gnus-tmp-indentation ?s)
1277     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1278     (?R gnus-tmp-replied ?c)
1279     (?\[ gnus-tmp-opening-bracket ?c)
1280     (?\] gnus-tmp-closing-bracket ?c)
1281     (?\> (make-string gnus-tmp-level ? ) ?s)
1282     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1283     (?i gnus-tmp-score ?d)
1284     (?z gnus-tmp-score-char ?c)
1285     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1286     (?U gnus-tmp-unread ?c)
1287     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1288         ?s)
1289     (?t (gnus-summary-number-of-articles-in-thread
1290          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1291         ?d)
1292     (?e (gnus-summary-number-of-articles-in-thread
1293          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1294         ?c)
1295     (?u gnus-tmp-user-defined ?s)
1296     (?P (gnus-pick-line-number) ?d)
1297     (?B gnus-tmp-thread-tree-header-string ?s)
1298     (user-date (gnus-user-date
1299                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1300   "An alist of format specifications that can appear in summary lines.
1301 These are paired with what variables they correspond with, along with
1302 the type of the variable (string, integer, character, etc).")
1303
1304 (defvar gnus-summary-dummy-line-format-alist
1305   `((?S gnus-tmp-subject ?s)
1306     (?N gnus-tmp-number ?d)
1307     (?u gnus-tmp-user-defined ?s)))
1308
1309 (defvar gnus-summary-mode-line-format-alist
1310   `((?G gnus-tmp-group-name ?s)
1311     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1312     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1313     (?A gnus-tmp-article-number ?d)
1314     (?Z gnus-tmp-unread-and-unselected ?s)
1315     (?V gnus-version ?s)
1316     (?U gnus-tmp-unread-and-unticked ?d)
1317     (?S gnus-tmp-subject ?s)
1318     (?e gnus-tmp-unselected ?d)
1319     (?u gnus-tmp-user-defined ?s)
1320     (?d (length gnus-newsgroup-dormant) ?d)
1321     (?t (length gnus-newsgroup-marked) ?d)
1322     (?h (length gnus-newsgroup-spam-marked) ?d)
1323     (?r (length gnus-newsgroup-reads) ?d)
1324     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1325     (?E gnus-newsgroup-expunged-tally ?d)
1326     (?s (gnus-current-score-file-nondirectory) ?s)))
1327
1328 (defvar gnus-last-search-regexp nil
1329   "Default regexp for article search command.")
1330
1331 (defvar gnus-summary-search-article-matched-data nil
1332   "Last matched data of article search command.  It is the local variable
1333 in `gnus-article-buffer' which consists of the list of start position,
1334 end position and text.")
1335
1336 (defvar gnus-last-shell-command nil
1337   "Default shell command on article.")
1338
1339 (defvar gnus-newsgroup-agentized nil
1340   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1341 (defvar gnus-newsgroup-begin nil)
1342 (defvar gnus-newsgroup-end nil)
1343 (defvar gnus-newsgroup-last-rmail nil)
1344 (defvar gnus-newsgroup-last-mail nil)
1345 (defvar gnus-newsgroup-last-folder nil)
1346 (defvar gnus-newsgroup-last-file nil)
1347 (defvar gnus-newsgroup-auto-expire nil)
1348 (defvar gnus-newsgroup-active nil)
1349
1350 (defvar gnus-newsgroup-data nil)
1351 (defvar gnus-newsgroup-data-reverse nil)
1352 (defvar gnus-newsgroup-limit nil)
1353 (defvar gnus-newsgroup-limits nil)
1354 (defvar gnus-summary-use-undownloaded-faces nil)
1355
1356 (defvar gnus-newsgroup-unreads nil
1357   "Sorted list of unread articles in the current newsgroup.")
1358
1359 (defvar gnus-newsgroup-unselected nil
1360   "Sorted list of unselected unread articles in the current newsgroup.")
1361
1362 (defvar gnus-newsgroup-reads nil
1363   "Alist of read articles and article marks in the current newsgroup.")
1364
1365 (defvar gnus-newsgroup-expunged-tally nil)
1366
1367 (defvar gnus-newsgroup-marked nil
1368   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1369
1370 (defvar gnus-newsgroup-spam-marked nil
1371   "List of ranges of articles that have been marked as spam.")
1372
1373 (defvar gnus-newsgroup-killed nil
1374   "List of ranges of articles that have been through the scoring process.")
1375
1376 (defvar gnus-newsgroup-cached nil
1377   "Sorted list of articles that come from the article cache.")
1378
1379 (defvar gnus-newsgroup-saved nil
1380   "List of articles that have been saved.")
1381
1382 (defvar gnus-newsgroup-kill-headers nil)
1383
1384 (defvar gnus-newsgroup-replied nil
1385   "List of articles that have been replied to in the current newsgroup.")
1386
1387 (defvar gnus-newsgroup-forwarded nil
1388   "List of articles that have been forwarded in the current newsgroup.")
1389
1390 (defvar gnus-newsgroup-recent nil
1391   "List of articles that have are recent in the current newsgroup.")
1392
1393 (defvar gnus-newsgroup-expirable nil
1394   "Sorted list of articles in the current newsgroup that can be expired.")
1395
1396 (defvar gnus-newsgroup-processable nil
1397   "List of articles in the current newsgroup that can be processed.")
1398
1399 (defvar gnus-newsgroup-downloadable nil
1400   "Sorted list of articles in the current newsgroup that can be processed.")
1401
1402 (defvar gnus-newsgroup-unfetched nil
1403   "Sorted list of articles in the current newsgroup whose headers have
1404 not been fetched into the agent.
1405
1406 This list will always be a subset of gnus-newsgroup-undownloaded.")
1407
1408 (defvar gnus-newsgroup-undownloaded nil
1409   "List of articles in the current newsgroup that haven't been downloaded.")
1410
1411 (defvar gnus-newsgroup-unsendable nil
1412   "List of articles in the current newsgroup that won't be sent.")
1413
1414 (defvar gnus-newsgroup-bookmarks nil
1415   "List of articles in the current newsgroup that have bookmarks.")
1416
1417 (defvar gnus-newsgroup-dormant nil
1418   "Sorted list of dormant articles in the current newsgroup.")
1419
1420 (defvar gnus-newsgroup-unseen nil
1421   "List of unseen articles in the current newsgroup.")
1422
1423 (defvar gnus-newsgroup-seen nil
1424   "Range of seen articles in the current newsgroup.")
1425
1426 (defvar gnus-newsgroup-articles nil
1427   "List of articles in the current newsgroup.")
1428
1429 (defvar gnus-newsgroup-scored nil
1430   "List of scored articles in the current newsgroup.")
1431
1432 (defvar gnus-newsgroup-incorporated nil
1433   "List of incorporated articles in the current newsgroup.")
1434
1435 (defvar gnus-newsgroup-headers nil
1436   "List of article headers in the current newsgroup.")
1437
1438 (defvar gnus-newsgroup-threads nil)
1439
1440 (defvar gnus-newsgroup-prepared nil
1441   "Whether the current group has been prepared properly.")
1442
1443 (defvar gnus-newsgroup-ancient nil
1444   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1445
1446 (defvar gnus-newsgroup-sparse nil)
1447
1448 (defvar gnus-current-article nil)
1449 (defvar gnus-article-current nil)
1450 (defvar gnus-current-headers nil)
1451 (defvar gnus-have-all-headers nil)
1452 (defvar gnus-last-article nil)
1453 (defvar gnus-newsgroup-history nil)
1454 (defvar gnus-newsgroup-charset nil)
1455 (defvar gnus-newsgroup-ephemeral-charset nil)
1456 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1457
1458 (defvar gnus-article-before-search nil)
1459
1460 (defvar gnus-summary-local-variables
1461   '(gnus-newsgroup-name
1462     gnus-newsgroup-begin gnus-newsgroup-end
1463     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1464     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1465     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1466     gnus-newsgroup-unselected gnus-newsgroup-marked
1467     gnus-newsgroup-spam-marked
1468     gnus-newsgroup-reads gnus-newsgroup-saved
1469     gnus-newsgroup-replied gnus-newsgroup-forwarded
1470     gnus-newsgroup-recent
1471     gnus-newsgroup-expirable
1472     gnus-newsgroup-processable gnus-newsgroup-killed
1473     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1474     gnus-newsgroup-unfetched
1475     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1476     gnus-newsgroup-seen gnus-newsgroup-articles
1477     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1478     gnus-newsgroup-headers gnus-newsgroup-threads
1479     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1480     gnus-current-article gnus-current-headers gnus-have-all-headers
1481     gnus-last-article gnus-article-internal-prepare-hook
1482     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1483     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1484     gnus-thread-expunge-below
1485     gnus-score-alist gnus-current-score-file
1486     (gnus-summary-expunge-below . global)
1487     (gnus-summary-mark-below . global)
1488     (gnus-orphan-score . global)
1489     gnus-newsgroup-active gnus-scores-exclude-files
1490     gnus-newsgroup-history gnus-newsgroup-ancient
1491     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1492     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1493     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1494     (gnus-newsgroup-expunged-tally . 0)
1495     gnus-cache-removable-articles gnus-newsgroup-cached
1496     gnus-newsgroup-data gnus-newsgroup-data-reverse
1497     gnus-newsgroup-limit gnus-newsgroup-limits
1498     gnus-newsgroup-charset gnus-newsgroup-display
1499     gnus-summary-use-undownloaded-faces
1500     gnus-newsgroup-incorporated)
1501   "Variables that are buffer-local to the summary buffers.")
1502
1503 (defvar gnus-newsgroup-variables nil
1504   "A list of variables that have separate values in different newsgroups.
1505 A list of newsgroup (summary buffer) local variables, or cons of
1506 variables and their default expressions to be evalled (when the default
1507 values are not nil), that should be made global while the summary buffer
1508 is active.
1509
1510 Note: The default expressions will be evaluated (using function `eval')
1511 before assignment to the local variable rather than just assigned to it.
1512 If the default expression is the symbol `global', that symbol will not
1513 be evaluated but the global value of the local variable will be used
1514 instead.
1515
1516 These variables can be used to set variables in the group parameters
1517 while still allowing them to affect operations done in other buffers.
1518 For example:
1519
1520 \(setq gnus-newsgroup-variables
1521      '(message-use-followup-to
1522        (gnus-visible-headers .
1523          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1524 ")
1525
1526 ;; Byte-compiler warning.
1527 (eval-when-compile
1528   ;; Bind features so that require will believe that gnus-sum has
1529   ;; already been loaded (avoids infinite recursion)
1530   (let ((features (cons 'gnus-sum features)))
1531     ;; Several of the declarations in gnus-sum are needed to load the
1532     ;; following files. Right now, these definitions have been
1533     ;; compiled but not defined (evaluated).  We could either do a
1534     ;; eval-and-compile about all of the declarations or evaluate the
1535     ;; source file.
1536     (if (boundp 'gnus-newsgroup-variables)
1537         nil
1538       (load "gnus-sum.el" t t t))
1539     (require 'gnus)
1540     (require 'gnus-art)))
1541
1542 ;; Subject simplification.
1543
1544 (defun gnus-simplify-whitespace (str)
1545   "Remove excessive whitespace from STR."
1546   ;; Multiple spaces.
1547   (while (string-match "[ \t][ \t]+" str)
1548     (setq str (concat (substring str 0 (match-beginning 0))
1549                         " "
1550                         (substring str (match-end 0)))))
1551   ;; Leading spaces.
1552   (when (string-match "^[ \t]+" str)
1553     (setq str (substring str (match-end 0))))
1554   ;; Trailing spaces.
1555   (when (string-match "[ \t]+$" str)
1556     (setq str (substring str 0 (match-beginning 0))))
1557   str)
1558
1559 (defun gnus-simplify-all-whitespace (str)
1560   "Remove all whitespace from STR."
1561   (while (string-match "[ \t\n]+" str)
1562     (setq str (replace-match "" nil nil str)))
1563   str)
1564
1565 (defsubst gnus-simplify-subject-re (subject)
1566   "Remove \"Re:\" from subject lines."
1567   (if (string-match message-subject-re-regexp subject)
1568       (substring subject (match-end 0))
1569     subject))
1570
1571 (defun gnus-simplify-subject (subject &optional re-only)
1572   "Remove `Re:' and words in parentheses.
1573 If RE-ONLY is non-nil, strip leading `Re:'s only."
1574   (let ((case-fold-search t))           ;Ignore case.
1575     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1576     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1577       (setq subject (substring subject (match-end 0))))
1578     ;; Remove uninteresting prefixes.
1579     (when (and (not re-only)
1580                gnus-simplify-ignored-prefixes
1581                (string-match gnus-simplify-ignored-prefixes subject))
1582       (setq subject (substring subject (match-end 0))))
1583     ;; Remove words in parentheses from end.
1584     (unless re-only
1585       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1586         (setq subject (substring subject 0 (match-beginning 0)))))
1587     ;; Return subject string.
1588     subject))
1589
1590 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1591 ;; all whitespace.
1592 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1593   (goto-char (point-min))
1594   (while (re-search-forward regexp nil t)
1595     (replace-match (or newtext ""))))
1596
1597 (defun gnus-simplify-buffer-fuzzy ()
1598   "Simplify string in the buffer fuzzily.
1599 The string in the accessible portion of the current buffer is simplified.
1600 It is assumed to be a single-line subject.
1601 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1602 matter is removed.  Additional things can be deleted by setting
1603 `gnus-simplify-subject-fuzzy-regexp'."
1604   (let ((case-fold-search t)
1605         (modified-tick))
1606     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1607
1608     (while (not (eq modified-tick (buffer-modified-tick)))
1609       (setq modified-tick (buffer-modified-tick))
1610       (cond
1611        ((listp gnus-simplify-subject-fuzzy-regexp)
1612         (mapcar 'gnus-simplify-buffer-fuzzy-step
1613                 gnus-simplify-subject-fuzzy-regexp))
1614        (gnus-simplify-subject-fuzzy-regexp
1615         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1616       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1617       (gnus-simplify-buffer-fuzzy-step
1618        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1619       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1620
1621     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1622     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1623     (gnus-simplify-buffer-fuzzy-step " $")
1624     (gnus-simplify-buffer-fuzzy-step "^ +")))
1625
1626 (defun gnus-simplify-subject-fuzzy (subject)
1627   "Simplify a subject string fuzzily.
1628 See `gnus-simplify-buffer-fuzzy' for details."
1629   (save-excursion
1630     (gnus-set-work-buffer)
1631     (let ((case-fold-search t))
1632       ;; Remove uninteresting prefixes.
1633       (when (and gnus-simplify-ignored-prefixes
1634                  (string-match gnus-simplify-ignored-prefixes subject))
1635         (setq subject (substring subject (match-end 0))))
1636       (insert subject)
1637       (inline (gnus-simplify-buffer-fuzzy))
1638       (buffer-string))))
1639
1640 (defsubst gnus-simplify-subject-fully (subject)
1641   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1642   (cond
1643    (gnus-simplify-subject-functions
1644     (gnus-map-function gnus-simplify-subject-functions subject))
1645    ((null gnus-summary-gather-subject-limit)
1646     (gnus-simplify-subject-re subject))
1647    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1648     (gnus-simplify-subject-fuzzy subject))
1649    ((numberp gnus-summary-gather-subject-limit)
1650     (gnus-limit-string (gnus-simplify-subject-re subject)
1651                        gnus-summary-gather-subject-limit))
1652    (t
1653     subject)))
1654
1655 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1656   "Check whether two subjects are equal.
1657 If optional argument SIMPLE-FIRST is t, first argument is already
1658 simplified."
1659   (cond
1660    ((null simple-first)
1661     (equal (gnus-simplify-subject-fully s1)
1662            (gnus-simplify-subject-fully s2)))
1663    (t
1664     (equal s1
1665            (gnus-simplify-subject-fully s2)))))
1666
1667 (defun gnus-summary-bubble-group ()
1668   "Increase the score of the current group.
1669 This is a handy function to add to `gnus-summary-exit-hook' to
1670 increase the score of each group you read."
1671   (gnus-group-add-score gnus-newsgroup-name))
1672
1673 \f
1674 ;;;
1675 ;;; Gnus summary mode
1676 ;;;
1677
1678 (put 'gnus-summary-mode 'mode-class 'special)
1679
1680 (defvar gnus-article-commands-menu)
1681
1682 ;; Non-orthogonal keys
1683
1684 (gnus-define-keys gnus-summary-mode-map
1685   " " gnus-summary-next-page
1686   "\177" gnus-summary-prev-page
1687   [delete] gnus-summary-prev-page
1688   [backspace] gnus-summary-prev-page
1689   "\r" gnus-summary-scroll-up
1690   "\M-\r" gnus-summary-scroll-down
1691   "n" gnus-summary-next-unread-article
1692   "p" gnus-summary-prev-unread-article
1693   "N" gnus-summary-next-article
1694   "P" gnus-summary-prev-article
1695   "\M-\C-n" gnus-summary-next-same-subject
1696   "\M-\C-p" gnus-summary-prev-same-subject
1697   "\M-n" gnus-summary-next-unread-subject
1698   "\M-p" gnus-summary-prev-unread-subject
1699   "." gnus-summary-first-unread-article
1700   "," gnus-summary-best-unread-article
1701   "\M-s" gnus-summary-search-article-forward
1702   "\M-r" gnus-summary-search-article-backward
1703   "<" gnus-summary-beginning-of-article
1704   ">" gnus-summary-end-of-article
1705   "j" gnus-summary-goto-article
1706   "^" gnus-summary-refer-parent-article
1707   "\M-^" gnus-summary-refer-article
1708   "u" gnus-summary-tick-article-forward
1709   "!" gnus-summary-tick-article-forward
1710   "U" gnus-summary-tick-article-backward
1711   "d" gnus-summary-mark-as-read-forward
1712   "D" gnus-summary-mark-as-read-backward
1713   "E" gnus-summary-mark-as-expirable
1714   "\M-u" gnus-summary-clear-mark-forward
1715   "\M-U" gnus-summary-clear-mark-backward
1716   "k" gnus-summary-kill-same-subject-and-select
1717   "\C-k" gnus-summary-kill-same-subject
1718   "\M-\C-k" gnus-summary-kill-thread
1719   "\M-\C-l" gnus-summary-lower-thread
1720   "e" gnus-summary-edit-article
1721   "#" gnus-summary-mark-as-processable
1722   "\M-#" gnus-summary-unmark-as-processable
1723   "\M-\C-t" gnus-summary-toggle-threads
1724   "\M-\C-s" gnus-summary-show-thread
1725   "\M-\C-h" gnus-summary-hide-thread
1726   "\M-\C-f" gnus-summary-next-thread
1727   "\M-\C-b" gnus-summary-prev-thread
1728   [(meta down)] gnus-summary-next-thread
1729   [(meta up)] gnus-summary-prev-thread
1730   "\M-\C-u" gnus-summary-up-thread
1731   "\M-\C-d" gnus-summary-down-thread
1732   "&" gnus-summary-execute-command
1733   "c" gnus-summary-catchup-and-exit
1734   "\C-w" gnus-summary-mark-region-as-read
1735   "\C-t" gnus-summary-toggle-truncation
1736   "?" gnus-summary-mark-as-dormant
1737   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1738   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1739   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1740   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1741   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1742   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1743   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1744   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1745   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1746   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1747   "=" gnus-summary-expand-window
1748   "\C-x\C-s" gnus-summary-reselect-current-group
1749   "\M-g" gnus-summary-rescan-group
1750   "w" gnus-summary-stop-page-breaking
1751   "\C-c\C-r" gnus-summary-caesar-message
1752   "\M-t" gnus-summary-toggle-mime
1753   "f" gnus-summary-followup
1754   "F" gnus-summary-followup-with-original
1755   "C" gnus-summary-cancel-article
1756   "r" gnus-summary-reply
1757   "R" gnus-summary-reply-with-original
1758   "\C-c\C-f" gnus-summary-mail-forward
1759   "o" gnus-summary-save-article
1760   "\C-o" gnus-summary-save-article-mail
1761   "|" gnus-summary-pipe-output
1762   "\M-k" gnus-summary-edit-local-kill
1763   "\M-K" gnus-summary-edit-global-kill
1764   ;; "V" gnus-version
1765   "\C-c\C-d" gnus-summary-describe-group
1766   "q" gnus-summary-exit
1767   "Q" gnus-summary-exit-no-update
1768   "\C-c\C-i" gnus-info-find-node
1769   gnus-mouse-2 gnus-mouse-pick-article
1770   "m" gnus-summary-mail-other-window
1771   "a" gnus-summary-post-news
1772   "i" gnus-summary-news-other-window
1773   "x" gnus-summary-limit-to-unread
1774   "s" gnus-summary-isearch-article
1775   "t" gnus-summary-toggle-header
1776   "g" gnus-summary-show-article
1777   "l" gnus-summary-goto-last-article
1778   "v" gnus-summary-preview-mime-message
1779   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1780   "\C-d" gnus-summary-enter-digest-group
1781   "\M-\C-d" gnus-summary-read-document
1782   "\M-\C-e" gnus-summary-edit-parameters
1783   "\M-\C-a" gnus-summary-customize-parameters
1784   "\C-c\C-b" gnus-bug
1785   "\C-c\C-n" gnus-namazu-search
1786   "*" gnus-cache-enter-article
1787   "\M-*" gnus-cache-remove-article
1788   "\M-&" gnus-summary-universal-argument
1789   "\C-l" gnus-recenter
1790   "I" gnus-summary-increase-score
1791   "L" gnus-summary-lower-score
1792   "\M-i" gnus-symbolic-argument
1793   "h" gnus-summary-select-article-buffer
1794
1795   "V" gnus-summary-score-map
1796   "X" gnus-uu-extract-map
1797   "S" gnus-summary-send-map)
1798
1799   ;; Sort of orthogonal keymap
1800 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1801   "t" gnus-summary-tick-article-forward
1802   "!" gnus-summary-tick-article-forward
1803   "d" gnus-summary-mark-as-read-forward
1804   "r" gnus-summary-mark-as-read-forward
1805   "c" gnus-summary-clear-mark-forward
1806   " " gnus-summary-clear-mark-forward
1807   "e" gnus-summary-mark-as-expirable
1808   "x" gnus-summary-mark-as-expirable
1809   "?" gnus-summary-mark-as-dormant
1810   "b" gnus-summary-set-bookmark
1811   "B" gnus-summary-remove-bookmark
1812   "#" gnus-summary-mark-as-processable
1813   "\M-#" gnus-summary-unmark-as-processable
1814   "S" gnus-summary-limit-include-expunged
1815   "C" gnus-summary-catchup
1816   "H" gnus-summary-catchup-to-here
1817   "h" gnus-summary-catchup-from-here
1818   "\C-c" gnus-summary-catchup-all
1819   "k" gnus-summary-kill-same-subject-and-select
1820   "K" gnus-summary-kill-same-subject
1821   "P" gnus-uu-mark-map)
1822
1823 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1824   "c" gnus-summary-clear-above
1825   "u" gnus-summary-tick-above
1826   "m" gnus-summary-mark-above
1827   "k" gnus-summary-kill-below)
1828
1829 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1830   "/" gnus-summary-limit-to-subject
1831   "n" gnus-summary-limit-to-articles
1832   "w" gnus-summary-pop-limit
1833   "s" gnus-summary-limit-to-subject
1834   "a" gnus-summary-limit-to-author
1835   "u" gnus-summary-limit-to-unread
1836   "m" gnus-summary-limit-to-marks
1837   "M" gnus-summary-limit-exclude-marks
1838   "v" gnus-summary-limit-to-score
1839   "*" gnus-summary-limit-include-cached
1840   "D" gnus-summary-limit-include-dormant
1841   "T" gnus-summary-limit-include-thread
1842   "d" gnus-summary-limit-exclude-dormant
1843   "t" gnus-summary-limit-to-age
1844   "." gnus-summary-limit-to-unseen
1845   "x" gnus-summary-limit-to-extra
1846   "p" gnus-summary-limit-to-display-predicate
1847   "E" gnus-summary-limit-include-expunged
1848   "c" gnus-summary-limit-exclude-childless-dormant
1849   "C" gnus-summary-limit-mark-excluded-as-read
1850   "o" gnus-summary-insert-old-articles
1851   "N" gnus-summary-insert-new-articles
1852   "r" gnus-summary-limit-to-replied)
1853
1854 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1855   "n" gnus-summary-next-unread-article
1856   "p" gnus-summary-prev-unread-article
1857   "N" gnus-summary-next-article
1858   "P" gnus-summary-prev-article
1859   "\C-n" gnus-summary-next-same-subject
1860   "\C-p" gnus-summary-prev-same-subject
1861   "\M-n" gnus-summary-next-unread-subject
1862   "\M-p" gnus-summary-prev-unread-subject
1863   "f" gnus-summary-first-unread-article
1864   "b" gnus-summary-best-unread-article
1865   "j" gnus-summary-goto-article
1866   "g" gnus-summary-goto-subject
1867   "l" gnus-summary-goto-last-article
1868   "o" gnus-summary-pop-article)
1869
1870 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1871   "k" gnus-summary-kill-thread
1872   "l" gnus-summary-lower-thread
1873   "i" gnus-summary-raise-thread
1874   "T" gnus-summary-toggle-threads
1875   "t" gnus-summary-rethread-current
1876   "^" gnus-summary-reparent-thread
1877   "s" gnus-summary-show-thread
1878   "S" gnus-summary-show-all-threads
1879   "h" gnus-summary-hide-thread
1880   "H" gnus-summary-hide-all-threads
1881   "n" gnus-summary-next-thread
1882   "p" gnus-summary-prev-thread
1883   "u" gnus-summary-up-thread
1884   "o" gnus-summary-top-thread
1885   "d" gnus-summary-down-thread
1886   "#" gnus-uu-mark-thread
1887   "\M-#" gnus-uu-unmark-thread)
1888
1889 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1890   "g" gnus-summary-prepare
1891   "c" gnus-summary-insert-cached-articles
1892   "d" gnus-summary-insert-dormant-articles)
1893
1894 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1895   "c" gnus-summary-catchup-and-exit
1896   "C" gnus-summary-catchup-all-and-exit
1897   "E" gnus-summary-exit-no-update
1898   "J" gnus-summary-jump-to-other-group
1899   "Q" gnus-summary-exit
1900   "Z" gnus-summary-exit
1901   "n" gnus-summary-catchup-and-goto-next-group
1902   "R" gnus-summary-reselect-current-group
1903   "G" gnus-summary-rescan-group
1904   "N" gnus-summary-next-group
1905   "s" gnus-summary-save-newsrc
1906   "P" gnus-summary-prev-group)
1907
1908 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1909   " " gnus-summary-next-page
1910   "n" gnus-summary-next-page
1911   "\177" gnus-summary-prev-page
1912   [delete] gnus-summary-prev-page
1913   "p" gnus-summary-prev-page
1914   "\r" gnus-summary-scroll-up
1915   "\M-\r" gnus-summary-scroll-down
1916   "<" gnus-summary-beginning-of-article
1917   ">" gnus-summary-end-of-article
1918   "b" gnus-summary-beginning-of-article
1919   "e" gnus-summary-end-of-article
1920   "^" gnus-summary-refer-parent-article
1921   "r" gnus-summary-refer-parent-article
1922   "D" gnus-summary-enter-digest-group
1923   "R" gnus-summary-refer-references
1924   "T" gnus-summary-refer-thread
1925   "g" gnus-summary-show-article
1926   "s" gnus-summary-isearch-article
1927   "P" gnus-summary-print-article
1928   "M" gnus-mailing-list-insinuate
1929   "t" gnus-article-babel)
1930
1931 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1932   "b" gnus-article-add-buttons
1933   "B" gnus-article-add-buttons-to-head
1934   "o" gnus-article-treat-overstrike
1935   "e" gnus-article-emphasize
1936   "w" gnus-article-fill-cited-article
1937   "Q" gnus-article-fill-long-lines
1938   "C" gnus-article-capitalize-sentences
1939   "c" gnus-article-remove-cr
1940   "Z" gnus-article-decode-HZ
1941   "A" gnus-article-treat-ansi-sequences
1942   "h" gnus-article-wash-html
1943   "u" gnus-article-unsplit-urls
1944   "f" gnus-article-display-x-face
1945   "l" gnus-summary-stop-page-breaking
1946   "r" gnus-summary-caesar-message
1947   "m" gnus-summary-morse-message
1948   "t" gnus-summary-toggle-header
1949   "g" gnus-treat-smiley
1950   "v" gnus-summary-verbose-headers
1951   "m" gnus-summary-toggle-mime
1952   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1953   "p" gnus-article-verify-x-pgp-sig
1954   "d" gnus-article-treat-dumbquotes)
1955
1956 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1957   ;; mnemonic: deuglif*Y*
1958   "u" gnus-article-outlook-unwrap-lines
1959   "a" gnus-article-outlook-repair-attribution
1960   "c" gnus-article-outlook-rearrange-citation
1961   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1962
1963 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1964   "a" gnus-article-hide
1965   "h" gnus-article-hide-headers
1966   "b" gnus-article-hide-boring-headers
1967   "s" gnus-article-hide-signature
1968   "c" gnus-article-hide-citation
1969   "C" gnus-article-hide-citation-in-followups
1970   "l" gnus-article-hide-list-identifiers
1971   "B" gnus-article-strip-banner
1972   "P" gnus-article-hide-pem
1973   "\C-c" gnus-article-hide-citation-maybe)
1974
1975 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1976   "a" gnus-article-highlight
1977   "h" gnus-article-highlight-headers
1978   "c" gnus-article-highlight-citation
1979   "s" gnus-article-highlight-signature)
1980
1981 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1982   "f" gnus-article-treat-fold-headers
1983   "u" gnus-article-treat-unfold-headers
1984   "n" gnus-article-treat-fold-newsgroups)
1985
1986 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1987   "x" gnus-article-display-x-face
1988   "d" gnus-article-display-face
1989   "s" gnus-treat-smiley
1990   "D" gnus-article-remove-images
1991   "f" gnus-treat-from-picon
1992   "m" gnus-treat-mail-picon
1993   "n" gnus-treat-newsgroups-picon)
1994
1995 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1996   "z" gnus-article-date-ut
1997   "u" gnus-article-date-ut
1998   "l" gnus-article-date-local
1999   "p" gnus-article-date-english
2000   "e" gnus-article-date-lapsed
2001   "o" gnus-article-date-original
2002   "i" gnus-article-date-iso8601
2003   "s" gnus-article-date-user)
2004
2005 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
2006   "t" gnus-article-remove-trailing-blank-lines
2007   "l" gnus-article-strip-leading-blank-lines
2008   "m" gnus-article-strip-multiple-blank-lines
2009   "a" gnus-article-strip-blank-lines
2010   "A" gnus-article-strip-all-blank-lines
2011   "s" gnus-article-strip-leading-space
2012   "e" gnus-article-strip-trailing-space
2013   "w" gnus-article-remove-leading-whitespace)
2014
2015 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
2016   "v" gnus-version
2017   "f" gnus-summary-fetch-faq
2018   "d" gnus-summary-describe-group
2019   "h" gnus-summary-describe-briefly
2020   "i" gnus-info-find-node
2021   "c" gnus-group-fetch-charter
2022   "C" gnus-group-fetch-control)
2023
2024 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
2025   "e" gnus-summary-expire-articles
2026   "\M-\C-e" gnus-summary-expire-articles-now
2027   "\177" gnus-summary-delete-article
2028   [delete] gnus-summary-delete-article
2029   [backspace] gnus-summary-delete-article
2030   "m" gnus-summary-move-article
2031   "r" gnus-summary-respool-article
2032   "w" gnus-summary-edit-article
2033   "c" gnus-summary-copy-article
2034   "B" gnus-summary-crosspost-article
2035   "q" gnus-summary-respool-query
2036   "t" gnus-summary-respool-trace
2037   "i" gnus-summary-import-article
2038   "I" gnus-summary-create-article
2039   "p" gnus-summary-article-posted-p)
2040
2041 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
2042   "o" gnus-summary-save-article
2043   "m" gnus-summary-save-article-mail
2044   "F" gnus-summary-write-article-file
2045   "r" gnus-summary-save-article-rmail
2046   "f" gnus-summary-save-article-file
2047   "b" gnus-summary-save-article-body-file
2048   "h" gnus-summary-save-article-folder
2049   "v" gnus-summary-save-article-vm
2050   "p" gnus-summary-pipe-output
2051   "P" gnus-summary-muttprint
2052   "s" gnus-soup-add-article)
2053
2054 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
2055   "b" gnus-summary-display-buttonized
2056   "m" gnus-summary-repair-multipart
2057   "v" gnus-article-view-part
2058   "o" gnus-article-save-part
2059   "c" gnus-article-copy-part
2060   "C" gnus-article-view-part-as-charset
2061   "e" gnus-article-view-part-externally
2062   "E" gnus-article-encrypt-body
2063   "i" gnus-article-inline-part
2064   "|" gnus-article-pipe-part)
2065
2066 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
2067   "p" gnus-summary-mark-as-processable
2068   "u" gnus-summary-unmark-as-processable
2069   "U" gnus-summary-unmark-all-processable
2070   "v" gnus-uu-mark-over
2071   "s" gnus-uu-mark-series
2072   "r" gnus-uu-mark-region
2073   "g" gnus-uu-unmark-region
2074   "R" gnus-uu-mark-by-regexp
2075   "G" gnus-uu-unmark-by-regexp
2076   "t" gnus-uu-mark-thread
2077   "T" gnus-uu-unmark-thread
2078   "a" gnus-uu-mark-all
2079   "b" gnus-uu-mark-buffer
2080   "S" gnus-uu-mark-sparse
2081   "k" gnus-summary-kill-process-mark
2082   "y" gnus-summary-yank-process-mark
2083   "w" gnus-summary-save-process-mark
2084   "i" gnus-uu-invert-processable)
2085
2086 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2087   ;;"x" gnus-uu-extract-any
2088   "m" gnus-summary-save-parts
2089   "u" gnus-uu-decode-uu
2090   "U" gnus-uu-decode-uu-and-save
2091   "s" gnus-uu-decode-unshar
2092   "S" gnus-uu-decode-unshar-and-save
2093   "o" gnus-uu-decode-save
2094   "O" gnus-uu-decode-save
2095   "b" gnus-uu-decode-binhex
2096   "B" gnus-uu-decode-binhex
2097   "p" gnus-uu-decode-postscript
2098   "P" gnus-uu-decode-postscript-and-save)
2099
2100 (gnus-define-keys
2101     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2102   "u" gnus-uu-decode-uu-view
2103   "U" gnus-uu-decode-uu-and-save-view
2104   "s" gnus-uu-decode-unshar-view
2105   "S" gnus-uu-decode-unshar-and-save-view
2106   "o" gnus-uu-decode-save-view
2107   "O" gnus-uu-decode-save-view
2108   "b" gnus-uu-decode-binhex-view
2109   "B" gnus-uu-decode-binhex-view
2110   "p" gnus-uu-decode-postscript-view
2111   "P" gnus-uu-decode-postscript-and-save-view)
2112
2113 (defvar gnus-article-post-menu nil)
2114
2115 (defconst gnus-summary-menu-maxlen 20)
2116
2117 (defun gnus-summary-menu-split (menu)
2118   ;; If we have lots of elements, divide them into groups of 20
2119   ;; and make a pane (or submenu) for each one.
2120   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2121       (let ((menu menu) sublists next
2122             (i 1))
2123         (while menu
2124           ;; Pull off the next gnus-summary-menu-maxlen elements
2125           ;; and make them the next element of sublist.
2126           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2127           (if next
2128               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2129                       nil))
2130           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2131                                              (aref (car (last menu)) 0)) menu)
2132                                sublists))
2133           (setq i (1+ i))
2134           (setq menu next))
2135         (nreverse sublists))
2136     ;; Few elements--put them all in one pane.
2137     menu))
2138
2139 (defun gnus-summary-make-menu-bar ()
2140   (gnus-turn-off-edit-menu 'summary)
2141
2142   (unless (boundp 'gnus-summary-misc-menu)
2143
2144     (easy-menu-define
2145       gnus-summary-kill-menu gnus-summary-mode-map ""
2146       (cons
2147        "Score"
2148        (nconc
2149         (list
2150          ["Customize" gnus-score-customize t])
2151         (gnus-make-score-map 'increase)
2152         (gnus-make-score-map 'lower)
2153         '(("Mark"
2154            ["Kill below" gnus-summary-kill-below t]
2155            ["Mark above" gnus-summary-mark-above t]
2156            ["Tick above" gnus-summary-tick-above t]
2157            ["Clear above" gnus-summary-clear-above t])
2158           ["Current score" gnus-summary-current-score t]
2159           ["Set score" gnus-summary-set-score t]
2160           ["Switch current score file..." gnus-score-change-score-file t]
2161           ["Set mark below..." gnus-score-set-mark-below t]
2162           ["Set expunge below..." gnus-score-set-expunge-below t]
2163           ["Edit current score file" gnus-score-edit-current-scores t]
2164           ["Edit score file" gnus-score-edit-file t]
2165           ["Trace score" gnus-score-find-trace t]
2166           ["Find words" gnus-score-find-favourite-words t]
2167           ["Rescore buffer" gnus-summary-rescore t]
2168           ["Increase score..." gnus-summary-increase-score t]
2169           ["Lower score..." gnus-summary-lower-score t]))))
2170
2171     ;; Define both the Article menu in the summary buffer and the
2172     ;; equivalent Commands menu in the article buffer here for
2173     ;; consistency.
2174     (let ((innards
2175            `(("Hide"
2176               ["All" gnus-article-hide t]
2177               ["Headers" gnus-article-hide-headers t]
2178               ["Signature" gnus-article-hide-signature t]
2179               ["Citation" gnus-article-hide-citation t]
2180               ["List identifiers" gnus-article-hide-list-identifiers t]
2181               ["Banner" gnus-article-strip-banner t]
2182               ["Boring headers" gnus-article-hide-boring-headers t])
2183              ("Highlight"
2184               ["All" gnus-article-highlight t]
2185               ["Headers" gnus-article-highlight-headers t]
2186               ["Signature" gnus-article-highlight-signature t]
2187               ["Citation" gnus-article-highlight-citation t])
2188              ("Date"
2189               ["Local" gnus-article-date-local t]
2190               ["ISO8601" gnus-article-date-iso8601 t]
2191               ["UT" gnus-article-date-ut t]
2192               ["Original" gnus-article-date-original t]
2193               ["Lapsed" gnus-article-date-lapsed t]
2194               ["User-defined" gnus-article-date-user t])
2195              ("Display"
2196               ["Remove images" gnus-article-remove-images t]
2197               ["Toggle smiley" gnus-treat-smiley t]
2198               ["Show X-Face" gnus-article-display-x-face t]
2199               ["Show picons in From" gnus-treat-from-picon t]
2200               ["Show picons in mail headers" gnus-treat-mail-picon t]
2201               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2202               ("View as different encoding"
2203                ,@(gnus-summary-menu-split
2204                   (mapcar
2205                    (lambda (cs)
2206                      ;; Since easymenu under Emacs doesn't allow
2207                      ;; lambda forms for menu commands, we should
2208                      ;; provide intern'ed function symbols.
2209                      (let ((command (intern (format "\
2210 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2211                        (fset command
2212                              `(lambda ()
2213                                 (interactive)
2214                                 (let ((gnus-summary-show-article-charset-alist
2215                                        '((1 . ,cs))))
2216                                   (gnus-summary-show-article 1))))
2217                        `[,(symbol-name cs) ,command t]))
2218                    (sort (if (fboundp 'coding-system-list)
2219                              (coding-system-list)
2220                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2221                            )
2222                          'string<)))))
2223              ("Washing"
2224               ("Remove Blanks"
2225                ["Leading" gnus-article-strip-leading-blank-lines t]
2226                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2227                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2228                ["All of the above" gnus-article-strip-blank-lines t]
2229                ["All" gnus-article-strip-all-blank-lines t]
2230                ["Leading space" gnus-article-strip-leading-space t]
2231                ["Trailing space" gnus-article-strip-trailing-space t]
2232                ["Leading space in headers"
2233                 gnus-article-remove-leading-whitespace t])
2234               ["Overstrike" gnus-article-treat-overstrike t]
2235               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2236               ["Emphasis" gnus-article-emphasize t]
2237               ["Word wrap" gnus-article-fill-cited-article t]
2238               ["Fill long lines" gnus-article-fill-long-lines t]
2239               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2240               ["Remove CR" gnus-article-remove-cr t]
2241               ["Rot 13" gnus-summary-caesar-message
2242                ,@(if (featurep 'xemacs) '(t)
2243                    '(:help "\"Caesar rotate\" article by 13"))]
2244               ["Morse decode" gnus-summary-morse-message t]
2245               ["Unix pipe..." gnus-summary-pipe-message t]
2246               ["Add buttons" gnus-article-add-buttons t]
2247               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2248               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2249               ["Toggle MIME" gnus-summary-toggle-mime t]
2250               ["Verbose header" gnus-summary-verbose-headers t]
2251               ["Toggle header" gnus-summary-toggle-header t]
2252               ["Unfold headers" gnus-article-treat-unfold-headers t]
2253               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2254               ["Html" gnus-article-wash-html t]
2255               ["Unsplit URLs" gnus-article-unsplit-urls t]
2256               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2257               ["Decode HZ" gnus-article-decode-HZ t]
2258               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2259               ("(Outlook) Deuglify"
2260                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2261                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2262                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2263                ["Full (Outlook) deuglify"
2264                 gnus-article-outlook-deuglify-article t])
2265               )
2266              ("Output"
2267               ["Save in default format..." gnus-summary-save-article
2268                ,@(if (featurep 'xemacs) '(t)
2269                    '(:help "Save article using default method"))]
2270               ["Save in file..." gnus-summary-save-article-file
2271                ,@(if (featurep 'xemacs) '(t)
2272                    '(:help "Save article in file"))]
2273               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2274               ["Save in MH folder..." gnus-summary-save-article-folder t]
2275               ["Save in VM folder..." gnus-summary-save-article-vm t]
2276               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2277               ["Save body in file..." gnus-summary-save-article-body-file t]
2278               ["Pipe through a filter..." gnus-summary-pipe-output t]
2279               ["Add to SOUP packet" gnus-soup-add-article t]
2280               ["Print with Muttprint..." gnus-summary-muttprint t]
2281               ["Print" gnus-summary-print-article
2282                ,@(if (featurep 'xemacs) '(t)
2283                    '(:help "Generate and print a PostScript image"))])
2284              ("Copy, move,... (Backend)"
2285               ,@(if (featurep 'xemacs) nil
2286                   '(:help "Copying, moving, expiring articles..."))
2287               ["Respool article..." gnus-summary-respool-article t]
2288               ["Move article..." gnus-summary-move-article
2289                (gnus-check-backend-function
2290                 'request-move-article gnus-newsgroup-name)]
2291               ["Copy article..." gnus-summary-copy-article t]
2292               ["Crosspost article..." gnus-summary-crosspost-article
2293                (gnus-check-backend-function
2294                 'request-replace-article gnus-newsgroup-name)]
2295               ["Import file..." gnus-summary-import-article
2296                (gnus-check-backend-function
2297                 'request-accept-article gnus-newsgroup-name)]
2298               ["Create article..." gnus-summary-create-article
2299                (gnus-check-backend-function
2300                 'request-accept-article gnus-newsgroup-name)]
2301               ["Check if posted" gnus-summary-article-posted-p t]
2302               ["Edit article" gnus-summary-edit-article
2303                (not (gnus-group-read-only-p))]
2304               ["Delete article" gnus-summary-delete-article
2305                (gnus-check-backend-function
2306                 'request-expire-articles gnus-newsgroup-name)]
2307               ["Query respool" gnus-summary-respool-query t]
2308               ["Trace respool" gnus-summary-respool-trace t]
2309               ["Delete expirable articles" gnus-summary-expire-articles-now
2310                (gnus-check-backend-function
2311                 'request-expire-articles gnus-newsgroup-name)])
2312              ("Extract"
2313               ["Uudecode" gnus-uu-decode-uu
2314                ,@(if (featurep 'xemacs) '(t)
2315                    '(:help "Decode uuencoded article(s)"))]
2316               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2317               ["Unshar" gnus-uu-decode-unshar t]
2318               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2319               ["Save" gnus-uu-decode-save t]
2320               ["Binhex" gnus-uu-decode-binhex t]
2321               ["Postscript" gnus-uu-decode-postscript t])
2322              ("Cache"
2323               ["Enter article" gnus-cache-enter-article t]
2324               ["Remove article" gnus-cache-remove-article t])
2325              ["Translate" gnus-article-babel t]
2326              ["Select article buffer" gnus-summary-select-article-buffer t]
2327              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2328              ["Isearch article..." gnus-summary-isearch-article t]
2329              ["Beginning of the article" gnus-summary-beginning-of-article t]
2330              ["End of the article" gnus-summary-end-of-article t]
2331              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2332              ["Fetch referenced articles" gnus-summary-refer-references t]
2333              ["Fetch current thread" gnus-summary-refer-thread t]
2334              ["Fetch article with id..." gnus-summary-refer-article t]
2335              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2336              ["Redisplay" gnus-summary-show-article t]
2337              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2338       (easy-menu-define
2339         gnus-summary-article-menu gnus-summary-mode-map ""
2340         (cons "Article" innards))
2341
2342       (if (not (keymapp gnus-summary-article-menu))
2343           (easy-menu-define
2344             gnus-article-commands-menu gnus-article-mode-map ""
2345             (cons "Commands" innards))
2346         ;; in Emacs, don't share menu.
2347         (setq gnus-article-commands-menu
2348               (copy-keymap gnus-summary-article-menu))
2349         (define-key gnus-article-mode-map [menu-bar commands]
2350           (cons "Commands" gnus-article-commands-menu))))
2351
2352     (easy-menu-define
2353       gnus-summary-thread-menu gnus-summary-mode-map ""
2354       '("Threads"
2355         ["Find all messages in thread" gnus-summary-refer-thread t]
2356         ["Toggle threading" gnus-summary-toggle-threads t]
2357         ["Hide threads" gnus-summary-hide-all-threads t]
2358         ["Show threads" gnus-summary-show-all-threads t]
2359         ["Hide thread" gnus-summary-hide-thread t]
2360         ["Show thread" gnus-summary-show-thread t]
2361         ["Go to next thread" gnus-summary-next-thread t]
2362         ["Go to previous thread" gnus-summary-prev-thread t]
2363         ["Go down thread" gnus-summary-down-thread t]
2364         ["Go up thread" gnus-summary-up-thread t]
2365         ["Top of thread" gnus-summary-top-thread t]
2366         ["Mark thread as read" gnus-summary-kill-thread t]
2367         ["Lower thread score" gnus-summary-lower-thread t]
2368         ["Raise thread score" gnus-summary-raise-thread t]
2369         ["Rethread current" gnus-summary-rethread-current t]))
2370
2371     (easy-menu-define
2372       gnus-summary-post-menu gnus-summary-mode-map ""
2373       `("Post"
2374         ["Send a message (mail or news)" gnus-summary-post-news
2375          ,@(if (featurep 'xemacs) '(t)
2376              '(:help "Compose a new message (mail or news)"))]
2377         ["Followup" gnus-summary-followup
2378          ,@(if (featurep 'xemacs) '(t)
2379              '(:help "Post followup to this article"))]
2380         ["Followup and yank" gnus-summary-followup-with-original
2381          ,@(if (featurep 'xemacs) '(t)
2382              '(:help "Post followup to this article, quoting its contents"))]
2383         ["Supersede article" gnus-summary-supersede-article t]
2384         ["Cancel article" gnus-summary-cancel-article
2385          ,@(if (featurep 'xemacs) '(t)
2386              '(:help "Cancel an article you posted"))]
2387         ["Reply" gnus-summary-reply t]
2388         ["Reply and yank" gnus-summary-reply-with-original t]
2389         ["Wide reply" gnus-summary-wide-reply t]
2390         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2391          ,@(if (featurep 'xemacs) '(t)
2392              '(:help "Mail a reply, quoting this article"))]
2393         ["Very wide reply" gnus-summary-very-wide-reply t]
2394         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2395          ,@(if (featurep 'xemacs) '(t)
2396              '(:help "Mail a very wide reply, quoting this article"))]
2397         ["Mail forward" gnus-summary-mail-forward t]
2398         ["Post forward" gnus-summary-post-forward t]
2399         ["Digest and mail" gnus-summary-digest-mail-forward t]
2400         ["Digest and post" gnus-summary-digest-post-forward t]
2401         ["Resend message" gnus-summary-resend-message t]
2402         ["Resend message edit" gnus-summary-resend-message-edit t]
2403         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2404         ["Send a mail" gnus-summary-mail-other-window t]
2405         ["Create a local message" gnus-summary-news-other-window t]
2406         ["Uuencode and post" gnus-uu-post-news
2407          ,@(if (featurep 'xemacs) '(t)
2408              '(:help "Post a uuencoded article"))]
2409         ["Followup via news" gnus-summary-followup-to-mail t]
2410         ["Followup via news and yank"
2411          gnus-summary-followup-to-mail-with-original t]
2412         ;;("Draft"
2413         ;;["Send" gnus-summary-send-draft t]
2414         ;;["Send bounced" gnus-resend-bounced-mail t])
2415         ))
2416
2417     (cond
2418      ((not (keymapp gnus-summary-post-menu))
2419       (setq gnus-article-post-menu gnus-summary-post-menu))
2420      ((not gnus-article-post-menu)
2421       ;; Don't share post menu.
2422       (setq gnus-article-post-menu
2423             (copy-keymap gnus-summary-post-menu))))
2424     (define-key gnus-article-mode-map [menu-bar post]
2425       (cons "Post" gnus-article-post-menu))
2426
2427     (easy-menu-define
2428       gnus-summary-misc-menu gnus-summary-mode-map ""
2429       `("Gnus"
2430         ("Mark Read"
2431          ["Mark as read" gnus-summary-mark-as-read-forward t]
2432          ["Mark same subject and select"
2433           gnus-summary-kill-same-subject-and-select t]
2434          ["Mark same subject" gnus-summary-kill-same-subject t]
2435          ["Catchup" gnus-summary-catchup
2436           ,@(if (featurep 'xemacs) '(t)
2437               '(:help "Mark unread articles in this group as read"))]
2438          ["Catchup all" gnus-summary-catchup-all t]
2439          ["Catchup to here" gnus-summary-catchup-to-here t]
2440          ["Catchup from here" gnus-summary-catchup-from-here t]
2441          ["Catchup region" gnus-summary-mark-region-as-read
2442           (gnus-mark-active-p)]
2443          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2444         ("Mark Various"
2445          ["Tick" gnus-summary-tick-article-forward t]
2446          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2447          ["Remove marks" gnus-summary-clear-mark-forward t]
2448          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2449          ["Set bookmark" gnus-summary-set-bookmark t]
2450          ["Remove bookmark" gnus-summary-remove-bookmark t])
2451         ("Limit to"
2452          ["Marks..." gnus-summary-limit-to-marks t]
2453          ["Subject..." gnus-summary-limit-to-subject t]
2454          ["Author..." gnus-summary-limit-to-author t]
2455          ["Age..." gnus-summary-limit-to-age t]
2456          ["Extra..." gnus-summary-limit-to-extra t]
2457          ["Score..." gnus-summary-limit-to-score t]
2458          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2459          ["Unread" gnus-summary-limit-to-unread t]
2460          ["Unseen" gnus-summary-limit-to-unseen t]
2461          ["Replied" gnus-summary-limit-to-replied t]
2462          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2463          ["Next articles" gnus-summary-limit-to-articles t]
2464          ["Pop limit" gnus-summary-pop-limit t]
2465          ["Show dormant" gnus-summary-limit-include-dormant t]
2466          ["Hide childless dormant"
2467           gnus-summary-limit-exclude-childless-dormant t]
2468          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2469          ["Hide marked" gnus-summary-limit-exclude-marks t]
2470          ["Show expunged" gnus-summary-limit-include-expunged t])
2471         ("Process Mark"
2472          ["Set mark" gnus-summary-mark-as-processable t]
2473          ["Remove mark" gnus-summary-unmark-as-processable t]
2474          ["Remove all marks" gnus-summary-unmark-all-processable t]
2475          ["Invert marks" gnus-uu-invert-processable t]
2476          ["Mark above" gnus-uu-mark-over t]
2477          ["Mark series" gnus-uu-mark-series t]
2478          ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2479          ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2480          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2481          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2482          ["Mark all" gnus-uu-mark-all t]
2483          ["Mark buffer" gnus-uu-mark-buffer t]
2484          ["Mark sparse" gnus-uu-mark-sparse t]
2485          ["Mark thread" gnus-uu-mark-thread t]
2486          ["Unmark thread" gnus-uu-unmark-thread t]
2487          ("Process Mark Sets"
2488           ["Kill" gnus-summary-kill-process-mark t]
2489           ["Yank" gnus-summary-yank-process-mark
2490            gnus-newsgroup-process-stack]
2491           ["Save" gnus-summary-save-process-mark t]
2492           ["Run command on marked..." gnus-summary-universal-argument t]))
2493         ("Scroll article"
2494          ["Page forward" gnus-summary-next-page
2495           ,@(if (featurep 'xemacs) '(t)
2496               '(:help "Show next page of article"))]
2497          ["Page backward" gnus-summary-prev-page
2498           ,@(if (featurep 'xemacs) '(t)
2499               '(:help "Show previous page of article"))]
2500          ["Line forward" gnus-summary-scroll-up t])
2501         ("Move"
2502          ["Next unread article" gnus-summary-next-unread-article t]
2503          ["Previous unread article" gnus-summary-prev-unread-article t]
2504          ["Next article" gnus-summary-next-article t]
2505          ["Previous article" gnus-summary-prev-article t]
2506          ["Next unread subject" gnus-summary-next-unread-subject t]
2507          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2508          ["Next article same subject" gnus-summary-next-same-subject t]
2509          ["Previous article same subject" gnus-summary-prev-same-subject t]
2510          ["First unread article" gnus-summary-first-unread-article t]
2511          ["Best unread article" gnus-summary-best-unread-article t]
2512          ["Go to subject number..." gnus-summary-goto-subject t]
2513          ["Go to article number..." gnus-summary-goto-article t]
2514          ["Go to the last article" gnus-summary-goto-last-article t]
2515          ["Pop article off history" gnus-summary-pop-article t])
2516         ("Sort"
2517          ["Sort by number" gnus-summary-sort-by-number t]
2518          ["Sort by author" gnus-summary-sort-by-author t]
2519          ["Sort by subject" gnus-summary-sort-by-subject t]
2520          ["Sort by date" gnus-summary-sort-by-date t]
2521          ["Sort by score" gnus-summary-sort-by-score t]
2522          ["Sort by lines" gnus-summary-sort-by-lines t]
2523          ["Sort by characters" gnus-summary-sort-by-chars t]
2524          ["Randomize" gnus-summary-sort-by-random t]
2525          ["Original sort" gnus-summary-sort-by-original t])
2526         ("Help"
2527          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2528          ["Describe group" gnus-summary-describe-group t]
2529          ["Fetch charter" gnus-group-fetch-charter
2530           ,@(if (featurep 'xemacs) nil
2531               '(:help "Display the charter of the current group"))]
2532          ["Fetch control message" gnus-group-fetch-control
2533           ,@(if (featurep 'xemacs) nil
2534               '(:help "Display the archived control message for the current group"))]
2535          ["Read manual" gnus-info-find-node t])
2536         ("Modes"
2537          ["Pick and read" gnus-pick-mode t]
2538          ["Binary" gnus-binary-mode t])
2539         ("Regeneration"
2540          ["Regenerate" gnus-summary-prepare t]
2541          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2542          ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2543          ["Toggle threading" gnus-summary-toggle-threads t])
2544         ["See old articles" gnus-summary-insert-old-articles t]
2545         ["See new articles" gnus-summary-insert-new-articles t]
2546         ["Filter articles..." gnus-summary-execute-command t]
2547         ["Run command on articles..." gnus-summary-universal-argument t]
2548         ["Search articles forward..." gnus-summary-search-article-forward t]
2549         ["Search articles backward..." gnus-summary-search-article-backward t]
2550         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2551         ["Expand window" gnus-summary-expand-window t]
2552         ["Expire expirable articles" gnus-summary-expire-articles
2553          (gnus-check-backend-function
2554           'request-expire-articles gnus-newsgroup-name)]
2555         ["Edit local kill file" gnus-summary-edit-local-kill t]
2556         ["Edit main kill file" gnus-summary-edit-global-kill t]
2557         ["Edit group parameters" gnus-summary-edit-parameters t]
2558         ["Customize group parameters" gnus-summary-customize-parameters t]
2559         ["Send a bug report" gnus-bug t]
2560         ("Exit"
2561          ["Catchup and exit" gnus-summary-catchup-and-exit
2562           ,@(if (featurep 'xemacs) '(t)
2563               '(:help "Mark unread articles in this group as read, then exit"))]
2564          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2565          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2566          ["Exit group" gnus-summary-exit
2567           ,@(if (featurep 'xemacs) '(t)
2568               '(:help "Exit current group, return to group selection mode"))]
2569          ["Exit group without updating" gnus-summary-exit-no-update t]
2570          ["Exit and goto next group" gnus-summary-next-group t]
2571          ["Exit and goto prev group" gnus-summary-prev-group t]
2572          ["Reselect group" gnus-summary-reselect-current-group t]
2573          ["Rescan group" gnus-summary-rescan-group t]
2574          ["Update dribble" gnus-summary-save-newsrc t])))
2575
2576     (gnus-run-hooks 'gnus-summary-menu-hook)))
2577
2578 (defvar gnus-summary-tool-bar-map nil)
2579
2580 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2581 (defun gnus-summary-make-tool-bar ()
2582   (if (and (fboundp 'tool-bar-add-item-from-menu)
2583            (default-value 'tool-bar-mode)
2584            (not gnus-summary-tool-bar-map))
2585       (setq gnus-summary-tool-bar-map
2586             (let ((tool-bar-map (make-sparse-keymap))
2587                   (load-path (mm-image-load-path)))
2588               (tool-bar-add-item-from-menu
2589                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2590               (tool-bar-add-item-from-menu
2591                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2592               (tool-bar-add-item-from-menu
2593                'gnus-summary-post-news "post" gnus-summary-mode-map)
2594               (tool-bar-add-item-from-menu
2595                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2596               (tool-bar-add-item-from-menu
2597                'gnus-summary-followup "followup" gnus-summary-mode-map)
2598               (tool-bar-add-item-from-menu
2599                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2600               (tool-bar-add-item-from-menu
2601                'gnus-summary-reply "reply" gnus-summary-mode-map)
2602               (tool-bar-add-item-from-menu
2603                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2604               (tool-bar-add-item-from-menu
2605                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2606               (tool-bar-add-item-from-menu
2607                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2608               (tool-bar-add-item-from-menu
2609                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2610               (tool-bar-add-item-from-menu
2611                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2612               (tool-bar-add-item-from-menu
2613                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2614               (tool-bar-add-item-from-menu
2615                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2616               (tool-bar-add-item-from-menu
2617                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2618               (tool-bar-add-item-from-menu
2619                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2620               tool-bar-map)))
2621   (if gnus-summary-tool-bar-map
2622       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2623
2624 (defun gnus-score-set-default (var value)
2625   "A version of set that updates the GNU Emacs menu-bar."
2626   (set var value)
2627   ;; It is the message that forces the active status to be updated.
2628   (message ""))
2629
2630 (defun gnus-make-score-map (type)
2631   "Make a summary score map of type TYPE."
2632   (if t
2633       nil
2634     (let ((headers '(("author" "from" string)
2635                      ("subject" "subject" string)
2636                      ("article body" "body" string)
2637                      ("article head" "head" string)
2638                      ("xref" "xref" string)
2639                      ("extra header" "extra" string)
2640                      ("lines" "lines" number)
2641                      ("followups to author" "followup" string)))
2642           (types '((number ("less than" <)
2643                            ("greater than" >)
2644                            ("equal" =))
2645                    (string ("substring" s)
2646                            ("exact string" e)
2647                            ("fuzzy string" f)
2648                            ("regexp" r))))
2649           (perms '(("temporary" (current-time-string))
2650                    ("permanent" nil)
2651                    ("immediate" now)))
2652           header)
2653       (list
2654        (apply
2655         'nconc
2656         (list
2657          (if (eq type 'lower)
2658              "Lower score"
2659            "Increase score"))
2660         (let (outh)
2661           (while headers
2662             (setq header (car headers))
2663             (setq outh
2664                   (cons
2665                    (apply
2666                     'nconc
2667                     (list (car header))
2668                     (let ((ts (cdr (assoc (nth 2 header) types)))
2669                           outt)
2670                       (while ts
2671                         (setq outt
2672                               (cons
2673                                (apply
2674                                 'nconc
2675                                 (list (caar ts))
2676                                 (let ((ps perms)
2677                                       outp)
2678                                   (while ps
2679                                     (setq outp
2680                                           (cons
2681                                            (vector
2682                                             (caar ps)
2683                                             (list
2684                                              'gnus-summary-score-entry
2685                                              (nth 1 header)
2686                                              (if (or (string= (nth 1 header)
2687                                                               "head")
2688                                                      (string= (nth 1 header)
2689                                                               "body"))
2690                                                  ""
2691                                                (list 'gnus-summary-header
2692                                                      (nth 1 header)))
2693                                              (list 'quote (nth 1 (car ts)))
2694                                              (list 'gnus-score-delta-default
2695                                                    nil)
2696                                              (nth 1 (car ps))
2697                                              t)
2698                                             t)
2699                                            outp))
2700                                     (setq ps (cdr ps)))
2701                                   (list (nreverse outp))))
2702                                outt))
2703                         (setq ts (cdr ts)))
2704                       (list (nreverse outt))))
2705                    outh))
2706             (setq headers (cdr headers)))
2707           (list (nreverse outh))))))))
2708
2709 \f
2710
2711 (defun gnus-summary-mode (&optional group)
2712   "Major mode for reading articles.
2713
2714 All normal editing commands are switched off.
2715 \\<gnus-summary-mode-map>
2716 Each line in this buffer represents one article.  To read an
2717 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2718 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2719 respectively.
2720
2721 You can also post articles and send mail from this buffer.  To
2722 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2723 of an article, type `\\[gnus-summary-reply]'.
2724
2725 There are approx. one gazillion commands you can execute in this
2726 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2727
2728 The following commands are available:
2729
2730 \\{gnus-summary-mode-map}"
2731   (interactive)
2732   (kill-all-local-variables)
2733   (when (gnus-visual-p 'summary-menu 'menu)
2734     (gnus-summary-make-menu-bar)
2735     (gnus-summary-make-tool-bar))
2736   (gnus-summary-make-local-variables)
2737   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2738     (gnus-summary-make-local-variables))
2739   (gnus-make-thread-indent-array)
2740   (gnus-simplify-mode-line)
2741   (setq major-mode 'gnus-summary-mode)
2742   (setq mode-name "Summary")
2743   (make-local-variable 'minor-mode-alist)
2744   (use-local-map gnus-summary-mode-map)
2745   (buffer-disable-undo)
2746   (setq buffer-read-only t              ;Disable modification
2747         show-trailing-whitespace nil)
2748   (setq truncate-lines t)
2749   (setq selective-display t)
2750   (setq selective-display-ellipses t)   ;Display `...'
2751   (gnus-summary-set-display-table)
2752   (gnus-set-default-directory)
2753   (setq gnus-newsgroup-name group)
2754   (unless (gnus-news-group-p group)
2755     (setq gnus-newsgroup-incorporated
2756           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2757   (make-local-variable 'gnus-summary-line-format)
2758   (make-local-variable 'gnus-summary-line-format-spec)
2759   (make-local-variable 'gnus-summary-dummy-line-format)
2760   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2761   (make-local-variable 'gnus-summary-mark-positions)
2762   (gnus-make-local-hook 'pre-command-hook)
2763   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2764   (gnus-run-hooks 'gnus-summary-mode-hook)
2765   (turn-on-gnus-mailing-list-mode)
2766   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2767   (gnus-update-summary-mark-positions))
2768
2769 (defun gnus-summary-make-local-variables ()
2770   "Make all the local summary buffer variables."
2771   (let (global)
2772     (dolist (local gnus-summary-local-variables)
2773       (if (consp local)
2774           (progn
2775             (if (eq (cdr local) 'global)
2776                 ;; Copy the global value of the variable.
2777                 (setq global (symbol-value (car local)))
2778               ;; Use the value from the list.
2779               (setq global (eval (cdr local))))
2780             (set (make-local-variable (car local)) global))
2781         ;; Simple nil-valued local variable.
2782         (set (make-local-variable local) nil)))))
2783
2784 (defun gnus-summary-clear-local-variables ()
2785   (let ((locals gnus-summary-local-variables))
2786     (while locals
2787       (if (consp (car locals))
2788           (and (vectorp (caar locals))
2789                (set (caar locals) nil))
2790         (and (vectorp (car locals))
2791              (set (car locals) nil)))
2792       (setq locals (cdr locals)))))
2793
2794 ;; Summary data functions.
2795
2796 (defmacro gnus-data-number (data)
2797   `(car ,data))
2798
2799 (defmacro gnus-data-set-number (data number)
2800   `(setcar ,data ,number))
2801
2802 (defmacro gnus-data-mark (data)
2803   `(nth 1 ,data))
2804
2805 (defmacro gnus-data-set-mark (data mark)
2806   `(setcar (nthcdr 1 ,data) ,mark))
2807
2808 (defmacro gnus-data-pos (data)
2809   `(nth 2 ,data))
2810
2811 (defmacro gnus-data-set-pos (data pos)
2812   `(setcar (nthcdr 2 ,data) ,pos))
2813
2814 (defmacro gnus-data-header (data)
2815   `(nth 3 ,data))
2816
2817 (defmacro gnus-data-set-header (data header)
2818   `(setcar (nthcdr 3 ,data) ,header))
2819
2820 (defmacro gnus-data-level (data)
2821   `(nth 4 ,data))
2822
2823 (defmacro gnus-data-unread-p (data)
2824   `(= (nth 1 ,data) gnus-unread-mark))
2825
2826 (defmacro gnus-data-read-p (data)
2827   `(/= (nth 1 ,data) gnus-unread-mark))
2828
2829 (defmacro gnus-data-pseudo-p (data)
2830   `(consp (nth 3 ,data)))
2831
2832 (defmacro gnus-data-find (number)
2833   `(assq ,number gnus-newsgroup-data))
2834
2835 (defmacro gnus-data-find-list (number &optional data)
2836   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2837      (memq (assq ,number bdata)
2838            bdata)))
2839
2840 (defmacro gnus-data-make (number mark pos header level)
2841   `(list ,number ,mark ,pos ,header ,level))
2842
2843 (defun gnus-data-enter (after-article number mark pos header level offset)
2844   (let ((data (gnus-data-find-list after-article)))
2845     (unless data
2846       (error "No such article: %d" after-article))
2847     (setcdr data (cons (gnus-data-make number mark pos header level)
2848                        (cdr data)))
2849     (setq gnus-newsgroup-data-reverse nil)
2850     (gnus-data-update-list (cddr data) offset)))
2851
2852 (defun gnus-data-enter-list (after-article list &optional offset)
2853   (when list
2854     (let ((data (and after-article (gnus-data-find-list after-article)))
2855           (ilist list))
2856       (if (not (or data
2857                    after-article))
2858           (let ((odata gnus-newsgroup-data))
2859             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2860             (when offset
2861               (gnus-data-update-list odata offset)))
2862         ;; Find the last element in the list to be spliced into the main
2863         ;; list.
2864         (setq list (last list))
2865         (if (not data)
2866             (progn
2867               (setcdr list gnus-newsgroup-data)
2868               (setq gnus-newsgroup-data ilist)
2869               (when offset
2870                 (gnus-data-update-list (cdr list) offset)))
2871           (setcdr list (cdr data))
2872           (setcdr data ilist)
2873           (when offset
2874             (gnus-data-update-list (cdr list) offset))))
2875       (setq gnus-newsgroup-data-reverse nil))))
2876
2877 (defun gnus-data-remove (article &optional offset)
2878   (let ((data gnus-newsgroup-data))
2879     (if (= (gnus-data-number (car data)) article)
2880         (progn
2881           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2882                 gnus-newsgroup-data-reverse nil)
2883           (when offset
2884             (gnus-data-update-list gnus-newsgroup-data offset)))
2885       (while (cdr data)
2886         (when (= (gnus-data-number (cadr data)) article)
2887           (setcdr data (cddr data))
2888           (when offset
2889             (gnus-data-update-list (cdr data) offset))
2890           (setq data nil
2891                 gnus-newsgroup-data-reverse nil))
2892         (setq data (cdr data))))))
2893
2894 (defmacro gnus-data-list (backward)
2895   `(if ,backward
2896        (or gnus-newsgroup-data-reverse
2897            (setq gnus-newsgroup-data-reverse
2898                  (reverse gnus-newsgroup-data)))
2899      gnus-newsgroup-data))
2900
2901 (defun gnus-data-update-list (data offset)
2902   "Add OFFSET to the POS of all data entries in DATA."
2903   (setq gnus-newsgroup-data-reverse nil)
2904   (while data
2905     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2906     (setq data (cdr data))))
2907
2908 (defun gnus-summary-article-pseudo-p (article)
2909   "Say whether this article is a pseudo article or not."
2910   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2911
2912 (defmacro gnus-summary-article-sparse-p (article)
2913   "Say whether this article is a sparse article or not."
2914   `(memq ,article gnus-newsgroup-sparse))
2915
2916 (defmacro gnus-summary-article-ancient-p (article)
2917   "Say whether this article is a sparse article or not."
2918   `(memq ,article gnus-newsgroup-ancient))
2919
2920 (defun gnus-article-parent-p (number)
2921   "Say whether this article is a parent or not."
2922   (let ((data (gnus-data-find-list number)))
2923     (and (cdr data)                     ; There has to be an article after...
2924          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2925             (gnus-data-level (nth 1 data))))))
2926
2927 (defun gnus-article-children (number)
2928   "Return a list of all children to NUMBER."
2929   (let* ((data (gnus-data-find-list number))
2930          (level (gnus-data-level (car data)))
2931          children)
2932     (setq data (cdr data))
2933     (while (and data
2934                 (= (gnus-data-level (car data)) (1+ level)))
2935       (push (gnus-data-number (car data)) children)
2936       (setq data (cdr data)))
2937     children))
2938
2939 (defmacro gnus-summary-skip-intangible ()
2940   "If the current article is intangible, then jump to a different article."
2941   '(let ((to (get-text-property (point) 'gnus-intangible)))
2942      (and to (gnus-summary-goto-subject to))))
2943
2944 (defmacro gnus-summary-article-intangible-p ()
2945   "Say whether this article is intangible or not."
2946   '(get-text-property (point) 'gnus-intangible))
2947
2948 (defun gnus-article-read-p (article)
2949   "Say whether ARTICLE is read or not."
2950   (not (or (memq article gnus-newsgroup-marked)
2951            (memq article gnus-newsgroup-spam-marked)
2952            (memq article gnus-newsgroup-unreads)
2953            (memq article gnus-newsgroup-unselected)
2954            (memq article gnus-newsgroup-dormant))))
2955
2956 ;; Some summary mode macros.
2957
2958 (defmacro gnus-summary-article-number ()
2959   "The article number of the article on the current line.
2960 If there isn't an article number here, then we return the current
2961 article number."
2962   '(progn
2963      (gnus-summary-skip-intangible)
2964      (or (get-text-property (point) 'gnus-number)
2965          (gnus-summary-last-subject))))
2966
2967 (defmacro gnus-summary-article-header (&optional number)
2968   "Return the header of article NUMBER."
2969   `(gnus-data-header (gnus-data-find
2970                       ,(or number '(gnus-summary-article-number)))))
2971
2972 (defmacro gnus-summary-thread-level (&optional number)
2973   "Return the level of thread that starts with article NUMBER."
2974   `(if (and (eq gnus-summary-make-false-root 'dummy)
2975             (get-text-property (point) 'gnus-intangible))
2976        0
2977      (gnus-data-level (gnus-data-find
2978                        ,(or number '(gnus-summary-article-number))))))
2979
2980 (defmacro gnus-summary-article-mark (&optional number)
2981   "Return the mark of article NUMBER."
2982   `(gnus-data-mark (gnus-data-find
2983                     ,(or number '(gnus-summary-article-number)))))
2984
2985 (defmacro gnus-summary-article-pos (&optional number)
2986   "Return the position of the line of article NUMBER."
2987   `(gnus-data-pos (gnus-data-find
2988                    ,(or number '(gnus-summary-article-number)))))
2989
2990 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2991 (defmacro gnus-summary-article-subject (&optional number)
2992   "Return current subject string or nil if nothing."
2993   `(let ((headers
2994           ,(if number
2995                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2996              '(gnus-data-header (assq (gnus-summary-article-number)
2997                                       gnus-newsgroup-data)))))
2998      (and headers
2999           (vectorp headers)
3000           (mail-header-subject headers))))
3001
3002 (defmacro gnus-summary-article-score (&optional number)
3003   "Return current article score."
3004   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
3005                   gnus-newsgroup-scored))
3006        gnus-summary-default-score 0))
3007
3008 (defun gnus-summary-article-children (&optional number)
3009   "Return a list of article numbers that are children of article NUMBER."
3010   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
3011          (level (gnus-data-level (car data)))
3012          l children)
3013     (while (and (setq data (cdr data))
3014                 (> (setq l (gnus-data-level (car data))) level))
3015       (and (= (1+ level) l)
3016            (push (gnus-data-number (car data))
3017                  children)))
3018     (nreverse children)))
3019
3020 (defun gnus-summary-article-parent (&optional number)
3021   "Return the article number of the parent of article NUMBER."
3022   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
3023                                     (gnus-data-list t)))
3024          (level (gnus-data-level (car data))))
3025     (if (zerop level)
3026         ()                              ; This is a root.
3027       ;; We search until we find an article with a level less than
3028       ;; this one.  That function has to be the parent.
3029       (while (and (setq data (cdr data))
3030                   (not (< (gnus-data-level (car data)) level))))
3031       (and data (gnus-data-number (car data))))))
3032
3033 (defun gnus-unread-mark-p (mark)
3034   "Say whether MARK is the unread mark."
3035   (= mark gnus-unread-mark))
3036
3037 (defun gnus-read-mark-p (mark)
3038   "Say whether MARK is one of the marks that mark as read.
3039 This is all marks except unread, ticked, dormant, and expirable."
3040   (not (or (= mark gnus-unread-mark)
3041            (= mark gnus-ticked-mark)
3042            (= mark gnus-spam-mark)
3043            (= mark gnus-dormant-mark)
3044            (= mark gnus-expirable-mark))))
3045
3046 (defmacro gnus-article-mark (number)
3047   "Return the MARK of article NUMBER.
3048 This macro should only be used when computing the mark the \"first\"
3049 time; i.e., when generating the summary lines.  After that,
3050 `gnus-summary-article-mark' should be used to examine the
3051 marks of articles."
3052   `(cond
3053     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
3054     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
3055     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
3056     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
3057     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
3058     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
3059     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
3060     (t (or (cdr (assq ,number gnus-newsgroup-reads))
3061            gnus-ancient-mark))))
3062
3063 ;; Saving hidden threads.
3064
3065 (defmacro gnus-save-hidden-threads (&rest forms)
3066   "Save hidden threads, eval FORMS, and restore the hidden threads."
3067   (let ((config (make-symbol "config")))
3068     `(let ((,config (gnus-hidden-threads-configuration)))
3069        (unwind-protect
3070            (save-excursion
3071              ,@forms)
3072          (gnus-restore-hidden-threads-configuration ,config)))))
3073 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
3074 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
3075
3076 (defun gnus-data-compute-positions ()
3077   "Compute the positions of all articles."
3078   (setq gnus-newsgroup-data-reverse nil)
3079   (let ((data gnus-newsgroup-data))
3080     (save-excursion
3081       (gnus-save-hidden-threads
3082         (gnus-summary-show-all-threads)
3083         (goto-char (point-min))
3084         (while data
3085           (while (get-text-property (point) 'gnus-intangible)
3086             (forward-line 1))
3087           (gnus-data-set-pos (car data) (+ (point) 3))
3088           (setq data (cdr data))
3089           (forward-line 1))))))
3090
3091 (defun gnus-hidden-threads-configuration ()
3092   "Return the current hidden threads configuration."
3093   (save-excursion
3094     (let (config)
3095       (goto-char (point-min))
3096       (while (search-forward "\r" nil t)
3097         (push (1- (point)) config))
3098       config)))
3099
3100 (defun gnus-restore-hidden-threads-configuration (config)
3101   "Restore hidden threads configuration from CONFIG."
3102   (save-excursion
3103     (let (point buffer-read-only)
3104       (while (setq point (pop config))
3105         (when (and (< point (point-max))
3106                    (goto-char point)
3107                    (eq (char-after) ?\n))
3108           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3109
3110 ;; Various summary mode internalish functions.
3111
3112 (defun gnus-mouse-pick-article (e)
3113   (interactive "e")
3114   (mouse-set-point e)
3115   (gnus-summary-next-page nil t))
3116
3117 (defun gnus-summary-set-display-table ()
3118   "Change the display table.
3119 Odd characters have a tendency to mess
3120 up nicely formatted displays - we make all possible glyphs
3121 display only a single character."
3122
3123   ;; We start from the standard display table, if any.
3124   (let ((table (or (copy-sequence standard-display-table)
3125                    (make-display-table)))
3126         (i 32))
3127     ;; Nix out all the control chars...
3128     (while (>= (setq i (1- i)) 0)
3129       (aset table i [??]))
3130     ;; ... but not newline and cr, of course.  (cr is necessary for the
3131     ;; selective display).
3132     (aset table ?\n nil)
3133     (aset table ?\r nil)
3134     ;; We keep TAB as well.
3135     (aset table ?\t nil)
3136     ;; We nix out any glyphs over 126 that are not set already.
3137     (let ((i 256))
3138       (while (>= (setq i (1- i)) 127)
3139         ;; Only modify if the entry is nil.
3140         (unless (aref table i)
3141           (aset table i [??]))))
3142     (setq buffer-display-table table)))
3143
3144 (defun gnus-summary-set-article-display-arrow (pos)
3145   "Update the overlay arrow to point to line at position POS."
3146   (when (and gnus-summary-display-arrow
3147              (boundp 'overlay-arrow-position)
3148              (boundp 'overlay-arrow-string))
3149     (save-excursion
3150       (goto-char pos)
3151       (beginning-of-line)
3152       (unless overlay-arrow-position
3153         (setq overlay-arrow-position (make-marker)))
3154       (setq overlay-arrow-string "=>"
3155             overlay-arrow-position (set-marker overlay-arrow-position
3156                                                (point)
3157                                                (current-buffer))))))
3158
3159 (defun gnus-summary-setup-buffer (group)
3160   "Initialize summary buffer."
3161   (let ((buffer (gnus-summary-buffer-name group))
3162         (dead-name (concat "*Dead Summary "
3163                            (gnus-group-decoded-name group) "*")))
3164     ;; If a dead summary buffer exists, we kill it.
3165     (when (gnus-buffer-live-p dead-name)
3166       (gnus-kill-buffer dead-name))
3167     (if (get-buffer buffer)
3168         (progn
3169           (set-buffer buffer)
3170           (setq gnus-summary-buffer (current-buffer))
3171           (not gnus-newsgroup-prepared))
3172       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3173       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3174       (gnus-summary-mode group)
3175       (when gnus-carpal
3176         (gnus-carpal-setup-buffer 'summary))
3177       (unless gnus-single-article-buffer
3178         (make-local-variable 'gnus-article-buffer)
3179         (make-local-variable 'gnus-article-current)
3180         (make-local-variable 'gnus-original-article-buffer))
3181       (setq gnus-newsgroup-name group)
3182       ;; Set any local variables in the group parameters.
3183       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3184       t)))
3185
3186 (defun gnus-set-global-variables ()
3187   "Set the global equivalents of the buffer-local variables.
3188 They are set to the latest values they had.  These reflect the summary
3189 buffer that was in action when the last article was fetched."
3190   (when (eq major-mode 'gnus-summary-mode)
3191     (setq gnus-summary-buffer (current-buffer))
3192     (let ((name gnus-newsgroup-name)
3193           (marked gnus-newsgroup-marked)
3194           (spam gnus-newsgroup-spam-marked)
3195           (unread gnus-newsgroup-unreads)
3196           (headers gnus-current-headers)
3197           (data gnus-newsgroup-data)
3198           (summary gnus-summary-buffer)
3199           (article-buffer gnus-article-buffer)
3200           (original gnus-original-article-buffer)
3201           (gac gnus-article-current)
3202           (reffed gnus-reffed-article-number)
3203           (score-file gnus-current-score-file)
3204           (default-charset gnus-newsgroup-charset)
3205           vlist)
3206       (let ((locals gnus-newsgroup-variables))
3207         (while locals
3208           (if (consp (car locals))
3209               (push (eval (caar locals)) vlist)
3210             (push (eval (car locals)) vlist))
3211           (setq locals (cdr locals)))
3212         (setq vlist (nreverse vlist)))
3213       (with-current-buffer gnus-group-buffer
3214         (setq gnus-newsgroup-name name
3215               gnus-newsgroup-marked marked
3216               gnus-newsgroup-spam-marked spam
3217               gnus-newsgroup-unreads unread
3218               gnus-current-headers headers
3219               gnus-newsgroup-data data
3220               gnus-article-current gac
3221               gnus-summary-buffer summary
3222               gnus-article-buffer article-buffer
3223               gnus-original-article-buffer original
3224               gnus-reffed-article-number reffed
3225               gnus-current-score-file score-file
3226               gnus-newsgroup-charset default-charset)
3227         (let ((locals gnus-newsgroup-variables))
3228           (while locals
3229             (if (consp (car locals))
3230                 (set (caar locals) (pop vlist))
3231               (set (car locals) (pop vlist)))
3232             (setq locals (cdr locals))))
3233         ;; The article buffer also has local variables.
3234         (when (gnus-buffer-live-p gnus-article-buffer)
3235           (set-buffer gnus-article-buffer)
3236           (setq gnus-summary-buffer summary))))))
3237
3238 (defun gnus-summary-article-unread-p (article)
3239   "Say whether ARTICLE is unread or not."
3240   (memq article gnus-newsgroup-unreads))
3241
3242 (defun gnus-summary-first-article-p (&optional article)
3243   "Return whether ARTICLE is the first article in the buffer."
3244   (if (not (setq article (or article (gnus-summary-article-number))))
3245       nil
3246     (eq article (caar gnus-newsgroup-data))))
3247
3248 (defun gnus-summary-last-article-p (&optional article)
3249   "Return whether ARTICLE is the last article in the buffer."
3250   (if (not (setq article (or article (gnus-summary-article-number))))
3251       ;; All non-existent numbers are the last article.  :-)
3252       t
3253     (not (cdr (gnus-data-find-list article)))))
3254
3255 (defun gnus-make-thread-indent-array ()
3256   (let ((n 200))
3257     (unless (and gnus-thread-indent-array
3258                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3259       (setq gnus-thread-indent-array (make-vector 201 "")
3260             gnus-thread-indent-array-level gnus-thread-indent-level)
3261       (while (>= n 0)
3262         (aset gnus-thread-indent-array n
3263               (make-string (* n gnus-thread-indent-level) ? ))
3264         (setq n (1- n))))))
3265
3266 (defun gnus-update-summary-mark-positions ()
3267   "Compute where the summary marks are to go."
3268   (save-excursion
3269     (when (gnus-buffer-exists-p gnus-summary-buffer)
3270       (set-buffer gnus-summary-buffer))
3271     (let ((gnus-replied-mark 129)
3272           (gnus-score-below-mark 130)
3273           (gnus-score-over-mark 130)
3274           (gnus-undownloaded-mark 131)
3275           (spec gnus-summary-line-format-spec)
3276           gnus-visual pos)
3277       (save-excursion
3278         (gnus-set-work-buffer)
3279         (let ((gnus-summary-line-format-spec spec)
3280               (gnus-newsgroup-downloadable '(0))
3281               marks)
3282           (insert ?\200 "\200" ?\201 "\201" ?\202 "\202" ?\203 "\203")
3283           (while (not (bobp))
3284             (push (buffer-substring (1- (point)) (point)) marks)
3285             (backward-char))
3286           (erase-buffer)
3287           (gnus-summary-insert-line
3288            (make-full-mail-header 0 "" "nobody"
3289                                   "05 Apr 2001 23:33:09 +0400"
3290                                   "" "" 0 0 "" nil)
3291            0 nil t 128 t nil "" nil 1)
3292           (goto-char (point-min))
3293           (setq pos (list (cons 'unread
3294                                 (and (or (search-forward (nth 0 marks) nil t)
3295                                          (search-forward (nth 1 marks) nil t))
3296                                      (- (point) (point-min) 1)))))
3297           (goto-char (point-min))
3298           (push (cons 'replied (and (or (search-forward (nth 2 marks) nil t)
3299                                         (search-forward (nth 3 marks) nil t))
3300                                     (- (point) (point-min) 1)))
3301                 pos)
3302           (goto-char (point-min))
3303           (push (cons 'score (and (or (search-forward (nth 4 marks) nil t)
3304                                       (search-forward (nth 5 marks) nil t))
3305                                   (- (point) (point-min) 1)))
3306                 pos)
3307           (goto-char (point-min))
3308           (push (cons 'download (and (or (search-forward (nth 6 marks) nil t)
3309                                          (search-forward (nth 7 marks) nil t))
3310                                      (- (point) (point-min) 1)))
3311                 pos)))
3312       (setq gnus-summary-mark-positions pos))))
3313
3314 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3315   "Insert a dummy root in the summary buffer."
3316   (beginning-of-line)
3317   (gnus-add-text-properties
3318    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3319    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3320
3321 (defun gnus-summary-extract-address-component (from)
3322   (or (car (funcall gnus-extract-address-components from))
3323       from))
3324
3325 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3326   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3327                                 default-mime-charset)))
3328     ;; Is it really necessary to do this next part for each summary line?
3329     ;; Luckily, doesn't seem to slow things down much.
3330     (or
3331      (and gnus-ignored-from-addresses
3332           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3333           (let ((extra-headers (mail-header-extra header))
3334                 to
3335                 newsgroups)
3336             (cond
3337              ((setq to (cdr (assq 'To extra-headers)))
3338               (concat "-> "
3339                       (inline
3340                         (gnus-summary-extract-address-component
3341                          (funcall gnus-decode-encoded-word-function to)))))
3342              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3343               (concat "=> " newsgroups)))))
3344      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3345
3346 (defun gnus-summary-insert-line (gnus-tmp-header
3347                                  gnus-tmp-level gnus-tmp-current
3348                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3349                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3350                                  &optional gnus-tmp-dummy gnus-tmp-score
3351                                  gnus-tmp-process)
3352   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3353          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3354          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3355          (gnus-tmp-score-char
3356           (if (or (null gnus-summary-default-score)
3357                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3358                       gnus-summary-zcore-fuzz))
3359               ?\ ;;;Whitespace
3360             (if (< gnus-tmp-score gnus-summary-default-score)
3361                 gnus-score-below-mark gnus-score-over-mark)))
3362          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3363          (gnus-tmp-replied
3364           (cond (gnus-tmp-process gnus-process-mark)
3365                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3366                  gnus-cached-mark)
3367                 (gnus-tmp-replied gnus-replied-mark)
3368                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3369                  gnus-forwarded-mark)
3370                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3371                  gnus-saved-mark)
3372                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3373                  gnus-recent-mark)
3374                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3375                  gnus-unseen-mark)
3376                 (t gnus-no-mark)))
3377          (gnus-tmp-downloaded
3378           (cond (undownloaded
3379                  gnus-undownloaded-mark)
3380                 (gnus-newsgroup-agentized
3381                  gnus-downloaded-mark)
3382                 (t
3383                  gnus-no-mark)))
3384          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3385          (gnus-tmp-name
3386           (cond
3387            ((string-match "<[^>]+> *$" gnus-tmp-from)
3388             (let ((beg (match-beginning 0)))
3389               (or (and (string-match "^\".+\"" gnus-tmp-from)
3390                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3391                   (substring gnus-tmp-from 0 beg))))
3392            ((string-match "(.+)" gnus-tmp-from)
3393             (substring gnus-tmp-from
3394                        (1+ (match-beginning 0)) (1- (match-end 0))))
3395            (t gnus-tmp-from)))
3396          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3397          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3398          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3399          (buffer-read-only nil))
3400     (when (string= gnus-tmp-name "")
3401       (setq gnus-tmp-name gnus-tmp-from))
3402     (unless (numberp gnus-tmp-lines)
3403       (setq gnus-tmp-lines -1))
3404     (if (= gnus-tmp-lines -1)
3405         (setq gnus-tmp-lines "?")
3406       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3407     (gnus-put-text-property-excluding-characters-with-faces
3408      (point)
3409      (progn (eval gnus-summary-line-format-spec) (point))
3410      'gnus-number gnus-tmp-number)
3411     (when (gnus-visual-p 'summary-highlight 'highlight)
3412       (forward-line -1)
3413       (gnus-run-hooks 'gnus-summary-update-hook)
3414       (forward-line 1))))
3415
3416 (defun gnus-summary-update-line (&optional dont-update)
3417   "Update summary line after change."
3418   (when (and gnus-summary-default-score
3419              (not gnus-summary-inhibit-highlight))
3420     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3421            (article (gnus-summary-article-number))
3422            (score (gnus-summary-article-score article)))
3423       (unless dont-update
3424         (if (and gnus-summary-mark-below
3425                  (< (gnus-summary-article-score)
3426                     gnus-summary-mark-below))
3427             ;; This article has a low score, so we mark it as read.
3428             (when (memq article gnus-newsgroup-unreads)
3429               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3430           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3431             ;; This article was previously marked as read on account
3432             ;; of a low score, but now it has risen, so we mark it as
3433             ;; unread.
3434             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3435         (gnus-summary-update-mark
3436          (if (or (null gnus-summary-default-score)
3437                  (<= (abs (- score gnus-summary-default-score))
3438                      gnus-summary-zcore-fuzz))
3439              ?\ ;;;Whitespace
3440            (if (< score gnus-summary-default-score)
3441                gnus-score-below-mark gnus-score-over-mark))
3442          'score))
3443       ;; Do visual highlighting.
3444       (when (gnus-visual-p 'summary-highlight 'highlight)
3445         (gnus-run-hooks 'gnus-summary-update-hook)))))
3446
3447 (defvar gnus-tmp-new-adopts nil)
3448
3449 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3450   "Return the number of articles in THREAD.
3451 This may be 0 in some cases -- if none of the articles in
3452 the thread are to be displayed."
3453   (let* ((number
3454           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3455           (cond
3456            ((not (listp thread))
3457             1)
3458            ((and (consp thread) (cdr thread))
3459             (apply
3460              '+ 1 (mapcar
3461                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3462            ((null thread)
3463             1)
3464            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3465             1)
3466            (t 0))))
3467     (when (and level (zerop level) gnus-tmp-new-adopts)
3468       (incf number
3469             (apply '+ (mapcar
3470                        'gnus-summary-number-of-articles-in-thread
3471                        gnus-tmp-new-adopts))))
3472     (if char
3473         (if (> number 1) gnus-not-empty-thread-mark
3474           gnus-empty-thread-mark)
3475       number)))
3476
3477 (defsubst gnus-summary-line-message-size (head)
3478   "Return pretty-printed version of message size.
3479 This function is intended to be used in
3480 `gnus-summary-line-format-alist'."
3481   (let ((c (or (mail-header-chars head) -1)))
3482     (cond ((< c 0) "n/a")               ; chars not available
3483           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3484           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3485           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3486           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3487
3488
3489 (defun gnus-summary-set-local-parameters (group)
3490   "Go through the local params of GROUP and set all variable specs in that list."
3491   (let ((vars '(quit-config)))          ; Ignore quit-config.
3492     (dolist (elem (gnus-group-find-parameter group))
3493       (and (consp elem)                 ; Has to be a cons.
3494            (consp (cdr elem))           ; The cdr has to be a list.
3495            (symbolp (car elem))         ; Has to be a symbol in there.
3496            (not (memq (car elem) vars))
3497            (ignore-errors               ; So we set it.
3498              (push (car elem) vars)
3499              (make-local-variable (car elem))
3500              (set (car elem) (eval (nth 1 elem))))))))
3501
3502 (defun gnus-summary-read-group (group &optional show-all no-article
3503                                       kill-buffer no-display backward
3504                                       select-articles)
3505   "Start reading news in newsgroup GROUP.
3506 If SHOW-ALL is non-nil, already read articles are also listed.
3507 If NO-ARTICLE is non-nil, no article is selected initially.
3508 If NO-DISPLAY, don't generate a summary buffer."
3509   (let (result)
3510     (while (and group
3511                 (null (setq result
3512                             (let ((gnus-auto-select-next nil))
3513                               (or (gnus-summary-read-group-1
3514                                    group show-all no-article
3515                                    kill-buffer no-display
3516                                    select-articles)
3517                                   (setq show-all nil
3518                                         select-articles nil)))))
3519                 (eq gnus-auto-select-next 'quietly))
3520       (set-buffer gnus-group-buffer)
3521       ;; The entry function called above goes to the next
3522       ;; group automatically, so we go two groups back
3523       ;; if we are searching for the previous group.
3524       (when backward
3525         (gnus-group-prev-unread-group 2))
3526       (if (not (equal group (gnus-group-group-name)))
3527           (setq group (gnus-group-group-name))
3528         (setq group nil)))
3529     result))
3530
3531 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3532   "Directly jump to the other GROUP from summary buffer.
3533 If SHOW-ALL is non-nil, already read articles are also listed."
3534   (interactive
3535    (if (eq gnus-summary-buffer (current-buffer))
3536        (list (completing-read
3537               "Group: " gnus-active-hashtb nil t
3538               (when (and gnus-newsgroup-name
3539                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3540                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3541               'gnus-group-history)
3542              current-prefix-arg)
3543      (error "%s must be invoked from a gnus summary buffer." this-command)))
3544   (unless (or (zerop (length group))
3545               (and gnus-newsgroup-name
3546                    (string-equal gnus-newsgroup-name group)))
3547     (gnus-summary-exit)
3548     (gnus-summary-read-group group show-all
3549                              gnus-dont-select-after-jump-to-other-group)))
3550
3551 (defun gnus-summary-read-group-1 (group show-all no-article
3552                                         kill-buffer no-display
3553                                         &optional select-articles)
3554   ;; Killed foreign groups can't be entered.
3555   ;;  (when (and (not (gnus-group-native-p group))
3556   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3557   ;;    (error "Dead non-native groups can't be entered"))
3558   (gnus-message 5 "Retrieving newsgroup: %s..."
3559                 (gnus-group-decoded-name group))
3560   (let* ((new-group (gnus-summary-setup-buffer group))
3561          (quit-config (gnus-group-quit-config group))
3562          (did-select (and new-group (gnus-select-newsgroup
3563                                      group show-all select-articles))))
3564     (cond
3565      ;; This summary buffer exists already, so we just select it.
3566      ((not new-group)
3567       (gnus-set-global-variables)
3568       (when kill-buffer
3569         (gnus-kill-or-deaden-summary kill-buffer))
3570       (gnus-configure-windows 'summary 'force)
3571       (gnus-set-mode-line 'summary)
3572       (gnus-summary-position-point)
3573       (message "")
3574       t)
3575      ;; We couldn't select this group.
3576      ((null did-select)
3577       (when (and (eq major-mode 'gnus-summary-mode)
3578                  (not (equal (current-buffer) kill-buffer)))
3579         (kill-buffer (current-buffer))
3580         (if (not quit-config)
3581             (progn
3582               ;; Update the info -- marks might need to be removed,
3583               ;; for instance.
3584               (gnus-summary-update-info)
3585               (set-buffer gnus-group-buffer)
3586               (gnus-group-jump-to-group group)
3587               (gnus-group-next-unread-group 1))
3588           (gnus-handle-ephemeral-exit quit-config)))
3589       (let ((grpinfo (gnus-get-info group)))
3590         (if (null (gnus-info-read grpinfo))
3591             (gnus-message 3 "Group %s contains no messages"
3592                           (gnus-group-decoded-name group))
3593           (gnus-message 3 "Can't select group")))
3594       nil)
3595      ;; The user did a `C-g' while prompting for number of articles,
3596      ;; so we exit this group.
3597      ((eq did-select 'quit)
3598       (and (eq major-mode 'gnus-summary-mode)
3599            (not (equal (current-buffer) kill-buffer))
3600            (kill-buffer (current-buffer)))
3601       (when kill-buffer
3602         (gnus-kill-or-deaden-summary kill-buffer))
3603       (if (not quit-config)
3604           (progn
3605             (set-buffer gnus-group-buffer)
3606             (gnus-group-jump-to-group group)
3607             (gnus-group-next-unread-group 1)
3608             (gnus-configure-windows 'group 'force))
3609         (gnus-handle-ephemeral-exit quit-config))
3610       ;; Finally signal the quit.
3611       (signal 'quit nil))
3612      ;; The group was successfully selected.
3613      (t
3614       (gnus-set-global-variables)
3615       ;; Save the active value in effect when the group was entered.
3616       (setq gnus-newsgroup-active
3617             (gnus-copy-sequence
3618              (gnus-active gnus-newsgroup-name)))
3619       ;; You can change the summary buffer in some way with this hook.
3620       (gnus-run-hooks 'gnus-select-group-hook)
3621       (gnus-update-format-specifications
3622        nil 'summary 'summary-mode 'summary-dummy)
3623       (gnus-update-summary-mark-positions)
3624       ;; Do score processing.
3625       (when gnus-use-scoring
3626         (gnus-possibly-score-headers))
3627       ;; Check whether to fill in the gaps in the threads.
3628       (when gnus-build-sparse-threads
3629         (gnus-build-sparse-threads))
3630       ;; Find the initial limit.
3631       (if show-all
3632           (let ((gnus-newsgroup-dormant nil))
3633             (gnus-summary-initial-limit show-all))
3634         (gnus-summary-initial-limit show-all))
3635       ;; Generate the summary buffer.
3636       (unless no-display
3637         (gnus-summary-prepare))
3638       (when gnus-use-trees
3639         (gnus-tree-open group)
3640         (setq gnus-summary-highlight-line-function
3641               'gnus-tree-highlight-article))
3642       ;; If the summary buffer is empty, but there are some low-scored
3643       ;; articles or some excluded dormants, we include these in the
3644       ;; buffer.
3645       (when (and (zerop (buffer-size))
3646                  (not no-display))
3647         (cond (gnus-newsgroup-dormant
3648                (gnus-summary-limit-include-dormant))
3649               ((and gnus-newsgroup-scored show-all)
3650                (gnus-summary-limit-include-expunged t))))
3651       ;; Function `gnus-apply-kill-file' must be called in this hook.
3652       (gnus-run-hooks 'gnus-apply-kill-hook)
3653       (if (and (zerop (buffer-size))
3654                (not no-display))
3655           (progn
3656             ;; This newsgroup is empty.
3657             (gnus-summary-catchup-and-exit nil t)
3658             (gnus-message 6 "No unread news")
3659             (when kill-buffer
3660               (gnus-kill-or-deaden-summary kill-buffer))
3661             ;; Return nil from this function.
3662             nil)
3663         ;; Hide conversation thread subtrees.  We cannot do this in
3664         ;; gnus-summary-prepare-hook since kill processing may not
3665         ;; work with hidden articles.
3666         (gnus-summary-maybe-hide-threads)
3667         (when kill-buffer
3668           (gnus-kill-or-deaden-summary kill-buffer))
3669         (gnus-summary-auto-select-subject)
3670         ;; Show first unread article if requested.
3671         (if (and (not no-article)
3672                  (not no-display)
3673                  gnus-newsgroup-unreads
3674                  gnus-auto-select-first)
3675             (progn
3676               (gnus-configure-windows 'summary)
3677               (let ((art (gnus-summary-article-number)))
3678                 (unless (and (not gnus-plugged)
3679                              (or (memq art gnus-newsgroup-undownloaded)
3680                                  (memq art gnus-newsgroup-downloadable)))
3681                   (gnus-summary-goto-article art))))
3682           ;; Don't select any articles.
3683           (gnus-summary-position-point)
3684           (gnus-configure-windows 'summary 'force)
3685           (gnus-set-mode-line 'summary))
3686         (when (and gnus-auto-center-group
3687                    (get-buffer-window gnus-group-buffer t))
3688           ;; Gotta use windows, because recenter does weird stuff if
3689           ;; the current buffer ain't the displayed window.
3690           (let ((owin (selected-window)))
3691             (select-window (get-buffer-window gnus-group-buffer t))
3692             (when (gnus-group-goto-group group)
3693               (recenter))
3694             (select-window owin)))
3695         ;; Mark this buffer as "prepared".
3696         (setq gnus-newsgroup-prepared t)
3697         (gnus-run-hooks 'gnus-summary-prepared-hook)
3698         (unless (gnus-ephemeral-group-p group)
3699           (gnus-group-update-group group))
3700         t)))))
3701
3702 (defun gnus-summary-auto-select-subject ()
3703   "Select the subject line on initial group entry."
3704   (goto-char (point-min))
3705   (cond
3706    ((eq gnus-auto-select-subject 'best)
3707     (gnus-summary-best-unread-subject))
3708    ((eq gnus-auto-select-subject 'unread)
3709     (gnus-summary-first-unread-subject))
3710    ((eq gnus-auto-select-subject 'unseen)
3711     (gnus-summary-first-unseen-subject))
3712    ((eq gnus-auto-select-subject 'unseen-or-unread)
3713     (gnus-summary-first-unseen-or-unread-subject))
3714    ((eq gnus-auto-select-subject 'first)
3715     ;; Do nothing.
3716     )
3717    ((functionp gnus-auto-select-subject)
3718     (funcall gnus-auto-select-subject))))
3719
3720 (defun gnus-summary-prepare ()
3721   "Generate the summary buffer."
3722   (interactive)
3723   (let ((buffer-read-only nil))
3724     (erase-buffer)
3725     (setq gnus-newsgroup-data nil
3726           gnus-newsgroup-data-reverse nil)
3727     (gnus-run-hooks 'gnus-summary-generate-hook)
3728     ;; Generate the buffer, either with threads or without.
3729     (when gnus-newsgroup-headers
3730       (gnus-summary-prepare-threads
3731        (if gnus-show-threads
3732            (gnus-sort-gathered-threads
3733             (funcall gnus-summary-thread-gathering-function
3734                      (gnus-sort-threads
3735                       (gnus-cut-threads (gnus-make-threads)))))
3736          ;; Unthreaded display.
3737          (gnus-sort-articles gnus-newsgroup-headers))))
3738     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3739     ;; Call hooks for modifying summary buffer.
3740     (goto-char (point-min))
3741     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3742
3743 (defsubst gnus-general-simplify-subject (subject)
3744   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3745   (setq subject
3746         (cond
3747          ;; Truncate the subject.
3748          (gnus-simplify-subject-functions
3749           (gnus-map-function gnus-simplify-subject-functions subject))
3750          ((numberp gnus-summary-gather-subject-limit)
3751           (setq subject (gnus-simplify-subject-re subject))
3752           (if (> (length subject) gnus-summary-gather-subject-limit)
3753               (substring subject 0 gnus-summary-gather-subject-limit)
3754             subject))
3755          ;; Fuzzily simplify it.
3756          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3757           (gnus-simplify-subject-fuzzy subject))
3758          ;; Just remove the leading "Re:".
3759          (t
3760           (gnus-simplify-subject-re subject))))
3761
3762   (if (and gnus-summary-gather-exclude-subject
3763            (string-match gnus-summary-gather-exclude-subject subject))
3764       nil                               ; This article shouldn't be gathered
3765     subject))
3766
3767 (defun gnus-summary-simplify-subject-query ()
3768   "Query where the respool algorithm would put this article."
3769   (interactive)
3770   (gnus-summary-select-article)
3771   (message "%s"
3772            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3773
3774 (defun gnus-gather-threads-by-subject (threads)
3775   "Gather threads by looking at Subject headers."
3776   (if (not gnus-summary-make-false-root)
3777       threads
3778     (let ((hashtb (gnus-make-hashtable 1024))
3779           (prev threads)
3780           (result threads)
3781           subject hthread whole-subject)
3782       (while threads
3783         (setq subject (gnus-general-simplify-subject
3784                        (setq whole-subject (mail-header-subject
3785                                             (caar threads)))))
3786         (when subject
3787           (if (setq hthread (gnus-gethash subject hashtb))
3788               (progn
3789                 ;; We enter a dummy root into the thread, if we
3790                 ;; haven't done that already.
3791                 (unless (stringp (caar hthread))
3792                   (setcar hthread (list whole-subject (car hthread))))
3793                 ;; We add this new gathered thread to this gathered
3794                 ;; thread.
3795                 (setcdr (car hthread)
3796                         (nconc (cdar hthread) (list (car threads))))
3797                 ;; Remove it from the list of threads.
3798                 (setcdr prev (cdr threads))
3799                 (setq threads prev))
3800             ;; Enter this thread into the hash table.
3801             (gnus-sethash subject
3802                           (if gnus-summary-make-false-root-always
3803                               (progn
3804                                 ;; If you want a dummy root above all
3805                                 ;; threads...
3806                                 (setcar threads (list whole-subject
3807                                                       (car threads)))
3808                                 threads)
3809                             threads)
3810                           hashtb)))
3811         (setq prev threads)
3812         (setq threads (cdr threads)))
3813       result)))
3814
3815 (defun gnus-gather-threads-by-references (threads)
3816   "Gather threads by looking at References headers."
3817   (let ((idhashtb (gnus-make-hashtable 1024))
3818         (thhashtb (gnus-make-hashtable 1024))
3819         (prev threads)
3820         (result threads)
3821         ids references id gthread gid entered ref)
3822     (while threads
3823       (when (setq references (mail-header-references (caar threads)))
3824         (setq id (mail-header-id (caar threads))
3825               ids (inline (gnus-split-references references))
3826               entered nil)
3827         (while (setq ref (pop ids))
3828           (setq ids (delete ref ids))
3829           (if (not (setq gid (gnus-gethash ref idhashtb)))
3830               (progn
3831                 (gnus-sethash ref id idhashtb)
3832                 (gnus-sethash id threads thhashtb))
3833             (setq gthread (gnus-gethash gid thhashtb))
3834             (unless entered
3835               ;; We enter a dummy root into the thread, if we
3836               ;; haven't done that already.
3837               (unless (stringp (caar gthread))
3838                 (setcar gthread (list (mail-header-subject (caar gthread))
3839                                       (car gthread))))
3840               ;; We add this new gathered thread to this gathered
3841               ;; thread.
3842               (setcdr (car gthread)
3843                       (nconc (cdar gthread) (list (car threads)))))
3844             ;; Add it into the thread hash table.
3845             (gnus-sethash id gthread thhashtb)
3846             (setq entered t)
3847             ;; Remove it from the list of threads.
3848             (setcdr prev (cdr threads))
3849             (setq threads prev))))
3850       (setq prev threads)
3851       (setq threads (cdr threads)))
3852     result))
3853
3854 (defun gnus-sort-gathered-threads (threads)
3855   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3856   (let ((result threads))
3857     (while threads
3858       (when (stringp (caar threads))
3859         (setcdr (car threads)
3860                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3861       (setq threads (cdr threads)))
3862     result))
3863
3864 (defun gnus-thread-loop-p (root thread)
3865   "Say whether ROOT is in THREAD."
3866   (let ((stack (list thread))
3867         (infloop 0)
3868         th)
3869     (while (setq thread (pop stack))
3870       (setq th (cdr thread))
3871       (while (and th
3872                   (not (eq (caar th) root)))
3873         (pop th))
3874       (if th
3875           ;; We have found a loop.
3876           (let (ref-dep)
3877             (setcdr thread (delq (car th) (cdr thread)))
3878             (if (boundp (setq ref-dep (intern "none"
3879                                               gnus-newsgroup-dependencies)))
3880                 (setcdr (symbol-value ref-dep)
3881                         (nconc (cdr (symbol-value ref-dep))
3882                                (list (car th))))
3883               (set ref-dep (list nil (car th))))
3884             (setq infloop 1
3885                   stack nil))
3886         ;; Push all the subthreads onto the stack.
3887         (push (cdr thread) stack)))
3888     infloop))
3889
3890 (defun gnus-make-threads ()
3891   "Go through the dependency hashtb and find the roots.  Return all threads."
3892   (let (threads)
3893     (while (catch 'infloop
3894              (mapatoms
3895               (lambda (refs)
3896                 ;; Deal with self-referencing References loops.
3897                 (when (and (car (symbol-value refs))
3898                            (not (zerop
3899                                  (apply
3900                                   '+
3901                                   (mapcar
3902                                    (lambda (thread)
3903                                      (gnus-thread-loop-p
3904                                       (car (symbol-value refs)) thread))
3905                                    (cdr (symbol-value refs)))))))
3906                   (setq threads nil)
3907                   (throw 'infloop t))
3908                 (unless (car (symbol-value refs))
3909                   ;; These threads do not refer back to any other articles,
3910                   ;; so they're roots.
3911                   (setq threads (append (cdr (symbol-value refs)) threads))))
3912               gnus-newsgroup-dependencies)))
3913     threads))
3914
3915 ;; Build the thread tree.
3916 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3917   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3918
3919 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3920 if it was already present.
3921
3922 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3923 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3924 Message-IDs will be renamed to a unique Message-ID before being
3925 entered.
3926
3927 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3928   (let* ((id (mail-header-id header))
3929          (id-dep (and id (intern id dependencies)))
3930          parent-id ref ref-dep ref-header replaced)
3931     ;; Enter this `header' in the `dependencies' table.
3932     (cond
3933      ((not id-dep)
3934       (setq header nil))
3935      ;; The first two cases do the normal part: enter a new `header'
3936      ;; in the `dependencies' table.
3937      ((not (boundp id-dep))
3938       (set id-dep (list header)))
3939      ((null (car (symbol-value id-dep)))
3940       (setcar (symbol-value id-dep) header))
3941
3942      ;; From here the `header' was already present in the
3943      ;; `dependencies' table.
3944      (force-new
3945       ;; Overrides an existing entry;
3946       ;; just set the header part of the entry.
3947       (setcar (symbol-value id-dep) header)
3948       (setq replaced t))
3949
3950      ;; Renames the existing `header' to a unique Message-ID.
3951      ((not gnus-summary-ignore-duplicates)
3952       ;; An article with this Message-ID has already been seen.
3953       ;; We rename the Message-ID.
3954       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3955            (list header))
3956       (mail-header-set-id header id))
3957
3958      ;; The last case ignores an existing entry, except it adds any
3959      ;; additional Xrefs (in case the two articles came from different
3960      ;; servers.
3961      ;; Also sets `header' to `nil' meaning that the `dependencies'
3962      ;; table was *not* modified.
3963      (t
3964       (mail-header-set-xref
3965        (car (symbol-value id-dep))
3966        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3967                    "")
3968                (or (mail-header-xref header) "")))
3969       (setq header nil)))
3970
3971     (when (and header (not replaced))
3972       ;; First check that we are not creating a References loop.
3973       (setq parent-id (gnus-parent-id (mail-header-references header)))
3974       (setq ref parent-id)
3975       (while (and ref
3976                   (setq ref-dep (intern-soft ref dependencies))
3977                   (boundp ref-dep)
3978                   (setq ref-header (car (symbol-value ref-dep))))
3979         (if (string= id ref)
3980             ;; Yuk!  This is a reference loop.  Make the article be a
3981             ;; root article.
3982             (progn
3983               (mail-header-set-references (car (symbol-value id-dep)) "none")
3984               (setq ref nil)
3985               (setq parent-id nil))
3986           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3987       (setq ref-dep (intern (or parent-id "none") dependencies))
3988       (if (boundp ref-dep)
3989           (setcdr (symbol-value ref-dep)
3990                   (nconc (cdr (symbol-value ref-dep))
3991                          (list (symbol-value id-dep))))
3992         (set ref-dep (list nil (symbol-value id-dep)))))
3993     header))
3994
3995 (defun gnus-extract-message-id-from-in-reply-to (string)
3996   (if (string-match "<[^>]+>" string)
3997       (substring string (match-beginning 0) (match-end 0))
3998     nil))
3999
4000 (defun gnus-build-sparse-threads ()
4001   (let ((headers gnus-newsgroup-headers)
4002         (mail-parse-charset gnus-newsgroup-charset)
4003         (gnus-summary-ignore-duplicates t)
4004         header references generation relations
4005         subject child end new-child date)
4006     ;; First we create an alist of generations/relations, where
4007     ;; generations is how much we trust the relation, and the relation
4008     ;; is parent/child.
4009     (gnus-message 7 "Making sparse threads...")
4010     (save-excursion
4011       (nnheader-set-temp-buffer " *gnus sparse threads*")
4012       (while (setq header (pop headers))
4013         (when (and (setq references (mail-header-references header))
4014                    (not (string= references "")))
4015           (insert references)
4016           (setq child (mail-header-id header)
4017                 subject (mail-header-subject header)
4018                 date (mail-header-date header)
4019                 generation 0)
4020           (while (search-backward ">" nil t)
4021             (setq end (1+ (point)))
4022             (when (search-backward "<" nil t)
4023               (setq new-child (buffer-substring (point) end))
4024               (push (list (incf generation)
4025                           child (setq child new-child)
4026                           subject date)
4027                     relations)))
4028           (when child
4029             (push (list (1+ generation) child nil subject) relations))
4030           (erase-buffer)))
4031       (kill-buffer (current-buffer)))
4032     ;; Sort over trustworthiness.
4033     (mapcar
4034      (lambda (relation)
4035        (when (gnus-dependencies-add-header
4036               (make-full-mail-header-from-decoded-header
4037                gnus-reffed-article-number
4038                (nth 3 relation) "" (or (nth 4 relation) "")
4039                (nth 1 relation)
4040                (or (nth 2 relation) "") 0 0 "")
4041               gnus-newsgroup-dependencies nil)
4042          (push gnus-reffed-article-number gnus-newsgroup-limit)
4043          (push gnus-reffed-article-number gnus-newsgroup-sparse)
4044          (push (cons gnus-reffed-article-number gnus-sparse-mark)
4045                gnus-newsgroup-reads)
4046          (decf gnus-reffed-article-number)))
4047      (sort relations 'car-less-than-car))
4048     (gnus-message 7 "Making sparse threads...done")))
4049
4050 (defun gnus-build-old-threads ()
4051   ;; Look at all the articles that refer back to old articles, and
4052   ;; fetch the headers for the articles that aren't there.  This will
4053   ;; build complete threads - if the roots haven't been expired by the
4054   ;; server, that is.
4055   (let ((mail-parse-charset gnus-newsgroup-charset)
4056         id heads)
4057     (mapatoms
4058      (lambda (refs)
4059        (when (not (car (symbol-value refs)))
4060          (setq heads (cdr (symbol-value refs)))
4061          (while heads
4062            (if (memq (mail-header-number (caar heads))
4063                      gnus-newsgroup-dormant)
4064                (setq heads (cdr heads))
4065              (setq id (symbol-name refs))
4066              (while (and (setq id (gnus-build-get-header id))
4067                          (not (car (gnus-id-to-thread id)))))
4068              (setq heads nil)))))
4069      gnus-newsgroup-dependencies)))
4070
4071 (defsubst gnus-remove-odd-characters (string)
4072   "Translate STRING into something that doesn't contain weird characters."
4073   (mm-subst-char-in-string
4074    ?\r ?\-
4075    (mm-subst-char-in-string ?\n ?\- string t) t))
4076
4077 ;; This function has to be called with point after the article number
4078 ;; on the beginning of the line.
4079 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4080   (let ((eol (point-at-eol))
4081         (buffer (current-buffer))
4082         header references in-reply-to)
4083
4084     ;; overview: [num subject from date id refs chars lines misc]
4085     (unwind-protect
4086         (progn
4087           (narrow-to-region (point) eol)
4088           (unless (eobp)
4089             (forward-char))
4090
4091           (setq header
4092                 (make-full-mail-header
4093                  number                         ; number
4094                  (nnheader-nov-field)           ; subject
4095                  (nnheader-nov-field)           ; from
4096                  (nnheader-nov-field)           ; date
4097                  (nnheader-nov-read-message-id number)  ; id
4098                  (setq references (nnheader-nov-field)) ; refs
4099                  (nnheader-nov-read-integer)    ; chars
4100                  (nnheader-nov-read-integer)    ; lines
4101                  (unless (eobp)
4102                    (if (looking-at "Xref: ")
4103                        (goto-char (match-end 0)))
4104                    (nnheader-nov-field))        ; Xref
4105                  (nnheader-nov-parse-extra))))  ; extra
4106
4107       (widen))
4108
4109     (when (and (string= references "")
4110                (setq in-reply-to (mail-header-extra header))
4111                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4112       (mail-header-set-references
4113        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4114
4115     (when gnus-alter-header-function
4116       (funcall gnus-alter-header-function header))
4117     (gnus-dependencies-add-header header dependencies force-new)))
4118
4119 (defun gnus-build-get-header (id)
4120   "Look through the buffer of NOV lines and find the header to ID.
4121 Enter this line into the dependencies hash table, and return
4122 the id of the parent article (if any)."
4123   (let ((deps gnus-newsgroup-dependencies)
4124         found header)
4125     (prog1
4126         (save-excursion
4127           (set-buffer nntp-server-buffer)
4128           (let ((case-fold-search nil))
4129             (goto-char (point-min))
4130             (while (and (not found)
4131                         (search-forward id nil t))
4132               (beginning-of-line)
4133               (setq found (looking-at
4134                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4135                                    (regexp-quote id))))
4136               (or found (beginning-of-line 2)))
4137             (when found
4138               (beginning-of-line)
4139               (and
4140                (setq header (gnus-nov-parse-line
4141                              (read (current-buffer)) deps))
4142                (gnus-parent-id (mail-header-references header))))))
4143       (when header
4144         (let ((number (mail-header-number header)))
4145           (push number gnus-newsgroup-limit)
4146           (push header gnus-newsgroup-headers)
4147           (if (memq number gnus-newsgroup-unselected)
4148               (progn
4149                 (setq gnus-newsgroup-unreads
4150                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4151                                                number))
4152                 (setq gnus-newsgroup-unselected
4153                       (delq number gnus-newsgroup-unselected)))
4154             (push number gnus-newsgroup-ancient)))))))
4155
4156 (defun gnus-build-all-threads ()
4157   "Read all the headers."
4158   (let ((gnus-summary-ignore-duplicates t)
4159         (mail-parse-charset gnus-newsgroup-charset)
4160         (dependencies gnus-newsgroup-dependencies)
4161         header article)
4162     (save-excursion
4163       (set-buffer nntp-server-buffer)
4164       (let ((case-fold-search nil))
4165         (goto-char (point-min))
4166         (while (not (eobp))
4167           (ignore-errors
4168             (setq article (read (current-buffer))
4169                   header (gnus-nov-parse-line article dependencies)))
4170           (when header
4171             (with-current-buffer gnus-summary-buffer
4172               (push header gnus-newsgroup-headers)
4173               (if (memq (setq article (mail-header-number header))
4174                         gnus-newsgroup-unselected)
4175                   (progn
4176                     (setq gnus-newsgroup-unreads
4177                           (gnus-add-to-sorted-list
4178                            gnus-newsgroup-unreads article))
4179                     (setq gnus-newsgroup-unselected
4180                           (delq article gnus-newsgroup-unselected)))
4181                 (push article gnus-newsgroup-ancient)))
4182             (forward-line 1)))))))
4183
4184 (defun gnus-summary-update-article-line (article header)
4185   "Update the line for ARTICLE using HEADER."
4186   (let* ((id (mail-header-id header))
4187          (thread (gnus-id-to-thread id)))
4188     (unless thread
4189       (error "Article in no thread"))
4190     ;; Update the thread.
4191     (setcar thread header)
4192     (gnus-summary-goto-subject article)
4193     (let* ((datal (gnus-data-find-list article))
4194            (data (car datal))
4195            (buffer-read-only nil)
4196            (level (gnus-summary-thread-level)))
4197       (gnus-delete-line)
4198       (let ((inserted (- (point)
4199                          (progn
4200                            (gnus-summary-insert-line
4201                             header level nil
4202                             (memq article gnus-newsgroup-undownloaded)
4203                             (gnus-article-mark article)
4204                             (memq article gnus-newsgroup-replied)
4205                             (memq article gnus-newsgroup-expirable)
4206                             ;; Only insert the Subject string when it's different
4207                             ;; from the previous Subject string.
4208                             (if (and
4209                                  gnus-show-threads
4210                                  (gnus-subject-equal
4211                                   (condition-case ()
4212                                       (mail-header-subject
4213                                        (gnus-data-header
4214                                         (cadr
4215                                          (gnus-data-find-list
4216                                           article
4217                                           (gnus-data-list t)))))
4218                                     ;; Error on the side of excessive subjects.
4219                                     (error ""))
4220                                   (mail-header-subject header)))
4221                                 ""
4222                               (mail-header-subject header))
4223                             nil (cdr (assq article gnus-newsgroup-scored))
4224                             (memq article gnus-newsgroup-processable))
4225                            (point)))))
4226         (when (cdr datal)
4227           (gnus-data-update-list
4228            (cdr datal)
4229            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4230
4231 (defun gnus-summary-update-article (article &optional iheader)
4232   "Update ARTICLE in the summary buffer."
4233   (set-buffer gnus-summary-buffer)
4234   (let* ((header (gnus-summary-article-header article))
4235          (id (mail-header-id header))
4236          (data (gnus-data-find article))
4237          (thread (gnus-id-to-thread id))
4238          (references (mail-header-references header))
4239          (parent
4240           (gnus-id-to-thread
4241            (or (gnus-parent-id
4242                 (when (and references
4243                            (not (equal "" references)))
4244                   references))
4245                "none")))
4246          (buffer-read-only nil)
4247          (old (car thread)))
4248     (when thread
4249       (unless iheader
4250         (setcar thread nil)
4251         (when parent
4252           (delq thread parent)))
4253       (if (gnus-summary-insert-subject id header)
4254           ;; Set the (possibly) new article number in the data structure.
4255           (gnus-data-set-number data (gnus-id-to-article id))
4256         (setcar thread old)
4257         nil))))
4258
4259 (defun gnus-rebuild-thread (id &optional line)
4260   "Rebuild the thread containing ID.
4261 If LINE, insert the rebuilt thread starting on line LINE."
4262   (let ((buffer-read-only nil)
4263         old-pos current thread data)
4264     (if (not gnus-show-threads)
4265         (setq thread (list (car (gnus-id-to-thread id))))
4266       ;; Get the thread this article is part of.
4267       (setq thread (gnus-remove-thread id)))
4268     (setq old-pos (point-at-bol))
4269     (setq current (save-excursion
4270                     (and (re-search-backward "[\r\n]" nil t)
4271                          (gnus-summary-article-number))))
4272     ;; If this is a gathered thread, we have to go some re-gathering.
4273     (when (stringp (car thread))
4274       (let ((subject (car thread))
4275             roots thr)
4276         (setq thread (cdr thread))
4277         (while thread
4278           (unless (memq (setq thr (gnus-id-to-thread
4279                                    (gnus-root-id
4280                                     (mail-header-id (caar thread)))))
4281                         roots)
4282             (push thr roots))
4283           (setq thread (cdr thread)))
4284         ;; We now have all (unique) roots.
4285         (if (= (length roots) 1)
4286             ;; All the loose roots are now one solid root.
4287             (setq thread (car roots))
4288           (setq thread (cons subject (gnus-sort-threads roots))))))
4289     (let (threads)
4290       ;; We then insert this thread into the summary buffer.
4291       (when line
4292         (goto-char (point-min))
4293         (forward-line (1- line)))
4294       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4295         (if gnus-show-threads
4296             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4297           (gnus-summary-prepare-unthreaded thread))
4298         (setq data (nreverse gnus-newsgroup-data))
4299         (setq threads gnus-newsgroup-threads))
4300       ;; We splice the new data into the data structure.
4301       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4302       ;;!!! then we want to insert at the beginning of the buffer.
4303       ;;!!! That happens to be true with Gnus now, but that may
4304       ;;!!! change in the future.  Perhaps.
4305       (gnus-data-enter-list
4306        (if line nil current) data (- (point) old-pos))
4307       (setq gnus-newsgroup-threads
4308             (nconc threads gnus-newsgroup-threads))
4309       (gnus-data-compute-positions))))
4310
4311 (defun gnus-number-to-header (number)
4312   "Return the header for article NUMBER."
4313   (let ((headers gnus-newsgroup-headers))
4314     (while (and headers
4315                 (not (= number (mail-header-number (car headers)))))
4316       (pop headers))
4317     (when headers
4318       (car headers))))
4319
4320 (defun gnus-parent-headers (in-headers &optional generation)
4321   "Return the headers of the GENERATIONeth parent of HEADERS."
4322   (unless generation
4323     (setq generation 1))
4324   (let ((parent t)
4325         (headers in-headers)
4326         references)
4327     (while (and parent
4328                 (not (zerop generation))
4329                 (setq references (mail-header-references headers)))
4330       (setq headers (if (and references
4331                              (setq parent (gnus-parent-id references)))
4332                         (car (gnus-id-to-thread parent))
4333                       nil))
4334       (decf generation))
4335     (and (not (eq headers in-headers))
4336          headers)))
4337
4338 (defun gnus-id-to-thread (id)
4339   "Return the (sub-)thread where ID appears."
4340   (gnus-gethash id gnus-newsgroup-dependencies))
4341
4342 (defun gnus-id-to-article (id)
4343   "Return the article number of ID."
4344   (let ((thread (gnus-id-to-thread id)))
4345     (when (and thread
4346                (car thread))
4347       (mail-header-number (car thread)))))
4348
4349 (defun gnus-id-to-header (id)
4350   "Return the article headers of ID."
4351   (car (gnus-id-to-thread id)))
4352
4353 (defun gnus-article-displayed-root-p (article)
4354   "Say whether ARTICLE is a root(ish) article."
4355   (let ((level (gnus-summary-thread-level article))
4356         (refs (mail-header-references  (gnus-summary-article-header article)))
4357         particle)
4358     (cond
4359      ((null level) nil)
4360      ((zerop level) t)
4361      ((null refs) t)
4362      ((null (gnus-parent-id refs)) t)
4363      ((and (= 1 level)
4364            (null (setq particle (gnus-id-to-article
4365                                  (gnus-parent-id refs))))
4366            (null (gnus-summary-thread-level particle)))))))
4367
4368 (defun gnus-root-id (id)
4369   "Return the id of the root of the thread where ID appears."
4370   (let (last-id prev)
4371     (while (and id (setq prev (car (gnus-id-to-thread id))))
4372       (setq last-id id
4373             id (gnus-parent-id (mail-header-references prev))))
4374     last-id))
4375
4376 (defun gnus-articles-in-thread (thread)
4377   "Return the list of articles in THREAD."
4378   (cons (mail-header-number (car thread))
4379         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4380
4381 (defun gnus-remove-thread (id &optional dont-remove)
4382   "Remove the thread that has ID in it."
4383   (let (headers thread last-id)
4384     ;; First go up in this thread until we find the root.
4385     (setq last-id (gnus-root-id id)
4386           headers (message-flatten-list (gnus-id-to-thread last-id)))
4387     ;; We have now found the real root of this thread.  It might have
4388     ;; been gathered into some loose thread, so we have to search
4389     ;; through the threads to find the thread we wanted.
4390     (let ((threads gnus-newsgroup-threads)
4391           sub)
4392       (while threads
4393         (setq sub (car threads))
4394         (if (stringp (car sub))
4395             ;; This is a gathered thread, so we look at the roots
4396             ;; below it to find whether this article is in this
4397             ;; gathered root.
4398             (progn
4399               (setq sub (cdr sub))
4400               (while sub
4401                 (when (member (caar sub) headers)
4402                   (setq thread (car threads)
4403                         threads nil
4404                         sub nil))
4405                 (setq sub (cdr sub))))
4406           ;; It's an ordinary thread, so we check it.
4407           (when (eq (car sub) (car headers))
4408             (setq thread sub
4409                   threads nil)))
4410         (setq threads (cdr threads)))
4411       ;; If this article is in no thread, then it's a root.
4412       (if thread
4413           (unless dont-remove
4414             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4415         (setq thread (gnus-id-to-thread last-id)))
4416       (when thread
4417         (prog1
4418             thread                      ; We return this thread.
4419           (unless dont-remove
4420             (if (stringp (car thread))
4421                 (progn
4422                   ;; If we use dummy roots, then we have to remove the
4423                   ;; dummy root as well.
4424                   (when (eq gnus-summary-make-false-root 'dummy)
4425                     ;; We go to the dummy root by going to
4426                     ;; the first sub-"thread", and then one line up.
4427                     (gnus-summary-goto-article
4428                      (mail-header-number (caadr thread)))
4429                     (forward-line -1)
4430                     (gnus-delete-line)
4431                     (gnus-data-compute-positions))
4432                   (setq thread (cdr thread))
4433                   (while thread
4434                     (gnus-remove-thread-1 (car thread))
4435                     (setq thread (cdr thread))))
4436               (gnus-remove-thread-1 thread))))))))
4437
4438 (defun gnus-remove-thread-1 (thread)
4439   "Remove the thread THREAD recursively."
4440   (let ((number (mail-header-number (pop thread)))
4441         d)
4442     (setq thread (reverse thread))
4443     (while thread
4444       (gnus-remove-thread-1 (pop thread)))
4445     (when (setq d (gnus-data-find number))
4446       (goto-char (gnus-data-pos d))
4447       (gnus-summary-show-thread)
4448       (gnus-data-remove
4449        number
4450        (- (point-at-bol)
4451           (prog1
4452               (1+ (point-at-eol))
4453             (gnus-delete-line)))))))
4454
4455 (defun gnus-sort-threads-1 (threads func)
4456   (sort (mapcar (lambda (thread)
4457                   (cons (car thread)
4458                         (and (cdr thread)
4459                              (gnus-sort-threads-1 (cdr thread) func))))
4460                 threads) func))
4461
4462 (defun gnus-sort-threads (threads)
4463   "Sort THREADS."
4464   (if (not gnus-thread-sort-functions)
4465       threads
4466     (gnus-message 8 "Sorting threads...")
4467     (let ((max-lisp-eval-depth 5000))
4468       (prog1 (gnus-sort-threads-1
4469          threads
4470          (gnus-make-sort-function gnus-thread-sort-functions))
4471         (gnus-message 8 "Sorting threads...done")))))
4472
4473 (defun gnus-sort-articles (articles)
4474   "Sort ARTICLES."
4475   (when gnus-article-sort-functions
4476     (gnus-message 7 "Sorting articles...")
4477     (prog1
4478         (setq gnus-newsgroup-headers
4479               (sort articles (gnus-make-sort-function
4480                               gnus-article-sort-functions)))
4481       (gnus-message 7 "Sorting articles...done"))))
4482
4483 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4484 (defmacro gnus-thread-header (thread)
4485   "Return header of first article in THREAD.
4486 Note that THREAD must never, ever be anything else than a variable -
4487 using some other form will lead to serious barfage."
4488   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4489   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4490   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4491         (vector thread) 2))
4492
4493 (defsubst gnus-article-sort-by-number (h1 h2)
4494   "Sort articles by article number."
4495   (< (mail-header-number h1)
4496      (mail-header-number h2)))
4497
4498 (defun gnus-thread-sort-by-number (h1 h2)
4499   "Sort threads by root article number."
4500   (gnus-article-sort-by-number
4501    (gnus-thread-header h1) (gnus-thread-header h2)))
4502
4503 (defsubst gnus-article-sort-by-random (h1 h2)
4504   "Sort articles by article number."
4505   (zerop (random 2)))
4506
4507 (defun gnus-thread-sort-by-random (h1 h2)
4508   "Sort threads by root article number."
4509   (gnus-article-sort-by-random
4510    (gnus-thread-header h1) (gnus-thread-header h2)))
4511
4512 (defsubst gnus-article-sort-by-lines (h1 h2)
4513   "Sort articles by article Lines header."
4514   (< (mail-header-lines h1)
4515      (mail-header-lines h2)))
4516
4517 (defun gnus-thread-sort-by-lines (h1 h2)
4518   "Sort threads by root article Lines header."
4519   (gnus-article-sort-by-lines
4520    (gnus-thread-header h1) (gnus-thread-header h2)))
4521
4522 (defsubst gnus-article-sort-by-chars (h1 h2)
4523   "Sort articles by octet length."
4524   (< (mail-header-chars h1)
4525      (mail-header-chars h2)))
4526
4527 (defun gnus-thread-sort-by-chars (h1 h2)
4528   "Sort threads by root article octet length."
4529   (gnus-article-sort-by-chars
4530    (gnus-thread-header h1) (gnus-thread-header h2)))
4531
4532 (defsubst gnus-article-sort-by-author (h1 h2)
4533   "Sort articles by root author."
4534   (string-lessp
4535    (let ((addr (car (mime-entity-read-field h1 'From))))
4536      (or (std11-full-name-string addr)
4537          (std11-address-string addr)
4538          ""))
4539    (let ((addr (car (mime-entity-read-field h2 'From))))
4540      (or (std11-full-name-string addr)
4541          (std11-address-string addr)
4542          ""))
4543    ))
4544
4545 (defun gnus-thread-sort-by-author (h1 h2)
4546   "Sort threads by root author."
4547   (gnus-article-sort-by-author
4548    (gnus-thread-header h1)  (gnus-thread-header h2)))
4549
4550 (defsubst gnus-article-sort-by-subject (h1 h2)
4551   "Sort articles by root subject."
4552   (string-lessp
4553    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4554    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4555
4556 (defun gnus-thread-sort-by-subject (h1 h2)
4557   "Sort threads by root subject."
4558   (gnus-article-sort-by-subject
4559    (gnus-thread-header h1) (gnus-thread-header h2)))
4560
4561 (defsubst gnus-article-sort-by-date (h1 h2)
4562   "Sort articles by root article date."
4563   (time-less-p
4564    (gnus-date-get-time (mail-header-date h1))
4565    (gnus-date-get-time (mail-header-date h2))))
4566
4567 (defun gnus-thread-sort-by-date (h1 h2)
4568   "Sort threads by root article date."
4569   (gnus-article-sort-by-date
4570    (gnus-thread-header h1) (gnus-thread-header h2)))
4571
4572 (defsubst gnus-article-sort-by-score (h1 h2)
4573   "Sort articles by root article score.
4574 Unscored articles will be counted as having a score of zero."
4575   (> (or (cdr (assq (mail-header-number h1)
4576                     gnus-newsgroup-scored))
4577          gnus-summary-default-score 0)
4578      (or (cdr (assq (mail-header-number h2)
4579                     gnus-newsgroup-scored))
4580          gnus-summary-default-score 0)))
4581
4582 (defun gnus-thread-sort-by-score (h1 h2)
4583   "Sort threads by root article score."
4584   (gnus-article-sort-by-score
4585    (gnus-thread-header h1) (gnus-thread-header h2)))
4586
4587 (defun gnus-thread-sort-by-total-score (h1 h2)
4588   "Sort threads by the sum of all scores in the thread.
4589 Unscored articles will be counted as having a score of zero."
4590   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4591
4592 (defun gnus-thread-total-score (thread)
4593   ;; This function find the total score of THREAD.
4594   (cond
4595    ((null thread)
4596     0)
4597    ((consp thread)
4598     (if (stringp (car thread))
4599         (apply gnus-thread-score-function 0
4600                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4601       (gnus-thread-total-score-1 thread)))
4602    (t
4603     (gnus-thread-total-score-1 (list thread)))))
4604
4605 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4606   "Sort threads such that the thread with the most recently arrived article comes first."
4607   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4608
4609 (defun gnus-thread-highest-number (thread)
4610   "Return the highest article number in THREAD."
4611   (apply 'max (mapcar (lambda (header)
4612                         (mail-header-number header))
4613                       (message-flatten-list thread))))
4614
4615 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4616   "Sort threads such that the thread with the most recently dated article comes first."
4617   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4618
4619 (defun gnus-thread-latest-date (thread)
4620   "Return the highest article date in THREAD."
4621   (let ((previous-time 0))
4622     (apply 'max
4623            (mapcar
4624             (lambda (header)
4625               (setq previous-time
4626                     (condition-case ()
4627                         (time-to-seconds (mail-header-parse-date
4628                                           (mail-header-date header)))
4629                       (error previous-time))))
4630             (sort
4631              (message-flatten-list thread)
4632              (lambda (h1 h2)
4633                (< (mail-header-number h1)
4634                   (mail-header-number h2))))))))
4635
4636 (defun gnus-thread-total-score-1 (root)
4637   ;; This function find the total score of the thread below ROOT.
4638   (setq root (car root))
4639   (apply gnus-thread-score-function
4640          (or (append
4641               (mapcar 'gnus-thread-total-score
4642                       (cdr (gnus-id-to-thread (mail-header-id root))))
4643               (when (> (mail-header-number root) 0)
4644                 (list (or (cdr (assq (mail-header-number root)
4645                                      gnus-newsgroup-scored))
4646                           gnus-summary-default-score 0))))
4647              (list gnus-summary-default-score)
4648              '(0))))
4649
4650 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4651 (defvar gnus-tmp-prev-subject nil)
4652 (defvar gnus-tmp-false-parent nil)
4653 (defvar gnus-tmp-root-expunged nil)
4654 (defvar gnus-tmp-dummy-line nil)
4655
4656 (eval-when-compile (defvar gnus-tmp-header))
4657 (defun gnus-extra-header (type &optional header)
4658   "Return the extra header of TYPE."
4659   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4660       ""))
4661
4662 (defvar gnus-tmp-thread-tree-header-string "")
4663
4664 (defcustom gnus-sum-thread-tree-root "> "
4665   "With %B spec, used for the root of a thread.
4666 If nil, use subject instead."
4667   :version "21.4"
4668   :type '(radio (const :format "%v  " nil) (string :size 0))
4669   :group 'gnus-thread)
4670 (defcustom gnus-sum-thread-tree-false-root "> "
4671   "With %B spec, used for a false root of a thread.
4672 If nil, use subject instead."
4673   :version "21.4"
4674   :type '(radio (const :format "%v  " nil) (string :size 0))
4675   :group 'gnus-thread)
4676 (defcustom gnus-sum-thread-tree-single-indent ""
4677   "With %B spec, used for a thread with just one message.
4678 If nil, use subject instead."
4679   :version "21.4"
4680   :type '(radio (const :format "%v  " nil) (string :size 0))
4681   :group 'gnus-thread)
4682 (defcustom gnus-sum-thread-tree-vertical "| "
4683   "With %B spec, used for drawing a vertical line."
4684   :version "21.4"
4685   :type 'string
4686   :group 'gnus-thread)
4687 (defcustom gnus-sum-thread-tree-indent "  "
4688   "With %B spec, used for indenting."
4689   :version "21.4"
4690   :type 'string
4691   :group 'gnus-thread)
4692 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4693   "With %B spec, used for a leaf with brothers."
4694   :version "21.4"
4695   :type 'string
4696   :group 'gnus-thread)
4697 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4698   "With %B spec, used for a leaf without brothers."
4699   :version "21.4"
4700   :type 'string
4701   :group 'gnus-thread)
4702
4703 (defun gnus-summary-prepare-threads (threads)
4704   "Prepare summary buffer from THREADS and indentation LEVEL.
4705 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4706 or a straight list of headers."
4707   (gnus-message 7 "Generating summary...")
4708
4709   (setq gnus-newsgroup-threads threads)
4710   (beginning-of-line)
4711
4712   (let ((gnus-tmp-level 0)
4713         (default-score (or gnus-summary-default-score 0))
4714         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4715         (building-line-count gnus-summary-display-while-building)
4716         (building-count (integerp gnus-summary-display-while-building))
4717         thread number subject stack state gnus-tmp-gathered beg-match
4718         new-roots gnus-tmp-new-adopts thread-end simp-subject
4719         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4720         gnus-tmp-replied gnus-tmp-subject-or-nil
4721         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4722         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4723         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4724         tree-stack)
4725
4726     (setq gnus-tmp-prev-subject nil
4727           gnus-tmp-thread-tree-header-string "")
4728
4729     (if (vectorp (car threads))
4730         ;; If this is a straight (sic) list of headers, then a
4731         ;; threaded summary display isn't required, so we just create
4732         ;; an unthreaded one.
4733         (gnus-summary-prepare-unthreaded threads)
4734
4735       ;; Do the threaded display.
4736
4737       (if gnus-summary-display-while-building
4738           (switch-to-buffer (buffer-name)))
4739       (while (or threads stack gnus-tmp-new-adopts new-roots)
4740
4741         (if (and (= gnus-tmp-level 0)
4742                  (or (not stack)
4743                      (= (caar stack) 0))
4744                  (not gnus-tmp-false-parent)
4745                  (or gnus-tmp-new-adopts new-roots))
4746             (if gnus-tmp-new-adopts
4747                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4748                       thread (list (car gnus-tmp-new-adopts))
4749                       gnus-tmp-header (caar thread)
4750                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4751               (when new-roots
4752                 (setq thread (list (car new-roots))
4753                       gnus-tmp-header (caar thread)
4754                       new-roots (cdr new-roots))))
4755
4756           (if threads
4757               ;; If there are some threads, we do them before the
4758               ;; threads on the stack.
4759               (setq thread threads
4760                     gnus-tmp-header (caar thread))
4761             ;; There were no current threads, so we pop something off
4762             ;; the stack.
4763             (setq state (car stack)
4764                   gnus-tmp-level (car state)
4765                   tree-stack (cadr state)
4766                   thread (caddr state)
4767                   stack (cdr stack)
4768                   gnus-tmp-header (caar thread))))
4769
4770         (setq gnus-tmp-false-parent nil)
4771         (setq gnus-tmp-root-expunged nil)
4772         (setq thread-end nil)
4773
4774         (if (stringp gnus-tmp-header)
4775             ;; The header is a dummy root.
4776             (cond
4777              ((eq gnus-summary-make-false-root 'adopt)
4778               ;; We let the first article adopt the rest.
4779               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4780                                                (cddar thread)))
4781               (setq gnus-tmp-gathered
4782                     (nconc (mapcar
4783                             (lambda (h) (mail-header-number (car h)))
4784                             (cddar thread))
4785                            gnus-tmp-gathered))
4786               (setq thread (cons (list (caar thread)
4787                                        (cadar thread))
4788                                  (cdr thread)))
4789               (setq gnus-tmp-level -1
4790                     gnus-tmp-false-parent t))
4791              ((eq gnus-summary-make-false-root 'empty)
4792               ;; We print adopted articles with empty subject fields.
4793               (setq gnus-tmp-gathered
4794                     (nconc (mapcar
4795                             (lambda (h) (mail-header-number (car h)))
4796                             (cddar thread))
4797                            gnus-tmp-gathered))
4798               (setq gnus-tmp-level -1))
4799              ((eq gnus-summary-make-false-root 'dummy)
4800               ;; We remember that we probably want to output a dummy
4801               ;; root.
4802               (setq gnus-tmp-dummy-line gnus-tmp-header)
4803               (setq gnus-tmp-prev-subject gnus-tmp-header))
4804              (t
4805               ;; We do not make a root for the gathered
4806               ;; sub-threads at all.
4807               (setq gnus-tmp-level -1)))
4808
4809           (setq number (mail-header-number gnus-tmp-header)
4810                 subject (mail-header-subject gnus-tmp-header)
4811                 simp-subject (gnus-simplify-subject-fully subject))
4812
4813           (cond
4814            ;; If the thread has changed subject, we might want to make
4815            ;; this subthread into a root.
4816            ((and (null gnus-thread-ignore-subject)
4817                  (not (zerop gnus-tmp-level))
4818                  gnus-tmp-prev-subject
4819                  (not (string= gnus-tmp-prev-subject simp-subject)))
4820             (setq new-roots (nconc new-roots (list (car thread)))
4821                   thread-end t
4822                   gnus-tmp-header nil))
4823            ;; If the article lies outside the current limit,
4824            ;; then we do not display it.
4825            ((not (memq number gnus-newsgroup-limit))
4826             (setq gnus-tmp-gathered
4827                   (nconc (mapcar
4828                           (lambda (h) (mail-header-number (car h)))
4829                           (cdar thread))
4830                          gnus-tmp-gathered))
4831             (setq gnus-tmp-new-adopts (if (cdar thread)
4832                                           (append gnus-tmp-new-adopts
4833                                                   (cdar thread))
4834                                         gnus-tmp-new-adopts)
4835                   thread-end t
4836                   gnus-tmp-header nil)
4837             (when (zerop gnus-tmp-level)
4838               (setq gnus-tmp-root-expunged t)))
4839            ;; Perhaps this article is to be marked as read?
4840            ((and gnus-summary-mark-below
4841                  (< (or (cdr (assq number gnus-newsgroup-scored))
4842                         default-score)
4843                     gnus-summary-mark-below)
4844                  ;; Don't touch sparse articles.
4845                  (not (gnus-summary-article-sparse-p number))
4846                  (not (gnus-summary-article-ancient-p number)))
4847             (setq gnus-newsgroup-unreads
4848                   (delq number gnus-newsgroup-unreads))
4849             (if gnus-newsgroup-auto-expire
4850                 (setq gnus-newsgroup-expirable
4851                       (gnus-add-to-sorted-list
4852                        gnus-newsgroup-expirable number))
4853               (push (cons number gnus-low-score-mark)
4854                     gnus-newsgroup-reads))))
4855
4856           (when gnus-tmp-header
4857             ;; We may have an old dummy line to output before this
4858             ;; article.
4859             (when (and gnus-tmp-dummy-line
4860                        (gnus-subject-equal
4861                         gnus-tmp-dummy-line
4862                         (mail-header-subject gnus-tmp-header)))
4863               (gnus-summary-insert-dummy-line
4864                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4865               (setq gnus-tmp-dummy-line nil))
4866
4867             ;; Compute the mark.
4868             (setq gnus-tmp-unread (gnus-article-mark number))
4869
4870             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4871                                   gnus-tmp-header gnus-tmp-level)
4872                   gnus-newsgroup-data)
4873
4874             ;; Actually insert the line.
4875             (setq
4876              gnus-tmp-subject-or-nil
4877              (cond
4878               ((and gnus-thread-ignore-subject
4879                     gnus-tmp-prev-subject
4880                     (not (string= gnus-tmp-prev-subject simp-subject)))
4881                subject)
4882               ((zerop gnus-tmp-level)
4883                (if (and (eq gnus-summary-make-false-root 'empty)
4884                         (memq number gnus-tmp-gathered)
4885                         gnus-tmp-prev-subject
4886                         (string= gnus-tmp-prev-subject simp-subject))
4887                    gnus-summary-same-subject
4888                  subject))
4889               (t gnus-summary-same-subject)))
4890             (if (and (eq gnus-summary-make-false-root 'adopt)
4891                      (= gnus-tmp-level 1)
4892                      (memq number gnus-tmp-gathered))
4893                 (setq gnus-tmp-opening-bracket ?\<
4894                       gnus-tmp-closing-bracket ?\>)
4895               (setq gnus-tmp-opening-bracket ?\[
4896                     gnus-tmp-closing-bracket ?\]))
4897             (setq
4898              gnus-tmp-indentation
4899              (aref gnus-thread-indent-array gnus-tmp-level)
4900              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4901              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4902                                 gnus-summary-default-score 0)
4903              gnus-tmp-score-char
4904              (if (or (null gnus-summary-default-score)
4905                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4906                          gnus-summary-zcore-fuzz))
4907                  ?\ ;;;Whitespace
4908                (if (< gnus-tmp-score gnus-summary-default-score)
4909                    gnus-score-below-mark gnus-score-over-mark))
4910              gnus-tmp-replied
4911              (cond ((memq number gnus-newsgroup-processable)
4912                     gnus-process-mark)
4913                    ((memq number gnus-newsgroup-cached)
4914                     gnus-cached-mark)
4915                    ((memq number gnus-newsgroup-replied)
4916                     gnus-replied-mark)
4917                    ((memq number gnus-newsgroup-forwarded)
4918                     gnus-forwarded-mark)
4919                    ((memq number gnus-newsgroup-saved)
4920                     gnus-saved-mark)
4921                    ((memq number gnus-newsgroup-recent)
4922                     gnus-recent-mark)
4923                    ((memq number gnus-newsgroup-unseen)
4924                     gnus-unseen-mark)
4925                    (t gnus-no-mark))
4926              gnus-tmp-downloaded
4927              (cond ((memq number gnus-newsgroup-undownloaded)
4928                     gnus-undownloaded-mark)
4929                    (gnus-newsgroup-agentized
4930                     gnus-downloaded-mark)
4931                    (t
4932                     gnus-no-mark))
4933              gnus-tmp-from (mail-header-from gnus-tmp-header)
4934              gnus-tmp-name
4935              (cond
4936               ((string-match "<[^>]+> *$" gnus-tmp-from)
4937                (setq beg-match (match-beginning 0))
4938                (or (and (string-match "^\".+\"" gnus-tmp-from)
4939                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4940                    (substring gnus-tmp-from 0 beg-match)))
4941               ((string-match "(.+)" gnus-tmp-from)
4942                (substring gnus-tmp-from
4943                           (1+ (match-beginning 0)) (1- (match-end 0))))
4944               (t gnus-tmp-from))
4945
4946              ;; Do the %B string
4947              gnus-tmp-thread-tree-header-string
4948              (cond
4949               ((not gnus-show-threads) "")
4950               ((zerop gnus-tmp-level)
4951                (cond ((cdar thread)
4952                       (or gnus-sum-thread-tree-root subject))
4953                      (gnus-tmp-new-adopts
4954                       (or gnus-sum-thread-tree-false-root subject))
4955                      (t
4956                       (or gnus-sum-thread-tree-single-indent subject))))
4957               (t
4958                (concat (apply 'concat
4959                               (mapcar (lambda (item)
4960                                         (if (= item 1)
4961                                             gnus-sum-thread-tree-vertical
4962                                           gnus-sum-thread-tree-indent))
4963                                       (cdr (reverse tree-stack))))
4964                        (if (nth 1 thread)
4965                            gnus-sum-thread-tree-leaf-with-other
4966                          gnus-sum-thread-tree-single-leaf)))))
4967             (when (string= gnus-tmp-name "")
4968               (setq gnus-tmp-name gnus-tmp-from))
4969             (unless (numberp gnus-tmp-lines)
4970               (setq gnus-tmp-lines -1))
4971             (if (= gnus-tmp-lines -1)
4972                 (setq gnus-tmp-lines "?")
4973               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4974               (gnus-put-text-property
4975              (point)
4976              (progn (eval gnus-summary-line-format-spec) (point))
4977                'gnus-number number)
4978             (when gnus-visual-p
4979               (forward-line -1)
4980               (gnus-run-hooks 'gnus-summary-update-hook)
4981               (forward-line 1))
4982
4983             (setq gnus-tmp-prev-subject simp-subject)))
4984
4985         (when (nth 1 thread)
4986           (push (list (max 0 gnus-tmp-level)
4987                       (copy-sequence tree-stack)
4988                       (nthcdr 1 thread))
4989                 stack))
4990         (push (if (nth 1 thread) 1 0) tree-stack)
4991         (incf gnus-tmp-level)
4992         (setq threads (if thread-end nil (cdar thread)))
4993         (if gnus-summary-display-while-building
4994             (if building-count
4995                 (progn
4996                   ;; use a set frequency
4997                   (setq building-line-count (1- building-line-count))
4998                   (when (= building-line-count 0)
4999                     (sit-for 0)
5000                     (setq building-line-count
5001                           gnus-summary-display-while-building)))
5002               ;; always
5003               (sit-for 0)))
5004         (unless threads
5005           (setq gnus-tmp-level 0)))))
5006   (gnus-message 7 "Generating summary...done"))
5007
5008 (defun gnus-summary-prepare-unthreaded (headers)
5009   "Generate an unthreaded summary buffer based on HEADERS."
5010   (let (header number mark)
5011
5012     (beginning-of-line)
5013
5014     (while headers
5015       ;; We may have to root out some bad articles...
5016       (when (memq (setq number (mail-header-number
5017                                 (setq header (pop headers))))
5018                   gnus-newsgroup-limit)
5019         ;; Mark article as read when it has a low score.
5020         (when (and gnus-summary-mark-below
5021                    (< (or (cdr (assq number gnus-newsgroup-scored))
5022                           gnus-summary-default-score 0)
5023                       gnus-summary-mark-below)
5024                    (not (gnus-summary-article-ancient-p number)))
5025           (setq gnus-newsgroup-unreads
5026                 (delq number gnus-newsgroup-unreads))
5027           (if gnus-newsgroup-auto-expire
5028               (push number gnus-newsgroup-expirable)
5029             (push (cons number gnus-low-score-mark)
5030                   gnus-newsgroup-reads)))
5031
5032         (setq mark (gnus-article-mark number))
5033         (push (gnus-data-make number mark (1+ (point)) header 0)
5034               gnus-newsgroup-data)
5035         (gnus-summary-insert-line
5036          header 0 number
5037          (memq number gnus-newsgroup-undownloaded)
5038          mark (memq number gnus-newsgroup-replied)
5039          (memq number gnus-newsgroup-expirable)
5040          (mail-header-subject header) nil
5041          (cdr (assq number gnus-newsgroup-scored))
5042          (memq number gnus-newsgroup-processable))))))
5043
5044 (defun gnus-summary-remove-list-identifiers ()
5045   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
5046   (let ((regexp (if (consp gnus-list-identifiers)
5047                     (mapconcat 'identity gnus-list-identifiers " *\\|")
5048                   gnus-list-identifiers))
5049         changed subject)
5050     (when regexp
5051       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
5052       (dolist (header gnus-newsgroup-headers)
5053         (setq subject (mail-header-subject header)
5054               changed nil)
5055         (while (string-match regexp subject)
5056           (setq subject
5057                 (concat (substring subject 0 (match-beginning 1))
5058                         (substring subject (match-end 0)))
5059                 changed t))
5060         (when changed
5061           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
5062             (setq subject
5063                   (concat (substring subject 0 (match-beginning 1))
5064                           (substring subject (match-end 1)))))
5065           (mail-header-set-subject header subject))))))
5066
5067 (defun gnus-fetch-headers (articles)
5068   "Fetch headers of ARTICLES."
5069   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
5070     (gnus-message 5 "Fetching headers for %s..." name)
5071     (prog1
5072         (if (eq 'nov
5073                 (setq gnus-headers-retrieved-by
5074                       (gnus-retrieve-headers
5075                        articles gnus-newsgroup-name
5076                        ;; We might want to fetch old headers, but
5077                        ;; not if there is only 1 article.
5078                        (and (or (and
5079                                  (not (eq gnus-fetch-old-headers 'some))
5080                                  (not (numberp gnus-fetch-old-headers)))
5081                                 (> (length articles) 1))
5082                             gnus-fetch-old-headers))))
5083             (gnus-get-newsgroup-headers-xover
5084              articles nil nil gnus-newsgroup-name t)
5085           (gnus-get-newsgroup-headers))
5086       (gnus-message 5 "Fetching headers for %s...done" name))))
5087
5088 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5089   "Select newsgroup GROUP.
5090 If READ-ALL is non-nil, all articles in the group are selected.
5091 If SELECT-ARTICLES, only select those articles from GROUP."
5092   (let* ((entry (gnus-group-entry group))
5093          ;;!!! Dirty hack; should be removed.
5094          (gnus-summary-ignore-duplicates
5095           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5096               t
5097             gnus-summary-ignore-duplicates))
5098          (info (nth 2 entry))
5099          articles fetched-articles cached)
5100
5101     (unless (gnus-check-server
5102              (set (make-local-variable 'gnus-current-select-method)
5103                   (gnus-find-method-for-group group)))
5104       (error "Couldn't open server"))
5105
5106     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5107         (gnus-activate-group group)     ; Or we can activate it...
5108         (progn                          ; Or we bug out.
5109           (when (equal major-mode 'gnus-summary-mode)
5110             (gnus-kill-buffer (current-buffer)))
5111           (error "Couldn't activate group %s: %s"
5112                  group (gnus-status-message group))))
5113
5114     (unless (gnus-request-group group t)
5115       (when (equal major-mode 'gnus-summary-mode)
5116         (gnus-kill-buffer (current-buffer)))
5117       (error "Couldn't request group %s: %s"
5118              group (gnus-status-message group)))
5119
5120     (when gnus-agent
5121       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5122       
5123       (setq gnus-summary-use-undownloaded-faces
5124             (gnus-agent-find-parameter
5125              group
5126              'agent-enable-undownloaded-faces)))
5127
5128     (setq gnus-newsgroup-name group
5129           gnus-newsgroup-unselected nil
5130           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5131
5132     (let ((display (gnus-group-find-parameter group 'display)))
5133       (setq gnus-newsgroup-display
5134             (cond
5135              ((not (zerop (or (car-safe read-all) 0)))
5136               ;; The user entered the group with C-u SPC/RET, let's show
5137               ;; all articles.
5138               'gnus-not-ignore)
5139              ((eq display 'all)
5140               'gnus-not-ignore)
5141              ((arrayp display)
5142               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5143              ((numberp display)
5144               ;; The following is probably the "correct" solution, but
5145               ;; it makes Gnus fetch all headers and then limit the
5146               ;; articles (which is slow), so instead we hack the
5147               ;; select-articles parameter instead. -- Simon Josefsson
5148               ;; <jas@kth.se>
5149               ;;
5150               ;; (gnus-byte-compile
5151               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5152               ;;                         display)))))
5153               (setq select-articles
5154                     (gnus-uncompress-range
5155                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5156                              (if (> tmp 0)
5157                                  tmp
5158                                1))
5159                            (cdr (gnus-active group)))))
5160               nil)
5161              (t
5162               nil))))
5163
5164     (gnus-summary-setup-default-charset)
5165
5166     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5167     (when (gnus-virtual-group-p group)
5168       (setq cached gnus-newsgroup-cached))
5169
5170     (setq gnus-newsgroup-unreads
5171           (gnus-sorted-ndifference
5172            (gnus-sorted-ndifference gnus-newsgroup-unreads
5173                                     gnus-newsgroup-marked)
5174            gnus-newsgroup-dormant))
5175
5176     (setq gnus-newsgroup-processable nil)
5177
5178     (gnus-update-read-articles group gnus-newsgroup-unreads)
5179
5180     ;; Adjust and set lists of article marks.
5181     (when info
5182       (gnus-adjust-marked-articles info))
5183     (if (setq articles select-articles)
5184         (setq gnus-newsgroup-unselected
5185               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5186       (setq articles (gnus-articles-to-read group read-all)))
5187
5188     (cond
5189      ((null articles)
5190       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5191       'quit)
5192      ((eq articles 0) nil)
5193      (t
5194       ;; Init the dependencies hash table.
5195       (setq gnus-newsgroup-dependencies
5196             (gnus-make-hashtable (length articles)))
5197       (gnus-set-global-variables)
5198       ;; Retrieve the headers and read them in.
5199
5200       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5201
5202       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5203       (when cached
5204         (setq gnus-newsgroup-cached cached))
5205
5206       ;; Suppress duplicates?
5207       (when gnus-suppress-duplicates
5208         (gnus-dup-suppress-articles))
5209
5210       ;; Set the initial limit.
5211       (setq gnus-newsgroup-limit (copy-sequence articles))
5212       ;; Remove canceled articles from the list of unread articles.
5213       (setq fetched-articles
5214             (mapcar (lambda (headers) (mail-header-number headers))
5215                     gnus-newsgroup-headers))
5216       (setq gnus-newsgroup-articles fetched-articles)
5217       (setq gnus-newsgroup-unreads
5218             (gnus-sorted-nintersection
5219              gnus-newsgroup-unreads fetched-articles))
5220       (gnus-compute-unseen-list)
5221
5222       ;; Removed marked articles that do not exist.
5223       (gnus-update-missing-marks
5224        (gnus-sorted-difference articles fetched-articles))
5225       ;; We might want to build some more threads first.
5226       (when (and gnus-fetch-old-headers
5227                  (eq gnus-headers-retrieved-by 'nov))
5228         (if (eq gnus-fetch-old-headers 'invisible)
5229             (gnus-build-all-threads)
5230           (gnus-build-old-threads)))
5231       ;; Let the Gnus agent mark articles as read.
5232       (when gnus-agent
5233         (gnus-agent-get-undownloaded-list))
5234       ;; Remove list identifiers from subject
5235       (when gnus-list-identifiers
5236         (gnus-summary-remove-list-identifiers))
5237       ;; Check whether auto-expire is to be done in this group.
5238       (setq gnus-newsgroup-auto-expire
5239             (gnus-group-auto-expirable-p group))
5240       ;; Set up the article buffer now, if necessary.
5241       (unless gnus-single-article-buffer
5242         (gnus-article-setup-buffer))
5243       ;; First and last article in this newsgroup.
5244       (when gnus-newsgroup-headers
5245         (setq gnus-newsgroup-begin
5246               (mail-header-number (car gnus-newsgroup-headers))
5247               gnus-newsgroup-end
5248               (mail-header-number
5249                (gnus-last-element gnus-newsgroup-headers))))
5250       ;; GROUP is successfully selected.
5251       (or gnus-newsgroup-headers t)))))
5252
5253 (defun gnus-compute-unseen-list ()
5254   ;; The `seen' marks are treated specially.
5255   (if (not gnus-newsgroup-seen)
5256       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5257     (setq gnus-newsgroup-unseen
5258           (gnus-inverse-list-range-intersection
5259            gnus-newsgroup-articles gnus-newsgroup-seen))))
5260
5261 (defun gnus-summary-display-make-predicate (display)
5262   (require 'gnus-agent)
5263   (when (= (length display) 1)
5264     (setq display (car display)))
5265   (unless gnus-summary-display-cache
5266     (dolist (elem (append '((unread . unread)
5267                             (read . read)
5268                             (unseen . unseen))
5269                           gnus-article-mark-lists))
5270       (push (cons (cdr elem)
5271                   (gnus-byte-compile
5272                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5273             gnus-summary-display-cache)))
5274   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5275         (gnus-category-predicate-cache gnus-summary-display-cache))
5276     (gnus-get-predicate display)))
5277
5278 ;; Uses the dynamically bound `number' variable.
5279 (eval-when-compile
5280   (defvar number))
5281 (defun gnus-article-marked-p (type &optional article)
5282   (let ((article (or article number)))
5283     (cond
5284      ((eq type 'tick)
5285       (memq article gnus-newsgroup-marked))
5286      ((eq type 'spam)
5287       (memq article gnus-newsgroup-spam-marked))
5288      ((eq type 'unsend)
5289       (memq article gnus-newsgroup-unsendable))
5290      ((eq type 'undownload)
5291       (memq article gnus-newsgroup-undownloaded))
5292      ((eq type 'download)
5293       (memq article gnus-newsgroup-downloadable))
5294      ((eq type 'unread)
5295       (memq article gnus-newsgroup-unreads))
5296      ((eq type 'read)
5297       (memq article gnus-newsgroup-reads))
5298      ((eq type 'dormant)
5299       (memq article gnus-newsgroup-dormant) )
5300      ((eq type 'expire)
5301       (memq article gnus-newsgroup-expirable))
5302      ((eq type 'reply)
5303       (memq article gnus-newsgroup-replied))
5304      ((eq type 'killed)
5305       (memq article gnus-newsgroup-killed))
5306      ((eq type 'bookmark)
5307       (assq article gnus-newsgroup-bookmarks))
5308      ((eq type 'score)
5309       (assq article gnus-newsgroup-scored))
5310      ((eq type 'save)
5311       (memq article gnus-newsgroup-saved))
5312      ((eq type 'cache)
5313       (memq article gnus-newsgroup-cached))
5314      ((eq type 'forward)
5315       (memq article gnus-newsgroup-forwarded))
5316      ((eq type 'seen)
5317       (not (memq article gnus-newsgroup-unseen)))
5318      ((eq type 'recent)
5319       (memq article gnus-newsgroup-recent))
5320      (t t))))
5321
5322 (defun gnus-articles-to-read (group &optional read-all)
5323   "Find out what articles the user wants to read."
5324   (let* ((articles
5325           ;; Select all articles if `read-all' is non-nil, or if there
5326           ;; are no unread articles.
5327           (if (or read-all
5328                   (and (zerop (length gnus-newsgroup-marked))
5329                        (zerop (length gnus-newsgroup-unreads)))
5330                   ;; Fetch all if the predicate is non-nil.
5331                   gnus-newsgroup-display)
5332               ;; We want to select the headers for all the articles in
5333               ;; the group, so we select either all the active
5334               ;; articles in the group, or (if that's nil), the
5335               ;; articles in the cache.
5336               (or
5337                (gnus-uncompress-range (gnus-active group))
5338                (gnus-cache-articles-in-group group))
5339             ;; Select only the "normal" subset of articles.
5340             (gnus-sorted-nunion
5341              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5342              gnus-newsgroup-unreads)))
5343          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5344          (scored (length scored-list))
5345          (number (length articles))
5346          (marked (+ (length gnus-newsgroup-marked)
5347                     (length gnus-newsgroup-dormant)))
5348          (select
5349           (cond
5350            ((numberp read-all)
5351             read-all)
5352            ((numberp gnus-newsgroup-display)
5353             gnus-newsgroup-display)
5354            (t
5355             (condition-case ()
5356                 (cond
5357                  ((and (or (<= scored marked) (= scored number))
5358                        (numberp gnus-large-newsgroup)
5359                        (> number gnus-large-newsgroup))
5360                   (let* ((cursor-in-echo-area nil)
5361                          (initial (gnus-parameter-large-newsgroup-initial
5362                                    gnus-newsgroup-name))
5363                          (input
5364                           (read-string
5365                            (format
5366                             "How many articles from %s (%s %d): "
5367                             (gnus-limit-string
5368                              (gnus-group-decoded-name gnus-newsgroup-name)
5369                              35)
5370                             (if initial "max" "default")
5371                             number)
5372                            (if initial
5373                                (cons (number-to-string initial)
5374                                      0)))))
5375                     (if (string-match "^[ \t]*$" input) number input)))
5376                  ((and (> scored marked) (< scored number)
5377                        (> (- scored number) 20))
5378                   (let ((input
5379                          (read-string
5380                           (format "%s %s (%d scored, %d total): "
5381                                   "How many articles from"
5382                                   (gnus-group-decoded-name group)
5383                                   scored number))))
5384                     (if (string-match "^[ \t]*$" input)
5385                         number input)))
5386                  (t number))
5387               (quit
5388                (message "Quit getting the articles to read")
5389                nil))))))
5390     (setq select (if (stringp select) (string-to-number select) select))
5391     (if (or (null select) (zerop select))
5392         select
5393       (if (and (not (zerop scored)) (<= (abs select) scored))
5394           (progn
5395             (setq articles (sort scored-list '<))
5396             (setq number (length articles)))
5397         (setq articles (copy-sequence articles)))
5398
5399       (when (< (abs select) number)
5400         (if (< select 0)
5401             ;; Select the N oldest articles.
5402             (setcdr (nthcdr (1- (abs select)) articles) nil)
5403           ;; Select the N most recent articles.
5404           (setq articles (nthcdr (- number select) articles))))
5405       (setq gnus-newsgroup-unselected
5406             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5407       (when gnus-alter-articles-to-read-function
5408         (setq articles
5409               (sort
5410                (funcall gnus-alter-articles-to-read-function
5411                         gnus-newsgroup-name articles)
5412                '<)))
5413       articles)))
5414
5415 (defun gnus-killed-articles (killed articles)
5416   (let (out)
5417     (while articles
5418       (when (inline (gnus-member-of-range (car articles) killed))
5419         (push (car articles) out))
5420       (setq articles (cdr articles)))
5421     out))
5422
5423 (defun gnus-uncompress-marks (marks)
5424   "Uncompress the mark ranges in MARKS."
5425   (let ((uncompressed '(score bookmark))
5426         out)
5427     (while marks
5428       (if (memq (caar marks) uncompressed)
5429           (push (car marks) out)
5430         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5431       (setq marks (cdr marks)))
5432     out))
5433
5434 (defun gnus-article-mark-to-type (mark)
5435   "Return the type of MARK."
5436   (or (cadr (assq mark gnus-article-special-mark-lists))
5437       'list))
5438
5439 (defun gnus-article-unpropagatable-p (mark)
5440   "Return whether MARK should be propagated to back end."
5441   (memq mark gnus-article-unpropagated-mark-lists))
5442
5443 (defun gnus-adjust-marked-articles (info)
5444   "Set all article lists and remove all marks that are no longer valid."
5445   (let* ((marked-lists (gnus-info-marks info))
5446          (active (gnus-active (gnus-info-group info)))
5447          (min (car active))
5448          (max (cdr active))
5449          (types gnus-article-mark-lists)
5450          marks var articles article mark mark-type
5451          bgn end)
5452
5453     (dolist (marks marked-lists)
5454       (setq mark (car marks)
5455             mark-type (gnus-article-mark-to-type mark)
5456             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5457
5458       ;; We set the variable according to the type of the marks list,
5459       ;; and then adjust the marks to a subset of the active articles.
5460       (cond
5461        ;; Adjust "simple" lists - compressed yet unsorted
5462        ((eq mark-type 'list)
5463         ;; Simultaneously uncompress and clip to active range
5464         ;; See gnus-uncompress-range for a description of possible marks
5465         (let (l lh)
5466           (if (not (cadr marks))
5467               (set var nil)
5468             (setq articles (if (numberp (cddr marks))
5469                                (list (cdr marks))
5470                              (cdr marks))
5471                   lh (cons nil nil)
5472                   l lh)
5473
5474             (while (setq article (pop articles))
5475               (cond ((consp article)
5476                      (setq bgn (max (car article) min)
5477                            end (min (cdr article) max))
5478                      (while (<= bgn end)
5479                        (setq l (setcdr l (cons bgn nil))
5480                              bgn (1+ bgn))))
5481                     ((and (<= min article)
5482                           (>= max article))
5483                      (setq l (setcdr l (cons article nil))))))
5484             (set var (cdr lh)))))
5485        ;; Adjust assocs.
5486        ((eq mark-type 'tuple)
5487         (set var (setq articles (cdr marks)))
5488         (when (not (listp (cdr (symbol-value var))))
5489           (set var (list (symbol-value var))))
5490         (when (not (listp (cdr articles)))
5491           (setq articles (list articles)))
5492         (while articles
5493           (when (or (not (consp (setq article (pop articles))))
5494                     (< (car article) min)
5495                     (> (car article) max))
5496             (set var (delq article (symbol-value var))))))
5497        ;; Adjust ranges (sloppily).
5498        ((eq mark-type 'range)
5499         (cond
5500          ((eq mark 'seen)
5501           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5502           ;; It should be (seen (NUM1 . NUM2)).
5503           (when (numberp (cddr marks))
5504             (setcdr marks (list (cdr marks))))
5505           (setq articles (cdr marks))
5506           (while (and articles
5507                       (or (and (consp (car articles))
5508                                (> min (cdar articles)))
5509                           (and (numberp (car articles))
5510                                (> min (car articles)))))
5511             (pop articles))
5512           (set var articles))))))))
5513
5514 (defun gnus-update-missing-marks (missing)
5515   "Go through the list of MISSING articles and remove them from the mark lists."
5516   (when missing
5517     (let (var m)
5518       ;; Go through all types.
5519       (dolist (elem gnus-article-mark-lists)
5520         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5521           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5522           (when (symbol-value var)
5523             ;; This list has articles.  So we delete all missing
5524             ;; articles from it.
5525             (setq m missing)
5526             (while m
5527               (set var (delq (pop m) (symbol-value var))))))))))
5528
5529 (defun gnus-update-marks ()
5530   "Enter the various lists of marked articles into the newsgroup info list."
5531   (let ((types gnus-article-mark-lists)
5532         (info (gnus-get-info gnus-newsgroup-name))
5533         type list newmarked symbol delta-marks)
5534     (when info
5535       ;; Add all marks lists to the list of marks lists.
5536       (while (setq type (pop types))
5537         (setq list (symbol-value
5538                     (setq symbol
5539                           (intern (format "gnus-newsgroup-%s" (car type))))))
5540
5541         (when list
5542           ;; Get rid of the entries of the articles that have the
5543           ;; default score.
5544           (when (and (eq (cdr type) 'score)
5545                      gnus-save-score
5546                      list)
5547             (let* ((arts list)
5548                    (prev (cons nil list))
5549                    (all prev))
5550               (while arts
5551                 (if (or (not (consp (car arts)))
5552                         (= (cdar arts) gnus-summary-default-score))
5553                     (setcdr prev (cdr arts))
5554                   (setq prev arts))
5555                 (setq arts (cdr arts)))
5556               (setq list (cdr all)))))
5557
5558         (when (eq (cdr type) 'seen)
5559           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5560
5561         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5562           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5563
5564         (when (and (gnus-check-backend-function
5565                     'request-set-mark gnus-newsgroup-name)
5566                    (not (gnus-article-unpropagatable-p (cdr type))))
5567           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5568                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5569                  (add (gnus-remove-from-range
5570                        (gnus-copy-sequence list) old)))
5571             (when add
5572               (push (list add 'add (list (cdr type))) delta-marks))
5573             (when del
5574               (push (list del 'del (list (cdr type))) delta-marks))))
5575
5576         (when list
5577           (push (cons (cdr type) list) newmarked)))
5578
5579       (when delta-marks
5580         (unless (gnus-check-group gnus-newsgroup-name)
5581           (error "Can't open server for %s" gnus-newsgroup-name))
5582         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5583
5584       ;; Enter these new marks into the info of the group.
5585       (if (nthcdr 3 info)
5586           (setcar (nthcdr 3 info) newmarked)
5587         ;; Add the marks lists to the end of the info.
5588         (when newmarked
5589           (setcdr (nthcdr 2 info) (list newmarked))))
5590
5591       ;; Cut off the end of the info if there's nothing else there.
5592       (let ((i 5))
5593         (while (and (> i 2)
5594                     (not (nth i info)))
5595           (when (nthcdr (decf i) info)
5596             (setcdr (nthcdr i info) nil)))))))
5597
5598 (defun gnus-set-mode-line (where)
5599   "Set the mode line of the article or summary buffers.
5600 If WHERE is `summary', the summary mode line format will be used."
5601   ;; Is this mode line one we keep updated?
5602   (when (and (memq where gnus-updated-mode-lines)
5603              (symbol-value
5604               (intern (format "gnus-%s-mode-line-format-spec" where))))
5605     (let (mode-string)
5606       (save-excursion
5607         ;; We evaluate this in the summary buffer since these
5608         ;; variables are buffer-local to that buffer.
5609         (set-buffer gnus-summary-buffer)
5610         ;; We bind all these variables that are used in the `eval' form
5611         ;; below.
5612         (let* ((mformat (symbol-value
5613                          (intern
5614                           (format "gnus-%s-mode-line-format-spec" where))))
5615                (gnus-tmp-group-name (gnus-group-decoded-name
5616                                      gnus-newsgroup-name))
5617                (gnus-tmp-article-number (or gnus-current-article 0))
5618                (gnus-tmp-unread gnus-newsgroup-unreads)
5619                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5620                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5621                (gnus-tmp-unread-and-unselected
5622                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5623                             (zerop gnus-tmp-unselected))
5624                        "")
5625                       ((zerop gnus-tmp-unselected)
5626                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5627                       (t (format "{%d(+%d) more}"
5628                                  gnus-tmp-unread-and-unticked
5629                                  gnus-tmp-unselected))))
5630                (gnus-tmp-subject
5631                 (if (and gnus-current-headers
5632                          (vectorp gnus-current-headers))
5633                     (gnus-mode-string-quote
5634                      (mail-header-subject gnus-current-headers))
5635                   ""))
5636                bufname-length max-len
5637                gnus-tmp-header);; passed as argument to any user-format-funcs
5638           (setq mode-string (eval mformat))
5639           (setq bufname-length (if (string-match "%b" mode-string)
5640                                    (- (length
5641                                        (buffer-name
5642                                         (if (eq where 'summary)
5643                                             nil
5644                                           (get-buffer gnus-article-buffer))))
5645                                       2)
5646                                  0))
5647           (setq max-len (max 4 (if gnus-mode-non-string-length
5648                                    (- (window-width)
5649                                       gnus-mode-non-string-length
5650                                       bufname-length)
5651                                  (length mode-string))))
5652           ;; We might have to chop a bit of the string off...
5653           (when (> (length mode-string) max-len)
5654             (setq mode-string
5655                   (concat (gnus-truncate-string mode-string (- max-len 3))
5656                           "...")))
5657           ;; Pad the mode string a bit.
5658           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5659       ;; Update the mode line.
5660       (setq mode-line-buffer-identification
5661             (gnus-mode-line-buffer-identification (list mode-string)))
5662       (set-buffer-modified-p t))))
5663
5664 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5665   "Go through the HEADERS list and add all Xrefs to a hash table.
5666 The resulting hash table is returned, or nil if no Xrefs were found."
5667   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5668          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5669          (xref-hashtb (gnus-make-hashtable))
5670          start group entry number xrefs header)
5671     (while headers
5672       (setq header (pop headers))
5673       (when (and (setq xrefs (mail-header-xref header))
5674                  (not (memq (setq number (mail-header-number header))
5675                             unreads)))
5676         (setq start 0)
5677         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5678           (setq start (match-end 0))
5679           (setq group (if prefix
5680                           (concat prefix (substring xrefs (match-beginning 1)
5681                                                     (match-end 1)))
5682                         (substring xrefs (match-beginning 1) (match-end 1))))
5683           (setq number
5684                 (string-to-int (substring xrefs (match-beginning 2)
5685                                           (match-end 2))))
5686           (if (setq entry (gnus-gethash group xref-hashtb))
5687               (setcdr entry (cons number (cdr entry)))
5688             (gnus-sethash group (cons number nil) xref-hashtb)))))
5689     (and start xref-hashtb)))
5690
5691 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5692   "Look through all the headers and mark the Xrefs as read."
5693   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5694         name info xref-hashtb idlist method nth4)
5695     (save-excursion
5696       (set-buffer gnus-group-buffer)
5697       (when (setq xref-hashtb
5698                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5699         (mapatoms
5700          (lambda (group)
5701            (unless (string= from-newsgroup (setq name (symbol-name group)))
5702              (setq idlist (symbol-value group))
5703              ;; Dead groups are not updated.
5704              (and (prog1
5705                       (setq info (gnus-get-info name))
5706                     (when (stringp (setq nth4 (gnus-info-method info)))
5707                       (setq nth4 (gnus-server-to-method nth4))))
5708                   ;; Only do the xrefs if the group has the same
5709                   ;; select method as the group we have just read.
5710                   (or (gnus-methods-equal-p
5711                        nth4 (gnus-find-method-for-group from-newsgroup))
5712                       virtual
5713                       (equal nth4 (setq method (gnus-find-method-for-group
5714                                                 from-newsgroup)))
5715                       (and (equal (car nth4) (car method))
5716                            (equal (nth 1 nth4) (nth 1 method))))
5717                   gnus-use-cross-reference
5718                   (or (not (eq gnus-use-cross-reference t))
5719                       virtual
5720                       ;; Only do cross-references on subscribed
5721                       ;; groups, if that is what is wanted.
5722                       (<= (gnus-info-level info) gnus-level-subscribed))
5723                   (gnus-group-make-articles-read name idlist))))
5724          xref-hashtb)))))
5725
5726 (defun gnus-compute-read-articles (group articles)
5727   (let* ((entry (gnus-group-entry group))
5728          (info (nth 2 entry))
5729          (active (gnus-active group))
5730          ninfo)
5731     (when entry
5732       ;; First peel off all invalid article numbers.
5733       (when active
5734         (let ((ids articles)
5735               id first)
5736           (while (setq id (pop ids))
5737             (when (and first (> id (cdr active)))
5738               ;; We'll end up in this situation in one particular
5739               ;; obscure situation.  If you re-scan a group and get
5740               ;; a new article that is cross-posted to a different
5741               ;; group that has not been re-scanned, you might get
5742               ;; crossposted article that has a higher number than
5743               ;; Gnus believes possible.  So we re-activate this
5744               ;; group as well.  This might mean doing the
5745               ;; crossposting thingy will *increase* the number
5746               ;; of articles in some groups.  Tsk, tsk.
5747               (setq active (or (gnus-activate-group group) active)))
5748             (when (or (> id (cdr active))
5749                       (< id (car active)))
5750               (setq articles (delq id articles))))))
5751       ;; If the read list is nil, we init it.
5752       (if (and active
5753                (null (gnus-info-read info))
5754                (> (car active) 1))
5755           (setq ninfo (cons 1 (1- (car active))))
5756         (setq ninfo (gnus-info-read info)))
5757       ;; Then we add the read articles to the range.
5758       (gnus-add-to-range
5759        ninfo (setq articles (sort articles '<))))))
5760
5761 (defun gnus-group-make-articles-read (group articles)
5762   "Update the info of GROUP to say that ARTICLES are read."
5763   (let* ((num 0)
5764          (entry (gnus-group-entry group))
5765          (info (nth 2 entry))
5766          (active (gnus-active group))
5767          range)
5768     (when entry
5769       (setq range (gnus-compute-read-articles group articles))
5770       (with-current-buffer gnus-group-buffer
5771         (gnus-undo-register
5772           `(progn
5773              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5774              (gnus-info-set-read ',info ',(gnus-info-read info))
5775              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5776              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5777              (gnus-group-update-group ,group t))))
5778       ;; Add the read articles to the range.
5779       (gnus-info-set-read info range)
5780       (gnus-request-set-mark group (list (list range 'add '(read))))
5781       ;; Then we have to re-compute how many unread
5782       ;; articles there are in this group.
5783       (when active
5784         (cond
5785          ((not range)
5786           (setq num (- (1+ (cdr active)) (car active))))
5787          ((not (listp (cdr range)))
5788           (setq num (- (cdr active) (- (1+ (cdr range))
5789                                        (car range)))))
5790          (t
5791           (while range
5792             (if (numberp (car range))
5793                 (setq num (1+ num))
5794               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5795             (setq range (cdr range)))
5796           (setq num (- (cdr active) num))))
5797         ;; Update the number of unread articles.
5798         (setcar entry num)
5799         ;; Update the group buffer.
5800         (unless (gnus-ephemeral-group-p group)
5801           (gnus-group-update-group group t))))))
5802
5803 (defvar gnus-newsgroup-none-id 0)
5804
5805 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5806   (let ((cur nntp-server-buffer)
5807         (dependencies
5808          (or dependencies
5809              (with-current-buffer gnus-summary-buffer
5810                gnus-newsgroup-dependencies)))
5811         headers id end ref number
5812         (mail-parse-charset gnus-newsgroup-charset)
5813         (mail-parse-ignored-charsets
5814          (save-excursion (condition-case nil
5815                              (set-buffer gnus-summary-buffer)
5816                            (error))
5817                          gnus-newsgroup-ignored-charsets)))
5818     (save-excursion
5819       (set-buffer nntp-server-buffer)
5820       ;; Translate all TAB characters into SPACE characters.
5821       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5822       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5823       (ietf-drums-unfold-fws)
5824       (gnus-run-hooks 'gnus-parse-headers-hook)
5825       (let ((case-fold-search t)
5826             in-reply-to header p lines chars ctype)
5827         (goto-char (point-min))
5828         ;; Search to the beginning of the next header.  Error messages
5829         ;; do not begin with 2 or 3.
5830         (while (re-search-forward "^[23][0-9]+ " nil t)
5831           (setq id nil
5832                 ref nil)
5833           ;; This implementation of this function, with nine
5834           ;; search-forwards instead of the one re-search-forward and
5835           ;; a case (which basically was the old function) is actually
5836           ;; about twice as fast, even though it looks messier.  You
5837           ;; can't have everything, I guess.  Speed and elegance
5838           ;; doesn't always go hand in hand.
5839           (setq
5840            header
5841            (make-full-mail-header
5842             ;; Number.
5843             (prog1
5844                 (setq number (read cur))
5845               (end-of-line)
5846               (setq p (point))
5847               (narrow-to-region (point)
5848                                 (or (and (search-forward "\n.\n" nil t)
5849                                          (- (point) 2))
5850                                     (point))))
5851             ;; Subject.
5852             (progn
5853               (goto-char p)
5854               (if (search-forward "\nsubject:" nil t)
5855                   (nnheader-header-value)
5856                 "(none)"))
5857             ;; From.
5858             (progn
5859               (goto-char p)
5860               (if (search-forward "\nfrom:" nil t)
5861                   (nnheader-header-value)
5862                 "(nobody)"))
5863             ;; Date.
5864             (progn
5865               (goto-char p)
5866               (if (search-forward "\ndate:" nil t)
5867                   (nnheader-header-value) ""))
5868             ;; Message-ID.
5869             (progn
5870               (goto-char p)
5871               (setq id (if (re-search-forward
5872                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5873                            ;; We do it this way to make sure the Message-ID
5874                            ;; is (somewhat) syntactically valid.
5875                            (buffer-substring (match-beginning 1)
5876                                              (match-end 1))
5877                          ;; If there was no message-id, we just fake one
5878                          ;; to make subsequent routines simpler.
5879                          (nnheader-generate-fake-message-id number))))
5880             ;; References.
5881             (progn
5882               (goto-char p)
5883               (if (search-forward "\nreferences:" nil t)
5884                   (progn
5885                     (setq end (point))
5886                     (prog1
5887                         (nnheader-header-value)
5888                       (setq ref
5889                             (buffer-substring
5890                              (progn
5891                                ;; (end-of-line)
5892                                (search-backward ">" end t)
5893                                (1+ (point)))
5894                              (progn
5895                                (search-backward "<" end t)
5896                                (point))))))
5897                 ;; Get the references from the in-reply-to header if there
5898                 ;; were no references and the in-reply-to header looks
5899                 ;; promising.
5900                 (if (and (search-forward "\nin-reply-to:" nil t)
5901                          (setq in-reply-to (nnheader-header-value))
5902                          (string-match "<[^>]+>" in-reply-to))
5903                     (let (ref2)
5904                       (setq ref (substring in-reply-to (match-beginning 0)
5905                                            (match-end 0)))
5906                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5907                         (setq ref2 (substring in-reply-to (match-beginning 0)
5908                                               (match-end 0)))
5909                         (when (> (length ref2) (length ref))
5910                           (setq ref ref2)))
5911                       ref)
5912                   (setq ref nil))))
5913             ;; Chars.
5914             (progn
5915               (goto-char p)
5916               (if (search-forward "\nchars: " nil t)
5917                   (if (numberp (setq chars (ignore-errors (read cur))))
5918                       chars -1)
5919                 -1))
5920             ;; Lines.
5921             (progn
5922               (goto-char p)
5923               (if (search-forward "\nlines: " nil t)
5924                   (if (numberp (setq lines (ignore-errors (read cur))))
5925                       lines -1)
5926                 -1))
5927             ;; Xref.
5928             (progn
5929               (goto-char p)
5930               (and (search-forward "\nxref:" nil t)
5931                    (nnheader-header-value)))
5932             ;; Extra.
5933             (when gnus-extra-headers
5934               (let ((extra gnus-extra-headers)
5935                     out)
5936                 (while extra
5937                   (goto-char p)
5938                   (when (search-forward
5939                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5940                     (push (cons (car extra) (nnheader-header-value)) out))
5941                   (pop extra))
5942                 out))))
5943           (goto-char p)
5944           (if (and (search-forward "\ncontent-type: " nil t)
5945                    (setq ctype (nnheader-header-value)))
5946               (mime-entity-set-content-type-internal
5947                header (mime-parse-Content-Type ctype)))
5948           (when (equal id ref)
5949             (setq ref nil))
5950
5951           (when gnus-alter-header-function
5952             (funcall gnus-alter-header-function header)
5953             (setq id (mail-header-id header)
5954                   ref (gnus-parent-id (mail-header-references header))))
5955
5956           (when (setq header
5957                       (gnus-dependencies-add-header
5958                        header dependencies force-new))
5959             (push header headers))
5960           (goto-char (point-max))
5961           (widen))
5962         (nreverse headers)))))
5963
5964 ;; Goes through the xover lines and returns a list of vectors
5965 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5966                                                   force-new dependencies
5967                                                   group also-fetch-heads)
5968   "Parse the news overview data in the server buffer.
5969 Return a list of headers that match SEQUENCE (see
5970 `nntp-retrieve-headers')."
5971   ;; Get the Xref when the users reads the articles since most/some
5972   ;; NNTP servers do not include Xrefs when using XOVER.
5973   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5974   (let ((mail-parse-charset gnus-newsgroup-charset)
5975         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5976         (cur nntp-server-buffer)
5977         (dependencies (or dependencies gnus-newsgroup-dependencies))
5978         (allp (cond
5979                ((eq gnus-read-all-available-headers t)
5980                 t)
5981                ((stringp gnus-read-all-available-headers)
5982                 (string-match gnus-read-all-available-headers group))
5983                (t
5984                 nil)))
5985         number headers header)
5986     (save-excursion
5987       (set-buffer nntp-server-buffer)
5988       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5989       ;; Allow the user to mangle the headers before parsing them.
5990       (gnus-run-hooks 'gnus-parse-headers-hook)
5991       (goto-char (point-min))
5992       (gnus-parse-without-error
5993         (while (and (or sequence allp)
5994                     (not (eobp)))
5995           (setq number (read cur))
5996           (when (not allp)
5997             (while (and sequence
5998                         (< (car sequence) number))
5999               (setq sequence (cdr sequence))))
6000           (when (and (or allp
6001                          (and sequence
6002                               (eq number (car sequence))))
6003                      (progn
6004                        (setq sequence (cdr sequence))
6005                        (setq header (inline
6006                                       (gnus-nov-parse-line
6007                                        number dependencies force-new)))))
6008             (push header headers))
6009           (forward-line 1)))
6010       ;; A common bug in inn is that if you have posted an article and
6011       ;; then retrieves the active file, it will answer correctly --
6012       ;; the new article is included.  However, a NOV entry for the
6013       ;; article may not have been generated yet, so this may fail.
6014       ;; We work around this problem by retrieving the last few
6015       ;; headers using HEAD.
6016       (if (or (not also-fetch-heads)
6017               (not sequence))
6018           ;; We (probably) got all the headers.
6019           (nreverse headers)
6020         (let ((gnus-nov-is-evil t))
6021           (nconc
6022            (nreverse headers)
6023            (when (eq (gnus-retrieve-headers sequence group) 'headers)
6024              (gnus-get-newsgroup-headers))))))))
6025
6026 (defun gnus-article-get-xrefs ()
6027   "Fill in the Xref value in `gnus-current-headers', if necessary.
6028 This is meant to be called in `gnus-article-internal-prepare-hook'."
6029   (let ((headers (with-current-buffer gnus-summary-buffer
6030                    gnus-current-headers)))
6031     (or (not gnus-use-cross-reference)
6032         (not headers)
6033         (and (mail-header-xref headers)
6034              (not (string= (mail-header-xref headers) "")))
6035         (let ((case-fold-search t)
6036               xref)
6037           (save-restriction
6038             (nnheader-narrow-to-headers)
6039             (goto-char (point-min))
6040             (when (or (and (not (eobp))
6041                            (eq (downcase (char-after)) ?x)
6042                            (looking-at "Xref:"))
6043                       (search-forward "\nXref:" nil t))
6044               (goto-char (1+ (match-end 0)))
6045               (setq xref (buffer-substring (point) (point-at-eol)))
6046               (mail-header-set-xref headers xref)))))))
6047
6048 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
6049   "Find article ID and insert the summary line for that article.
6050 OLD-HEADER can either be a header or a line number to insert
6051 the subject line on."
6052   (let* ((line (and (numberp old-header) old-header))
6053          (old-header (and (vectorp old-header) old-header))
6054          (header (cond ((and old-header use-old-header)
6055                         old-header)
6056                        ((and (numberp id)
6057                              (gnus-number-to-header id))
6058                         (gnus-number-to-header id))
6059                        (t
6060                         (gnus-read-header id))))
6061          (number (and (numberp id) id))
6062          d)
6063     (when header
6064       ;; Rebuild the thread that this article is part of and go to the
6065       ;; article we have fetched.
6066       (when (and (not gnus-show-threads)
6067                  old-header)
6068         (when (and number
6069                    (setq d (gnus-data-find (mail-header-number old-header))))
6070           (goto-char (gnus-data-pos d))
6071           (gnus-data-remove
6072            number
6073            (- (point-at-bol)
6074               (prog1
6075                   (1+ (point-at-eol))
6076                 (gnus-delete-line))))))
6077       ;; Remove list identifiers from subject.
6078       (when gnus-list-identifiers
6079         (let ((gnus-newsgroup-headers (list header)))
6080           (gnus-summary-remove-list-identifiers)))
6081       (when old-header
6082         (mail-header-set-number header (mail-header-number old-header)))
6083       (setq gnus-newsgroup-sparse
6084             (delq (setq number (mail-header-number header))
6085                   gnus-newsgroup-sparse))
6086       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6087       (push number gnus-newsgroup-limit)
6088       (gnus-rebuild-thread (mail-header-id header) line)
6089       (gnus-summary-goto-subject number nil t))
6090     (when (and (numberp number)
6091                (> number 0))
6092       ;; We have to update the boundaries even if we can't fetch the
6093       ;; article if ID is a number -- so that the next `P' or `N'
6094       ;; command will fetch the previous (or next) article even
6095       ;; if the one we tried to fetch this time has been canceled.
6096       (when (> number gnus-newsgroup-end)
6097         (setq gnus-newsgroup-end number))
6098       (when (< number gnus-newsgroup-begin)
6099         (setq gnus-newsgroup-begin number))
6100       (setq gnus-newsgroup-unselected
6101             (delq number gnus-newsgroup-unselected)))
6102     ;; Report back a success?
6103     (and header (mail-header-number header))))
6104
6105 ;;; Process/prefix in the summary buffer
6106
6107 (defun gnus-summary-work-articles (n)
6108   "Return a list of articles to be worked upon.
6109 The prefix argument, the list of process marked articles, and the
6110 current article will be taken into consideration."
6111   (save-excursion
6112     (set-buffer gnus-summary-buffer)
6113     (cond
6114      (n
6115       ;; A numerical prefix has been given.
6116       (setq n (prefix-numeric-value n))
6117       (let ((backward (< n 0))
6118             (n (abs (prefix-numeric-value n)))
6119             articles article)
6120         (save-excursion
6121           (while
6122               (and (> n 0)
6123                    (push (setq article (gnus-summary-article-number))
6124                          articles)
6125                    (if backward
6126                        (gnus-summary-find-prev nil article)
6127                      (gnus-summary-find-next nil article)))
6128             (decf n)))
6129         (nreverse articles)))
6130      ((and (gnus-region-active-p) (mark))
6131       (message "region active")
6132       ;; Work on the region between point and mark.
6133       (let ((max (max (point) (mark)))
6134             articles article)
6135         (save-excursion
6136           (goto-char (min (point) (mark)))
6137           (while
6138               (and
6139                (push (setq article (gnus-summary-article-number)) articles)
6140                (gnus-summary-find-next nil article)
6141                (< (point) max)))
6142           (nreverse articles))))
6143      (gnus-newsgroup-processable
6144       ;; There are process-marked articles present.
6145       ;; Save current state.
6146       (gnus-summary-save-process-mark)
6147       ;; Return the list.
6148       (reverse gnus-newsgroup-processable))
6149      (t
6150       ;; Just return the current article.
6151       (list (gnus-summary-article-number))))))
6152
6153 (defmacro gnus-summary-iterate (arg &rest forms)
6154   "Iterate over the process/prefixed articles and do FORMS.
6155 ARG is the interactive prefix given to the command.  FORMS will be
6156 executed with point over the summary line of the articles."
6157   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6158     `(let ((,articles (gnus-summary-work-articles ,arg)))
6159        (while ,articles
6160          (gnus-summary-goto-subject (car ,articles))
6161          ,@forms
6162          (pop ,articles)))))
6163
6164 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6165 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6166
6167 (defun gnus-summary-save-process-mark ()
6168   "Push the current set of process marked articles on the stack."
6169   (interactive)
6170   (push (copy-sequence gnus-newsgroup-processable)
6171         gnus-newsgroup-process-stack))
6172
6173 (defun gnus-summary-kill-process-mark ()
6174   "Push the current set of process marked articles on the stack and unmark."
6175   (interactive)
6176   (gnus-summary-save-process-mark)
6177   (gnus-summary-unmark-all-processable))
6178
6179 (defun gnus-summary-yank-process-mark ()
6180   "Pop the last process mark state off the stack and restore it."
6181   (interactive)
6182   (unless gnus-newsgroup-process-stack
6183     (error "Empty mark stack"))
6184   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6185
6186 (defun gnus-summary-process-mark-set (set)
6187   "Make SET into the current process marked articles."
6188   (gnus-summary-unmark-all-processable)
6189   (mapc 'gnus-summary-set-process-mark set))
6190
6191 ;;; Searching and stuff
6192
6193 (defun gnus-summary-search-group (&optional backward use-level)
6194   "Search for next unread newsgroup.
6195 If optional argument BACKWARD is non-nil, search backward instead."
6196   (save-excursion
6197     (set-buffer gnus-group-buffer)
6198     (when (gnus-group-search-forward
6199            backward nil (if use-level (gnus-group-group-level) nil))
6200       (gnus-group-group-name))))
6201
6202 (defun gnus-summary-best-group (&optional exclude-group)
6203   "Find the name of the best unread group.
6204 If EXCLUDE-GROUP, do not go to this group."
6205   (with-current-buffer gnus-group-buffer
6206     (save-excursion
6207       (gnus-group-best-unread-group exclude-group))))
6208
6209 (defun gnus-summary-find-next (&optional unread article backward)
6210   (if backward
6211       (gnus-summary-find-prev unread article)
6212     (let* ((dummy (gnus-summary-article-intangible-p))
6213            (article (or article (gnus-summary-article-number)))
6214            (data (gnus-data-find-list article))
6215            result)
6216       (when (and (not dummy)
6217                  (or (not gnus-summary-check-current)
6218                      (not unread)
6219                      (not (gnus-data-unread-p (car data)))))
6220         (setq data (cdr data)))
6221       (when (setq result
6222                   (if unread
6223                       (progn
6224                         (while data
6225                           (unless (memq (gnus-data-number (car data))
6226                                         (cond
6227                                          ((eq gnus-auto-goto-ignores
6228                                               'always-undownloaded)
6229                                           gnus-newsgroup-undownloaded)
6230                                          (gnus-plugged
6231                                           nil)
6232                                          ((eq gnus-auto-goto-ignores
6233                                               'unfetched)
6234                                           gnus-newsgroup-unfetched)
6235                                          ((eq gnus-auto-goto-ignores
6236                                               'undownloaded)
6237                                           gnus-newsgroup-undownloaded)))
6238                             (when (gnus-data-unread-p (car data))
6239                               (setq result (car data)
6240                                     data nil)))
6241                           (setq data (cdr data)))
6242                         result)
6243                     (car data)))
6244         (goto-char (gnus-data-pos result))
6245         (gnus-data-number result)))))
6246
6247 (defun gnus-summary-find-prev (&optional unread article)
6248   (let* ((eobp (eobp))
6249          (article (or article (gnus-summary-article-number)))
6250          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6251          result)
6252     (when (and (not eobp)
6253                (or (not gnus-summary-check-current)
6254                    (not unread)
6255                    (not (gnus-data-unread-p (car data)))))
6256       (setq data (cdr data)))
6257     (when (setq result
6258                 (if unread
6259                     (progn
6260                       (while data
6261                         (unless (memq (gnus-data-number (car data))
6262                                       (cond
6263                                        ((eq gnus-auto-goto-ignores
6264                                             'always-undownloaded)
6265                                         gnus-newsgroup-undownloaded)
6266                                        (gnus-plugged
6267                                         nil)
6268                                        ((eq gnus-auto-goto-ignores
6269                                             'unfetched)
6270                                         gnus-newsgroup-unfetched)
6271                                        ((eq gnus-auto-goto-ignores
6272                                             'undownloaded)
6273                                         gnus-newsgroup-undownloaded)))
6274                           (when (gnus-data-unread-p (car data))
6275                             (setq result (car data)
6276                                   data nil)))
6277                         (setq data (cdr data)))
6278                       result)
6279                   (car data)))
6280       (goto-char (gnus-data-pos result))
6281       (gnus-data-number result))))
6282
6283 (defun gnus-summary-find-subject (subject &optional unread backward article)
6284   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6285          (article (or article (gnus-summary-article-number)))
6286          (articles (gnus-data-list backward))
6287          (arts (gnus-data-find-list article articles))
6288          result)
6289     (when (or (not gnus-summary-check-current)
6290               (not unread)
6291               (not (gnus-data-unread-p (car arts))))
6292       (setq arts (cdr arts)))
6293     (while arts
6294       (and (or (not unread)
6295                (gnus-data-unread-p (car arts)))
6296            (vectorp (gnus-data-header (car arts)))
6297            (gnus-subject-equal
6298             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6299            (setq result (car arts)
6300                  arts nil))
6301       (setq arts (cdr arts)))
6302     (and result
6303          (goto-char (gnus-data-pos result))
6304          (gnus-data-number result))))
6305
6306 (defun gnus-summary-search-forward (&optional unread subject backward)
6307   "Search forward for an article.
6308 If UNREAD, look for unread articles.  If SUBJECT, look for
6309 articles with that subject.  If BACKWARD, search backward instead."
6310   (cond (subject (gnus-summary-find-subject subject unread backward))
6311         (backward (gnus-summary-find-prev unread))
6312         (t (gnus-summary-find-next unread))))
6313
6314 (defun gnus-recenter (&optional n)
6315   "Center point in window and redisplay frame.
6316 Also do horizontal recentering."
6317   (interactive "P")
6318   (when (and gnus-auto-center-summary
6319              (not (eq gnus-auto-center-summary 'vertical)))
6320     (gnus-horizontal-recenter))
6321   (recenter n))
6322
6323 (defun gnus-summary-recenter ()
6324   "Center point in the summary window.
6325 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6326 displayed, no centering will be performed."
6327   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6328   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6329   (interactive)
6330   ;; The user has to want it.
6331   (when gnus-auto-center-summary
6332     (let* ((top (cond ((< (window-height) 4) 0)
6333                       ((< (window-height) 7) 1)
6334                       (t (if (numberp gnus-auto-center-summary)
6335                              gnus-auto-center-summary
6336                            (/ (1- (window-height)) 2)))))
6337            (height (1- (window-height)))
6338            (bottom (save-excursion (goto-char (point-max))
6339                                    (forward-line (- height))
6340                                    (point)))
6341            (window (get-buffer-window (current-buffer))))
6342       (when (get-buffer-window gnus-article-buffer)
6343         ;; Only do recentering when the article buffer is displayed,
6344         ;; Set the window start to either `bottom', which is the biggest
6345         ;; possible valid number, or the second line from the top,
6346         ;; whichever is the least.
6347         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6348           (if (> bottom top-pos)
6349               ;; Keep the second line from the top visible
6350               (set-window-start window top-pos t)
6351             ;; Try to keep the bottom line visible; if it's partially
6352             ;; obscured, either scroll one more line to make it fully
6353             ;; visible, or revert to using TOP-POS.
6354             (save-excursion
6355               (goto-char (point-max))
6356               (forward-line -1)
6357               (let ((last-line-start (point)))
6358                 (goto-char bottom)
6359                 (set-window-start window (point) t)
6360                 (when (not (pos-visible-in-window-p last-line-start window))
6361                   (forward-line 1)
6362                   (set-window-start window (min (point) top-pos) t)))))))
6363       ;; Do horizontal recentering while we're at it.
6364       (when (and (get-buffer-window (current-buffer) t)
6365                  (not (eq gnus-auto-center-summary 'vertical)))
6366         (let ((selected (selected-window)))
6367           (select-window (get-buffer-window (current-buffer) t))
6368           (gnus-summary-position-point)
6369           (gnus-horizontal-recenter)
6370           (select-window selected))))))
6371
6372 (defun gnus-summary-jump-to-group (newsgroup)
6373   "Move point to NEWSGROUP in group mode buffer."
6374   ;; Keep update point of group mode buffer if visible.
6375   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6376       (save-window-excursion
6377         ;; Take care of tree window mode.
6378         (when (get-buffer-window gnus-group-buffer)
6379           (pop-to-buffer gnus-group-buffer))
6380         (gnus-group-jump-to-group newsgroup))
6381     (save-excursion
6382       ;; Take care of tree window mode.
6383       (if (get-buffer-window gnus-group-buffer)
6384           (pop-to-buffer gnus-group-buffer)
6385         (set-buffer gnus-group-buffer))
6386       (gnus-group-jump-to-group newsgroup))))
6387
6388 ;; This function returns a list of article numbers based on the
6389 ;; difference between the ranges of read articles in this group and
6390 ;; the range of active articles.
6391 (defun gnus-list-of-unread-articles (group)
6392   (let* ((read (gnus-info-read (gnus-get-info group)))
6393          (active (or (gnus-active group) (gnus-activate-group group)))
6394          (last (cdr active))
6395          first nlast unread)
6396     ;; If none are read, then all are unread.
6397     (if (not read)
6398         (setq first (car active))
6399       ;; If the range of read articles is a single range, then the
6400       ;; first unread article is the article after the last read
6401       ;; article.  Sounds logical, doesn't it?
6402       (if (and (not (listp (cdr read)))
6403                (or (< (car read) (car active))
6404                    (progn (setq read (list read))
6405                           nil)))
6406           (setq first (max (car active) (1+ (cdr read))))
6407         ;; `read' is a list of ranges.
6408         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6409                                   (caar read)))
6410                   1)
6411           (setq first (car active)))
6412         (while read
6413           (when first
6414             (while (< first nlast)
6415               (setq unread (cons first unread)
6416                     first (1+ first))))
6417           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6418           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6419           (setq read (cdr read)))))
6420     ;; And add the last unread articles.
6421     (while (<= first last)
6422       (setq unread (cons first unread)
6423             first (1+ first)))
6424     ;; Return the list of unread articles.
6425     (delq 0 (nreverse unread))))
6426
6427 (defun gnus-list-of-read-articles (group)
6428   "Return a list of unread, unticked and non-dormant articles."
6429   (let* ((info (gnus-get-info group))
6430          (marked (gnus-info-marks info))
6431          (active (gnus-active group)))
6432     (and info active
6433          (gnus-list-range-difference
6434           (gnus-list-range-difference
6435            (gnus-sorted-complement
6436             (gnus-uncompress-range active)
6437             (gnus-list-of-unread-articles group))
6438            (cdr (assq 'dormant marked)))
6439           (cdr (assq 'tick marked))))))
6440
6441 ;; This function returns a sequence of article numbers based on the
6442 ;; difference between the ranges of read articles in this group and
6443 ;; the range of active articles.
6444 (defun gnus-sequence-of-unread-articles (group)
6445   (let* ((read (gnus-info-read (gnus-get-info group)))
6446          (active (or (gnus-active group) (gnus-activate-group group)))
6447          (last (cdr active))
6448          first nlast unread)
6449     ;; If none are read, then all are unread.
6450     (if (not read)
6451         (setq first (car active))
6452       ;; If the range of read articles is a single range, then the
6453       ;; first unread article is the article after the last read
6454       ;; article.  Sounds logical, doesn't it?
6455       (if (and (not (listp (cdr read)))
6456                (or (< (car read) (car active))
6457                    (progn (setq read (list read))
6458                           nil)))
6459           (setq first (max (car active) (1+ (cdr read))))
6460         ;; `read' is a list of ranges.
6461         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6462                                   (caar read)))
6463                   1)
6464           (setq first (car active)))
6465         (while read
6466           (when first
6467             (push (cons first nlast) unread))
6468           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6469           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6470           (setq read (cdr read)))))
6471     ;; And add the last unread articles.
6472     (cond ((< first last)
6473            (push (cons first last) unread))
6474           ((= first last)
6475            (push first unread)))
6476     ;; Return the sequence of unread articles.
6477     (delq 0 (nreverse unread))))
6478
6479 ;; Various summary commands
6480
6481 (defun gnus-summary-select-article-buffer ()
6482   "Reconfigure windows to show article buffer."
6483   (interactive)
6484   (if (not (gnus-buffer-live-p gnus-article-buffer))
6485       (error "There is no article buffer for this summary buffer")
6486     (gnus-configure-windows 'article)
6487     (select-window (get-buffer-window gnus-article-buffer))))
6488
6489 (defun gnus-summary-universal-argument (arg)
6490   "Perform any operation on all articles that are process/prefixed."
6491   (interactive "P")
6492   (let ((articles (gnus-summary-work-articles arg))
6493         func article)
6494     (if (eq
6495          (setq
6496           func
6497           (key-binding
6498            (read-key-sequence
6499             (substitute-command-keys
6500              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6501          'undefined)
6502         (gnus-error 1 "Undefined key")
6503       (save-excursion
6504         (while articles
6505           (gnus-summary-goto-subject (setq article (pop articles)))
6506           (let (gnus-newsgroup-processable)
6507             (command-execute func))
6508           (gnus-summary-remove-process-mark article)))))
6509   (gnus-summary-position-point))
6510
6511 (defun gnus-summary-toggle-truncation (&optional arg)
6512   "Toggle truncation of summary lines.
6513 With ARG, turn line truncation on if ARG is positive."
6514   (interactive "P")
6515   (setq truncate-lines
6516         (if (null arg) (not truncate-lines)
6517           (> (prefix-numeric-value arg) 0)))
6518   (redraw-display))
6519
6520 (defun gnus-summary-find-for-reselect ()
6521   "Return the number of an article to stay on across a reselect.
6522 The current article is considered, then following articles, then previous
6523 articles.  An article is sought which is not cancelled and isn't a temporary
6524 insertion from another group.  If there's no such then return a dummy 0."
6525   (let (found)
6526     (dolist (rev '(nil t))
6527       (unless found      ; don't demand the reverse list if we don't need it
6528         (let ((data (gnus-data-find-list
6529                      (gnus-summary-article-number) (gnus-data-list rev))))
6530           (while (and data (not found))
6531             (if (and (< 0 (gnus-data-number (car data)))
6532                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6533                 (setq found (gnus-data-number (car data))))
6534             (setq data (cdr data))))))
6535     (or found 0)))
6536
6537 (defun gnus-summary-reselect-current-group (&optional all rescan)
6538   "Exit and then reselect the current newsgroup.
6539 The prefix argument ALL means to select all articles."
6540   (interactive "P")
6541   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6542     (error "Ephemeral groups can't be reselected"))
6543   (let ((current-subject (gnus-summary-find-for-reselect))
6544         (group gnus-newsgroup-name))
6545     (setq gnus-newsgroup-begin nil)
6546     (gnus-summary-exit nil 'leave-hidden)
6547     ;; We have to adjust the point of group mode buffer because
6548     ;; point was moved to the next unread newsgroup by exiting.
6549     (gnus-summary-jump-to-group group)
6550     (when rescan
6551       (save-excursion
6552         (save-window-excursion
6553           ;; Don't show group contents.
6554           (set-window-start (selected-window) (point-max))
6555           (gnus-group-get-new-news-this-group 1))))
6556     (gnus-group-read-group all t)
6557     (gnus-summary-goto-subject current-subject nil t)))
6558
6559 (defun gnus-summary-rescan-group (&optional all)
6560   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6561   (interactive "P")
6562   (gnus-summary-reselect-current-group all t))
6563
6564 (defun gnus-summary-update-info (&optional non-destructive)
6565   (save-excursion
6566     (let ((group gnus-newsgroup-name))
6567       (when group
6568         (when gnus-newsgroup-kill-headers
6569           (setq gnus-newsgroup-killed
6570                 (gnus-compress-sequence
6571                  (gnus-sorted-union
6572                   (gnus-list-range-intersection
6573                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6574                   gnus-newsgroup-unreads)
6575                  t)))
6576         (unless (listp (cdr gnus-newsgroup-killed))
6577           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6578         (let ((headers gnus-newsgroup-headers))
6579           ;; Set the new ranges of read articles.
6580           (with-current-buffer gnus-group-buffer
6581             (gnus-undo-force-boundary))
6582           (gnus-update-read-articles
6583            group (gnus-sorted-union
6584                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6585           ;; Set the current article marks.
6586           (let ((gnus-newsgroup-scored
6587                  (if (and (not gnus-save-score)
6588                           (not non-destructive))
6589                      nil
6590                    gnus-newsgroup-scored)))
6591             (save-excursion
6592               (gnus-update-marks)))
6593           ;; Do the cross-ref thing.
6594           (when gnus-use-cross-reference
6595             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6596           ;; Do not switch windows but change the buffer to work.
6597           (set-buffer gnus-group-buffer)
6598           (unless (gnus-ephemeral-group-p group)
6599             (gnus-group-update-group group)))))))
6600
6601 (defun gnus-summary-save-newsrc (&optional force)
6602   "Save the current number of read/marked articles in the dribble buffer.
6603 The dribble buffer will then be saved.
6604 If FORCE (the prefix), also save the .newsrc file(s)."
6605   (interactive "P")
6606   (gnus-summary-update-info t)
6607   (if force
6608       (gnus-save-newsrc-file)
6609     (gnus-dribble-save)))
6610
6611 (defun gnus-summary-exit (&optional temporary leave-hidden)
6612   "Exit reading current newsgroup, and then return to group selection mode.
6613 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6614   (interactive)
6615   (gnus-set-global-variables)
6616   (gnus-kill-save-kill-buffer)
6617   (gnus-async-halt-prefetch)
6618   (let* ((group gnus-newsgroup-name)
6619          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6620          (gnus-group-is-exiting-p t)
6621          (mode major-mode)
6622          (group-point nil)
6623          (buf (current-buffer)))
6624     (unless quit-config
6625       ;; Do adaptive scoring, and possibly save score files.
6626       (when gnus-newsgroup-adaptive
6627         (gnus-score-adaptive))
6628       (when gnus-use-scoring
6629         (gnus-score-save)))
6630     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6631     ;; If we have several article buffers, we kill them at exit.
6632     (unless gnus-single-article-buffer
6633       (gnus-kill-buffer gnus-original-article-buffer)
6634       (setq gnus-article-current nil))
6635     (when gnus-use-cache
6636       (gnus-cache-possibly-remove-articles)
6637       (gnus-cache-save-buffers))
6638     (gnus-async-prefetch-remove-group group)
6639     (when gnus-suppress-duplicates
6640       (gnus-dup-enter-articles))
6641     (when gnus-use-trees
6642       (gnus-tree-close group))
6643     (when gnus-use-cache
6644       (gnus-cache-write-active))
6645     ;; Remove entries for this group.
6646     (nnmail-purge-split-history (gnus-group-real-name group))
6647     ;; Make all changes in this group permanent.
6648     (unless quit-config
6649       (gnus-run-hooks 'gnus-exit-group-hook)
6650       (gnus-summary-update-info))
6651     (gnus-close-group group)
6652     ;; Make sure where we were, and go to next newsgroup.
6653     (set-buffer gnus-group-buffer)
6654     (unless quit-config
6655       (gnus-group-jump-to-group group))
6656     (gnus-run-hooks 'gnus-summary-exit-hook)
6657     (unless (or quit-config
6658                 ;; If this group has disappeared from the summary
6659                 ;; buffer, don't skip forwards.
6660                 (not (string= group (gnus-group-group-name))))
6661       (gnus-group-next-unread-group 1))
6662     (setq group-point (point))
6663     (if temporary
6664         nil                             ;Nothing to do.
6665       ;; If we have several article buffers, we kill them at exit.
6666       (unless gnus-single-article-buffer
6667         (gnus-kill-buffer gnus-article-buffer)
6668         (gnus-kill-buffer gnus-original-article-buffer)
6669         (setq gnus-article-current nil))
6670       (set-buffer buf)
6671       (if (not gnus-kill-summary-on-exit)
6672           (progn
6673             (gnus-deaden-summary)
6674             (setq mode nil))
6675         ;; We set all buffer-local variables to nil.  It is unclear why
6676         ;; this is needed, but if we don't, buffer-local variables are
6677         ;; not garbage-collected, it seems.  This would the lead to en
6678         ;; ever-growing Emacs.
6679         (gnus-summary-clear-local-variables)
6680         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6681           (gnus-summary-clear-local-variables))
6682         (when (get-buffer gnus-article-buffer)
6683           (bury-buffer gnus-article-buffer))
6684         ;; We clear the global counterparts of the buffer-local
6685         ;; variables as well, just to be on the safe side.
6686         (set-buffer gnus-group-buffer)
6687         (gnus-summary-clear-local-variables)
6688         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6689           (gnus-summary-clear-local-variables))
6690         ;; Return to group mode buffer.
6691         (when (eq mode 'gnus-summary-mode)
6692           (gnus-kill-buffer buf)))
6693       (setq gnus-current-select-method gnus-select-method)
6694       (if leave-hidden
6695           (set-buffer gnus-group-buffer)
6696         (pop-to-buffer gnus-group-buffer))
6697       (if (not quit-config)
6698           (progn
6699             (goto-char group-point)
6700             (unless leave-hidden
6701               (gnus-configure-windows 'group 'force))
6702             (unless (pos-visible-in-window-p)
6703               (forward-line (/ (static-if (featurep 'xemacs)
6704                                    (window-displayed-height)
6705                                  (1- (window-height)))
6706                                -2))
6707               (set-window-start (selected-window) (point))
6708               (goto-char group-point)))
6709         (gnus-handle-ephemeral-exit quit-config))
6710       ;; Clear the current group name.
6711       (unless quit-config
6712         (setq gnus-newsgroup-name nil)))))
6713
6714 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6715 (defun gnus-summary-exit-no-update (&optional no-questions)
6716   "Quit reading current newsgroup without updating read article info."
6717   (interactive)
6718   (let* ((group gnus-newsgroup-name)
6719          (gnus-group-is-exiting-p t)
6720          (gnus-group-is-exiting-without-update-p t)
6721          (quit-config (gnus-group-quit-config group)))
6722     (when (or no-questions
6723               gnus-expert-user
6724               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6725       (gnus-async-halt-prefetch)
6726       (run-hooks 'gnus-summary-prepare-exit-hook)
6727       ;; If we have several article buffers, we kill them at exit.
6728       (unless gnus-single-article-buffer
6729         (gnus-kill-buffer gnus-article-buffer)
6730         (gnus-kill-buffer gnus-original-article-buffer)
6731         (setq gnus-article-current nil))
6732       (if (not gnus-kill-summary-on-exit)
6733           (gnus-deaden-summary)
6734         (gnus-close-group group)
6735         (gnus-summary-clear-local-variables)
6736         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6737           (gnus-summary-clear-local-variables))
6738         (set-buffer gnus-group-buffer)
6739         (gnus-summary-clear-local-variables)
6740         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6741           (gnus-summary-clear-local-variables))
6742         (gnus-kill-buffer gnus-summary-buffer))
6743       (unless gnus-single-article-buffer
6744         (setq gnus-article-current nil))
6745       (when gnus-use-trees
6746         (gnus-tree-close group))
6747       (gnus-async-prefetch-remove-group group)
6748       (when (get-buffer gnus-article-buffer)
6749         (bury-buffer gnus-article-buffer))
6750       ;; Return to the group buffer.
6751       (gnus-configure-windows 'group 'force)
6752       ;; Clear the current group name.
6753       (setq gnus-newsgroup-name nil)
6754       (unless (gnus-ephemeral-group-p group)
6755         (gnus-group-update-group group))
6756       (when (equal (gnus-group-group-name) group)
6757         (gnus-group-next-unread-group 1))
6758       (when quit-config
6759         (gnus-handle-ephemeral-exit quit-config)))))
6760
6761 (defun gnus-handle-ephemeral-exit (quit-config)
6762   "Handle movement when leaving an ephemeral group.
6763 The state which existed when entering the ephemeral is reset."
6764   (if (not (buffer-name (car quit-config)))
6765       (gnus-configure-windows 'group 'force)
6766     (set-buffer (car quit-config))
6767     (cond ((eq major-mode 'gnus-summary-mode)
6768            (gnus-set-global-variables))
6769           ((eq major-mode 'gnus-article-mode)
6770            (save-excursion
6771              ;; The `gnus-summary-buffer' variable may point
6772              ;; to the old summary buffer when using a single
6773              ;; article buffer.
6774              (unless (gnus-buffer-live-p gnus-summary-buffer)
6775                (set-buffer gnus-group-buffer))
6776              (set-buffer gnus-summary-buffer)
6777              (gnus-set-global-variables))))
6778     (if (or (eq (cdr quit-config) 'article)
6779             (eq (cdr quit-config) 'pick))
6780         (progn
6781           ;; The current article may be from the ephemeral group
6782           ;; thus it is best that we reload this article
6783           ;;
6784           ;; If we're exiting from a large digest, this can be
6785           ;; extremely slow.  So, it's better not to reload it. -- jh.
6786           ;;(gnus-summary-show-article)
6787           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6788               (gnus-configure-windows 'pick 'force)
6789             (gnus-configure-windows (cdr quit-config) 'force)))
6790       (gnus-configure-windows (cdr quit-config) 'force))
6791     (when (eq major-mode 'gnus-summary-mode)
6792       (gnus-summary-next-subject 1 nil t)
6793       (gnus-summary-recenter)
6794       (gnus-summary-position-point))))
6795
6796 (defun gnus-summary-preview-mime-message ()
6797   "MIME decode and play this message."
6798   (interactive)
6799   (let ((gnus-break-pages nil)
6800         (gnus-show-mime t))
6801     (gnus-summary-select-article gnus-show-all-headers t))
6802   (let ((w (get-buffer-window gnus-article-buffer)))
6803     (when w
6804       (select-window (get-buffer-window gnus-article-buffer)))))
6805
6806 ;;; Dead summaries.
6807
6808 (defvar gnus-dead-summary-mode-map nil)
6809
6810 (unless gnus-dead-summary-mode-map
6811   (setq gnus-dead-summary-mode-map (make-keymap))
6812   (suppress-keymap gnus-dead-summary-mode-map)
6813   (substitute-key-definition
6814    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6815   (dolist (key '("\C-d" "\r" "\177" [delete]))
6816     (define-key gnus-dead-summary-mode-map
6817       key 'gnus-summary-wake-up-the-dead))
6818   (dolist (key '("q" "Q"))
6819     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6820
6821 (defvar gnus-dead-summary-mode nil
6822   "Minor mode for Gnus summary buffers.")
6823
6824 (defun gnus-dead-summary-mode (&optional arg)
6825   "Minor mode for Gnus summary buffers."
6826   (interactive "P")
6827   (when (eq major-mode 'gnus-summary-mode)
6828     (make-local-variable 'gnus-dead-summary-mode)
6829     (setq gnus-dead-summary-mode
6830           (if (null arg) (not gnus-dead-summary-mode)
6831             (> (prefix-numeric-value arg) 0)))
6832     (when gnus-dead-summary-mode
6833       (add-minor-mode
6834        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6835
6836 (defun gnus-deaden-summary ()
6837   "Make the current summary buffer into a dead summary buffer."
6838   ;; Kill any previous dead summary buffer.
6839   (when (and gnus-dead-summary
6840              (buffer-name gnus-dead-summary))
6841     (with-current-buffer gnus-dead-summary
6842       (when gnus-dead-summary-mode
6843         (kill-buffer (current-buffer)))))
6844   ;; Make this the current dead summary.
6845   (setq gnus-dead-summary (current-buffer))
6846   (gnus-dead-summary-mode 1)
6847   (let ((name (buffer-name)))
6848     (when (string-match "Summary" name)
6849       (rename-buffer
6850        (concat (substring name 0 (match-beginning 0)) "Dead "
6851                (substring name (match-beginning 0)))
6852        t)
6853       (bury-buffer))))
6854
6855 (defun gnus-kill-or-deaden-summary (buffer)
6856   "Kill or deaden the summary BUFFER."
6857   (save-excursion
6858     (when (and (buffer-name buffer)
6859                (not gnus-single-article-buffer))
6860       (with-current-buffer buffer
6861         (gnus-kill-buffer gnus-article-buffer)
6862         (gnus-kill-buffer gnus-original-article-buffer)))
6863     (cond
6864      ;; Kill the buffer.
6865      (gnus-kill-summary-on-exit
6866       (when (and gnus-use-trees
6867                  (gnus-buffer-exists-p buffer))
6868         (save-excursion
6869           (set-buffer buffer)
6870           (gnus-tree-close gnus-newsgroup-name)))
6871       (gnus-kill-buffer buffer))
6872      ;; Deaden the buffer.
6873      ((gnus-buffer-exists-p buffer)
6874       (save-excursion
6875         (set-buffer buffer)
6876         (gnus-deaden-summary))))))
6877
6878 (defun gnus-summary-wake-up-the-dead (&rest args)
6879   "Wake up the dead summary buffer."
6880   (interactive)
6881   (gnus-dead-summary-mode -1)
6882   (let ((name (buffer-name)))
6883     (when (string-match "Dead " name)
6884       (rename-buffer
6885        (concat (substring name 0 (match-beginning 0))
6886                (substring name (match-end 0)))
6887        t)))
6888   (gnus-message 3 "This dead summary is now alive again"))
6889
6890 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6891 (defun gnus-summary-fetch-faq (&optional faq-dir)
6892   "Fetch the FAQ for the current group.
6893 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6894 in."
6895   (interactive
6896    (list
6897     (when current-prefix-arg
6898       (completing-read
6899        "FAQ dir: " (and (listp gnus-group-faq-directory)
6900                         (mapcar 'list
6901                                 gnus-group-faq-directory))))))
6902   (let (gnus-faq-buffer)
6903     (when (setq gnus-faq-buffer
6904                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6905       (gnus-configure-windows 'summary-faq))))
6906
6907 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6908 (defun gnus-summary-describe-group (&optional force)
6909   "Describe the current newsgroup."
6910   (interactive "P")
6911   (gnus-group-describe-group force gnus-newsgroup-name))
6912
6913 (defun gnus-summary-describe-briefly ()
6914   "Describe summary mode commands briefly."
6915   (interactive)
6916   (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")))
6917
6918 ;; Walking around group mode buffer from summary mode.
6919
6920 (defun gnus-summary-next-group (&optional no-article target-group backward)
6921   "Exit current newsgroup and then select next unread newsgroup.
6922 If prefix argument NO-ARTICLE is non-nil, no article is selected
6923 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6924 previous group instead."
6925   (interactive "P")
6926   ;; Stop pre-fetching.
6927   (gnus-async-halt-prefetch)
6928   (let ((current-group gnus-newsgroup-name)
6929         (current-buffer (current-buffer))
6930         entered)
6931     ;; First we semi-exit this group to update Xrefs and all variables.
6932     ;; We can't do a real exit, because the window conf must remain
6933     ;; the same in case the user is prompted for info, and we don't
6934     ;; want the window conf to change before that...
6935     (gnus-summary-exit t)
6936     (while (not entered)
6937       ;; Then we find what group we are supposed to enter.
6938       (set-buffer gnus-group-buffer)
6939       (gnus-group-jump-to-group current-group)
6940       (setq target-group
6941             (or target-group
6942                 (if (eq gnus-keep-same-level 'best)
6943                     (gnus-summary-best-group gnus-newsgroup-name)
6944                   (gnus-summary-search-group backward gnus-keep-same-level))))
6945       (if (not target-group)
6946           ;; There are no further groups, so we return to the group
6947           ;; buffer.
6948           (progn
6949             (gnus-message 5 "Returning to the group buffer")
6950             (setq entered t)
6951             (when (gnus-buffer-live-p current-buffer)
6952               (set-buffer current-buffer)
6953               (gnus-summary-exit))
6954             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6955         ;; We try to enter the target group.
6956         (gnus-group-jump-to-group target-group)
6957         (let ((unreads (gnus-group-group-unread)))
6958           (if (and (or (eq t unreads)
6959                        (and unreads (not (zerop unreads))))
6960                    (gnus-summary-read-group
6961                     target-group nil no-article
6962                     (and (buffer-name current-buffer) current-buffer)
6963                     nil backward))
6964               (setq entered t)
6965             (setq current-group target-group
6966                   target-group nil)))))))
6967
6968 (defun gnus-summary-prev-group (&optional no-article)
6969   "Exit current newsgroup and then select previous unread newsgroup.
6970 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6971   (interactive "P")
6972   (gnus-summary-next-group no-article nil t))
6973
6974 ;; Walking around summary lines.
6975
6976 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6977   "Go to the first subject satisfying any non-nil constraint.
6978 If UNREAD is non-nil, the article should be unread.
6979 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6980 If UNSEEN is non-nil, the article should be unseen.
6981 Returns the article selected or nil if there are no matching articles."
6982   (interactive "P")
6983   (cond
6984    ;; Empty summary.
6985    ((null gnus-newsgroup-data)
6986     (gnus-message 3 "No articles in the group")
6987     nil)
6988    ;; Pick the first article.
6989    ((not (or unread undownloaded unseen))
6990     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6991     (gnus-data-number (car gnus-newsgroup-data)))
6992    ;; Find the first unread article.
6993    (t
6994     (let ((data gnus-newsgroup-data))
6995       (while (and data
6996                   (let ((num (gnus-data-number (car data))))
6997                     (or (memq num gnus-newsgroup-unfetched)
6998                         (not (or (and unread
6999                                       (memq num gnus-newsgroup-unreads))
7000                                  (and undownloaded
7001                                       (memq num gnus-newsgroup-undownloaded))
7002                                  (and unseen
7003                                       (memq num gnus-newsgroup-unseen)))))))
7004         (setq data (cdr data)))
7005       (prog1
7006           (if data
7007               (progn
7008                 (goto-char (gnus-data-pos (car data)))
7009                 (gnus-data-number (car data)))
7010             (gnus-message 3 "No more%s articles"
7011                           (let* ((r (when unread " unread"))
7012                                  (d (when undownloaded " undownloaded"))
7013                                  (s (when unseen " unseen"))
7014                                  (l (delq nil (list r d s))))
7015                             (cond ((= 3 (length l))
7016                                    (concat r "," d ", or" s))
7017                                   ((= 2 (length l))
7018                                    (concat (car l) ", or" (cadr l)))
7019                                   ((= 1 (length l))
7020                                    (car l))
7021                                   (t
7022                                    ""))))
7023             nil
7024             )
7025         (gnus-summary-position-point))))))
7026
7027 (defun gnus-summary-next-subject (n &optional unread dont-display)
7028   "Go to next N'th summary line.
7029 If N is negative, go to the previous N'th subject line.
7030 If UNREAD is non-nil, only unread articles are selected.
7031 The difference between N and the actual number of steps taken is
7032 returned."
7033   (interactive "p")
7034   (let ((backward (< n 0))
7035         (n (abs n)))
7036     (while (and (> n 0)
7037                 (if backward
7038                     (gnus-summary-find-prev unread)
7039                   (gnus-summary-find-next unread)))
7040       (unless (zerop (setq n (1- n)))
7041         (gnus-summary-show-thread)))
7042     (when (/= 0 n)
7043       (gnus-message 7 "No more%s articles"
7044                     (if unread " unread" "")))
7045     (unless dont-display
7046       (gnus-summary-recenter)
7047       (gnus-summary-position-point))
7048     n))
7049
7050 (defun gnus-summary-next-unread-subject (n)
7051   "Go to next N'th unread summary line."
7052   (interactive "p")
7053   (gnus-summary-next-subject n t))
7054
7055 (defun gnus-summary-prev-subject (n &optional unread)
7056   "Go to previous N'th summary line.
7057 If optional argument UNREAD is non-nil, only unread article is selected."
7058   (interactive "p")
7059   (gnus-summary-next-subject (- n) unread))
7060
7061 (defun gnus-summary-prev-unread-subject (n)
7062   "Go to previous N'th unread summary line."
7063   (interactive "p")
7064   (gnus-summary-next-subject (- n) t))
7065
7066 (defun gnus-summary-goto-subjects (articles)
7067   "Insert the subject header for ARTICLES in the current buffer."
7068   (save-excursion
7069     (dolist (article articles)
7070       (gnus-summary-goto-subject article t)))
7071   (gnus-summary-limit (append articles gnus-newsgroup-limit))
7072   (gnus-summary-position-point))
7073  
7074 (defun gnus-summary-goto-subject (article &optional force silent)
7075   "Go the subject line of ARTICLE.
7076 If FORCE, also allow jumping to articles not currently shown."
7077   (interactive "nArticle number: ")
7078   (unless (numberp article)
7079     (error "Article %s is not a number" article))
7080   (let ((b (point))
7081         (data (gnus-data-find article)))
7082     ;; We read in the article if we have to.
7083     (and (not data)
7084          force
7085          (gnus-summary-insert-subject
7086           article
7087           (if (or (numberp force) (vectorp force)) force)
7088           t)
7089          (setq data (gnus-data-find article)))
7090     (goto-char b)
7091     (if (not data)
7092         (progn
7093           (unless silent
7094             (gnus-message 3 "Can't find article %d" article))
7095           nil)
7096       (let ((pt (gnus-data-pos data)))
7097         (goto-char pt)
7098         (gnus-summary-set-article-display-arrow pt))
7099       (gnus-summary-position-point)
7100       article)))
7101
7102 ;; Walking around summary lines with displaying articles.
7103
7104 (defun gnus-summary-expand-window (&optional arg)
7105   "Make the summary buffer take up the entire Emacs frame.
7106 Given a prefix, will force an `article' buffer configuration."
7107   (interactive "P")
7108   (if arg
7109       (gnus-configure-windows 'article 'force)
7110     (gnus-configure-windows 'summary 'force)))
7111
7112 (defun gnus-summary-display-article (article &optional all-header)
7113   "Display ARTICLE in article buffer."
7114   (when (gnus-buffer-live-p gnus-article-buffer)
7115     (with-current-buffer gnus-article-buffer
7116       (set-buffer-multibyte t)))
7117   (gnus-set-global-variables)
7118   (when (gnus-buffer-live-p gnus-article-buffer)
7119     (with-current-buffer gnus-article-buffer
7120       (setq gnus-article-charset gnus-newsgroup-charset)
7121       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7122       (set-buffer-multibyte t)))
7123   (if (null article)
7124       nil
7125     (prog1
7126         (if gnus-summary-display-article-function
7127             (funcall gnus-summary-display-article-function article all-header)
7128           (gnus-article-prepare article all-header))
7129       (with-current-buffer gnus-article-buffer
7130         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7131              nil))
7132       (gnus-run-hooks 'gnus-select-article-hook)
7133       (when (and gnus-current-article
7134                  (not (zerop gnus-current-article)))
7135         (gnus-summary-goto-subject gnus-current-article))
7136       (gnus-summary-recenter)
7137       (when (and gnus-use-trees gnus-show-threads)
7138         (gnus-possibly-generate-tree article)
7139         (gnus-highlight-selected-tree article))
7140       ;; Successfully display article.
7141       (gnus-article-set-window-start
7142        (cdr (assq article gnus-newsgroup-bookmarks))))))
7143
7144 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7145   "Select the current article.
7146 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7147 non-nil, the article will be re-fetched even if it already present in
7148 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7149 be displayed."
7150   ;; Make sure we are in the summary buffer to work around bbdb bug.
7151   (unless (eq major-mode 'gnus-summary-mode)
7152     (set-buffer gnus-summary-buffer))
7153   (let ((article (or article (gnus-summary-article-number)))
7154         (all-headers (not (not all-headers))) ;Must be T or NIL.
7155         gnus-summary-display-article-function)
7156     (and (not pseudo)
7157          (gnus-summary-article-pseudo-p article)
7158          (error "This is a pseudo-article"))
7159     (save-excursion
7160       (set-buffer gnus-summary-buffer)
7161       (if (or (and gnus-single-article-buffer
7162                    (or (null gnus-current-article)
7163                        (null gnus-article-current)
7164                        (null (get-buffer gnus-article-buffer))
7165                        (not (eq article (cdr gnus-article-current)))
7166                        (not (equal (car gnus-article-current)
7167                                    gnus-newsgroup-name))))
7168               (and (not gnus-single-article-buffer)
7169                    (or (null gnus-current-article)
7170                        (not (eq gnus-current-article article))))
7171               force)
7172           ;; The requested article is different from the current article.
7173           (progn
7174             (gnus-summary-display-article article all-headers)
7175             (gnus-article-set-window-start
7176              (cdr (assq article gnus-newsgroup-bookmarks)))
7177             article)
7178         'old))))
7179
7180 (defun gnus-summary-force-verify-and-decrypt ()
7181   "Display buttons for signed/encrypted parts and verify/decrypt them."
7182   (interactive)
7183   (let ((mm-verify-option 'known)
7184         (mm-decrypt-option 'known)
7185         (gnus-article-emulate-mime t)
7186         (gnus-buttonized-mime-types (append (list "multipart/signed"
7187                                                   "multipart/encrypted")
7188                                             gnus-buttonized-mime-types)))
7189     (gnus-summary-select-article nil 'force)))
7190
7191 (defun gnus-summary-set-current-mark (&optional current-mark)
7192   "Obsolete function."
7193   nil)
7194
7195 (defun gnus-summary-next-article (&optional unread subject backward push)
7196   "Select the next article.
7197 If UNREAD, only unread articles are selected.
7198 If SUBJECT, only articles with SUBJECT are selected.
7199 If BACKWARD, the previous article is selected instead of the next."
7200   (interactive "P")
7201   (cond
7202    ;; Is there such an article?
7203    ((and (gnus-summary-search-forward unread subject backward)
7204          (or (gnus-summary-display-article (gnus-summary-article-number))
7205              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7206     (gnus-summary-position-point))
7207    ;; If not, we try the first unread, if that is wanted.
7208    ((and subject
7209          gnus-auto-select-same
7210          (gnus-summary-first-unread-article))
7211     (gnus-summary-position-point)
7212     (gnus-message 6 "Wrapped"))
7213    ;; Try to get next/previous article not displayed in this group.
7214    ((and gnus-auto-extend-newsgroup
7215          (not unread) (not subject))
7216     (gnus-summary-goto-article
7217      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7218      nil (count-lines (point-min) (point))))
7219    ;; Go to next/previous group.
7220    (t
7221     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7222       (gnus-summary-jump-to-group gnus-newsgroup-name))
7223     (let ((cmd last-command-char)
7224           (point
7225            (with-current-buffer gnus-group-buffer
7226              (point)))
7227           (group
7228            (if (eq gnus-keep-same-level 'best)
7229                (gnus-summary-best-group gnus-newsgroup-name)
7230              (gnus-summary-search-group backward gnus-keep-same-level))))
7231       ;; For some reason, the group window gets selected.  We change
7232       ;; it back.
7233       (select-window (get-buffer-window (current-buffer)))
7234       ;; Select next unread newsgroup automagically.
7235       (cond
7236        ((or (not gnus-auto-select-next)
7237             (not cmd))
7238         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7239        ((or (eq gnus-auto-select-next 'quietly)
7240             (and (eq gnus-auto-select-next 'slightly-quietly)
7241                  push)
7242             (and (eq gnus-auto-select-next 'almost-quietly)
7243                  (gnus-summary-last-article-p)))
7244         ;; Select quietly.
7245         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7246             (gnus-summary-exit)
7247           (gnus-message 7 "No more%s articles (%s)..."
7248                         (if unread " unread" "")
7249                         (if group (concat "selecting " group)
7250                           "exiting"))
7251           (gnus-summary-next-group nil group backward)))
7252        (t
7253         (when (gnus-key-press-event-p last-input-event)
7254           (gnus-summary-walk-group-buffer
7255            gnus-newsgroup-name cmd unread backward point))))))))
7256
7257 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7258   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7259                       (?\C-p (gnus-group-prev-unread-group 1))))
7260         (cursor-in-echo-area t)
7261         keve key group ended prompt)
7262     (save-excursion
7263       (set-buffer gnus-group-buffer)
7264       (goto-char start)
7265       (setq group
7266             (if (eq gnus-keep-same-level 'best)
7267                 (gnus-summary-best-group gnus-newsgroup-name)
7268               (gnus-summary-search-group backward gnus-keep-same-level))))
7269     (while (not ended)
7270       (setq prompt
7271             (format
7272              "No more%s articles%s " (if unread " unread" "")
7273              (if (and group
7274                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7275                  (format " (Type %s for %s [%s])"
7276                          (single-key-description cmd) group
7277                          (gnus-group-unread group))
7278                (format " (Type %s to exit %s)"
7279                        (single-key-description cmd)
7280                        gnus-newsgroup-name))))
7281       ;; Confirm auto selection.
7282       (setq key (car (setq keve (gnus-read-event-char prompt)))
7283             ended t)
7284       (cond
7285        ((assq key keystrokes)
7286         (let ((obuf (current-buffer)))
7287           (switch-to-buffer gnus-group-buffer)
7288           (when group
7289             (gnus-group-jump-to-group group))
7290           (eval (cadr (assq key keystrokes)))
7291           (setq group (gnus-group-group-name))
7292           (switch-to-buffer obuf))
7293         (setq ended nil))
7294        ((equal key cmd)
7295         (if (or (not group)
7296                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7297             (gnus-summary-exit)
7298           (gnus-summary-next-group nil group backward)))
7299        (t
7300         (push (cdr keve) unread-command-events))))))
7301
7302 (defun gnus-summary-next-unread-article ()
7303   "Select unread article after current one."
7304   (interactive)
7305   (gnus-summary-next-article
7306    (or (not (eq gnus-summary-goto-unread 'never))
7307        (gnus-summary-last-article-p (gnus-summary-article-number)))
7308    (and gnus-auto-select-same
7309         (gnus-summary-article-subject))))
7310
7311 (defun gnus-summary-prev-article (&optional unread subject)
7312   "Select the article after the current one.
7313 If UNREAD is non-nil, only unread articles are selected."
7314   (interactive "P")
7315   (gnus-summary-next-article unread subject t))
7316
7317 (defun gnus-summary-prev-unread-article ()
7318   "Select unread article before current one."
7319   (interactive)
7320   (gnus-summary-prev-article
7321    (or (not (eq gnus-summary-goto-unread 'never))
7322        (gnus-summary-first-article-p (gnus-summary-article-number)))
7323    (and gnus-auto-select-same
7324         (gnus-summary-article-subject))))
7325
7326 (defun gnus-summary-next-page (&optional lines circular stop)
7327   "Show next page of the selected article.
7328 If at the end of the current article, select the next article.
7329 LINES says how many lines should be scrolled up.
7330
7331 If CIRCULAR is non-nil, go to the start of the article instead of
7332 selecting the next article when reaching the end of the current
7333 article.
7334
7335 If STOP is non-nil, just stop when reaching the end of the message.
7336
7337 Also see the variable `gnus-article-skip-boring'."
7338   (interactive "P")
7339   (setq gnus-summary-buffer (current-buffer))
7340   (gnus-set-global-variables)
7341   (let ((article (gnus-summary-article-number))
7342         (article-window (get-buffer-window gnus-article-buffer t))
7343         endp)
7344     ;; If the buffer is empty, we have no article.
7345     (unless article
7346       (error "No article to select"))
7347     (gnus-configure-windows 'article)
7348     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7349         (if (and (eq gnus-summary-goto-unread 'never)
7350                  (not (gnus-summary-last-article-p article)))
7351             (gnus-summary-next-article)
7352           (gnus-summary-next-unread-article))
7353       (if (or (null gnus-current-article)
7354               (null gnus-article-current)
7355               (/= article (cdr gnus-article-current))
7356               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7357           ;; Selected subject is different from current article's.
7358           (gnus-summary-display-article article)
7359         (when article-window
7360           (gnus-eval-in-buffer-window gnus-article-buffer
7361             (setq endp (or (gnus-article-next-page lines)
7362                            (gnus-article-only-boring-p))))
7363           (when endp
7364             (cond (stop
7365                    (gnus-message 3 "End of message"))
7366                   (circular
7367                    (gnus-summary-beginning-of-article))
7368                   (lines
7369                    (gnus-message 3 "End of message"))
7370                   ((null lines)
7371                    (if (and (eq gnus-summary-goto-unread 'never)
7372                             (not (gnus-summary-last-article-p article)))
7373                        (gnus-summary-next-article)
7374                      (gnus-summary-next-unread-article))))))))
7375     (gnus-summary-recenter)
7376     (gnus-summary-position-point)))
7377
7378 (defun gnus-summary-prev-page (&optional lines move)
7379   "Show previous page of selected article.
7380 Argument LINES specifies lines to be scrolled down.
7381 If MOVE, move to the previous unread article if point is at
7382 the beginning of the buffer."
7383   (interactive "P")
7384   (let ((article (gnus-summary-article-number))
7385         (article-window (get-buffer-window gnus-article-buffer t))
7386         endp)
7387     (gnus-configure-windows 'article)
7388     (if (or (null gnus-current-article)
7389             (null gnus-article-current)
7390             (/= article (cdr gnus-article-current))
7391             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7392         ;; Selected subject is different from current article's.
7393         (gnus-summary-display-article article)
7394       (gnus-summary-recenter)
7395       (when article-window
7396         (gnus-eval-in-buffer-window gnus-article-buffer
7397           (setq endp (gnus-article-prev-page lines)))
7398         (when (and move endp)
7399           (cond (lines
7400                  (gnus-message 3 "Beginning of message"))
7401                 ((null lines)
7402                  (if (and (eq gnus-summary-goto-unread 'never)
7403                           (not (gnus-summary-first-article-p article)))
7404                      (gnus-summary-prev-article)
7405                    (gnus-summary-prev-unread-article))))))))
7406   (gnus-summary-position-point))
7407
7408 (defun gnus-summary-prev-page-or-article (&optional lines)
7409   "Show previous page of selected article.
7410 Argument LINES specifies lines to be scrolled down.
7411 If at the beginning of the article, go to the next article."
7412   (interactive "P")
7413   (gnus-summary-prev-page lines t))
7414
7415 (defun gnus-summary-scroll-up (lines)
7416   "Scroll up (or down) one line current article.
7417 Argument LINES specifies lines to be scrolled up (or down if negative)."
7418   (interactive "p")
7419   (gnus-configure-windows 'article)
7420   (gnus-summary-show-thread)
7421   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7422     (gnus-eval-in-buffer-window gnus-article-buffer
7423       (cond ((> lines 0)
7424              (when (gnus-article-next-page lines)
7425                (gnus-message 3 "End of message")))
7426             ((< lines 0)
7427              (gnus-article-prev-page (- lines))))))
7428   (gnus-summary-recenter)
7429   (gnus-summary-position-point))
7430
7431 (defun gnus-summary-scroll-down (lines)
7432   "Scroll down (or up) one line current article.
7433 Argument LINES specifies lines to be scrolled down (or up if negative)."
7434   (interactive "p")
7435   (gnus-summary-scroll-up (- lines)))
7436
7437 (defun gnus-summary-next-same-subject ()
7438   "Select next article which has the same subject as current one."
7439   (interactive)
7440   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7441
7442 (defun gnus-summary-prev-same-subject ()
7443   "Select previous article which has the same subject as current one."
7444   (interactive)
7445   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7446
7447 (defun gnus-summary-next-unread-same-subject ()
7448   "Select next unread article which has the same subject as current one."
7449   (interactive)
7450   (gnus-summary-next-article t (gnus-summary-article-subject)))
7451
7452 (defun gnus-summary-prev-unread-same-subject ()
7453   "Select previous unread article which has the same subject as current one."
7454   (interactive)
7455   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7456
7457 (defun gnus-summary-first-unread-article ()
7458   "Select the first unread article.
7459 Return nil if there are no unread articles."
7460   (interactive)
7461   (prog1
7462       (when (gnus-summary-first-subject t)
7463         (gnus-summary-show-thread)
7464         (gnus-summary-first-subject t)
7465         (gnus-summary-display-article (gnus-summary-article-number)))
7466     (gnus-summary-position-point)))
7467
7468 (defun gnus-summary-first-unread-subject ()
7469   "Place the point on the subject line of the first unread article.
7470 Return nil if there are no unread articles."
7471   (interactive)
7472   (prog1
7473       (when (gnus-summary-first-subject t)
7474         (gnus-summary-show-thread)
7475         (gnus-summary-first-subject t))
7476     (gnus-summary-position-point)))
7477
7478 (defun gnus-summary-first-unseen-subject ()
7479   "Place the point on the subject line of the first unseen article.
7480 Return nil if there are no unseen articles."
7481   (interactive)
7482   (prog1
7483       (when (gnus-summary-first-subject nil nil t)
7484         (gnus-summary-show-thread)
7485         (gnus-summary-first-subject nil nil t))
7486     (gnus-summary-position-point)))
7487
7488 (defun gnus-summary-first-unseen-or-unread-subject ()
7489   "Place the point on the subject line of the first unseen article or,
7490 if all article have been seen, on the subject line of the first unread
7491 article."
7492   (interactive)
7493   (prog1
7494       (unless (when (gnus-summary-first-subject nil nil t)
7495                 (gnus-summary-show-thread)
7496                 (gnus-summary-first-subject nil nil t))
7497         (when (gnus-summary-first-subject t)
7498           (gnus-summary-show-thread)
7499           (gnus-summary-first-subject t)))
7500     (gnus-summary-position-point)))
7501
7502 (defun gnus-summary-first-article ()
7503   "Select the first article.
7504 Return nil if there are no articles."
7505   (interactive)
7506   (prog1
7507       (when (gnus-summary-first-subject)
7508         (gnus-summary-show-thread)
7509         (gnus-summary-first-subject)
7510         (gnus-summary-display-article (gnus-summary-article-number)))
7511     (gnus-summary-position-point)))
7512
7513 (defun gnus-summary-best-unread-article (&optional arg)
7514   "Select the unread article with the highest score.
7515 If given a prefix argument, select the next unread article that has a
7516 score higher than the default score."
7517   (interactive "P")
7518   (let ((article (if arg
7519                      (gnus-summary-better-unread-subject)
7520                    (gnus-summary-best-unread-subject))))
7521     (if article
7522         (gnus-summary-goto-article article)
7523       (error "No unread articles"))))
7524
7525 (defun gnus-summary-best-unread-subject ()
7526   "Select the unread subject with the highest score."
7527   (interactive)
7528   (let ((best -1000000)
7529         (data gnus-newsgroup-data)
7530         article score)
7531     (while data
7532       (and (gnus-data-unread-p (car data))
7533            (> (setq score
7534                     (gnus-summary-article-score (gnus-data-number (car data))))
7535               best)
7536            (setq best score
7537                  article (gnus-data-number (car data))))
7538       (setq data (cdr data)))
7539     (when article
7540       (gnus-summary-goto-subject article))
7541     (gnus-summary-position-point)
7542     article))
7543
7544 (defun gnus-summary-better-unread-subject ()
7545   "Select the first unread subject that has a score over the default score."
7546   (interactive)
7547   (let ((data gnus-newsgroup-data)
7548         article score)
7549     (while (and (setq article (gnus-data-number (car data)))
7550                 (or (gnus-data-read-p (car data))
7551                     (not (> (gnus-summary-article-score article)
7552                             gnus-summary-default-score))))
7553       (setq data (cdr data)))
7554     (when article
7555       (gnus-summary-goto-subject article))
7556     (gnus-summary-position-point)
7557     article))
7558
7559 (defun gnus-summary-last-subject ()
7560   "Go to the last displayed subject line in the group."
7561   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7562     (when article
7563       (gnus-summary-goto-subject article))))
7564
7565 (defun gnus-summary-goto-article (article &optional all-headers force)
7566   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7567 If ALL-HEADERS is non-nil, no header lines are hidden.
7568 If FORCE, go to the article even if it isn't displayed.  If FORCE
7569 is a number, it is the line the article is to be displayed on."
7570   (interactive
7571    (list
7572     (completing-read
7573      "Article number or Message-ID: "
7574      (mapcar (lambda (number) (list (int-to-string number)))
7575              gnus-newsgroup-limit))
7576     current-prefix-arg
7577     t))
7578   (prog1
7579       (if (and (stringp article)
7580                (string-match "@\\|%40" article))
7581           (gnus-summary-refer-article article)
7582         (when (stringp article)
7583           (setq article (string-to-number article)))
7584         (if (gnus-summary-goto-subject article force)
7585             (gnus-summary-display-article article all-headers)
7586           (gnus-message 4 "Couldn't go to article %s" article) nil))
7587     (gnus-summary-position-point)))
7588
7589 (defun gnus-summary-goto-last-article ()
7590   "Go to the previously read article."
7591   (interactive)
7592   (prog1
7593       (when gnus-last-article
7594         (gnus-summary-goto-article gnus-last-article nil t))
7595     (gnus-summary-position-point)))
7596
7597 (defun gnus-summary-pop-article (number)
7598   "Pop one article off the history and go to the previous.
7599 NUMBER articles will be popped off."
7600   (interactive "p")
7601   (let (to)
7602     (setq gnus-newsgroup-history
7603           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7604     (if to
7605         (gnus-summary-goto-article (car to) nil t)
7606       (error "Article history empty")))
7607   (gnus-summary-position-point))
7608
7609 ;; Summary commands and functions for limiting the summary buffer.
7610
7611 (defun gnus-summary-limit-to-articles (n)
7612   "Limit the summary buffer to the next N articles.
7613 If not given a prefix, use the process marked articles instead."
7614   (interactive "P")
7615   (prog1
7616       (let ((articles (gnus-summary-work-articles n)))
7617         (setq gnus-newsgroup-processable nil)
7618         (gnus-summary-limit articles))
7619     (gnus-summary-position-point)))
7620
7621 (defun gnus-summary-pop-limit (&optional total)
7622   "Restore the previous limit.
7623 If given a prefix, remove all limits."
7624   (interactive "P")
7625   (when total
7626     (setq gnus-newsgroup-limits
7627           (list (mapcar (lambda (h) (mail-header-number h))
7628                         gnus-newsgroup-headers))))
7629   (unless gnus-newsgroup-limits
7630     (error "No limit to pop"))
7631   (prog1
7632       (gnus-summary-limit nil 'pop)
7633     (gnus-summary-position-point)))
7634
7635 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7636   "Limit the summary buffer to articles that have subjects that match a regexp.
7637 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7638   (interactive
7639    (list (read-string (if current-prefix-arg
7640                           "Exclude subject (regexp): "
7641                         "Limit to subject (regexp): "))
7642          nil current-prefix-arg))
7643   (unless header
7644     (setq header "subject"))
7645   (when (not (equal "" subject))
7646     (prog1
7647         (let ((articles (gnus-summary-find-matching
7648                          (or header "subject") subject 'all nil nil
7649                          not-matching)))
7650           (unless articles
7651             (error "Found no matches for \"%s\"" subject))
7652           (gnus-summary-limit articles))
7653       (gnus-summary-position-point))))
7654
7655 (defun gnus-summary-limit-to-author (from &optional not-matching)
7656   "Limit the summary buffer to articles that have authors that match a regexp.
7657 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7658   (interactive
7659    (list (read-string (if current-prefix-arg
7660                           "Exclude author (regexp): "
7661                         "Limit to author (regexp): "))
7662          current-prefix-arg))
7663   (gnus-summary-limit-to-subject from "from" not-matching))
7664
7665 (defun gnus-summary-limit-to-age (age &optional younger-p)
7666   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7667 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7668 articles that are younger than AGE days."
7669   (interactive
7670    (let ((younger current-prefix-arg)
7671          (days-got nil)
7672          days)
7673      (while (not days-got)
7674        (setq days (if younger
7675                       (read-string "Limit to articles younger than (in days, older when negative): ")
7676                     (read-string
7677                      "Limit to articles older than (in days, younger when negative): ")))
7678        (when (> (length days) 0)
7679          (setq days (read days)))
7680        (if (numberp days)
7681            (progn
7682              (setq days-got t)
7683              (when (< days 0)
7684                (setq younger (not younger))
7685                (setq days (* days -1))))
7686          (message "Please enter a number.")
7687          (sleep-for 1)))
7688      (list days younger)))
7689   (prog1
7690       (let ((data gnus-newsgroup-data)
7691             (cutoff (days-to-time age))
7692             articles d date is-younger)
7693         (while (setq d (pop data))
7694           (when (and (vectorp (gnus-data-header d))
7695                      (setq date (mail-header-date (gnus-data-header d))))
7696             (setq is-younger (time-less-p
7697                               (time-since (condition-case ()
7698                                               (date-to-time date)
7699                                             (error '(0 0))))
7700                               cutoff))
7701             (when (if younger-p
7702                       is-younger
7703                     (not is-younger))
7704               (push (gnus-data-number d) articles))))
7705         (gnus-summary-limit (nreverse articles)))
7706     (gnus-summary-position-point)))
7707
7708 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7709   "Limit the summary buffer to articles that match an 'extra' header."
7710   (interactive
7711    (let ((header
7712           (intern
7713            (gnus-completing-read-with-default
7714             (symbol-name (car gnus-extra-headers))
7715             (if current-prefix-arg
7716                 "Exclude extra header:"
7717               "Limit extra header:")
7718             (mapcar (lambda (x)
7719                       (cons (symbol-name x) x))
7720                     gnus-extra-headers)
7721             nil
7722             t))))
7723      (list header
7724            (read-string (format "%s header %s (regexp): "
7725                                 (if current-prefix-arg "Exclude" "Limit to")
7726                                 header))
7727            current-prefix-arg)))
7728   (when (not (equal "" regexp))
7729     (prog1
7730         (let ((articles (gnus-summary-find-matching
7731                          (cons 'extra header) regexp 'all nil nil
7732                          not-matching)))
7733           (unless articles
7734             (error "Found no matches for \"%s\"" regexp))
7735           (gnus-summary-limit articles))
7736       (gnus-summary-position-point))))
7737
7738 (defun gnus-summary-limit-to-display-predicate ()
7739   "Limit the summary buffer to the predicated in the `display' group parameter."
7740   (interactive)
7741   (unless gnus-newsgroup-display
7742     (error "There is no `display' group parameter"))
7743   (let (articles)
7744     (dolist (number gnus-newsgroup-articles)
7745       (when (funcall gnus-newsgroup-display)
7746         (push number articles)))
7747     (gnus-summary-limit articles))
7748   (gnus-summary-position-point))
7749
7750 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7751 (make-obsolete
7752  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7753
7754 (defun gnus-summary-limit-to-unread (&optional all)
7755   "Limit the summary buffer to articles that are not marked as read.
7756 If ALL is non-nil, limit strictly to unread articles."
7757   (interactive "P")
7758   (if all
7759       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7760     (gnus-summary-limit-to-marks
7761      ;; Concat all the marks that say that an article is read and have
7762      ;; those removed.
7763      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7764            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7765            gnus-low-score-mark gnus-expirable-mark
7766            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7767            gnus-duplicate-mark gnus-souped-mark)
7768      'reverse)))
7769
7770 (defun gnus-summary-limit-to-replied (&optional unreplied)
7771   "Limit the summary buffer to replied articles.
7772 If UNREPLIED (the prefix), limit to unreplied articles."
7773   (interactive "P")
7774   (if unreplied
7775       (gnus-summary-limit
7776        (gnus-set-difference gnus-newsgroup-articles
7777         gnus-newsgroup-replied))
7778     (gnus-summary-limit gnus-newsgroup-replied))
7779   (gnus-summary-position-point))
7780
7781 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7782 (make-obsolete 'gnus-summary-delete-marked-with
7783                'gnus-summary-limit-exclude-marks)
7784
7785 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7786   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7787 If REVERSE, limit the summary buffer to articles that are marked
7788 with MARKS.  MARKS can either be a string of marks or a list of marks.
7789 Returns how many articles were removed."
7790   (interactive "sMarks: ")
7791   (gnus-summary-limit-to-marks marks t))
7792
7793 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7794   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7795 If REVERSE (the prefix), limit the summary buffer to articles that are
7796 not marked with MARKS.  MARKS can either be a string of marks or a
7797 list of marks.
7798 Returns how many articles were removed."
7799   (interactive "sMarks: \nP")
7800   (prog1
7801       (let ((data gnus-newsgroup-data)
7802             (marks (if (listp marks) marks
7803                      (append marks nil))) ; Transform to list.
7804             articles)
7805         (while data
7806           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7807                   (memq (gnus-data-mark (car data)) marks))
7808             (push (gnus-data-number (car data)) articles))
7809           (setq data (cdr data)))
7810         (gnus-summary-limit articles))
7811     (gnus-summary-position-point)))
7812
7813 (defun gnus-summary-limit-to-score (score)
7814   "Limit to articles with score at or above SCORE."
7815   (interactive "NLimit to articles with score of at least: ")
7816   (let ((data gnus-newsgroup-data)
7817         articles)
7818     (while data
7819       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7820                 score)
7821         (push (gnus-data-number (car data)) articles))
7822       (setq data (cdr data)))
7823     (prog1
7824         (gnus-summary-limit articles)
7825       (gnus-summary-position-point))))
7826
7827 (defun gnus-summary-limit-to-unseen ()
7828   "Limit to unseen articles."
7829   (interactive)
7830   (prog1
7831       (gnus-summary-limit gnus-newsgroup-unseen)
7832     (gnus-summary-position-point)))
7833
7834 (defun gnus-summary-limit-include-thread (id)
7835   "Display all the hidden articles that is in the thread with ID in it.
7836 When called interactively, ID is the Message-ID of the current
7837 article."
7838   (interactive (list (mail-header-id (gnus-summary-article-header))))
7839   (let ((articles (gnus-articles-in-thread
7840                    (gnus-id-to-thread (gnus-root-id id)))))
7841     (prog1
7842         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7843       (gnus-summary-limit-include-matching-articles
7844        "subject"
7845        (regexp-quote (gnus-simplify-subject-re
7846                       (mail-header-subject (gnus-id-to-header id)))))
7847       (gnus-summary-position-point))))
7848
7849 (defun gnus-summary-limit-include-matching-articles (header regexp)
7850   "Display all the hidden articles that have HEADERs that match REGEXP."
7851   (interactive (list (read-string "Match on header: ")
7852                      (read-string "Regexp: ")))
7853   (let ((articles (gnus-find-matching-articles header regexp)))
7854     (prog1
7855         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7856       (gnus-summary-position-point))))
7857
7858 (defun gnus-summary-insert-dormant-articles ()
7859   "Insert all the dormant articles for this group into the current buffer."
7860   (interactive)
7861   (let ((gnus-verbose (max 6 gnus-verbose)))
7862     (if (not gnus-newsgroup-dormant)
7863         (gnus-message 3 "No cached articles for this group")
7864       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7865
7866 (defun gnus-summary-limit-include-dormant ()
7867   "Display all the hidden articles that are marked as dormant.
7868 Note that this command only works on a subset of the articles currently
7869 fetched for this group."
7870   (interactive)
7871   (unless gnus-newsgroup-dormant
7872     (error "There are no dormant articles in this group"))
7873   (prog1
7874       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7875     (gnus-summary-position-point)))
7876
7877 (defun gnus-summary-limit-exclude-dormant ()
7878   "Hide all dormant articles."
7879   (interactive)
7880   (prog1
7881       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7882     (gnus-summary-position-point)))
7883
7884 (defun gnus-summary-limit-exclude-childless-dormant ()
7885   "Hide all dormant articles that have no children."
7886   (interactive)
7887   (let ((data (gnus-data-list t))
7888         articles d children)
7889     ;; Find all articles that are either not dormant or have
7890     ;; children.
7891     (while (setq d (pop data))
7892       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7893                 (and (setq children
7894                            (gnus-article-children (gnus-data-number d)))
7895                      (let (found)
7896                        (while children
7897                          (when (memq (car children) articles)
7898                            (setq children nil
7899                                  found t))
7900                          (pop children))
7901                        found)))
7902         (push (gnus-data-number d) articles)))
7903     ;; Do the limiting.
7904     (prog1
7905         (gnus-summary-limit articles)
7906       (gnus-summary-position-point))))
7907
7908 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7909   "Mark all unread excluded articles as read.
7910 If ALL, mark even excluded ticked and dormants as read."
7911   (interactive "P")
7912   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7913   (let ((articles (gnus-sorted-ndifference
7914                    (sort
7915                     (mapcar (lambda (h) (mail-header-number h))
7916                             gnus-newsgroup-headers)
7917                     '<)
7918                    gnus-newsgroup-limit))
7919         article)
7920     (setq gnus-newsgroup-unreads
7921           (gnus-sorted-intersection gnus-newsgroup-unreads
7922                                     gnus-newsgroup-limit))
7923     (if all
7924         (setq gnus-newsgroup-dormant nil
7925               gnus-newsgroup-marked nil
7926               gnus-newsgroup-reads
7927               (nconc
7928                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7929                gnus-newsgroup-reads))
7930       (while (setq article (pop articles))
7931         (unless (or (memq article gnus-newsgroup-dormant)
7932                     (memq article gnus-newsgroup-marked))
7933           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7934
7935 (defun gnus-summary-limit (articles &optional pop)
7936   (if pop
7937       ;; We pop the previous limit off the stack and use that.
7938       (setq articles (car gnus-newsgroup-limits)
7939             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7940     ;; We use the new limit, so we push the old limit on the stack.
7941     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7942   ;; Set the limit.
7943   (setq gnus-newsgroup-limit articles)
7944   (let ((total (length gnus-newsgroup-data))
7945         (data (gnus-data-find-list (gnus-summary-article-number)))
7946         (gnus-summary-mark-below nil)   ; Inhibit this.
7947         found)
7948     ;; This will do all the work of generating the new summary buffer
7949     ;; according to the new limit.
7950     (gnus-summary-prepare)
7951     ;; Hide any threads, possibly.
7952     (gnus-summary-maybe-hide-threads)
7953     ;; Try to return to the article you were at, or one in the
7954     ;; neighborhood.
7955     (when data
7956       ;; We try to find some article after the current one.
7957       (while data
7958         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7959           (setq data nil
7960                 found t))
7961         (setq data (cdr data))))
7962     (unless found
7963       ;; If there is no data, that means that we were after the last
7964       ;; article.  The same goes when we can't find any articles
7965       ;; after the current one.
7966       (goto-char (point-max))
7967       (gnus-summary-find-prev))
7968     (gnus-set-mode-line 'summary)
7969     ;; We return how many articles were removed from the summary
7970     ;; buffer as a result of the new limit.
7971     (- total (length gnus-newsgroup-data))))
7972
7973 (defsubst gnus-invisible-cut-children (threads)
7974   (let ((num 0))
7975     (while threads
7976       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7977         (incf num))
7978       (pop threads))
7979     (< num 2)))
7980
7981 (defsubst gnus-cut-thread (thread)
7982   "Go forwards in the thread until we find an article that we want to display."
7983   (when (or (eq gnus-fetch-old-headers 'some)
7984             (eq gnus-fetch-old-headers 'invisible)
7985             (numberp gnus-fetch-old-headers)
7986             (eq gnus-build-sparse-threads 'some)
7987             (eq gnus-build-sparse-threads 'more))
7988     ;; Deal with old-fetched headers and sparse threads.
7989     (while (and
7990             thread
7991             (or
7992              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7993              (gnus-summary-article-ancient-p
7994               (mail-header-number (car thread))))
7995             (if (or (<= (length (cdr thread)) 1)
7996                     (eq gnus-fetch-old-headers 'invisible))
7997                 (setq gnus-newsgroup-limit
7998                       (delq (mail-header-number (car thread))
7999                             gnus-newsgroup-limit)
8000                       thread (cadr thread))
8001               (when (gnus-invisible-cut-children (cdr thread))
8002                 (let ((th (cdr thread)))
8003                   (while th
8004                     (if (memq (mail-header-number (caar th))
8005                               gnus-newsgroup-limit)
8006                         (setq thread (car th)
8007                               th nil)
8008                       (setq th (cdr th))))))))))
8009   thread)
8010
8011 (defun gnus-cut-threads (threads)
8012   "Cut off all uninteresting articles from the beginning of THREADS."
8013   (when (or (eq gnus-fetch-old-headers 'some)
8014             (eq gnus-fetch-old-headers 'invisible)
8015             (numberp gnus-fetch-old-headers)
8016             (eq gnus-build-sparse-threads 'some)
8017             (eq gnus-build-sparse-threads 'more))
8018     (let ((th threads))
8019       (while th
8020         (setcar th (gnus-cut-thread (car th)))
8021         (setq th (cdr th)))))
8022   ;; Remove nixed out threads.
8023   (delq nil threads))
8024
8025 (defun gnus-summary-initial-limit (&optional show-if-empty)
8026   "Figure out what the initial limit is supposed to be on group entry.
8027 This entails weeding out unwanted dormants, low-scored articles,
8028 fetch-old-headers verbiage, and so on."
8029   ;; Most groups have nothing to remove.
8030   (if (or gnus-inhibit-limiting
8031           (and (null gnus-newsgroup-dormant)
8032                (eq gnus-newsgroup-display 'gnus-not-ignore)
8033                (not (eq gnus-fetch-old-headers 'some))
8034                (not (numberp gnus-fetch-old-headers))
8035                (not (eq gnus-fetch-old-headers 'invisible))
8036                (null gnus-summary-expunge-below)
8037                (not (eq gnus-build-sparse-threads 'some))
8038                (not (eq gnus-build-sparse-threads 'more))
8039                (null gnus-thread-expunge-below)
8040                (not gnus-use-nocem)))
8041       ()                                ; Do nothing.
8042     (push gnus-newsgroup-limit gnus-newsgroup-limits)
8043     (setq gnus-newsgroup-limit nil)
8044     (mapatoms
8045      (lambda (node)
8046        (unless (car (symbol-value node))
8047          ;; These threads have no parents -- they are roots.
8048          (let ((nodes (cdr (symbol-value node)))
8049                thread)
8050            (while nodes
8051              (if (and gnus-thread-expunge-below
8052                       (< (gnus-thread-total-score (car nodes))
8053                          gnus-thread-expunge-below))
8054                  (gnus-expunge-thread (pop nodes))
8055                (setq thread (pop nodes))
8056                (gnus-summary-limit-children thread))))))
8057      gnus-newsgroup-dependencies)
8058     ;; If this limitation resulted in an empty group, we might
8059     ;; pop the previous limit and use it instead.
8060     (when (and (not gnus-newsgroup-limit)
8061                show-if-empty)
8062       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
8063     gnus-newsgroup-limit))
8064
8065 (defun gnus-summary-limit-children (thread)
8066   "Return 1 if this subthread is visible and 0 if it is not."
8067   ;; First we get the number of visible children to this thread.  This
8068   ;; is done by recursing down the thread using this function, so this
8069   ;; will really go down to a leaf article first, before slowly
8070   ;; working its way up towards the root.
8071   (when thread
8072     (let* ((max-lisp-eval-depth 5000)
8073            (children
8074            (if (cdr thread)
8075                (apply '+ (mapcar 'gnus-summary-limit-children
8076                                  (cdr thread)))
8077              0))
8078           (number (mail-header-number (car thread)))
8079           score)
8080       (if (and
8081            (not (memq number gnus-newsgroup-marked))
8082            (or
8083             ;; If this article is dormant and has absolutely no visible
8084             ;; children, then this article isn't visible.
8085             (and (memq number gnus-newsgroup-dormant)
8086                  (zerop children))
8087             ;; If this is "fetch-old-headered" and there is no
8088             ;; visible children, then we don't want this article.
8089             (and (or (eq gnus-fetch-old-headers 'some)
8090                      (numberp gnus-fetch-old-headers))
8091                  (gnus-summary-article-ancient-p number)
8092                  (zerop children))
8093             ;; If this is "fetch-old-headered" and `invisible', then
8094             ;; we don't want this article.
8095             (and (eq gnus-fetch-old-headers 'invisible)
8096                  (gnus-summary-article-ancient-p number))
8097             ;; If this is a sparsely inserted article with no children,
8098             ;; we don't want it.
8099             (and (eq gnus-build-sparse-threads 'some)
8100                  (gnus-summary-article-sparse-p number)
8101                  (zerop children))
8102             ;; If we use expunging, and this article is really
8103             ;; low-scored, then we don't want this article.
8104             (when (and gnus-summary-expunge-below
8105                        (< (setq score
8106                                 (or (cdr (assq number gnus-newsgroup-scored))
8107                                     gnus-summary-default-score))
8108                           gnus-summary-expunge-below))
8109               ;; We increase the expunge-tally here, but that has
8110               ;; nothing to do with the limits, really.
8111               (incf gnus-newsgroup-expunged-tally)
8112               ;; We also mark as read here, if that's wanted.
8113               (when (and gnus-summary-mark-below
8114                          (< score gnus-summary-mark-below))
8115                 (setq gnus-newsgroup-unreads
8116                       (delq number gnus-newsgroup-unreads))
8117                 (if gnus-newsgroup-auto-expire
8118                     (push number gnus-newsgroup-expirable)
8119                   (push (cons number gnus-low-score-mark)
8120                         gnus-newsgroup-reads)))
8121               t)
8122             ;; Do the `display' group parameter.
8123             (and gnus-newsgroup-display
8124                  (not (funcall gnus-newsgroup-display)))
8125             ;; Check NoCeM things.
8126             (when (and gnus-use-nocem
8127                        (gnus-nocem-unwanted-article-p
8128                         (mail-header-id (car thread))))
8129               (setq gnus-newsgroup-unreads
8130                     (delq number gnus-newsgroup-unreads))
8131               t)))
8132           ;; Nope, invisible article.
8133           0
8134         ;; Ok, this article is to be visible, so we add it to the limit
8135         ;; and return 1.
8136         (push number gnus-newsgroup-limit)
8137         1))))
8138
8139 (defun gnus-expunge-thread (thread)
8140   "Mark all articles in THREAD as read."
8141   (let* ((number (mail-header-number (car thread))))
8142     (incf gnus-newsgroup-expunged-tally)
8143     ;; We also mark as read here, if that's wanted.
8144     (setq gnus-newsgroup-unreads
8145           (delq number gnus-newsgroup-unreads))
8146     (if gnus-newsgroup-auto-expire
8147         (push number gnus-newsgroup-expirable)
8148       (push (cons number gnus-low-score-mark)
8149             gnus-newsgroup-reads)))
8150   ;; Go recursively through all subthreads.
8151   (mapcar 'gnus-expunge-thread (cdr thread)))
8152
8153 ;; Summary article oriented commands
8154
8155 (defun gnus-summary-refer-parent-article (n)
8156   "Refer parent article N times.
8157 If N is negative, go to ancestor -N instead.
8158 The difference between N and the number of articles fetched is returned."
8159   (interactive "p")
8160   (let ((skip 1)
8161         error header ref)
8162     (when (not (natnump n))
8163       (setq skip (abs n)
8164             n 1))
8165     (while (and (> n 0)
8166                 (not error))
8167       (setq header (gnus-summary-article-header))
8168       (if (and (eq (mail-header-number header)
8169                    (cdr gnus-article-current))
8170                (equal gnus-newsgroup-name
8171                       (car gnus-article-current)))
8172           ;; If we try to find the parent of the currently
8173           ;; displayed article, then we take a look at the actual
8174           ;; References header, since this is slightly more
8175           ;; reliable than the References field we got from the
8176           ;; server.
8177           (save-excursion
8178             (set-buffer gnus-original-article-buffer)
8179             (nnheader-narrow-to-headers)
8180             (unless (setq ref (message-fetch-field "references"))
8181               (when (setq ref (message-fetch-field "in-reply-to"))
8182                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8183             (widen))
8184         (setq ref
8185               ;; It's not the current article, so we take a bet on
8186               ;; the value we got from the server.
8187               (mail-header-references header)))
8188       (if (and ref
8189                (not (equal ref "")))
8190           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8191             (gnus-message 1 "Couldn't find parent"))
8192         (gnus-message 1 "No references in article %d"
8193                       (gnus-summary-article-number))
8194         (setq error t))
8195       (decf n))
8196     (gnus-summary-position-point)
8197     n))
8198
8199 (defun gnus-summary-refer-references ()
8200   "Fetch all articles mentioned in the References header.
8201 Return the number of articles fetched."
8202   (interactive)
8203   (let ((ref (mail-header-references (gnus-summary-article-header)))
8204         (current (gnus-summary-article-number))
8205         (n 0))
8206     (if (or (not ref)
8207             (equal ref ""))
8208         (error "No References in the current article")
8209       ;; For each Message-ID in the References header...
8210       (while (string-match "<[^>]*>" ref)
8211         (incf n)
8212         ;; ... fetch that article.
8213         (gnus-summary-refer-article
8214          (prog1 (match-string 0 ref)
8215            (setq ref (substring ref (match-end 0))))))
8216       (gnus-summary-goto-subject current)
8217       (gnus-summary-position-point)
8218       n)))
8219
8220 (defun gnus-summary-refer-thread (&optional limit)
8221   "Fetch all articles in the current thread.
8222 If LIMIT (the numerical prefix), fetch that many old headers instead
8223 of what's specified by the `gnus-refer-thread-limit' variable."
8224   (interactive "P")
8225   (let ((id (mail-header-id (gnus-summary-article-header)))
8226         (limit (if limit (prefix-numeric-value limit)
8227                  gnus-refer-thread-limit)))
8228     (unless (eq gnus-fetch-old-headers 'invisible)
8229       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8230       ;; Retrieve the headers and read them in.
8231       (if (eq (if (numberp limit)
8232                   (gnus-retrieve-headers
8233                    (list (min
8234                           (+ (mail-header-number
8235                               (gnus-summary-article-header))
8236                              limit)
8237                           gnus-newsgroup-end))
8238                    gnus-newsgroup-name (* limit 2))
8239                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8240                 ;; headers.
8241                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8242                                        gnus-newsgroup-name limit))
8243               'nov)
8244           (gnus-build-all-threads)
8245         (error "Can't fetch thread from back ends that don't support NOV"))
8246       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8247     (gnus-summary-limit-include-thread id)))
8248
8249 (defun gnus-summary-refer-article (message-id)
8250   "Fetch an article specified by MESSAGE-ID."
8251   (interactive "sMessage-ID: ")
8252   (when (and (stringp message-id)
8253              (not (zerop (length message-id))))
8254     (setq message-id (gnus-replace-in-string message-id " " ""))
8255     ;; Construct the correct Message-ID if necessary.
8256     ;; Suggested by tale@pawl.rpi.edu.
8257     (unless (string-match "^<" message-id)
8258       (setq message-id (concat "<" message-id)))
8259     (unless (string-match ">$" message-id)
8260       (setq message-id (concat message-id ">")))
8261     ;; People often post MIDs from URLs, so unhex it:
8262     (unless (string-match "@" message-id)
8263       (setq message-id (gnus-url-unhex-string message-id)))
8264     (let* ((header (gnus-id-to-header message-id))
8265            (sparse (and header
8266                         (gnus-summary-article-sparse-p
8267                          (mail-header-number header))
8268                         (memq (mail-header-number header)
8269                               gnus-newsgroup-limit)))
8270            number)
8271       (cond
8272        ;; If the article is present in the buffer we just go to it.
8273        ((and header
8274              (or (not (gnus-summary-article-sparse-p
8275                        (mail-header-number header)))
8276                  sparse))
8277         (prog1
8278             (gnus-summary-goto-article
8279              (mail-header-number header) nil t)
8280           (when sparse
8281             (gnus-summary-update-article (mail-header-number header)))))
8282        (t
8283         ;; We fetch the article.
8284         (catch 'found
8285           (dolist (gnus-override-method (gnus-refer-article-methods))
8286             (when (and (gnus-check-server gnus-override-method)
8287                        ;; Fetch the header,
8288                        (setq number (gnus-summary-insert-subject message-id)))
8289               ;; and display the article.
8290               (gnus-summary-select-article nil nil nil number)
8291               (throw 'found t)))
8292           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8293
8294 (defun gnus-refer-article-methods ()
8295   "Return a list of referable methods."
8296   (cond
8297    ;; No method, so we default to current and native.
8298    ((null gnus-refer-article-method)
8299     (list gnus-current-select-method gnus-select-method))
8300    ;; Current.
8301    ((eq 'current gnus-refer-article-method)
8302     (list gnus-current-select-method))
8303    ;; List of select methods.
8304    ((not (and (symbolp (car gnus-refer-article-method))
8305               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8306     (let (out)
8307       (dolist (method gnus-refer-article-method)
8308         (push (if (eq 'current method)
8309                   gnus-current-select-method
8310                 method)
8311               out))
8312       (nreverse out)))
8313    ;; One single select method.
8314    (t
8315     (list gnus-refer-article-method))))
8316
8317 (defun gnus-summary-edit-parameters ()
8318   "Edit the group parameters of the current group."
8319   (interactive)
8320   (gnus-group-edit-group gnus-newsgroup-name 'params))
8321
8322 (defun gnus-summary-customize-parameters ()
8323   "Customize the group parameters of the current group."
8324   (interactive)
8325   (gnus-group-customize gnus-newsgroup-name))
8326
8327 (defun gnus-summary-enter-digest-group (&optional force)
8328   "Enter an nndoc group based on the current article.
8329 If FORCE, force a digest interpretation.  If not, try
8330 to guess what the document format is."
8331   (interactive "P")
8332   (let ((conf gnus-current-window-configuration))
8333     (save-window-excursion
8334       (save-excursion
8335         (let (gnus-article-prepare-hook
8336               gnus-display-mime-function
8337               gnus-break-pages)
8338           (gnus-summary-select-article))))
8339     (setq gnus-current-window-configuration conf)
8340     (let* ((name (format "%s-%d"
8341                          (gnus-group-prefixed-name
8342                           gnus-newsgroup-name (list 'nndoc ""))
8343                          (with-current-buffer gnus-summary-buffer
8344                            gnus-current-article)))
8345            (ogroup gnus-newsgroup-name)
8346            (params (append (gnus-info-params (gnus-get-info ogroup))
8347                            (list (cons 'to-group ogroup))
8348                            (list (cons 'parent-group ogroup))
8349                            (list (cons 'save-article-group ogroup))))
8350            (case-fold-search t)
8351            (buf (current-buffer))
8352            dig to-address)
8353       (save-excursion
8354         (set-buffer gnus-original-article-buffer)
8355         ;; Have the digest group inherit the main mail address of
8356         ;; the parent article.
8357         (when (setq to-address (or (gnus-fetch-field "reply-to")
8358                                    (gnus-fetch-field "from")))
8359           (setq params (append
8360                         (list (cons 'to-address
8361                                     (funcall gnus-decode-encoded-word-function
8362                                              to-address))))))
8363         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8364         (insert-buffer-substring gnus-original-article-buffer)
8365         ;; Remove lines that may lead nndoc to misinterpret the
8366         ;; document type.
8367         (narrow-to-region
8368          (goto-char (point-min))
8369          (or (search-forward "\n\n" nil t) (point)))
8370         (goto-char (point-min))
8371         (delete-matching-lines "^Path:\\|^From ")
8372         (widen))
8373       (unwind-protect
8374           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8375                     (gnus-newsgroup-ephemeral-ignored-charsets
8376                      gnus-newsgroup-ignored-charsets))
8377                 (gnus-group-read-ephemeral-group
8378                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8379                               (nndoc-article-type
8380                                ,(if force 'mbox 'guess)))
8381                  t nil nil nil
8382                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8383                                                         "ADAPT")))))
8384               ;; Make all postings to this group go to the parent group.
8385               (nconc (gnus-info-params (gnus-get-info name))
8386                      params)
8387             ;; Couldn't select this doc group.
8388             (switch-to-buffer buf)
8389             (gnus-set-global-variables)
8390             (gnus-configure-windows 'summary)
8391             (gnus-message 3 "Article couldn't be entered?"))
8392         (kill-buffer dig)))))
8393
8394 (defun gnus-summary-read-document (n)
8395   "Open a new group based on the current article(s).
8396 This will allow you to read digests and other similar
8397 documents as newsgroups.
8398 Obeys the standard process/prefix convention."
8399   (interactive "P")
8400   (let* ((ogroup gnus-newsgroup-name)
8401          (params (append (gnus-info-params (gnus-get-info ogroup))
8402                          (list (cons 'to-group ogroup))))
8403          group egroup groups vgroup)
8404     (dolist (article (gnus-summary-work-articles n))
8405       (setq group (format "%s-%d" gnus-newsgroup-name article))
8406       (gnus-summary-remove-process-mark article)
8407       (when (gnus-summary-display-article article)
8408         (save-excursion
8409           (with-temp-buffer
8410             (insert-buffer-substring gnus-original-article-buffer)
8411             ;; Remove some headers that may lead nndoc to make
8412             ;; the wrong guess.
8413             (message-narrow-to-head)
8414             (goto-char (point-min))
8415             (delete-matching-lines "^Path:\\|^From ")
8416             (widen)
8417             (if (setq egroup
8418                       (gnus-group-read-ephemeral-group
8419                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8420                                      (nndoc-article-type guess))
8421                        t nil t))
8422                 (progn
8423                   ;; Make all postings to this group go to the parent group.
8424                   (nconc (gnus-info-params (gnus-get-info egroup))
8425                          params)
8426                   (push egroup groups))
8427               ;; Couldn't select this doc group.
8428               (gnus-error 3 "Article couldn't be entered"))))))
8429     ;; Now we have selected all the documents.
8430     (cond
8431      ((not groups)
8432       (error "None of the articles could be interpreted as documents"))
8433      ((gnus-group-read-ephemeral-group
8434        (setq vgroup (format
8435                      "nnvirtual:%s-%s" gnus-newsgroup-name
8436                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8437        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8438        t
8439        (cons (current-buffer) 'summary)))
8440      (t
8441       (error "Couldn't select virtual nndoc group")))))
8442
8443 (defun gnus-summary-isearch-article (&optional regexp-p)
8444   "Do incremental search forward on the current article.
8445 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8446   (interactive "P")
8447   (let* ((gnus-inhibit-treatment t)
8448          (old (gnus-summary-select-article)))
8449     (gnus-configure-windows 'article)
8450     (gnus-eval-in-buffer-window gnus-article-buffer
8451       (save-restriction
8452         (widen)
8453         (when (eq 'old old)
8454           (gnus-article-show-all-headers))
8455         (goto-char (point-min))
8456         (isearch-forward regexp-p)))))
8457
8458 (defun gnus-summary-search-article-forward (regexp &optional backward)
8459   "Search for an article containing REGEXP forward.
8460 If BACKWARD, search backward instead."
8461   (interactive
8462    (list (read-string
8463           (format "Search article %s (regexp%s): "
8464                   (if current-prefix-arg "backward" "forward")
8465                   (if gnus-last-search-regexp
8466                       (concat ", default " gnus-last-search-regexp)
8467                     "")))
8468          current-prefix-arg))
8469   (if (string-equal regexp "")
8470       (setq regexp (or gnus-last-search-regexp ""))
8471     (setq gnus-last-search-regexp regexp)
8472     (setq gnus-article-before-search gnus-current-article))
8473   ;; Intentionally set gnus-last-article.
8474   (setq gnus-last-article gnus-article-before-search)
8475   (let ((gnus-last-article gnus-last-article))
8476     (if (gnus-summary-search-article regexp backward)
8477         (gnus-summary-show-thread)
8478       (signal 'search-failed (list regexp)))))
8479
8480 (defun gnus-summary-search-article-backward (regexp)
8481   "Search for an article containing REGEXP backward."
8482   (interactive
8483    (list (read-string
8484           (format "Search article backward (regexp%s): "
8485                   (if gnus-last-search-regexp
8486                       (concat ", default " gnus-last-search-regexp)
8487                     "")))))
8488   (gnus-summary-search-article-forward regexp 'backward))
8489
8490 (eval-when-compile
8491   (defmacro gnus-summary-search-article-position-point (regexp backward)
8492     "Dehighlight the last matched text and goto the beginning position."
8493     (` (if (and gnus-summary-search-article-matched-data
8494                 (let ((text (caddr gnus-summary-search-article-matched-data))
8495                       (inhibit-read-only t)
8496                       buffer-read-only)
8497                   (delete-region
8498                    (goto-char (car gnus-summary-search-article-matched-data))
8499                    (cadr gnus-summary-search-article-matched-data))
8500                   (insert text)
8501                   (string-match (, regexp) text)))
8502            (if (, backward) (beginning-of-line) (end-of-line))
8503          (goto-char (if (, backward) (point-max) (point-min))))))
8504
8505   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8506     "Place point where X-Face image is displayed."
8507     (if (featurep 'xemacs)
8508         (` (let ((end (if (search-forward "\n\n" nil t)
8509                           (goto-char (1- (point)))
8510                         (point-min)))
8511                  extent)
8512              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8513              (unless (and (re-search-forward "^From:" end t)
8514                           (setq extent (extent-at (point)))
8515                           (extent-begin-glyph extent))
8516                (goto-char (, opoint)))))
8517       (` (let ((end (if (search-forward "\n\n" nil t)
8518                         (goto-char (1- (point)))
8519                       (point-min)))
8520                (start (or (search-backward "\n\n" nil t) (point-min))))
8521            (goto-char
8522             (or (text-property-any start end 'x-face-image t);; x-face-e21
8523                 (text-property-any start end 'x-face-mule-bitmap-image t)
8524                 (, opoint)))))))
8525
8526   (defmacro gnus-summary-search-article-highlight-matched-text
8527     (backward treated x-face)
8528     "Highlight matched text in the function `gnus-summary-search-article'."
8529     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8530              (end (set-marker (make-marker) (match-end 0)))
8531              (inhibit-read-only t)
8532              buffer-read-only)
8533          (unless treated
8534            (let ((,@
8535                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8536                     (mapcar
8537                      (lambda (item) (setq items (delq item items)))
8538                      '(gnus-treat-buttonize
8539                        gnus-treat-fill-article
8540                        gnus-treat-fill-long-lines
8541                        gnus-treat-emphasize
8542                        gnus-treat-highlight-headers
8543                        gnus-treat-highlight-citation
8544                        gnus-treat-highlight-signature
8545                        gnus-treat-overstrike
8546                        gnus-treat-display-x-face
8547                        gnus-treat-buttonize-head
8548                        gnus-treat-decode-article-as-default-mime-charset))
8549                     (static-if (featurep 'xemacs)
8550                         items
8551                       (cons '(x-face-mule-delete-x-face-field
8552                               (quote never))
8553                             items))))
8554                  (gnus-treat-display-x-face
8555                   (when (, x-face) gnus-treat-display-x-face)))
8556              (gnus-article-prepare-mime-display)))
8557          (goto-char (if (, backward) start end))
8558          (when (, x-face)
8559            (gnus-summary-search-article-highlight-goto-x-face (point)))
8560          (setq gnus-summary-search-article-matched-data
8561                (list start end (buffer-substring start end)))
8562          (unless (eq start end);; matched text has been deleted. :-<
8563            (put-text-property start end 'face
8564                               (or (find-face 'isearch)
8565                                   'secondary-selection))))))
8566   )
8567
8568 (defun gnus-summary-search-article (regexp &optional backward)
8569   "Search for an article containing REGEXP.
8570 Optional argument BACKWARD means do search for backward.
8571 `gnus-select-article-hook' is not called during the search."
8572   ;; We have to require this here to make sure that the following
8573   ;; dynamic binding isn't shadowed by autoloading.
8574   (require 'gnus-async)
8575   (require 'gnus-art)
8576   (let ((gnus-select-article-hook nil)  ;Disable hook.
8577         (gnus-article-prepare-hook nil)
8578         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8579         (gnus-use-article-prefetch nil)
8580         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8581         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8582         (gnus-visual nil)
8583         (gnus-keep-backlog nil)
8584         (gnus-break-pages nil)
8585         (gnus-summary-display-arrow nil)
8586         (gnus-updated-mode-lines nil)
8587         (gnus-auto-center-summary nil)
8588         (sum (current-buffer))
8589         (found nil)
8590         point treated)
8591     (gnus-save-hidden-threads
8592       (static-if (featurep 'xemacs)
8593           (let ((gnus-inhibit-treatment t))
8594             (setq treated (eq 'old (gnus-summary-select-article)))
8595             (when (and treated
8596                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8597                                  (window-live-p (get-buffer-window
8598                                                  gnus-article-buffer t)))))
8599               (gnus-summary-select-article nil t)
8600               (setq treated nil)))
8601         (let ((gnus-inhibit-treatment t)
8602               (x-face-mule-delete-x-face-field 'never))
8603           (setq treated (eq 'old (gnus-summary-select-article)))
8604           (when (and treated
8605                      (not
8606                       (and (gnus-buffer-live-p gnus-article-buffer)
8607                            (window-live-p (get-buffer-window
8608                                            gnus-article-buffer t))
8609                            (or (not (string-match "^\\^X-Face:" regexp))
8610                                (with-current-buffer gnus-article-buffer
8611                                  gnus-summary-search-article-matched-data)))))
8612             (gnus-summary-select-article nil t)
8613             (setq treated nil))))
8614       (set-buffer gnus-article-buffer)
8615       (widen)
8616       (if treated
8617           (progn
8618             (gnus-article-show-all-headers)
8619             (gnus-summary-search-article-position-point regexp backward))
8620         (goto-char (if backward (point-max) (point-min))))
8621       (while (not found)
8622         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8623         (if (if backward
8624                 (re-search-backward regexp nil t)
8625               (re-search-forward regexp nil t))
8626             ;; We found the regexp.
8627             (progn
8628               (gnus-summary-search-article-highlight-matched-text
8629                backward treated (string-match "^\\^X-Face:" regexp))
8630               (setq found 'found)
8631               (forward-line
8632                (/ (- 2 (window-height
8633                         (get-buffer-window gnus-article-buffer t)))
8634                   2))
8635               (set-window-start
8636                (get-buffer-window (current-buffer))
8637                (point))
8638               (set-buffer sum)
8639               (setq point (point)))
8640           ;; We didn't find it, so we go to the next article.
8641           (set-buffer sum)
8642           (setq found 'not)
8643           (while (eq found 'not)
8644             (if (not (if backward (gnus-summary-find-prev)
8645                        (gnus-summary-find-next)))
8646                 ;; No more articles.
8647                 (setq found t)
8648               ;; Select the next article and adjust point.
8649               (unless (gnus-summary-article-sparse-p
8650                        (gnus-summary-article-number))
8651                 (setq found nil)
8652                 (let ((gnus-inhibit-treatment t))
8653                   (gnus-summary-select-article))
8654                 (setq treated nil)
8655                 (set-buffer gnus-article-buffer)
8656                 (widen)
8657                 (goto-char (if backward (point-max) (point-min))))))))
8658       (gnus-message 7 ""))
8659     ;; Return whether we found the regexp.
8660     (when (eq found 'found)
8661       (goto-char point)
8662       (gnus-summary-show-thread)
8663       (gnus-summary-goto-subject gnus-current-article)
8664       (gnus-summary-position-point)
8665       t)))
8666
8667 (defun gnus-find-matching-articles (header regexp)
8668   "Return a list of all articles that match REGEXP on HEADER.
8669 This search includes all articles in the current group that Gnus has
8670 fetched headers for, whether they are displayed or not."
8671   (let ((articles nil)
8672         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8673         (case-fold-search t))
8674     (dolist (header gnus-newsgroup-headers)
8675       (when (string-match regexp (funcall func header))
8676         (push (mail-header-number header) articles)))
8677     (nreverse articles)))
8678
8679 (defun gnus-summary-find-matching (header regexp &optional backward unread
8680                                           not-case-fold not-matching)
8681   "Return a list of all articles that match REGEXP on HEADER.
8682 The search stars on the current article and goes forwards unless
8683 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8684 If UNREAD is non-nil, only unread articles will
8685 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8686 in the comparisons. If NOT-MATCHING, return a list of all articles that
8687 not match REGEXP on HEADER."
8688   (let ((case-fold-search (not not-case-fold))
8689         articles d func)
8690     (if (consp header)
8691         (if (eq (car header) 'extra)
8692             (setq func
8693                   `(lambda (h)
8694                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8695                          "")))
8696           (error "%s is an invalid header" header))
8697       (unless (fboundp (intern (concat "mail-header-" header)))
8698         (error "%s is not a valid header" header))
8699       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8700     (dolist (d (if (eq backward 'all)
8701                    gnus-newsgroup-data
8702                  (gnus-data-find-list
8703                   (gnus-summary-article-number)
8704                   (gnus-data-list backward))))
8705       (when (and (or (not unread)       ; We want all articles...
8706                      (gnus-data-unread-p d)) ; Or just unreads.
8707                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8708                  (if not-matching
8709                      (not (string-match
8710                            regexp
8711                            (funcall func (gnus-data-header d))))
8712                    (string-match regexp
8713                                  (funcall func (gnus-data-header d)))))
8714         (push (gnus-data-number d) articles))) ; Success!
8715     (nreverse articles)))
8716
8717 (defun gnus-summary-execute-command (header regexp command &optional backward)
8718   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8719 If HEADER is an empty string (or nil), the match is done on the entire
8720 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8721   (interactive
8722    (list (let ((completion-ignore-case t))
8723            (completing-read
8724             "Header name: "
8725             (mapcar (lambda (header) (list (format "%s" header)))
8726                     (append
8727                      '("Number" "Subject" "From" "Lines" "Date"
8728                        "Message-ID" "Xref" "References" "Body")
8729                      gnus-extra-headers))
8730             nil 'require-match))
8731          (read-string "Regexp: ")
8732          (read-key-sequence "Command: ")
8733          current-prefix-arg))
8734   (when (equal header "Body")
8735     (setq header ""))
8736   ;; Hidden thread subtrees must be searched as well.
8737   (gnus-summary-show-all-threads)
8738   ;; We don't want to change current point nor window configuration.
8739   (save-excursion
8740     (save-window-excursion
8741       (let (gnus-visual
8742             gnus-treat-strip-trailing-blank-lines
8743             gnus-treat-strip-leading-blank-lines
8744             gnus-treat-strip-multiple-blank-lines
8745             gnus-treat-hide-boring-headers
8746             gnus-treat-fold-newsgroups
8747             gnus-article-prepare-hook)
8748         (gnus-message 6 "Executing %s..." (key-description command))
8749         ;; We'd like to execute COMMAND interactively so as to give arguments.
8750         (gnus-execute header regexp
8751                       `(call-interactively ',(key-binding command))
8752                       backward)
8753         (gnus-message 6 "Executing %s...done" (key-description command))))))
8754
8755 (defun gnus-summary-beginning-of-article ()
8756   "Scroll the article back to the beginning."
8757   (interactive)
8758   (gnus-summary-select-article)
8759   (gnus-configure-windows 'article)
8760   (gnus-eval-in-buffer-window gnus-article-buffer
8761     (widen)
8762     (goto-char (point-min))
8763     (when gnus-break-pages
8764       (gnus-narrow-to-page))))
8765
8766 (defun gnus-summary-end-of-article ()
8767   "Scroll to the end of the article."
8768   (interactive)
8769   (gnus-summary-select-article)
8770   (gnus-configure-windows 'article)
8771   (gnus-eval-in-buffer-window gnus-article-buffer
8772     (widen)
8773     (goto-char (point-max))
8774     (recenter -3)
8775     (when gnus-break-pages
8776       (when (re-search-backward page-delimiter nil t)
8777         (narrow-to-region (match-end 0) (point-max)))
8778       (gnus-narrow-to-page))))
8779
8780 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8781   "Truncate to LEN and quote all \"(\"'s in STRING."
8782   (gnus-replace-in-string (if (and len (> (length string) len))
8783                               (substring string 0 len)
8784                             string)
8785                           "[()]" "\\\\\\&"))
8786
8787 (defun gnus-summary-print-article (&optional filename n)
8788   "Generate and print a PostScript image of the process-marked (mail) articles.
8789
8790 If used interactively, print the current article if none are
8791 process-marked.  With prefix arg, prompt the user for the name of the
8792 file to save in.
8793
8794 When used from Lisp, accept two optional args FILENAME and N.  N means
8795 to print the next N articles.  If N is negative, print the N previous
8796 articles.  If N is nil and articles have been marked with the process
8797 mark, print these instead.
8798
8799 If the optional first argument FILENAME is nil, send the image to the
8800 printer.  If FILENAME is a string, save the PostScript image in a file with
8801 that name.  If FILENAME is a number, prompt the user for the name of the file
8802 to save in."
8803   (interactive (list (ps-print-preprint current-prefix-arg)))
8804   (dolist (article (gnus-summary-work-articles n))
8805     (gnus-summary-select-article nil nil 'pseudo article)
8806     (gnus-eval-in-buffer-window gnus-article-buffer
8807       (gnus-print-buffer))
8808     (gnus-summary-remove-process-mark article))
8809   (ps-despool filename))
8810
8811 (defun gnus-print-buffer ()
8812   (let ((buffer (generate-new-buffer " *print*")))
8813     (unwind-protect
8814         (progn
8815           (copy-to-buffer buffer (point-min) (point-max))
8816           (set-buffer buffer)
8817           (gnus-remove-text-with-property 'gnus-decoration)
8818           (when (gnus-visual-p 'article-highlight 'highlight)
8819             ;; Copy-to-buffer doesn't copy overlay.  So redo
8820             ;; highlight.
8821             (let ((gnus-article-buffer buffer))
8822               (gnus-article-highlight-citation t)
8823               (gnus-article-highlight-signature)
8824               (gnus-article-emphasize)
8825               (gnus-article-delete-invisible-text)))
8826           (let ((ps-left-header
8827                  (list
8828                   (concat "("
8829                           (gnus-summary-print-truncate-and-quote
8830                            (mail-header-subject gnus-current-headers)
8831                            66) ")")
8832                   (concat "("
8833                           (gnus-summary-print-truncate-and-quote
8834                            (mail-header-from gnus-current-headers)
8835                            45) ")")))
8836                 (ps-right-header
8837                  (list
8838                   "/pagenumberstring load"
8839                   (concat "("
8840                           (mail-header-date gnus-current-headers) ")"))))
8841             (gnus-run-hooks 'gnus-ps-print-hook)
8842             (save-excursion
8843               (if window-system
8844                   (ps-spool-buffer-with-faces)
8845                 (ps-spool-buffer)))))
8846       (kill-buffer buffer))))
8847
8848 (defun gnus-summary-show-article (&optional arg)
8849   "Force redisplaying of the current article.
8850 If ARG (the prefix) is a number, show the article with the charset
8851 defined in `gnus-summary-show-article-charset-alist', or the charset
8852 input.
8853 If ARG (the prefix) is non-nil and not a number, show the raw article
8854 without any article massaging functions being run.  Normally, the key
8855 strokes are `C-u g'."
8856   (interactive "P")
8857   (cond
8858    ((numberp arg)
8859     (gnus-summary-show-article t)
8860     (let* ((gnus-newsgroup-charset
8861             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8862                 (mm-read-coding-system
8863                  "View as charset: " ;; actually it is coding system.
8864                  (with-current-buffer gnus-article-buffer
8865                    (mm-detect-coding-region (point) (point-max))))))
8866            (default-mime-charset gnus-newsgroup-charset)
8867            (gnus-newsgroup-ignored-charsets 'gnus-all))
8868       (gnus-summary-select-article nil 'force)
8869       (let ((deps gnus-newsgroup-dependencies)
8870             head header lines)
8871         (save-excursion
8872           (set-buffer gnus-original-article-buffer)
8873           (save-restriction
8874             (message-narrow-to-head)
8875             (setq head (buffer-string))
8876             (goto-char (point-min))
8877             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8878               (goto-char (point-max))
8879               (widen)
8880               (setq lines (1- (count-lines (point) (point-max))))))
8881           (with-temp-buffer
8882             (insert (format "211 %d Article retrieved.\n"
8883                             (cdr gnus-article-current)))
8884             (insert head)
8885             (if lines (insert (format "Lines: %d\n" lines)))
8886             (insert ".\n")
8887             (let ((nntp-server-buffer (current-buffer)))
8888               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8889         (gnus-data-set-header
8890          (gnus-data-find (cdr gnus-article-current))
8891          header)
8892         (gnus-summary-update-article-line
8893          (cdr gnus-article-current) header)
8894         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8895           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8896    ((not arg)
8897     ;; Select the article the normal way.
8898     (gnus-summary-select-article nil 'force))
8899    (t
8900     ;; We have to require this here to make sure that the following
8901     ;; dynamic binding isn't shadowed by autoloading.
8902     (require 'gnus-async)
8903     (require 'gnus-art)
8904     ;; Bind the article treatment functions to nil.
8905     (let ((gnus-have-all-headers t)
8906           gnus-article-prepare-hook
8907           gnus-article-decode-hook
8908           gnus-break-pages
8909           gnus-show-mime
8910           (gnus-inhibit-treatment t))
8911       (gnus-summary-select-article nil 'force))))
8912   (gnus-summary-goto-subject gnus-current-article)
8913   (gnus-summary-position-point))
8914
8915 (defun gnus-summary-show-raw-article ()
8916   "Show the raw article without any article massaging functions being run."
8917   (interactive)
8918   (gnus-summary-show-article t))
8919
8920 (defun gnus-summary-verbose-headers (&optional arg)
8921   "Toggle permanent full header display.
8922 If ARG is a positive number, turn header display on.
8923 If ARG is a negative number, turn header display off."
8924   (interactive "P")
8925   (setq gnus-show-all-headers
8926         (cond ((or (not (numberp arg))
8927                    (zerop arg))
8928                (not gnus-show-all-headers))
8929               ((natnump arg)
8930                t)))
8931   (gnus-summary-show-article))
8932
8933 (defun gnus-summary-toggle-header (&optional arg)
8934   "Show the headers if they are hidden, or hide them if they are shown.
8935 If ARG is a positive number, show the entire header.
8936 If ARG is a negative number, hide the unwanted header lines."
8937   (interactive "P")
8938   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8939                      (get-buffer-window gnus-article-buffer t))))
8940     (with-current-buffer gnus-article-buffer
8941       (widen)
8942       (article-narrow-to-head)
8943       (let* ((buffer-read-only nil)
8944              (inhibit-point-motion-hooks t)
8945              (hidden (if (numberp arg)
8946                          (>= arg 0)
8947                        (or (not (looking-at "[^ \t\n]+:"))
8948                            (gnus-article-hidden-text-p 'headers))))
8949              s e)
8950         (delete-region (point-min) (point-max))
8951         (with-current-buffer gnus-original-article-buffer
8952           (goto-char (setq s (point-min)))
8953           (setq e (if (search-forward "\n\n" nil t)
8954                       (1- (point))
8955                     (point-max))))
8956         (insert-buffer-substring gnus-original-article-buffer s e)
8957         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8958         ;; article-decode-encoded-words by default.
8959         (article-decode-encoded-words)
8960         (run-hooks 'gnus-article-decode-hook)
8961         (if hidden
8962             (let ((gnus-treat-hide-headers nil)
8963                   (gnus-treat-hide-boring-headers nil))
8964               (gnus-delete-wash-type 'headers)
8965               (gnus-treat-article 'head))
8966           (gnus-treat-article 'head))
8967         (widen)
8968         (if window
8969             (set-window-start window (goto-char (point-min))))
8970         (if gnus-break-pages
8971             (gnus-narrow-to-page)
8972           (when (gnus-visual-p 'page-marker)
8973             (let ((buffer-read-only nil))
8974               (gnus-remove-text-with-property 'gnus-prev)
8975               (gnus-remove-text-with-property 'gnus-next))))
8976         (gnus-set-mode-line 'article)))))
8977
8978 (defun gnus-summary-show-all-headers ()
8979   "Make all header lines visible."
8980   (interactive)
8981   (gnus-summary-toggle-header 1))
8982
8983 (defun gnus-summary-toggle-mime (&optional arg)
8984   "Toggle MIME processing.
8985 If ARG is a positive number, turn MIME processing on."
8986   (interactive "P")
8987   (setq gnus-show-mime
8988         (if (null arg)
8989             (not gnus-show-mime)
8990           (> (prefix-numeric-value arg) 0)))
8991   (gnus-summary-select-article t 'force))
8992
8993 (defun gnus-summary-caesar-message (&optional arg)
8994   "Caesar rotate the current article by 13.
8995 The numerical prefix specifies how many places to rotate each letter
8996 forward."
8997   (interactive "P")
8998   (gnus-summary-select-article)
8999   (let ((mail-header-separator ""))
9000     (gnus-eval-in-buffer-window gnus-article-buffer
9001       (save-restriction
9002         (widen)
9003         (let ((start (window-start))
9004               buffer-read-only)
9005           (message-caesar-buffer-body arg)
9006           (set-window-start (get-buffer-window (current-buffer)) start))))))
9007
9008 (autoload 'unmorse-region "morse"
9009   "Convert morse coded text in region to ordinary ASCII text."
9010   t)
9011
9012 (defun gnus-summary-morse-message (&optional arg)
9013   "Morse decode the current article."
9014   (interactive "P")
9015   (gnus-summary-select-article)
9016   (let ((mail-header-separator ""))
9017     (gnus-eval-in-buffer-window gnus-article-buffer
9018       (save-excursion
9019         (save-restriction
9020           (widen)
9021           (let ((pos (window-start))
9022                 buffer-read-only)
9023             (goto-char (point-min))
9024             (when (message-goto-body)
9025               (gnus-narrow-to-body))
9026             (goto-char (point-min))
9027             (while (search-forward "·" (point-max) t)
9028               (replace-match "."))
9029             (unmorse-region (point-min) (point-max))
9030             (widen)
9031             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
9032
9033 (defun gnus-summary-stop-page-breaking ()
9034   "Stop page breaking in the current article."
9035   (interactive)
9036   (gnus-summary-select-article)
9037   (gnus-eval-in-buffer-window gnus-article-buffer
9038     (widen)
9039     (when (gnus-visual-p 'page-marker)
9040       (let ((buffer-read-only nil))
9041         (gnus-remove-text-with-property 'gnus-prev)
9042         (gnus-remove-text-with-property 'gnus-next))
9043       (setq gnus-page-broken nil))))
9044
9045 (defun gnus-summary-move-article (&optional n to-newsgroup
9046                                             select-method action)
9047   "Move the current article to a different newsgroup.
9048 If N is a positive number, move the N next articles.
9049 If N is a negative number, move the N previous articles.
9050 If N is nil and any articles have been marked with the process mark,
9051 move those articles instead.
9052 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9053 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9054 re-spool using this method.
9055
9056 When called interactively with TO-NEWSGROUP being nil, the value of
9057 the variable `gnus-move-split-methods' is used for finding a default
9058 for the target newsgroup.
9059
9060 For this function to work, both the current newsgroup and the
9061 newsgroup that you want to move to have to support the `request-move'
9062 and `request-accept' functions.
9063
9064 ACTION can be either `move' (the default), `crosspost' or `copy'."
9065   (interactive "P")
9066   (unless action
9067     (setq action 'move))
9068   ;; Check whether the source group supports the required functions.
9069   (cond ((and (eq action 'move)
9070               (not (gnus-check-backend-function
9071                     'request-move-article gnus-newsgroup-name)))
9072          (error "The current group does not support article moving"))
9073         ((and (eq action 'crosspost)
9074               (not (gnus-check-backend-function
9075                     'request-replace-article gnus-newsgroup-name)))
9076          (error "The current group does not support article editing")))
9077   (let ((articles (gnus-summary-work-articles n))
9078         (prefix (if (gnus-check-backend-function
9079                      'request-move-article gnus-newsgroup-name)
9080                     (funcall gnus-move-group-prefix-function
9081                              gnus-newsgroup-name)
9082                   ""))
9083         (names '((move "Move" "Moving")
9084                  (copy "Copy" "Copying")
9085                  (crosspost "Crosspost" "Crossposting")))
9086         (copy-buf (save-excursion
9087                     (nnheader-set-temp-buffer " *copy article*")))
9088         art-group to-method new-xref article to-groups)
9089     (unless (assq action names)
9090       (error "Unknown action %s" action))
9091     ;; Read the newsgroup name.
9092     (when (and (not to-newsgroup)
9093                (not select-method))
9094       (if (and gnus-move-split-methods
9095                (not
9096                 (and (memq gnus-current-article articles)
9097                      (gnus-buffer-live-p gnus-original-article-buffer))))
9098           ;; When `gnus-move-split-methods' is non-nil, we have to
9099           ;; select an article to give `gnus-read-move-group-name' an
9100           ;; opportunity to suggest an appropriate default.  However,
9101           ;; we needn't render or mark the article.
9102           (let ((gnus-display-mime-function nil)
9103                 (gnus-article-prepare-hook nil)
9104                 (gnus-mark-article-hook nil))
9105             (gnus-summary-select-article nil nil nil (car articles))))
9106       (setq to-newsgroup
9107             (gnus-read-move-group-name
9108              (cadr (assq action names))
9109              (symbol-value (intern (format "gnus-current-%s-group" action)))
9110              articles prefix))
9111       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9112     (setq to-method (or select-method
9113                         (gnus-server-to-method
9114                          (gnus-group-method to-newsgroup))))
9115     ;; Check the method we are to move this article to...
9116     (unless (gnus-check-backend-function
9117              'request-accept-article (car to-method))
9118       (error "%s does not support article copying" (car to-method)))
9119     (unless (gnus-check-server to-method)
9120       (error "Can't open server %s" (car to-method)))
9121     (gnus-message 6 "%s to %s: %s..."
9122                   (caddr (assq action names))
9123                   (or (car select-method) to-newsgroup) articles)
9124     (while articles
9125       (setq article (pop articles))
9126       (setq
9127        art-group
9128        (cond
9129         ;; Move the article.
9130         ((eq action 'move)
9131          ;; Remove this article from future suppression.
9132          (gnus-dup-unsuppress-article article)
9133          (gnus-request-move-article
9134           article                       ; Article to move
9135           gnus-newsgroup-name           ; From newsgroup
9136           (nth 1 (gnus-find-method-for-group
9137                   gnus-newsgroup-name)) ; Server
9138           (list 'gnus-request-accept-article
9139                 to-newsgroup (list 'quote select-method)
9140                 (not articles) t)       ; Accept form
9141           (not articles)))              ; Only save nov last time
9142         ;; Copy the article.
9143         ((eq action 'copy)
9144          (save-excursion
9145            (set-buffer copy-buf)
9146            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9147              (save-restriction
9148                (nnheader-narrow-to-headers)
9149                (dolist (hdr gnus-copy-article-ignored-headers)
9150                  (message-remove-header hdr t)))
9151              (gnus-request-accept-article
9152               to-newsgroup select-method (not articles) t))))
9153         ;; Crosspost the article.
9154         ((eq action 'crosspost)
9155          (let ((xref (message-tokenize-header
9156                       (mail-header-xref (gnus-summary-article-header article))
9157                       " ")))
9158            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9159                                   ":" (number-to-string article)))
9160            (unless xref
9161              (setq xref (list (system-name))))
9162            (setq new-xref
9163                  (concat
9164                   (mapconcat 'identity
9165                              (delete "Xref:" (delete new-xref xref))
9166                              " ")
9167                   " " new-xref))
9168            (save-excursion
9169              (set-buffer copy-buf)
9170              ;; First put the article in the destination group.
9171              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9172              (when (consp (setq art-group
9173                                 (gnus-request-accept-article
9174                                  to-newsgroup select-method (not articles))))
9175                (setq new-xref (concat new-xref " " (car art-group)
9176                                       ":"
9177                                       (number-to-string (cdr art-group))))
9178                ;; Now we have the new Xrefs header, so we insert
9179                ;; it and replace the new article.
9180                (nnheader-replace-header "Xref" new-xref)
9181                (gnus-request-replace-article
9182                 (cdr art-group) to-newsgroup (current-buffer))
9183                art-group))))))
9184       (cond
9185        ((not art-group)
9186         (gnus-message 1 "Couldn't %s article %s: %s"
9187                       (cadr (assq action names)) article
9188                       (nnheader-get-report (car to-method))))
9189        ((eq art-group 'junk)
9190         (when (eq action 'move)
9191           (gnus-summary-mark-article article gnus-canceled-mark)
9192           (gnus-message 4 "Deleted article %s" article)
9193           ;; run the delete hook
9194           (run-hook-with-args 'gnus-summary-article-delete-hook
9195                               action
9196                               (gnus-data-header
9197                                (assoc article (gnus-data-list nil)))
9198                               gnus-newsgroup-name nil
9199                               select-method)))
9200        (t
9201         (let* ((pto-group (gnus-group-prefixed-name
9202                            (car art-group) to-method))
9203                (info (gnus-get-info pto-group))
9204                (to-group (gnus-info-group info))
9205                to-marks)
9206           ;; Update the group that has been moved to.
9207           (when (and info
9208                      (memq action '(move copy)))
9209             (unless (member to-group to-groups)
9210               (push to-group to-groups))
9211
9212             (unless (memq article gnus-newsgroup-unreads)
9213               (push 'read to-marks)
9214               (gnus-info-set-read
9215                info (gnus-add-to-range (gnus-info-read info)
9216                                        (list (cdr art-group)))))
9217
9218             ;; See whether the article is to be put in the cache.
9219             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9220                              gnus-article-mark-lists
9221                            (delete '(expirable . expire)
9222                                    (copy-sequence gnus-article-mark-lists))))
9223                   (to-article (cdr art-group)))
9224
9225               ;; Enter the article into the cache in the new group,
9226               ;; if that is required.
9227               (when gnus-use-cache
9228                 (gnus-cache-possibly-enter-article
9229                  to-group to-article
9230                  (let ((header (copy-sequence
9231                                 (gnus-summary-article-header article))))
9232                    (mail-header-set-number header to-article)
9233                    header)
9234                  (memq article gnus-newsgroup-marked)
9235                  (memq article gnus-newsgroup-dormant)
9236                  (memq article gnus-newsgroup-unreads)))
9237
9238               (when gnus-preserve-marks
9239                 ;; Copy any marks over to the new group.
9240                 (when (and (equal to-group gnus-newsgroup-name)
9241                            (not (memq article gnus-newsgroup-unreads)))
9242                   ;; Mark this article as read in this group.
9243                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9244                   (setcdr (gnus-active to-group) to-article)
9245                   (setcdr gnus-newsgroup-active to-article))
9246
9247                 (while marks
9248                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9249                     (when (memq article (symbol-value
9250                                          (intern (format "gnus-newsgroup-%s"
9251                                                          (caar marks)))))
9252                       (push (cdar marks) to-marks)
9253                       ;; If the other group is the same as this group,
9254                       ;; then we have to add the mark to the list.
9255                       (when (equal to-group gnus-newsgroup-name)
9256                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9257                              (cons to-article
9258                                    (symbol-value
9259                                     (intern (format "gnus-newsgroup-%s"
9260                                                     (caar marks)))))))
9261                       ;; Copy the marks to other group.
9262                       (gnus-add-marked-articles
9263                        to-group (cdar marks) (list to-article) info)))
9264                   (setq marks (cdr marks)))
9265
9266                 (gnus-request-set-mark
9267                  to-group (list (list (list to-article) 'add to-marks))))
9268
9269               (gnus-dribble-enter
9270                (concat "(gnus-group-set-info '"
9271                        (gnus-prin1-to-string (gnus-get-info to-group))
9272                        ")"))))
9273
9274           ;; Update the Xref header in this article to point to
9275           ;; the new crossposted article we have just created.
9276           (when (eq action 'crosspost)
9277             (save-excursion
9278               (set-buffer copy-buf)
9279               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9280               (nnheader-replace-header "Xref" new-xref)
9281               (gnus-request-replace-article
9282                article gnus-newsgroup-name (current-buffer))))
9283
9284           ;; run the move/copy/crosspost/respool hook
9285           (run-hook-with-args 'gnus-summary-article-move-hook
9286                               action
9287                               (gnus-data-header
9288                                (assoc article (gnus-data-list nil)))
9289                               gnus-newsgroup-name
9290                               to-newsgroup
9291                               select-method))
9292
9293         ;;;!!!Why is this necessary?
9294         (set-buffer gnus-summary-buffer)
9295         
9296         (gnus-summary-goto-subject article)
9297         (when (eq action 'move)
9298           (gnus-summary-mark-article article gnus-canceled-mark))))
9299       (gnus-summary-remove-process-mark article))
9300     ;; Re-activate all groups that have been moved to.
9301     (save-excursion
9302       (set-buffer gnus-group-buffer)
9303       (let ((gnus-group-marked to-groups))
9304         (gnus-group-get-new-news-this-group nil t)))
9305
9306     (gnus-kill-buffer copy-buf)
9307     (gnus-summary-position-point)
9308     (gnus-set-mode-line 'summary)))
9309
9310 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9311   "Copy the current article to some other group.
9312 If TO-NEWSGROUP is string, do not prompt for a newsgroup to copy to.
9313 When called interactively, if TO-NEWSGROUP is nil, use the value of
9314 the variable `gnus-move-split-methods' for finding a default target
9315 newsgroup.
9316 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9317 re-spool using this method."
9318   (interactive "P")
9319   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9320
9321 (defun gnus-summary-crosspost-article (&optional n)
9322   "Crosspost the current article to some other group."
9323   (interactive "P")
9324   (gnus-summary-move-article n nil nil 'crosspost))
9325
9326 (defcustom gnus-summary-respool-default-method nil
9327   "Default method type for respooling an article.
9328 If nil, use to the current newsgroup method."
9329   :type 'symbol
9330   :group 'gnus-summary-mail)
9331
9332 (defcustom gnus-summary-display-while-building nil
9333   "If non-nil, show and update the summary buffer as it's being built.
9334 If the value is t, update the buffer after every line is inserted.  If
9335 the value is an integer (N), update the display every N lines."
9336   :group 'gnus-thread
9337   :type '(choice (const :tag "off" nil)
9338                  number
9339                  (const :tag "frequently" t)))
9340
9341 (defun gnus-summary-respool-article (&optional n method)
9342   "Respool the current article.
9343 The article will be squeezed through the mail spooling process again,
9344 which means that it will be put in some mail newsgroup or other
9345 depending on `nnmail-split-methods'.
9346 If N is a positive number, respool the N next articles.
9347 If N is a negative number, respool the N previous articles.
9348 If N is nil and any articles have been marked with the process mark,
9349 respool those articles instead.
9350
9351 Respooling can be done both from mail groups and \"real\" newsgroups.
9352 In the former case, the articles in question will be moved from the
9353 current group into whatever groups they are destined to.  In the
9354 latter case, they will be copied into the relevant groups."
9355   (interactive
9356    (list current-prefix-arg
9357          (let* ((methods (gnus-methods-using 'respool))
9358                 (methname
9359                  (symbol-name (or gnus-summary-respool-default-method
9360                                   (car (gnus-find-method-for-group
9361                                         gnus-newsgroup-name)))))
9362                 (method
9363                  (gnus-completing-read-with-default
9364                   methname "What backend do you want to use when respooling?"
9365                   methods nil t nil 'gnus-mail-method-history))
9366                 ms)
9367            (cond
9368             ((zerop (length (setq ms (gnus-servers-using-backend
9369                                       (intern method)))))
9370              (list (intern method) ""))
9371             ((= 1 (length ms))
9372              (car ms))
9373             (t
9374              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9375                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9376                            ms-alist))))))))
9377   (unless method
9378     (error "No method given for respooling"))
9379   (if (assoc (symbol-name
9380               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9381              (gnus-methods-using 'respool))
9382       (gnus-summary-move-article n nil method)
9383     (gnus-summary-copy-article n nil method)))
9384
9385 (defun gnus-summary-import-article (file &optional edit)
9386   "Import an arbitrary file into a mail newsgroup."
9387   (interactive "fImport file: \nP")
9388   (let ((group gnus-newsgroup-name)
9389         (now (current-time))
9390         atts lines group-art)
9391     (unless (gnus-check-backend-function 'request-accept-article group)
9392       (error "%s does not support article importing" group))
9393     (or (file-readable-p file)
9394         (not (file-regular-p file))
9395         (error "Can't read %s" file))
9396     (save-excursion
9397       (set-buffer (gnus-get-buffer-create " *import file*"))
9398       (erase-buffer)
9399       (nnheader-insert-file-contents file)
9400       (goto-char (point-min))
9401       (if (nnheader-article-p)
9402           (save-restriction
9403             (goto-char (point-min))
9404             (search-forward "\n\n" nil t)
9405             (narrow-to-region (point-min) (1- (point)))
9406             (goto-char (point-min))
9407             (unless (re-search-forward "^date:" nil t)
9408               (goto-char (point-max))
9409               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9410         ;; This doesn't look like an article, so we fudge some headers.
9411         (setq atts (file-attributes file)
9412               lines (count-lines (point-min) (point-max)))
9413         (insert "From: " (read-string "From: ") "\n"
9414                 "Subject: " (read-string "Subject: ") "\n"
9415                 "Date: " (message-make-date (nth 5 atts)) "\n"
9416                 "Message-ID: " (message-make-message-id) "\n"
9417                 "Lines: " (int-to-string lines) "\n"
9418                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9419       (setq group-art (gnus-request-accept-article group nil t))
9420       (kill-buffer (current-buffer)))
9421     (setq gnus-newsgroup-active (gnus-activate-group group))
9422     (forward-line 1)
9423     (gnus-summary-goto-article (cdr group-art) nil t)
9424     (when edit
9425       (gnus-summary-edit-article))))
9426
9427 (defun gnus-summary-create-article ()
9428   "Create an article in a mail newsgroup."
9429   (interactive)
9430   (let ((group gnus-newsgroup-name)
9431         (now (current-time))
9432         group-art)
9433     (unless (gnus-check-backend-function 'request-accept-article group)
9434       (error "%s does not support article importing" group))
9435     (save-excursion
9436       (set-buffer (gnus-get-buffer-create " *import file*"))
9437       (erase-buffer)
9438       (goto-char (point-min))
9439       ;; This doesn't look like an article, so we fudge some headers.
9440       (insert "From: " (read-string "From: ") "\n"
9441               "Subject: " (read-string "Subject: ") "\n"
9442               "Date: " (message-make-date now) "\n"
9443               "Message-ID: " (message-make-message-id) "\n")
9444       (setq group-art (gnus-request-accept-article group nil t))
9445       (kill-buffer (current-buffer)))
9446     (setq gnus-newsgroup-active (gnus-activate-group group))
9447     (forward-line 1)
9448     (gnus-summary-goto-article (cdr group-art) nil t)
9449     (gnus-summary-edit-article)))
9450
9451 (defun gnus-summary-article-posted-p ()
9452   "Say whether the current (mail) article is available from news as well.
9453 This will be the case if the article has both been mailed and posted."
9454   (interactive)
9455   (let ((id (mail-header-references (gnus-summary-article-header)))
9456         (gnus-override-method (car (gnus-refer-article-methods))))
9457     (if (gnus-request-head id "")
9458         (gnus-message 2 "The current message was found on %s"
9459                       gnus-override-method)
9460       (gnus-message 2 "The current message couldn't be found on %s"
9461                     gnus-override-method)
9462       nil)))
9463
9464 (defun gnus-summary-expire-articles (&optional now)
9465   "Expire all articles that are marked as expirable in the current group."
9466   (interactive)
9467   (when (and (not gnus-group-is-exiting-without-update-p)
9468              (gnus-check-backend-function
9469               'request-expire-articles gnus-newsgroup-name))
9470     ;; This backend supports expiry.
9471     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9472            (expirable (if total
9473                           (progn
9474                             ;; We need to update the info for
9475                             ;; this group for `gnus-list-of-read-articles'
9476                             ;; to give us the right answer.
9477                             (gnus-run-hooks 'gnus-exit-group-hook)
9478                             (gnus-summary-update-info)
9479                             (gnus-list-of-read-articles gnus-newsgroup-name))
9480                         (setq gnus-newsgroup-expirable
9481                               (sort gnus-newsgroup-expirable '<))))
9482            (expiry-wait (if now 'immediate
9483                           (gnus-group-find-parameter
9484                            gnus-newsgroup-name 'expiry-wait)))
9485            (nnmail-expiry-target
9486             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9487                 nnmail-expiry-target))
9488            es)
9489       (when expirable
9490         ;; There are expirable articles in this group, so we run them
9491         ;; through the expiry process.
9492         (gnus-message 6 "Expiring articles...")
9493         (unless (gnus-check-group gnus-newsgroup-name)
9494           (error "Can't open server for %s" gnus-newsgroup-name))
9495         ;; The list of articles that weren't expired is returned.
9496         (save-excursion
9497           (if expiry-wait
9498               (let ((nnmail-expiry-wait-function nil)
9499                     (nnmail-expiry-wait expiry-wait))
9500                 (setq es (gnus-request-expire-articles
9501                           expirable gnus-newsgroup-name)))
9502             (setq es (gnus-request-expire-articles
9503                       expirable gnus-newsgroup-name)))
9504           (unless total
9505             (setq gnus-newsgroup-expirable es))
9506           ;; We go through the old list of expirable, and mark all
9507           ;; really expired articles as nonexistent.
9508           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9509             (let ((gnus-use-cache nil))
9510               (dolist (article expirable)
9511                 (when (and (not (memq article es))
9512                            (gnus-data-find article))
9513                   (gnus-summary-mark-article article gnus-canceled-mark)
9514                   (run-hook-with-args 'gnus-summary-article-expire-hook
9515                                       'delete
9516                                       (gnus-data-header
9517                                        (assoc article (gnus-data-list nil)))
9518                                       gnus-newsgroup-name
9519                                       nil
9520                                       nil))))))
9521         (gnus-message 6 "Expiring articles...done")))))
9522
9523 (defun gnus-summary-expire-articles-now ()
9524   "Expunge all expirable articles in the current group.
9525 This means that *all* articles that are marked as expirable will be
9526 deleted forever, right now."
9527   (interactive)
9528   (or gnus-expert-user
9529       (gnus-yes-or-no-p
9530        "Are you really, really, really sure you want to delete all these messages? ")
9531       (error "Phew!"))
9532   (gnus-summary-expire-articles t))
9533
9534 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9535 (defun gnus-summary-delete-article (&optional n)
9536   "Delete the N next (mail) articles.
9537 This command actually deletes articles.  This is not a marking
9538 command.  The article will disappear forever from your life, never to
9539 return.
9540
9541 If N is negative, delete backwards.
9542 If N is nil and articles have been marked with the process mark,
9543 delete these instead.
9544
9545 If `gnus-novice-user' is non-nil you will be asked for
9546 confirmation before the articles are deleted."
9547   (interactive "P")
9548   (unless (gnus-check-backend-function 'request-expire-articles
9549                                        gnus-newsgroup-name)
9550     (error "The current newsgroup does not support article deletion"))
9551   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9552     (error "Couldn't open server"))
9553   ;; Compute the list of articles to delete.
9554   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9555         (nnmail-expiry-target 'delete)
9556         not-deleted)
9557     (if (and gnus-novice-user
9558              (not (gnus-yes-or-no-p
9559                    (format "Do you really want to delete %s forever? "
9560                            (if (> (length articles) 1)
9561                                (format "these %s articles" (length articles))
9562                              "this article")))))
9563         ()
9564       ;; Delete the articles.
9565       (setq not-deleted (gnus-request-expire-articles
9566                          articles gnus-newsgroup-name 'force))
9567       (while articles
9568         (gnus-summary-remove-process-mark (car articles))
9569         ;; The backend might not have been able to delete the article
9570         ;; after all.
9571         (unless (memq (car articles) not-deleted)
9572           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9573         (let* ((article (car articles))
9574                (ghead  (gnus-data-header
9575                                     (assoc article (gnus-data-list nil)))))
9576           (run-hook-with-args 'gnus-summary-article-delete-hook
9577                               'delete ghead gnus-newsgroup-name nil
9578                               nil))
9579         (setq articles (cdr articles)))
9580       (when not-deleted
9581         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9582     (gnus-summary-position-point)
9583     (gnus-set-mode-line 'summary)
9584     not-deleted))
9585
9586 (defun gnus-summary-edit-article (&optional force)
9587   "Edit the current article.
9588 This will have permanent effect only in mail groups.
9589 If FORCE is non-nil, allow editing of articles even in read-only
9590 groups."
9591   (interactive "P")
9592   (save-excursion
9593     (set-buffer gnus-summary-buffer)
9594     (let ((mail-parse-charset gnus-newsgroup-charset)
9595           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9596       (gnus-set-global-variables)
9597       (when (and (not force)
9598                  (gnus-group-read-only-p))
9599         (error "The current newsgroup does not support article editing"))
9600       (gnus-summary-show-article t)
9601       (gnus-article-edit-article
9602        'ignore
9603        `(lambda (no-highlight)
9604           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9605                 (message-options message-options)
9606                 (message-options-set-recipient)
9607                 (mail-parse-ignored-charsets
9608                  ',gnus-newsgroup-ignored-charsets))
9609             (gnus-summary-edit-article-done
9610              ,(or (mail-header-references gnus-current-headers) "")
9611              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9612
9613 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9614
9615 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9616                                                  no-highlight)
9617   "Make edits to the current article permanent."
9618   (interactive)
9619   (save-excursion
9620     ;; The buffer restriction contains the entire article if it exists.
9621     (when (article-goto-body)
9622       (let ((lines (count-lines (point) (point-max)))
9623             (length (- (point-max) (point)))
9624             (case-fold-search t)
9625             (body (copy-marker (point))))
9626         (goto-char (point-min))
9627         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9628           (delete-region (match-beginning 1) (match-end 1))
9629           (insert (number-to-string length)))
9630         (goto-char (point-min))
9631         (when (re-search-forward
9632                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9633           (delete-region (match-beginning 1) (match-end 1))
9634           (insert (number-to-string length)))
9635         (goto-char (point-min))
9636         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9637           (delete-region (match-beginning 1) (match-end 1))
9638           (insert (number-to-string lines))))))
9639   ;; Replace the article.
9640   (let ((buf (current-buffer)))
9641     (with-temp-buffer
9642       (insert-buffer-substring buf)
9643
9644       (if (and (not read-only)
9645                (not (gnus-request-replace-article
9646                      (cdr gnus-article-current) (car gnus-article-current)
9647                      (current-buffer) t)))
9648           (error "Couldn't replace article")
9649         ;; Update the summary buffer.
9650         (if (and references
9651                  (equal (message-tokenize-header references " ")
9652                         (message-tokenize-header
9653                          (or (message-fetch-field "references") "") " ")))
9654             ;; We only have to update this line.
9655             (save-excursion
9656               (save-restriction
9657                 (message-narrow-to-head)
9658                 (let ((head (buffer-string))
9659                       header)
9660                   (with-temp-buffer
9661                     (insert (format "211 %d Article retrieved.\n"
9662                                     (cdr gnus-article-current)))
9663                     (insert head)
9664                     (insert ".\n")
9665                     (let ((nntp-server-buffer (current-buffer)))
9666                       (setq header (car (gnus-get-newsgroup-headers
9667                                          nil t))))
9668                     (save-excursion
9669                       (set-buffer gnus-summary-buffer)
9670                       (gnus-data-set-header
9671                        (gnus-data-find (cdr gnus-article-current))
9672                        header)
9673                       (gnus-summary-update-article-line
9674                        (cdr gnus-article-current) header)
9675                       (if (gnus-summary-goto-subject
9676                            (cdr gnus-article-current) nil t)
9677                           (gnus-summary-update-secondary-mark
9678                            (cdr gnus-article-current))))))))
9679           ;; Update threads.
9680           (set-buffer (or buffer gnus-summary-buffer))
9681           (gnus-summary-update-article (cdr gnus-article-current))
9682           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9683               (gnus-summary-update-secondary-mark
9684                (cdr gnus-article-current))))
9685         ;; Prettify the article buffer again.
9686         (unless no-highlight
9687           (save-excursion
9688             (set-buffer gnus-article-buffer)
9689             ;;;!!! Fix this -- article should be rehighlighted.
9690             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9691             (set-buffer gnus-original-article-buffer)
9692             (gnus-request-article
9693              (cdr gnus-article-current)
9694              (car gnus-article-current) (current-buffer))))
9695         ;; Prettify the summary buffer line.
9696         (when (gnus-visual-p 'summary-highlight 'highlight)
9697           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9698
9699 (defun gnus-summary-edit-wash (key)
9700   "Perform editing command KEY in the article buffer."
9701   (interactive
9702    (list
9703     (progn
9704       (message "%s" (concat (this-command-keys) "- "))
9705       (read-char))))
9706   (message "")
9707   (gnus-summary-edit-article)
9708   (execute-kbd-macro (concat (this-command-keys) key))
9709   (gnus-article-edit-done))
9710
9711 ;;; Respooling
9712
9713 (defun gnus-summary-respool-query (&optional silent trace)
9714   "Query where the respool algorithm would put this article."
9715   (interactive)
9716   (let (gnus-mark-article-hook)
9717     (gnus-summary-select-article)
9718     (save-excursion
9719       (set-buffer gnus-original-article-buffer)
9720       (let ((groups (nnmail-article-group 'identity trace)))
9721         (unless silent
9722           (if groups
9723               (message "This message would go to %s"
9724                        (mapconcat 'car groups ", "))
9725             (message "This message would go to no groups"))
9726           groups)))))
9727
9728 (defun gnus-summary-respool-trace ()
9729   "Trace where the respool algorithm would put this article.
9730 Display a buffer showing all fancy splitting patterns which matched."
9731   (interactive)
9732   (gnus-summary-respool-query nil t))
9733
9734 ;; Summary marking commands.
9735
9736 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9737   "Mark articles which has the same subject as read, and then select the next.
9738 If UNMARK is positive, remove any kind of mark.
9739 If UNMARK is negative, tick articles."
9740   (interactive "P")
9741   (when unmark
9742     (setq unmark (prefix-numeric-value unmark)))
9743   (let ((count
9744          (gnus-summary-mark-same-subject
9745           (gnus-summary-article-subject) unmark)))
9746     ;; Select next unread article.  If auto-select-same mode, should
9747     ;; select the first unread article.
9748     (gnus-summary-next-article t (and gnus-auto-select-same
9749                                       (gnus-summary-article-subject)))
9750     (gnus-message 7 "%d article%s marked as %s"
9751                   count (if (= count 1) " is" "s are")
9752                   (if unmark "unread" "read"))))
9753
9754 (defun gnus-summary-kill-same-subject (&optional unmark)
9755   "Mark articles which has the same subject as read.
9756 If UNMARK is positive, remove any kind of mark.
9757 If UNMARK is negative, tick articles."
9758   (interactive "P")
9759   (when unmark
9760     (setq unmark (prefix-numeric-value unmark)))
9761   (let ((count
9762          (gnus-summary-mark-same-subject
9763           (gnus-summary-article-subject) unmark)))
9764     ;; If marked as read, go to next unread subject.
9765     (when (null unmark)
9766       ;; Go to next unread subject.
9767       (gnus-summary-next-subject 1 t))
9768     (gnus-message 7 "%d articles are marked as %s"
9769                   count (if unmark "unread" "read"))))
9770
9771 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9772   "Mark articles with same SUBJECT as read, and return marked number.
9773 If optional argument UNMARK is positive, remove any kinds of marks.
9774 If optional argument UNMARK is negative, mark articles as unread instead."
9775   (let ((count 1))
9776     (save-excursion
9777       (cond
9778        ((null unmark)                   ; Mark as read.
9779         (while (and
9780                 (progn
9781                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9782                   (gnus-summary-show-thread) t)
9783                 (gnus-summary-find-subject subject))
9784           (setq count (1+ count))))
9785        ((> unmark 0)                    ; Tick.
9786         (while (and
9787                 (progn
9788                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9789                   (gnus-summary-show-thread) t)
9790                 (gnus-summary-find-subject subject))
9791           (setq count (1+ count))))
9792        (t                               ; Mark as unread.
9793         (while (and
9794                 (progn
9795                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9796                   (gnus-summary-show-thread) t)
9797                 (gnus-summary-find-subject subject))
9798           (setq count (1+ count)))))
9799       (gnus-set-mode-line 'summary)
9800       ;; Return the number of marked articles.
9801       count)))
9802
9803 (defun gnus-summary-mark-as-processable (n &optional unmark)
9804   "Set the process mark on the next N articles.
9805 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9806 the process mark instead.  The difference between N and the actual
9807 number of articles marked is returned."
9808   (interactive "P")
9809   (if (and (null n) (gnus-region-active-p))
9810       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9811     (setq n (prefix-numeric-value n))
9812     (let ((backward (< n 0))
9813           (n (abs n)))
9814       (while (and
9815               (> n 0)
9816               (if unmark
9817                   (gnus-summary-remove-process-mark
9818                    (gnus-summary-article-number))
9819                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9820               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9821         (setq n (1- n)))
9822       (when (/= 0 n)
9823         (gnus-message 7 "No more articles"))
9824       (gnus-summary-recenter)
9825       (gnus-summary-position-point)
9826       n)))
9827
9828 (defun gnus-summary-unmark-as-processable (n)
9829   "Remove the process mark from the next N articles.
9830 If N is negative, unmark backward instead.  The difference between N and
9831 the actual number of articles unmarked is returned."
9832   (interactive "P")
9833   (gnus-summary-mark-as-processable n t))
9834
9835 (defun gnus-summary-unmark-all-processable ()
9836   "Remove the process mark from all articles."
9837   (interactive)
9838   (save-excursion
9839     (while gnus-newsgroup-processable
9840       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9841   (gnus-summary-position-point))
9842
9843 (defun gnus-summary-add-mark (article type)
9844   "Mark ARTICLE with a mark of TYPE."
9845   (let ((vtype (car (assq type gnus-article-mark-lists)))
9846         var)
9847     (if (not vtype)
9848         (error "No such mark type: %s" type)
9849       (setq var (intern (format "gnus-newsgroup-%s" type)))
9850       (set var (cons article (symbol-value var)))
9851       (if (memq type '(processable cached replied forwarded recent saved))
9852           (gnus-summary-update-secondary-mark article)
9853         ;;; !!! This is bogus.  We should find out what primary
9854         ;;; !!! mark we want to set.
9855         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9856
9857 (defun gnus-summary-mark-as-expirable (n)
9858   "Mark N articles forward as expirable.
9859 If N is negative, mark backward instead.  The difference between N and
9860 the actual number of articles marked is returned."
9861   (interactive "p")
9862   (gnus-summary-mark-forward n gnus-expirable-mark))
9863
9864 (defun gnus-summary-mark-as-spam (n)
9865   "Mark N articles forward as spam.
9866 If N is negative, mark backward instead.  The difference between N and
9867 the actual number of articles marked is returned."
9868   (interactive "p")
9869   (gnus-summary-mark-forward n gnus-spam-mark))
9870
9871 (defun gnus-summary-mark-article-as-replied (article)
9872   "Mark ARTICLE as replied to and update the summary line.
9873 ARTICLE can also be a list of articles."
9874   (interactive (list (gnus-summary-article-number)))
9875   (let ((articles (if (listp article) article (list article))))
9876     (dolist (article articles)
9877       (unless (numberp article)
9878         (error "%s is not a number" article))
9879       (push article gnus-newsgroup-replied)
9880       (let ((buffer-read-only nil))
9881         (when (gnus-summary-goto-subject article nil t)
9882           (gnus-summary-update-secondary-mark article))))))
9883
9884 (defun gnus-summary-mark-article-as-forwarded (article)
9885   "Mark ARTICLE as forwarded and update the summary line.
9886 ARTICLE can also be a list of articles."
9887   (let ((articles (if (listp article) article (list article))))
9888     (dolist (article articles)
9889       (push article gnus-newsgroup-forwarded)
9890       (let ((buffer-read-only nil))
9891         (when (gnus-summary-goto-subject article nil t)
9892           (gnus-summary-update-secondary-mark article))))))
9893
9894 (defun gnus-summary-set-bookmark (article)
9895   "Set a bookmark in current article."
9896   (interactive (list (gnus-summary-article-number)))
9897   (when (or (not (get-buffer gnus-article-buffer))
9898             (not gnus-current-article)
9899             (not gnus-article-current)
9900             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9901     (error "No current article selected"))
9902   ;; Remove old bookmark, if one exists.
9903   (gnus-pull article gnus-newsgroup-bookmarks)
9904   ;; Set the new bookmark, which is on the form
9905   ;; (article-number . line-number-in-body).
9906   (push
9907    (cons article
9908          (with-current-buffer gnus-article-buffer
9909            (count-lines
9910             (min (point)
9911                  (save-excursion
9912                    (article-goto-body)
9913                    (point)))
9914             (point))))
9915    gnus-newsgroup-bookmarks)
9916   (gnus-message 6 "A bookmark has been added to the current article."))
9917
9918 (defun gnus-summary-remove-bookmark (article)
9919   "Remove the bookmark from the current article."
9920   (interactive (list (gnus-summary-article-number)))
9921   ;; Remove old bookmark, if one exists.
9922   (if (not (assq article gnus-newsgroup-bookmarks))
9923       (gnus-message 6 "No bookmark in current article.")
9924     (gnus-pull article gnus-newsgroup-bookmarks)
9925     (gnus-message 6 "Removed bookmark.")))
9926
9927 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9928 (defun gnus-summary-mark-as-dormant (n)
9929   "Mark N articles forward as dormant.
9930 If N is negative, mark backward instead.  The difference between N and
9931 the actual number of articles marked is returned."
9932   (interactive "p")
9933   (gnus-summary-mark-forward n gnus-dormant-mark))
9934
9935 (defun gnus-summary-set-process-mark (article)
9936   "Set the process mark on ARTICLE and update the summary line."
9937   (setq gnus-newsgroup-processable
9938         (cons article
9939               (delq article gnus-newsgroup-processable)))
9940   (when (gnus-summary-goto-subject article)
9941     (gnus-summary-show-thread)
9942     (gnus-summary-goto-subject article)
9943     (gnus-summary-update-secondary-mark article)))
9944
9945 (defun gnus-summary-remove-process-mark (article)
9946   "Remove the process mark from ARTICLE and update the summary line."
9947   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9948   (when (gnus-summary-goto-subject article)
9949     (gnus-summary-show-thread)
9950     (gnus-summary-goto-subject article)
9951     (gnus-summary-update-secondary-mark article)))
9952
9953 (defun gnus-summary-set-saved-mark (article)
9954   "Set the process mark on ARTICLE and update the summary line."
9955   (push article gnus-newsgroup-saved)
9956   (when (gnus-summary-goto-subject article)
9957     (gnus-summary-update-secondary-mark article)))
9958
9959 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9960   "Mark N articles as read forwards.
9961 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9962 The difference between N and the actual number of articles marked is
9963 returned.
9964 If NO-EXPIRE, auto-expiry will be inhibited."
9965   (interactive "p")
9966   (gnus-summary-show-thread)
9967   (let ((backward (< n 0))
9968         (gnus-summary-goto-unread
9969          (and gnus-summary-goto-unread
9970               (not (eq gnus-summary-goto-unread 'never))
9971               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9972                                     gnus-ticked-mark gnus-dormant-mark)))))
9973         (n (abs n))
9974         (mark (or mark gnus-del-mark)))
9975     (while (and (> n 0)
9976                 (gnus-summary-mark-article nil mark no-expire)
9977                 (zerop (gnus-summary-next-subject
9978                         (if backward -1 1)
9979                         (and gnus-summary-goto-unread
9980                              (not (eq gnus-summary-goto-unread 'never)))
9981                         t)))
9982       (setq n (1- n)))
9983     (when (/= 0 n)
9984       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9985     (gnus-summary-recenter)
9986     (gnus-summary-position-point)
9987     (gnus-set-mode-line 'summary)
9988     n))
9989
9990 (defun gnus-summary-mark-article-as-read (mark)
9991   "Mark the current article quickly as read with MARK."
9992   (let ((article (gnus-summary-article-number)))
9993     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9994     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9995     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9996     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9997     (push (cons article mark) gnus-newsgroup-reads)
9998     ;; Possibly remove from cache, if that is used.
9999     (when gnus-use-cache
10000       (gnus-cache-enter-remove-article article))
10001     ;; Allow the backend to change the mark.
10002     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10003     ;; Check for auto-expiry.
10004     (when (and gnus-newsgroup-auto-expire
10005                (memq mark gnus-auto-expirable-marks))
10006       (setq mark gnus-expirable-mark)
10007       ;; Let the backend know about the mark change.
10008       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10009       (push article gnus-newsgroup-expirable))
10010     ;; Set the mark in the buffer.
10011     (gnus-summary-update-mark mark 'unread)
10012     t))
10013
10014 (defun gnus-summary-mark-article-as-unread (mark)
10015   "Mark the current article quickly as unread with MARK."
10016   (let* ((article (gnus-summary-article-number))
10017          (old-mark (gnus-summary-article-mark article)))
10018     ;; Allow the backend to change the mark.
10019     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10020     (if (eq mark old-mark)
10021         t
10022       (if (<= article 0)
10023           (progn
10024             (gnus-error 1 "Can't mark negative article numbers")
10025             nil)
10026         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10027         (setq gnus-newsgroup-spam-marked
10028               (delq article gnus-newsgroup-spam-marked))
10029         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10030         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
10031         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
10032         (cond ((= mark gnus-ticked-mark)
10033                (setq gnus-newsgroup-marked
10034                      (gnus-add-to-sorted-list gnus-newsgroup-marked
10035                                               article)))
10036               ((= mark gnus-spam-mark)
10037                (setq gnus-newsgroup-spam-marked
10038                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10039                                               article)))
10040               ((= mark gnus-dormant-mark)
10041                (setq gnus-newsgroup-dormant
10042                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
10043                                               article)))
10044               (t
10045                (setq gnus-newsgroup-unreads
10046                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
10047                                               article))))
10048         (gnus-pull article gnus-newsgroup-reads)
10049
10050         ;; See whether the article is to be put in the cache.
10051         (and gnus-use-cache
10052              (vectorp (gnus-summary-article-header article))
10053              (save-excursion
10054                (gnus-cache-possibly-enter-article
10055                 gnus-newsgroup-name article
10056                 (gnus-summary-article-header article)
10057                 (= mark gnus-ticked-mark)
10058                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10059
10060         ;; Fix the mark.
10061         (gnus-summary-update-mark mark 'unread)
10062         t))))
10063
10064 (defun gnus-summary-mark-article (&optional article mark no-expire)
10065   "Mark ARTICLE with MARK.  MARK can be any character.
10066 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
10067 `??' (dormant) and `?E' (expirable).
10068 If MARK is nil, then the default character `?r' is used.
10069 If ARTICLE is nil, then the article on the current line will be
10070 marked.
10071 If NO-EXPIRE, auto-expiry will be inhibited."
10072   ;; The mark might be a string.
10073   (when (stringp mark)
10074     (setq mark (aref mark 0)))
10075   ;; If no mark is given, then we check auto-expiring.
10076   (when (null mark)
10077     (setq mark gnus-del-mark))
10078   (when (and (not no-expire)
10079              gnus-newsgroup-auto-expire
10080              (memq mark gnus-auto-expirable-marks))
10081     (setq mark gnus-expirable-mark))
10082   (let ((article (or article (gnus-summary-article-number)))
10083         (old-mark (gnus-summary-article-mark article)))
10084     ;; Allow the backend to change the mark.
10085     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10086     (if (eq mark old-mark)
10087         t
10088       (unless article
10089         (error "No article on current line"))
10090       (if (not (if (or (= mark gnus-unread-mark)
10091                        (= mark gnus-ticked-mark)
10092                        (= mark gnus-spam-mark)
10093                        (= mark gnus-dormant-mark))
10094                    (gnus-mark-article-as-unread article mark)
10095                  (gnus-mark-article-as-read article mark)))
10096           t
10097         ;; See whether the article is to be put in the cache.
10098         (and gnus-use-cache
10099              (not (= mark gnus-canceled-mark))
10100              (vectorp (gnus-summary-article-header article))
10101              (save-excursion
10102                (gnus-cache-possibly-enter-article
10103                 gnus-newsgroup-name article
10104                 (gnus-summary-article-header article)
10105                 (= mark gnus-ticked-mark)
10106                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10107
10108         (when (gnus-summary-goto-subject article nil t)
10109           (let ((buffer-read-only nil))
10110             (gnus-summary-show-thread)
10111             ;; Fix the mark.
10112             (gnus-summary-update-mark mark 'unread)
10113             t))))))
10114
10115 (defun gnus-summary-update-secondary-mark (article)
10116   "Update the secondary (read, process, cache) mark."
10117   (gnus-summary-update-mark
10118    (cond ((memq article gnus-newsgroup-processable)
10119           gnus-process-mark)
10120          ((memq article gnus-newsgroup-cached)
10121           gnus-cached-mark)
10122          ((memq article gnus-newsgroup-replied)
10123           gnus-replied-mark)
10124          ((memq article gnus-newsgroup-forwarded)
10125           gnus-forwarded-mark)
10126          ((memq article gnus-newsgroup-saved)
10127           gnus-saved-mark)
10128          ((memq article gnus-newsgroup-recent)
10129           gnus-recent-mark)
10130          ((memq article gnus-newsgroup-unseen)
10131           gnus-unseen-mark)
10132          (t gnus-no-mark))
10133    'replied)
10134   (when (gnus-visual-p 'summary-highlight 'highlight)
10135     (gnus-run-hooks 'gnus-summary-update-hook))
10136   t)
10137
10138 (defun gnus-summary-update-download-mark (article)
10139   "Update the download mark."
10140   (gnus-summary-update-mark
10141    (cond ((memq article gnus-newsgroup-undownloaded)
10142           gnus-undownloaded-mark)
10143          (gnus-newsgroup-agentized
10144           gnus-downloaded-mark)
10145          (t
10146           gnus-no-mark))
10147    'download)
10148   (gnus-summary-update-line t)
10149   t)
10150
10151 (defun gnus-summary-update-mark (mark type)
10152   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10153         (buffer-read-only nil))
10154     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10155     (when forward
10156       (when (looking-at "\r")
10157         (incf forward))
10158       (when (<= (+ forward (point)) (point-max))
10159         ;; Go to the right position on the line.
10160         (goto-char (+ forward (point)))
10161         ;; Replace the old mark with the new mark.
10162         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10163         ;; Optionally update the marks by some user rule.
10164         (when (eq type 'unread)
10165           (gnus-data-set-mark
10166            (gnus-data-find (gnus-summary-article-number)) mark)
10167           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10168
10169 (defun gnus-mark-article-as-read (article &optional mark)
10170   "Enter ARTICLE in the pertinent lists and remove it from others."
10171   ;; Make the article expirable.
10172   (let ((mark (or mark gnus-del-mark)))
10173     (setq gnus-newsgroup-expirable
10174           (if (= mark gnus-expirable-mark)
10175               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10176             (delq article gnus-newsgroup-expirable)))
10177     ;; Remove from unread and marked lists.
10178     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10179     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10180     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10181     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10182     (push (cons article mark) gnus-newsgroup-reads)
10183     ;; Possibly remove from cache, if that is used.
10184     (when gnus-use-cache
10185       (gnus-cache-enter-remove-article article))
10186     t))
10187
10188 (defun gnus-mark-article-as-unread (article &optional mark)
10189   "Enter ARTICLE in the pertinent lists and remove it from others."
10190   (let ((mark (or mark gnus-ticked-mark)))
10191     (if (<= article 0)
10192         (progn
10193           (gnus-error 1 "Can't mark negative article numbers")
10194           nil)
10195       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10196             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10197             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10198             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10199             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10200
10201       ;; Unsuppress duplicates?
10202       (when gnus-suppress-duplicates
10203         (gnus-dup-unsuppress-article article))
10204
10205       (cond ((= mark gnus-ticked-mark)
10206              (setq gnus-newsgroup-marked
10207                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10208             ((= mark gnus-spam-mark)
10209              (setq gnus-newsgroup-spam-marked
10210                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10211                                             article)))
10212             ((= mark gnus-dormant-mark)
10213              (setq gnus-newsgroup-dormant
10214                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10215             (t
10216              (setq gnus-newsgroup-unreads
10217                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10218       (gnus-pull article gnus-newsgroup-reads)
10219       t)))
10220
10221 (defalias 'gnus-summary-mark-as-unread-forward
10222   'gnus-summary-tick-article-forward)
10223 (make-obsolete 'gnus-summary-mark-as-unread-forward
10224                'gnus-summary-tick-article-forward)
10225 (defun gnus-summary-tick-article-forward (n)
10226   "Tick N articles forwards.
10227 If N is negative, tick backwards instead.
10228 The difference between N and the number of articles ticked is returned."
10229   (interactive "p")
10230   (gnus-summary-mark-forward n gnus-ticked-mark))
10231
10232 (defalias 'gnus-summary-mark-as-unread-backward
10233   'gnus-summary-tick-article-backward)
10234 (make-obsolete 'gnus-summary-mark-as-unread-backward
10235                'gnus-summary-tick-article-backward)
10236 (defun gnus-summary-tick-article-backward (n)
10237   "Tick N articles backwards.
10238 The difference between N and the number of articles ticked is returned."
10239   (interactive "p")
10240   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10241
10242 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10243 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10244 (defun gnus-summary-tick-article (&optional article clear-mark)
10245   "Mark current article as unread.
10246 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10247 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10248   (interactive)
10249   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10250                                        gnus-ticked-mark)))
10251
10252 (defun gnus-summary-mark-as-read-forward (n)
10253   "Mark N articles as read forwards.
10254 If N is negative, mark backwards instead.
10255 The difference between N and the actual number of articles marked is
10256 returned."
10257   (interactive "p")
10258   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10259
10260 (defun gnus-summary-mark-as-read-backward (n)
10261   "Mark the N articles as read backwards.
10262 The difference between N and the actual number of articles marked is
10263 returned."
10264   (interactive "p")
10265   (gnus-summary-mark-forward
10266    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10267
10268 (defun gnus-summary-mark-as-read (&optional article mark)
10269   "Mark current article as read.
10270 ARTICLE specifies the article to be marked as read.
10271 MARK specifies a string to be inserted at the beginning of the line."
10272   (gnus-summary-mark-article article mark))
10273
10274 (defun gnus-summary-clear-mark-forward (n)
10275   "Clear marks from N articles forward.
10276 If N is negative, clear backward instead.
10277 The difference between N and the number of marks cleared is returned."
10278   (interactive "p")
10279   (gnus-summary-mark-forward n gnus-unread-mark))
10280
10281 (defun gnus-summary-clear-mark-backward (n)
10282   "Clear marks from N articles backward.
10283 The difference between N and the number of marks cleared is returned."
10284   (interactive "p")
10285   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10286
10287 (defun gnus-summary-mark-unread-as-read ()
10288   "Intended to be used by `gnus-summary-mark-article-hook'."
10289   (when (memq gnus-current-article gnus-newsgroup-unreads)
10290     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10291
10292 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10293   "Intended to be used by `gnus-summary-mark-article-hook'."
10294   (let ((mark (gnus-summary-article-mark)))
10295     (when (or (gnus-unread-mark-p mark)
10296               (gnus-read-mark-p mark))
10297       (gnus-summary-mark-article gnus-current-article
10298                                  (or new-mark gnus-read-mark)))))
10299
10300 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10301   "Intended to be used by `gnus-summary-mark-article-hook'."
10302   (let ((mark (gnus-summary-article-mark)))
10303     (when (or (gnus-unread-mark-p mark)
10304               (gnus-read-mark-p mark))
10305       (gnus-summary-mark-article (gnus-summary-article-number)
10306                                  (or new-mark gnus-read-mark)))))
10307
10308 (defun gnus-summary-mark-unread-as-ticked ()
10309   "Intended to be used by `gnus-summary-mark-article-hook'."
10310   (when (memq gnus-current-article gnus-newsgroup-unreads)
10311     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10312
10313 (defun gnus-summary-mark-region-as-read (point mark all)
10314   "Mark all unread articles between point and mark as read.
10315 If given a prefix, mark all articles between point and mark as read,
10316 even ticked and dormant ones."
10317   (interactive "r\nP")
10318   (save-excursion
10319     (let (article)
10320       (goto-char point)
10321       (beginning-of-line)
10322       (while (and
10323               (< (point) mark)
10324               (progn
10325                 (when (or all
10326                           (memq (setq article (gnus-summary-article-number))
10327                                 gnus-newsgroup-unreads))
10328                   (gnus-summary-mark-article article gnus-del-mark))
10329                 t)
10330               (gnus-summary-find-next))))))
10331
10332 (defun gnus-summary-mark-below (score mark)
10333   "Mark articles with score less than SCORE with MARK."
10334   (interactive "P\ncMark: ")
10335   (setq score (if score
10336                   (prefix-numeric-value score)
10337                 (or gnus-summary-default-score 0)))
10338   (save-excursion
10339     (set-buffer gnus-summary-buffer)
10340     (goto-char (point-min))
10341     (while
10342         (progn
10343           (and (< (gnus-summary-article-score) score)
10344                (gnus-summary-mark-article nil mark))
10345           (gnus-summary-find-next)))))
10346
10347 (defun gnus-summary-kill-below (&optional score)
10348   "Mark articles with score below SCORE as read."
10349   (interactive "P")
10350   (gnus-summary-mark-below score gnus-killed-mark))
10351
10352 (defun gnus-summary-clear-above (&optional score)
10353   "Clear all marks from articles with score above SCORE."
10354   (interactive "P")
10355   (gnus-summary-mark-above score gnus-unread-mark))
10356
10357 (defun gnus-summary-tick-above (&optional score)
10358   "Tick all articles with score above SCORE."
10359   (interactive "P")
10360   (gnus-summary-mark-above score gnus-ticked-mark))
10361
10362 (defun gnus-summary-mark-above (score mark)
10363   "Mark articles with score over SCORE with MARK."
10364   (interactive "P\ncMark: ")
10365   (setq score (if score
10366                   (prefix-numeric-value score)
10367                 (or gnus-summary-default-score 0)))
10368   (save-excursion
10369     (set-buffer gnus-summary-buffer)
10370     (goto-char (point-min))
10371     (while (and (progn
10372                   (when (> (gnus-summary-article-score) score)
10373                     (gnus-summary-mark-article nil mark))
10374                   t)
10375                 (gnus-summary-find-next)))))
10376
10377 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10378 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10379 (defun gnus-summary-limit-include-expunged (&optional no-error)
10380   "Display all the hidden articles that were expunged for low scores."
10381   (interactive)
10382   (let ((buffer-read-only nil))
10383     (let ((scored gnus-newsgroup-scored)
10384           headers h)
10385       (while scored
10386         (unless (gnus-summary-article-header (caar scored))
10387           (and (setq h (gnus-number-to-header (caar scored)))
10388                (< (cdar scored) gnus-summary-expunge-below)
10389                (push h headers)))
10390         (setq scored (cdr scored)))
10391       (if (not headers)
10392           (when (not no-error)
10393             (error "No expunged articles hidden"))
10394         (goto-char (point-min))
10395         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10396         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10397         (mapcar (lambda (x) (push (mail-header-number x)
10398                                   gnus-newsgroup-limit))
10399                 headers)
10400         (gnus-summary-prepare-unthreaded (nreverse headers))
10401         (goto-char (point-min))
10402         (gnus-summary-position-point)
10403         t))))
10404
10405 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10406   "Mark all unread articles in this newsgroup as read.
10407 If prefix argument ALL is non-nil, ticked and dormant articles will
10408 also be marked as read.
10409 If QUIETLY is non-nil, no questions will be asked.
10410
10411 If TO-HERE is non-nil, it should be a point in the buffer.  All
10412 articles before (after, if REVERSE is set) this point will be marked
10413 as read.
10414
10415 Note that this function will only catch up the unread article
10416 in the current summary buffer limitation.
10417
10418 The number of articles marked as read is returned."
10419   (interactive "P")
10420   (prog1
10421       (save-excursion
10422         (when (or quietly
10423                   (not gnus-interactive-catchup) ;Without confirmation?
10424                   gnus-expert-user
10425                   (gnus-y-or-n-p
10426                    (if all
10427                        "Mark absolutely all articles as read? "
10428                      "Mark all unread articles as read? ")))
10429           (if (and not-mark
10430                    (not gnus-newsgroup-adaptive)
10431                    (not gnus-newsgroup-auto-expire)
10432                    (not gnus-suppress-duplicates)
10433                    (or (not gnus-use-cache)
10434                        (eq gnus-use-cache 'passive)))
10435               (progn
10436                 (when all
10437                   (setq gnus-newsgroup-marked nil
10438                         gnus-newsgroup-spam-marked nil
10439                         gnus-newsgroup-dormant nil))
10440                 (setq gnus-newsgroup-unreads
10441                       (gnus-sorted-nunion
10442                        (gnus-intersection gnus-newsgroup-unreads
10443                                           gnus-newsgroup-downloadable)
10444                        gnus-newsgroup-unfetched)))
10445             ;; We actually mark all articles as canceled, which we
10446             ;; have to do when using auto-expiry or adaptive scoring.
10447             (gnus-summary-show-all-threads)
10448             (if (and to-here reverse)
10449                 (progn
10450                   (goto-char to-here)
10451                   (gnus-summary-mark-current-read-and-unread-as-read
10452                    gnus-catchup-mark)
10453                   (while (gnus-summary-find-next (not all))
10454                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10455               (when (gnus-summary-first-subject (not all))
10456                 (while (and
10457                         (if to-here (< (point) to-here) t)
10458                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10459                         (gnus-summary-find-next (not all))))))
10460             (gnus-set-mode-line 'summary))
10461           t))
10462     (gnus-summary-position-point)))
10463
10464 (defun gnus-summary-catchup-to-here (&optional all)
10465   "Mark all unticked articles before the current one as read.
10466 If ALL is non-nil, also mark ticked and dormant articles as read."
10467   (interactive "P")
10468   (save-excursion
10469     (gnus-save-hidden-threads
10470       (let ((beg (point)))
10471         ;; We check that there are unread articles.
10472         (when (or all (gnus-summary-find-prev))
10473           (gnus-summary-catchup all t beg)))))
10474   (gnus-summary-position-point))
10475
10476 (defun gnus-summary-catchup-from-here (&optional all)
10477   "Mark all unticked articles after (and including) the current one as read.
10478 If ALL is non-nil, also mark ticked and dormant articles as read."
10479   (interactive "P")
10480   (save-excursion
10481     (gnus-save-hidden-threads
10482       (let ((beg (point)))
10483         ;; We check that there are unread articles.
10484         (when (or all (gnus-summary-find-next))
10485           (gnus-summary-catchup all t beg nil t)))))
10486   (gnus-summary-position-point))
10487
10488 (defun gnus-summary-catchup-all (&optional quietly)
10489   "Mark all articles in this newsgroup as read.
10490 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10491 instead, which marks only unread articles as read."
10492   (interactive "P")
10493   (gnus-summary-catchup t quietly))
10494
10495 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10496   "Mark all unread articles in this group as read, then exit.
10497 If prefix argument ALL is non-nil, all articles are marked as read.
10498 If QUIETLY is non-nil, no questions will be asked."
10499   (interactive "P")
10500   (when (gnus-summary-catchup all quietly nil 'fast)
10501     ;; Select next newsgroup or exit.
10502     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10503              (eq gnus-auto-select-next 'quietly))
10504         (gnus-summary-next-group nil)
10505       (gnus-summary-exit))))
10506
10507 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10508   "Mark all articles in this newsgroup as read, and then exit.
10509 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10510 instead, which marks only unread articles as read."
10511   (interactive "P")
10512   (gnus-summary-catchup-and-exit t quietly))
10513
10514 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10515   "Mark all articles in this group as read and select the next group.
10516 If given a prefix, mark all articles, unread as well as ticked, as
10517 read."
10518   (interactive "P")
10519   (save-excursion
10520     (gnus-summary-catchup all))
10521   (gnus-summary-next-group))
10522
10523 ;;;
10524 ;;; with article
10525 ;;;
10526
10527 (defmacro gnus-with-article (article &rest forms)
10528   "Select ARTICLE and perform FORMS in the original article buffer.
10529 Then replace the article with the result."
10530   `(progn
10531      ;; We don't want the article to be marked as read.
10532      (let (gnus-mark-article-hook)
10533        (gnus-summary-select-article t t nil ,article))
10534      (set-buffer gnus-original-article-buffer)
10535      ,@forms
10536      (if (not (gnus-check-backend-function
10537                'request-replace-article (car gnus-article-current)))
10538          (gnus-message 5 "Read-only group; not replacing")
10539        (unless (gnus-request-replace-article
10540                 ,article (car gnus-article-current)
10541                 (current-buffer) t)
10542          (error "Couldn't replace article")))
10543      ;; The cache and backlog have to be flushed somewhat.
10544      (when gnus-keep-backlog
10545        (gnus-backlog-remove-article
10546         (car gnus-article-current) (cdr gnus-article-current)))
10547      (when gnus-use-cache
10548        (gnus-cache-update-article
10549         (car gnus-article-current) (cdr gnus-article-current)))))
10550
10551 (put 'gnus-with-article 'lisp-indent-function 1)
10552 (put 'gnus-with-article 'edebug-form-spec '(form body))
10553
10554 ;; Thread-based commands.
10555
10556 (defun gnus-summary-articles-in-thread (&optional article)
10557   "Return a list of all articles in the current thread.
10558 If ARTICLE is non-nil, return all articles in the thread that starts
10559 with that article."
10560   (let* ((article (or article (gnus-summary-article-number)))
10561          (data (gnus-data-find-list article))
10562          (top-level (gnus-data-level (car data)))
10563          (top-subject
10564           (cond ((null gnus-thread-operation-ignore-subject)
10565                  (gnus-simplify-subject-re
10566                   (mail-header-subject (gnus-data-header (car data)))))
10567                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10568                  (gnus-simplify-subject-fuzzy
10569                   (mail-header-subject (gnus-data-header (car data)))))
10570                 (t nil)))
10571          (end-point (save-excursion
10572                       (if (gnus-summary-go-to-next-thread)
10573                           (point) (point-max))))
10574          articles)
10575     (while (and data
10576                 (< (gnus-data-pos (car data)) end-point))
10577       (when (or (not top-subject)
10578                 (string= top-subject
10579                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10580                              (gnus-simplify-subject-fuzzy
10581                               (mail-header-subject
10582                                (gnus-data-header (car data))))
10583                            (gnus-simplify-subject-re
10584                             (mail-header-subject
10585                              (gnus-data-header (car data)))))))
10586         (push (gnus-data-number (car data)) articles))
10587       (unless (and (setq data (cdr data))
10588                    (> (gnus-data-level (car data)) top-level))
10589         (setq data nil)))
10590     ;; Return the list of articles.
10591     (nreverse articles)))
10592
10593 (defun gnus-summary-rethread-current ()
10594   "Rethread the thread the current article is part of."
10595   (interactive)
10596   (let* ((gnus-show-threads t)
10597          (article (gnus-summary-article-number))
10598          (id (mail-header-id (gnus-summary-article-header)))
10599          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10600     (unless id
10601       (error "No article on the current line"))
10602     (gnus-rebuild-thread id)
10603     (gnus-summary-goto-subject article)))
10604
10605 (defun gnus-summary-reparent-thread ()
10606   "Make the current article child of the marked (or previous) article.
10607
10608 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10609 is non-nil or the Subject: of both articles are the same."
10610   (interactive)
10611   (unless (not (gnus-group-read-only-p))
10612     (error "The current newsgroup does not support article editing"))
10613   (unless (<= (length gnus-newsgroup-processable) 1)
10614     (error "No more than one article may be marked"))
10615   (save-window-excursion
10616     (let ((gnus-article-buffer " *reparent*")
10617           (current-article (gnus-summary-article-number))
10618           ;; First grab the marked article, otherwise one line up.
10619           (parent-article (if (not (null gnus-newsgroup-processable))
10620                               (car gnus-newsgroup-processable)
10621                             (save-excursion
10622                               (if (eq (forward-line -1) 0)
10623                                   (gnus-summary-article-number)
10624                                 (error "Beginning of summary buffer"))))))
10625       (unless (not (eq current-article parent-article))
10626         (error "An article may not be self-referential"))
10627       (let ((message-id (mail-header-id
10628                          (gnus-summary-article-header parent-article))))
10629         (unless (and message-id (not (equal message-id "")))
10630           (error "No message-id in desired parent"))
10631         (gnus-with-article current-article
10632           (save-restriction
10633             (goto-char (point-min))
10634             (message-narrow-to-head)
10635             (if (re-search-forward "^References: " nil t)
10636                 (progn
10637                   (re-search-forward "^[^ \t]" nil t)
10638                   (forward-line -1)
10639                   (end-of-line)
10640                   (insert " " message-id))
10641               (insert "References: " message-id "\n"))))
10642         (set-buffer gnus-summary-buffer)
10643         (gnus-summary-unmark-all-processable)
10644         (gnus-summary-update-article current-article)
10645         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10646             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10647         (gnus-summary-rethread-current)
10648         (gnus-message 3 "Article %d is now the child of article %d"
10649                       current-article parent-article)))))
10650
10651 (defun gnus-summary-toggle-threads (&optional arg)
10652   "Toggle showing conversation threads.
10653 If ARG is positive number, turn showing conversation threads on."
10654   (interactive "P")
10655   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10656     (setq gnus-show-threads
10657           (if (null arg) (not gnus-show-threads)
10658             (> (prefix-numeric-value arg) 0)))
10659     (gnus-summary-prepare)
10660     (gnus-summary-goto-subject current)
10661     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10662     (gnus-summary-position-point)))
10663
10664 (defun gnus-summary-show-all-threads ()
10665   "Show all threads."
10666   (interactive)
10667   (save-excursion
10668     (let ((buffer-read-only nil))
10669       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10670   (gnus-summary-position-point))
10671
10672 (defun gnus-summary-show-thread ()
10673   "Show thread subtrees.
10674 Returns nil if no thread was there to be shown."
10675   (interactive)
10676   (let ((buffer-read-only nil)
10677         (orig (point))
10678         (end (point-at-eol))
10679         ;; Leave point at bol
10680         (beg (progn (beginning-of-line) (point))))
10681     (prog1
10682         ;; Any hidden lines here?
10683         (search-forward "\r" end t)
10684       (subst-char-in-region beg end ?\^M ?\n t)
10685       (goto-char orig)
10686       (gnus-summary-position-point))))
10687
10688 (defun gnus-summary-maybe-hide-threads ()
10689   "If requested, hide the threads that should be hidden."
10690   (when (and gnus-show-threads
10691              gnus-thread-hide-subtree)
10692     (gnus-summary-hide-all-threads
10693      (if (or (consp gnus-thread-hide-subtree)
10694              (functionp gnus-thread-hide-subtree))
10695          (gnus-make-predicate gnus-thread-hide-subtree)
10696        nil))))
10697
10698 ;;; Hiding predicates.
10699
10700 (defun gnus-article-unread-p (header)
10701   (memq (mail-header-number header) gnus-newsgroup-unreads))
10702
10703 (defun gnus-article-unseen-p (header)
10704   (memq (mail-header-number header) gnus-newsgroup-unseen))
10705
10706 (defun gnus-map-articles (predicate articles)
10707   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10708   (apply 'gnus-or (mapcar predicate
10709                           (mapcar 'gnus-summary-article-header articles))))
10710
10711 (defun gnus-summary-hide-all-threads (&optional predicate)
10712   "Hide all thread subtrees.
10713 If PREDICATE is supplied, threads that satisfy this predicate
10714 will not be hidden."
10715   (interactive)
10716   (save-excursion
10717     (goto-char (point-min))
10718     (let ((end nil))
10719       (while (not end)
10720         (when (or (not predicate)
10721                   (gnus-map-articles
10722                    predicate (gnus-summary-article-children)))
10723             (gnus-summary-hide-thread))
10724         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10725   (gnus-summary-position-point))
10726
10727 (defun gnus-summary-hide-thread ()
10728   "Hide thread subtrees.
10729 If PREDICATE is supplied, threads that satisfy this predicate
10730 will not be hidden.
10731 Returns nil if no threads were there to be hidden."
10732   (interactive)
10733   (let ((buffer-read-only nil)
10734         (start (point))
10735         (article (gnus-summary-article-number)))
10736     (goto-char start)
10737     ;; Go forward until either the buffer ends or the subthread
10738     ;; ends.
10739     (when (and (not (eobp))
10740                (or (zerop (gnus-summary-next-thread 1 t))
10741                    (goto-char (point-max))))
10742       (prog1
10743           (if (and (> (point) start)
10744                    (search-backward "\n" start t))
10745               (progn
10746                 (subst-char-in-region start (point) ?\n ?\^M)
10747                 (gnus-summary-goto-subject article))
10748             (goto-char start)
10749             nil)))))
10750
10751 (defun gnus-summary-go-to-next-thread (&optional previous)
10752   "Go to the same level (or less) next thread.
10753 If PREVIOUS is non-nil, go to previous thread instead.
10754 Return the article number moved to, or nil if moving was impossible."
10755   (let ((level (gnus-summary-thread-level))
10756         (way (if previous -1 1))
10757         (beg (point)))
10758     (forward-line way)
10759     (while (and (not (eobp))
10760                 (< level (gnus-summary-thread-level)))
10761       (forward-line way))
10762     (if (eobp)
10763         (progn
10764           (goto-char beg)
10765           nil)
10766       (setq beg (point))
10767       (prog1
10768           (gnus-summary-article-number)
10769         (goto-char beg)))))
10770
10771 (defun gnus-summary-next-thread (n &optional silent)
10772   "Go to the same level next N'th thread.
10773 If N is negative, search backward instead.
10774 Returns the difference between N and the number of skips actually
10775 done.
10776
10777 If SILENT, don't output messages."
10778   (interactive "p")
10779   (let ((backward (< n 0))
10780         (n (abs n)))
10781     (while (and (> n 0)
10782                 (gnus-summary-go-to-next-thread backward))
10783       (decf n))
10784     (unless silent
10785       (gnus-summary-position-point))
10786     (when (and (not silent) (/= 0 n))
10787       (gnus-message 7 "No more threads"))
10788     n))
10789
10790 (defun gnus-summary-prev-thread (n)
10791   "Go to the same level previous N'th thread.
10792 Returns the difference between N and the number of skips actually
10793 done."
10794   (interactive "p")
10795   (gnus-summary-next-thread (- n)))
10796
10797 (defun gnus-summary-go-down-thread ()
10798   "Go down one level in the current thread."
10799   (let ((children (gnus-summary-article-children)))
10800     (when children
10801       (gnus-summary-goto-subject (car children)))))
10802
10803 (defun gnus-summary-go-up-thread ()
10804   "Go up one level in the current thread."
10805   (let ((parent (gnus-summary-article-parent)))
10806     (when parent
10807       (gnus-summary-goto-subject parent))))
10808
10809 (defun gnus-summary-down-thread (n)
10810   "Go down thread N steps.
10811 If N is negative, go up instead.
10812 Returns the difference between N and how many steps down that were
10813 taken."
10814   (interactive "p")
10815   (let ((up (< n 0))
10816         (n (abs n)))
10817     (while (and (> n 0)
10818                 (if up (gnus-summary-go-up-thread)
10819                   (gnus-summary-go-down-thread)))
10820       (setq n (1- n)))
10821     (gnus-summary-position-point)
10822     (when (/= 0 n)
10823       (gnus-message 7 "Can't go further"))
10824     n))
10825
10826 (defun gnus-summary-up-thread (n)
10827   "Go up thread N steps.
10828 If N is negative, go down instead.
10829 Returns the difference between N and how many steps down that were
10830 taken."
10831   (interactive "p")
10832   (gnus-summary-down-thread (- n)))
10833
10834 (defun gnus-summary-top-thread ()
10835   "Go to the top of the thread."
10836   (interactive)
10837   (while (gnus-summary-go-up-thread))
10838   (gnus-summary-article-number))
10839
10840 (defun gnus-summary-kill-thread (&optional unmark)
10841   "Mark articles under current thread as read.
10842 If the prefix argument is positive, remove any kinds of marks.
10843 If the prefix argument is negative, tick articles instead."
10844   (interactive "P")
10845   (when unmark
10846     (setq unmark (prefix-numeric-value unmark)))
10847   (let ((articles (gnus-summary-articles-in-thread)))
10848     (save-excursion
10849       ;; Expand the thread.
10850       (gnus-summary-show-thread)
10851       ;; Mark all the articles.
10852       (while articles
10853         (gnus-summary-goto-subject (car articles))
10854         (cond ((null unmark)
10855                (gnus-summary-mark-article-as-read gnus-killed-mark))
10856               ((> unmark 0)
10857                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10858               (t
10859                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10860         (setq articles (cdr articles))))
10861     ;; Hide killed subtrees.
10862     (and (null unmark)
10863          gnus-thread-hide-killed
10864          (gnus-summary-hide-thread))
10865     ;; If marked as read, go to next unread subject.
10866     (when (null unmark)
10867       ;; Go to next unread subject.
10868       (gnus-summary-next-subject 1 t)))
10869   (gnus-set-mode-line 'summary))
10870
10871 ;; Summary sorting commands
10872
10873 (defun gnus-summary-sort-by-number (&optional reverse)
10874   "Sort the summary buffer by article number.
10875 Argument REVERSE means reverse order."
10876   (interactive "P")
10877   (gnus-summary-sort 'number reverse))
10878
10879 (defun gnus-summary-sort-by-random (&optional reverse)
10880   "Randomize the order in the summary buffer.
10881 Argument REVERSE means to randomize in reverse order."
10882   (interactive "P")
10883   (gnus-summary-sort 'random reverse))
10884
10885 (defun gnus-summary-sort-by-author (&optional reverse)
10886   "Sort the summary buffer by author name alphabetically.
10887 If `case-fold-search' is non-nil, case of letters is ignored.
10888 Argument REVERSE means reverse order."
10889   (interactive "P")
10890   (gnus-summary-sort 'author reverse))
10891
10892 (defun gnus-summary-sort-by-subject (&optional reverse)
10893   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10894 If `case-fold-search' is non-nil, case of letters is ignored.
10895 Argument REVERSE means reverse order."
10896   (interactive "P")
10897   (gnus-summary-sort 'subject reverse))
10898
10899 (defun gnus-summary-sort-by-date (&optional reverse)
10900   "Sort the summary buffer by date.
10901 Argument REVERSE means reverse order."
10902   (interactive "P")
10903   (gnus-summary-sort 'date reverse))
10904
10905 (defun gnus-summary-sort-by-score (&optional reverse)
10906   "Sort the summary buffer by score.
10907 Argument REVERSE means reverse order."
10908   (interactive "P")
10909   (gnus-summary-sort 'score reverse))
10910
10911 (defun gnus-summary-sort-by-lines (&optional reverse)
10912   "Sort the summary buffer by the number of lines.
10913 Argument REVERSE means reverse order."
10914   (interactive "P")
10915   (gnus-summary-sort 'lines reverse))
10916
10917 (defun gnus-summary-sort-by-chars (&optional reverse)
10918   "Sort the summary buffer by article length.
10919 Argument REVERSE means reverse order."
10920   (interactive "P")
10921   (gnus-summary-sort 'chars reverse))
10922
10923 (defun gnus-summary-sort-by-original (&optional reverse)
10924   "Sort the summary buffer using the default sorting method.
10925 Argument REVERSE means reverse order."
10926   (interactive "P")
10927   (let* ((buffer-read-only)
10928          (gnus-summary-prepare-hook nil))
10929     ;; We do the sorting by regenerating the threads.
10930     (gnus-summary-prepare)
10931     ;; Hide subthreads if needed.
10932     (gnus-summary-maybe-hide-threads)))
10933
10934 (defun gnus-summary-sort (predicate reverse)
10935   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10936   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10937          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10938          (gnus-thread-sort-functions
10939           (if (not reverse)
10940               thread
10941             `(lambda (t1 t2)
10942                (,thread t2 t1))))
10943          (gnus-sort-gathered-threads-function
10944           gnus-thread-sort-functions)
10945          (gnus-article-sort-functions
10946           (if (not reverse)
10947               article
10948             `(lambda (t1 t2)
10949                (,article t2 t1))))
10950          (buffer-read-only)
10951          (gnus-summary-prepare-hook nil))
10952     ;; We do the sorting by regenerating the threads.
10953     (gnus-summary-prepare)
10954     ;; Hide subthreads if needed.
10955     (gnus-summary-maybe-hide-threads)))
10956
10957 ;; Summary saving commands.
10958
10959 (defun gnus-summary-save-article (&optional n not-saved)
10960   "Save the current article using the default saver function.
10961 If N is a positive number, save the N next articles.
10962 If N is a negative number, save the N previous articles.
10963 If N is nil and any articles have been marked with the process mark,
10964 save those articles instead.
10965 The variable `gnus-default-article-saver' specifies the saver function."
10966   (interactive "P")
10967   (let* ((articles (gnus-summary-work-articles n))
10968          (save-buffer (save-excursion
10969                         (nnheader-set-temp-buffer " *Gnus Save*")))
10970          (num (length articles))
10971          header file)
10972     (dolist (article articles)
10973       (setq header (gnus-summary-article-header article))
10974       (if (not (vectorp header))
10975           ;; This is a pseudo-article.
10976           (if (assq 'name header)
10977               (gnus-copy-file (cdr (assq 'name header)))
10978             (gnus-message 1 "Article %d is unsaveable" article))
10979         ;; This is a real article.
10980         (save-window-excursion
10981           (let ((gnus-display-mime-function nil)
10982                 (gnus-article-prepare-hook nil))
10983             (gnus-summary-select-article t nil nil article)))
10984         (save-excursion
10985           (set-buffer save-buffer)
10986           (erase-buffer)
10987           (insert-buffer-substring gnus-original-article-buffer))
10988         (setq file (gnus-article-save save-buffer file num))
10989         (gnus-summary-remove-process-mark article)
10990         (unless not-saved
10991           (gnus-summary-set-saved-mark article))))
10992     (gnus-kill-buffer save-buffer)
10993     (gnus-summary-position-point)
10994     (gnus-set-mode-line 'summary)
10995     n))
10996
10997 (defun gnus-summary-pipe-output (&optional arg headers)
10998   "Pipe the current article to a subprocess.
10999 If N is a positive number, pipe the N next articles.
11000 If N is a negative number, pipe the N previous articles.
11001 If N is nil and any articles have been marked with the process mark,
11002 pipe those articles instead.
11003 If HEADERS (the symbolic prefix), include the headers, too."
11004   (interactive (gnus-interactive "P\ny"))
11005   (require 'gnus-art)
11006   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
11007         (gnus-save-all-headers (or headers gnus-save-all-headers)))
11008     (gnus-summary-save-article arg t))
11009   (let ((buffer (get-buffer "*Shell Command Output*")))
11010     (when (and buffer
11011                (not (zerop (buffer-size buffer))))
11012       (gnus-configure-windows 'pipe))))
11013
11014 (defun gnus-summary-save-article-mail (&optional arg)
11015   "Append the current article to an mail file.
11016 If N is a positive number, save the N next articles.
11017 If N is a negative number, save the N previous articles.
11018 If N is nil and any articles have been marked with the process mark,
11019 save those articles instead."
11020   (interactive "P")
11021   (require 'gnus-art)
11022   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11023     (gnus-summary-save-article arg)))
11024
11025 (defun gnus-summary-save-article-rmail (&optional arg)
11026   "Append the current article to an rmail file.
11027 If N is a positive number, save the N next articles.
11028 If N is a negative number, save the N previous articles.
11029 If N is nil and any articles have been marked with the process mark,
11030 save those articles instead."
11031   (interactive "P")
11032   (require 'gnus-art)
11033   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11034     (gnus-summary-save-article arg)))
11035
11036 (defun gnus-summary-save-article-file (&optional arg)
11037   "Append the current article to a file.
11038 If N is a positive number, save the N next articles.
11039 If N is a negative number, save the N previous articles.
11040 If N is nil and any articles have been marked with the process mark,
11041 save those articles instead."
11042   (interactive "P")
11043   (require 'gnus-art)
11044   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11045     (gnus-summary-save-article arg)))
11046
11047 (defun gnus-summary-write-article-file (&optional arg)
11048   "Write the current article to a file, deleting the previous file.
11049 If N is a positive number, save the N next articles.
11050 If N is a negative number, save the N previous articles.
11051 If N is nil and any articles have been marked with the process mark,
11052 save those articles instead."
11053   (interactive "P")
11054   (require 'gnus-art)
11055   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
11056     (gnus-summary-save-article arg)))
11057
11058 (defun gnus-summary-save-article-body-file (&optional arg)
11059   "Append the current article body to a file.
11060 If N is a positive number, save the N next articles.
11061 If N is a negative number, save the N previous articles.
11062 If N is nil and any articles have been marked with the process mark,
11063 save those articles instead."
11064   (interactive "P")
11065   (require 'gnus-art)
11066   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
11067     (gnus-summary-save-article arg)))
11068
11069 (defun gnus-summary-muttprint (&optional arg)
11070   "Print the current article using Muttprint.
11071 If N is a positive number, save the N next articles.
11072 If N is a negative number, save the N previous articles.
11073 If N is nil and any articles have been marked with the process mark,
11074 save those articles instead."
11075   (interactive "P")
11076   (require 'gnus-art)
11077   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
11078     (gnus-summary-save-article arg t)))
11079
11080 (defun gnus-summary-pipe-message (program)
11081   "Pipe the current article through PROGRAM."
11082   (interactive "sProgram: ")
11083   (gnus-summary-select-article)
11084   (let ((mail-header-separator ""))
11085     (gnus-eval-in-buffer-window gnus-article-buffer
11086       (save-restriction
11087         (widen)
11088         (let ((start (window-start))
11089               buffer-read-only)
11090           (message-pipe-buffer-body program)
11091           (set-window-start (get-buffer-window (current-buffer)) start))))))
11092
11093 (defun gnus-get-split-value (methods)
11094   "Return a value based on the split METHODS."
11095   (let (split-name method result match)
11096     (when methods
11097       (save-excursion
11098         (set-buffer gnus-original-article-buffer)
11099         (save-restriction
11100           (nnheader-narrow-to-headers)
11101           (while (and methods (not split-name))
11102             (goto-char (point-min))
11103             (setq method (pop methods))
11104             (setq match (car method))
11105             (when (cond
11106                    ((stringp match)
11107                     ;; Regular expression.
11108                     (ignore-errors
11109                       (re-search-forward match nil t)))
11110                    ((functionp match)
11111                     ;; Function.
11112                     (save-restriction
11113                       (widen)
11114                       (setq result (funcall match gnus-newsgroup-name))))
11115                    ((consp match)
11116                     ;; Form.
11117                     (save-restriction
11118                       (widen)
11119                       (setq result (eval match)))))
11120               (setq split-name (cdr method))
11121               (cond ((stringp result)
11122                      (push (expand-file-name
11123                             result gnus-article-save-directory)
11124                            split-name))
11125                     ((consp result)
11126                      (setq split-name (append result split-name)))))))))
11127     (nreverse split-name)))
11128
11129 (defun gnus-valid-move-group-p (group)
11130   (and (boundp group)
11131        (symbol-name group)
11132        (symbol-value group)
11133        (gnus-get-function (gnus-find-method-for-group
11134                            (symbol-name group)) 'request-accept-article t)))
11135
11136 (defun gnus-read-move-group-name (prompt default articles prefix)
11137   "Read a group name."
11138   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11139          (minibuffer-confirm-incomplete nil) ; XEmacs
11140          (prom
11141           (format "%s %s to:"
11142                   prompt
11143                   (if (> (length articles) 1)
11144                       (format "these %d articles" (length articles))
11145                     "this article")))
11146          (to-newsgroup
11147           (cond
11148            ((null split-name)
11149             (gnus-completing-read-with-default
11150              default prom
11151              gnus-active-hashtb
11152              'gnus-valid-move-group-p
11153              nil prefix
11154              'gnus-group-history))
11155            ((= 1 (length split-name))
11156             (gnus-completing-read-with-default
11157              (car split-name) prom
11158              gnus-active-hashtb
11159              'gnus-valid-move-group-p
11160              nil nil
11161              'gnus-group-history))
11162            (t
11163             (gnus-completing-read-with-default
11164              nil prom
11165              (mapcar 'list (nreverse split-name))
11166              nil nil nil
11167              'gnus-group-history))))
11168          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11169     (when to-newsgroup
11170       (if (or (string= to-newsgroup "")
11171               (string= to-newsgroup prefix))
11172           (setq to-newsgroup default))
11173       (unless to-newsgroup
11174         (error "No group name entered"))
11175       (or (gnus-active to-newsgroup)
11176           (gnus-activate-group to-newsgroup nil nil to-method)
11177           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11178                                      to-newsgroup))
11179               (or (and (gnus-request-create-group to-newsgroup to-method)
11180                        (gnus-activate-group
11181                         to-newsgroup nil nil to-method)
11182                        (gnus-subscribe-group to-newsgroup))
11183                   (error "Couldn't create group %s" to-newsgroup)))
11184           (error "No such group: %s" to-newsgroup)))
11185     to-newsgroup))
11186
11187 (defun gnus-summary-save-parts (type dir n &optional reverse)
11188   "Save parts matching TYPE to DIR.
11189 If REVERSE, save parts that do not match TYPE."
11190   (interactive
11191    (list (read-string "Save parts of type: "
11192                       (or (car gnus-summary-save-parts-type-history)
11193                           gnus-summary-save-parts-default-mime)
11194                       'gnus-summary-save-parts-type-history)
11195          (setq gnus-summary-save-parts-last-directory
11196                (read-file-name "Save to directory: "
11197                                gnus-summary-save-parts-last-directory
11198                                nil t))
11199          current-prefix-arg))
11200   (gnus-summary-iterate n
11201     (let ((gnus-display-mime-function nil)
11202           (gnus-inhibit-treatment t))
11203       (gnus-summary-select-article))
11204     (save-excursion
11205       (set-buffer gnus-article-buffer)
11206       (let ((handles (or gnus-article-mime-handles
11207                          (mm-dissect-buffer nil gnus-article-loose-mime)
11208                          (and gnus-article-emulate-mime
11209                               (mm-uu-dissect)))))
11210         (when handles
11211           (gnus-summary-save-parts-1 type dir handles reverse)
11212           (unless gnus-article-mime-handles ;; Don't destroy this case.
11213             (mm-destroy-parts handles)))))))
11214
11215 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11216   (if (stringp (car handle))
11217       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11218               (cdr handle))
11219     (when (if reverse
11220               (not (string-match type (mm-handle-media-type handle)))
11221             (string-match type (mm-handle-media-type handle)))
11222       (let ((file (expand-file-name
11223                    (gnus-map-function
11224                     mm-file-name-rewrite-functions
11225                     (file-name-nondirectory
11226                      (or
11227                       (mail-content-type-get
11228                        (mm-handle-disposition handle) 'filename)
11229                       (mail-content-type-get
11230                        (mm-handle-type handle) 'name)
11231                       (concat gnus-newsgroup-name
11232                               "." (number-to-string
11233                                    (cdr gnus-article-current))))))
11234                    dir)))
11235         (unless (file-exists-p file)
11236           (mm-save-part-to-file handle file))))))
11237
11238 ;; Summary extract commands
11239
11240 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11241   (let ((buffer-read-only nil)
11242         (article (gnus-summary-article-number))
11243         after-article b e)
11244     (unless (gnus-summary-goto-subject article)
11245       (error "No such article: %d" article))
11246     (gnus-summary-position-point)
11247     ;; If all commands are to be bunched up on one line, we collect
11248     ;; them here.
11249     (unless gnus-view-pseudos-separately
11250       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11251             files action)
11252         (while ps
11253           (setq action (cdr (assq 'action (car ps))))
11254           (setq files (list (cdr (assq 'name (car ps)))))
11255           (while (and ps (cdr ps)
11256                       (string= (or action "1")
11257                                (or (cdr (assq 'action (cadr ps))) "2")))
11258             (push (cdr (assq 'name (cadr ps))) files)
11259             (setcdr ps (cddr ps)))
11260           (when files
11261             (when (not (string-match "%s" action))
11262               (push " " files))
11263             (push " " files)
11264             (when (assq 'execute (car ps))
11265               (setcdr (assq 'execute (car ps))
11266                       (funcall (if (string-match "%s" action)
11267                                    'format 'concat)
11268                                action
11269                                (mapconcat
11270                                 (lambda (f)
11271                                   (if (equal f " ")
11272                                       f
11273                                     (shell-quote-argument f)))
11274                                 files " ")))))
11275           (setq ps (cdr ps)))))
11276     (if (and gnus-view-pseudos (not not-view))
11277         (while pslist
11278           (when (assq 'execute (car pslist))
11279             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11280                                   (eq gnus-view-pseudos 'not-confirm)))
11281           (setq pslist (cdr pslist)))
11282       (save-excursion
11283         (while pslist
11284           (setq after-article (or (cdr (assq 'article (car pslist)))
11285                                   (gnus-summary-article-number)))
11286           (gnus-summary-goto-subject after-article)
11287           (forward-line 1)
11288           (setq b (point))
11289           (insert "    " (file-name-nondirectory
11290                           (cdr (assq 'name (car pslist))))
11291                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11292           (setq e (point))
11293           (forward-line -1)             ; back to `b'
11294           (gnus-add-text-properties
11295            b (1- e) (list 'gnus-number gnus-reffed-article-number
11296                           gnus-mouse-face-prop gnus-mouse-face))
11297           (gnus-data-enter
11298            after-article gnus-reffed-article-number
11299            gnus-unread-mark b (car pslist) 0 (- e b))
11300           (setq gnus-newsgroup-unreads
11301                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11302                                          gnus-reffed-article-number))
11303           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11304           (setq pslist (cdr pslist)))))))
11305
11306 (defun gnus-pseudos< (p1 p2)
11307   (let ((c1 (cdr (assq 'action p1)))
11308         (c2 (cdr (assq 'action p2))))
11309     (and c1 c2 (string< c1 c2))))
11310
11311 (defun gnus-request-pseudo-article (props)
11312   (cond ((assq 'execute props)
11313          (gnus-execute-command (cdr (assq 'execute props)))))
11314   (let ((gnus-current-article (gnus-summary-article-number)))
11315     (gnus-run-hooks 'gnus-mark-article-hook)))
11316
11317 (defun gnus-execute-command (command &optional automatic)
11318   (save-excursion
11319     (gnus-article-setup-buffer)
11320     (set-buffer gnus-article-buffer)
11321     (setq buffer-read-only nil)
11322     (let ((command (if automatic command
11323                      (read-string "Command: " (cons command 0)))))
11324       (erase-buffer)
11325       (insert "$ " command "\n\n")
11326       (if gnus-view-pseudo-asynchronously
11327           (start-process "gnus-execute" (current-buffer) shell-file-name
11328                          shell-command-switch command)
11329         (call-process shell-file-name nil t nil
11330                       shell-command-switch command)))))
11331
11332 ;; Summary kill commands.
11333
11334 (defun gnus-summary-edit-global-kill (article)
11335   "Edit the \"global\" kill file."
11336   (interactive (list (gnus-summary-article-number)))
11337   (gnus-group-edit-global-kill article))
11338
11339 (defun gnus-summary-edit-local-kill ()
11340   "Edit a local kill file applied to the current newsgroup."
11341   (interactive)
11342   (setq gnus-current-headers (gnus-summary-article-header))
11343   (gnus-group-edit-local-kill
11344    (gnus-summary-article-number) gnus-newsgroup-name))
11345
11346 ;;; Header reading.
11347
11348 (defun gnus-read-header (id &optional header)
11349   "Read the headers of article ID and enter them into the Gnus system."
11350   (let ((group gnus-newsgroup-name)
11351         (gnus-override-method
11352          (or
11353           gnus-override-method
11354           (and (gnus-news-group-p gnus-newsgroup-name)
11355                (car (gnus-refer-article-methods)))))
11356         where)
11357     ;; First we check to see whether the header in question is already
11358     ;; fetched.
11359     (if (stringp id)
11360         ;; This is a Message-ID.
11361         (setq header (or header (gnus-id-to-header id)))
11362       ;; This is an article number.
11363       (setq header (or header (gnus-summary-article-header id))))
11364     (if (and header
11365              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11366         ;; We have found the header.
11367         header
11368       ;; We have to really fetch the header to this article.
11369       (save-excursion
11370         (set-buffer nntp-server-buffer)
11371         (when (setq where (gnus-request-head id group))
11372           (nnheader-fold-continuation-lines)
11373           (goto-char (point-max))
11374           (insert ".\n")
11375           (goto-char (point-min))
11376           (insert "211 ")
11377           (princ (cond
11378                   ((numberp id) id)
11379                   ((cdr where) (cdr where))
11380                   (header (mail-header-number header))
11381                   (t gnus-reffed-article-number))
11382                  (current-buffer))
11383           (insert " Article retrieved.\n"))
11384         (if (or (not where)
11385                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11386             ()                          ; Malformed head.
11387           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11388             (when (and (stringp id)
11389                        (not (string= (gnus-group-real-name group)
11390                                      (car where))))
11391               ;; If we fetched by Message-ID and the article came
11392               ;; from a different group, we fudge some bogus article
11393               ;; numbers for this article.
11394               (mail-header-set-number header gnus-reffed-article-number))
11395             (save-excursion
11396               (set-buffer gnus-summary-buffer)
11397               (decf gnus-reffed-article-number)
11398               (gnus-remove-header (mail-header-number header))
11399               (push header gnus-newsgroup-headers)
11400               (setq gnus-current-headers header)
11401               (push (mail-header-number header) gnus-newsgroup-limit)))
11402           header)))))
11403
11404 (defun gnus-remove-header (number)
11405   "Remove header NUMBER from `gnus-newsgroup-headers'."
11406   (if (and gnus-newsgroup-headers
11407            (= number (mail-header-number (car gnus-newsgroup-headers))))
11408       (pop gnus-newsgroup-headers)
11409     (let ((headers gnus-newsgroup-headers))
11410       (while (and (cdr headers)
11411                   (not (= number (mail-header-number (cadr headers)))))
11412         (pop headers))
11413       (when (cdr headers)
11414         (setcdr headers (cddr headers))))))
11415
11416 ;;;
11417 ;;; summary highlights
11418 ;;;
11419
11420 (defun gnus-highlight-selected-summary ()
11421   "Highlight selected article in summary buffer."
11422   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11423   (when gnus-summary-selected-face
11424     (save-excursion
11425       (let* ((beg (point-at-bol))
11426              (end (point-at-eol))
11427              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11428              (from (if (get-text-property beg gnus-mouse-face-prop)
11429                        beg
11430                      (or (next-single-property-change
11431                           beg gnus-mouse-face-prop nil end)
11432                          beg)))
11433              (to
11434               (if (= from end)
11435                   (- from 2)
11436                 (or (next-single-property-change
11437                      from gnus-mouse-face-prop nil end)
11438                     end))))
11439         ;; If no mouse-face prop on line we will have to = from = end,
11440         ;; so we highlight the entire line instead.
11441         (when (= (+ to 2) from)
11442           (setq from beg)
11443           (setq to end))
11444         (if gnus-newsgroup-selected-overlay
11445             ;; Move old overlay.
11446             (gnus-move-overlay
11447              gnus-newsgroup-selected-overlay from to (current-buffer))
11448           ;; Create new overlay.
11449           (gnus-overlay-put
11450            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11451            'face gnus-summary-selected-face))))))
11452
11453 (defvar gnus-summary-highlight-line-cached nil)
11454 (defvar gnus-summary-highlight-line-trigger nil)
11455
11456 (defun gnus-summary-highlight-line-0 ()
11457   (if (and (eq gnus-summary-highlight-line-trigger
11458                gnus-summary-highlight)
11459            gnus-summary-highlight-line-cached)
11460       gnus-summary-highlight-line-cached
11461     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11462           gnus-summary-highlight-line-cached
11463           (let* ((cond (list 'cond))
11464                  (c cond)
11465                  (list gnus-summary-highlight))
11466             (while list
11467               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11468                               nil))
11469               (setq c (cdr c)
11470                     list (cdr list)))
11471             (gnus-byte-compile (list 'lambda nil cond))))))
11472
11473 (defun gnus-summary-highlight-line ()
11474   "Highlight current line according to `gnus-summary-highlight'."
11475   (let* ((beg (point-at-bol))
11476          (article (or (gnus-summary-article-number) gnus-current-article))
11477          (score (or (cdr (assq article
11478                                gnus-newsgroup-scored))
11479                     gnus-summary-default-score 0))
11480          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11481          (inhibit-read-only t)
11482          (default gnus-summary-default-score)
11483          (default-high gnus-summary-default-high-score)
11484          (default-low gnus-summary-default-low-score)
11485          (uncached (and gnus-summary-use-undownloaded-faces
11486                         (memq article gnus-newsgroup-undownloaded)
11487                         (not (memq article gnus-newsgroup-cached)))))
11488     (let ((face (funcall (gnus-summary-highlight-line-0))))
11489       (unless (eq face (get-text-property beg 'face))
11490         (gnus-put-text-property-excluding-characters-with-faces
11491          beg (point-at-eol) 'face
11492          (setq face (if (boundp face) (symbol-value face) face)))
11493         (when gnus-summary-highlight-line-function
11494           (funcall gnus-summary-highlight-line-function article face))))))
11495
11496 (defun gnus-update-read-articles (group unread &optional compute)
11497   "Update the list of read articles in GROUP.
11498 UNREAD is a sorted list."
11499   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11500         (info (gnus-get-info group))
11501         (prev 1)
11502         read)
11503     (if (or (not info) (not active))
11504         ;; There is no info on this group if it was, in fact,
11505         ;; killed.  Gnus stores no information on killed groups, so
11506         ;; there's nothing to be done.
11507         ;; One could store the information somewhere temporarily,
11508         ;; perhaps...  Hmmm...
11509         ()
11510       ;; Remove any negative articles numbers.
11511       (while (and unread (< (car unread) 0))
11512         (setq unread (cdr unread)))
11513       ;; Remove any expired article numbers
11514       (while (and unread (< (car unread) (car active)))
11515         (setq unread (cdr unread)))
11516       ;; Compute the ranges of read articles by looking at the list of
11517       ;; unread articles.
11518       (while unread
11519         (when (/= (car unread) prev)
11520           (push (if (= prev (1- (car unread))) prev
11521                   (cons prev (1- (car unread))))
11522                 read))
11523         (setq prev (1+ (car unread)))
11524         (setq unread (cdr unread)))
11525       (when (<= prev (cdr active))
11526         (push (cons prev (cdr active)) read))
11527       (setq read (if (> (length read) 1) (nreverse read) read))
11528       (if compute
11529           read
11530         (save-excursion
11531           (let (setmarkundo)
11532             ;; Propagate the read marks to the backend.
11533             (when (gnus-check-backend-function 'request-set-mark group)
11534               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11535                     (add (gnus-remove-from-range read (gnus-info-read info))))
11536                 (when (or add del)
11537                   (unless (gnus-check-group group)
11538                     (error "Can't open server for %s" group))
11539                   (gnus-request-set-mark
11540                    group (delq nil (list (if add (list add 'add '(read)))
11541                                          (if del (list del 'del '(read))))))
11542                   (setq setmarkundo
11543                         `(gnus-request-set-mark
11544                           ,group
11545                           ',(delq nil (list
11546                                        (if del (list del 'add '(read)))
11547                                        (if add (list add 'del '(read))))))))))
11548             (set-buffer gnus-group-buffer)
11549             (gnus-undo-register
11550               `(progn
11551                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11552                  (gnus-info-set-read ',info ',(gnus-info-read info))
11553                  (gnus-get-unread-articles-in-group ',info
11554                                                     (gnus-active ,group))
11555                  (gnus-group-update-group ,group t)
11556                  ,setmarkundo))))
11557         ;; Enter this list into the group info.
11558         (gnus-info-set-read info read)
11559         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11560         (gnus-get-unread-articles-in-group info (gnus-active group))
11561         t))))
11562
11563 (defun gnus-offer-save-summaries ()
11564   "Offer to save all active summary buffers."
11565   (let (buffers)
11566     ;; Go through all buffers and find all summaries.
11567     (dolist (buffer (buffer-list))
11568       (when (and (setq buffer (buffer-name buffer))
11569                  (string-match "Summary" buffer)
11570                  (with-current-buffer buffer
11571                    ;; We check that this is, indeed, a summary buffer.
11572                    (and (eq major-mode 'gnus-summary-mode)
11573                         ;; Also make sure this isn't bogus.
11574                         gnus-newsgroup-prepared
11575                         ;; Also make sure that this isn't a
11576                         ;; dead summary buffer.
11577                         (not gnus-dead-summary-mode))))
11578         (push buffer buffers)))
11579     ;; Go through all these summary buffers and offer to save them.
11580     (when buffers
11581       (save-excursion
11582         (map-y-or-n-p
11583          "Update summary buffer %s? "
11584          (lambda (buf)
11585            (switch-to-buffer buf)
11586            (gnus-summary-exit))
11587          buffers)))))
11588
11589
11590 ;;; @ for mime-partial
11591 ;;;
11592
11593 (defun gnus-request-partial-message ()
11594   (save-excursion
11595     (let ((number (gnus-summary-article-number))
11596           (group gnus-newsgroup-name)
11597           (mother gnus-article-buffer))
11598       (set-buffer (get-buffer-create " *Partial Article*"))
11599       (erase-buffer)
11600       (setq mime-preview-buffer mother)
11601       (gnus-request-article-this-buffer number group)
11602       (mime-parse-buffer)
11603       )))
11604
11605 (autoload 'mime-combine-message/partial-pieces-automatically
11606   "mime-partial"
11607   "Internal method to combine message/partial messages automatically.")
11608
11609 (mime-add-condition
11610  'action '((type . message)(subtype . partial)
11611            (major-mode . gnus-original-article-mode)
11612            (method . mime-combine-message/partial-pieces-automatically)
11613            (summary-buffer-exp . gnus-summary-buffer)
11614            (request-partial-message-method . gnus-request-partial-message)
11615            ))
11616
11617
11618 ;;; @ for message/rfc822
11619 ;;;
11620
11621 (defun gnus-mime-extract-message/rfc822 (entity situation)
11622   "Burst a forwarded article."
11623   (save-excursion
11624     (set-buffer gnus-summary-buffer)
11625     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11626                                    gnus-newsgroup-name 'gnus-group-history))
11627            (gnus-group-marked (list group))
11628            article info)
11629       (with-temp-buffer
11630         (mime-insert-entity-content entity)
11631         (setq article (gnus-request-accept-article group)))
11632       (when (and (consp article)
11633                  (numberp (setq article (cdr article))))
11634         (setq info (gnus-get-info group))
11635         (gnus-info-set-read info
11636                             (gnus-remove-from-range (gnus-info-read info)
11637                                                     (list article)))
11638         (when (string-equal group gnus-newsgroup-name)
11639           (forward-line 1)
11640           (let (gnus-show-threads)
11641             (gnus-summary-goto-subject article t))
11642           (gnus-summary-clear-mark-forward 1))
11643         (set-buffer gnus-group-buffer)
11644         (gnus-group-get-new-news-this-group nil t)))))
11645
11646 (mime-add-condition
11647  'action '((type . message)(subtype . rfc822)
11648            (major-mode . gnus-original-article-mode)
11649            (method . gnus-mime-extract-message/rfc822)
11650            (mode . "extract")
11651            ))
11652
11653 (mime-add-condition
11654  'action '((type . message)(subtype . news)
11655            (major-mode . gnus-original-article-mode)
11656            (method . gnus-mime-extract-message/rfc822)
11657            (mode . "extract")
11658            ))
11659
11660 (defun gnus-mime-extract-multipart (entity situation)
11661   (let ((children (mime-entity-children entity))
11662         mime-acting-situation-to-override
11663         f)
11664     (while children
11665       (mime-play-entity (car children)
11666                         (cons (assq 'mode situation)
11667                               mime-acting-situation-to-override))
11668       (setq children (cdr children)))
11669     (if (setq f (cdr (assq 'after-method
11670                            mime-acting-situation-to-override)))
11671         (eval f)
11672       )))
11673
11674 (mime-add-condition
11675  'action '((type . multipart)
11676            (method . gnus-mime-extract-multipart)
11677            (mode . "extract")
11678            )
11679  'with-default)
11680
11681
11682 ;;; @ end
11683 ;;;
11684
11685 (defun gnus-summary-inherit-default-charset ()
11686   "Import `default-mime-charset' from summary buffer.
11687 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11688 of FLIM is used."
11689   (if (buffer-live-p gnus-summary-buffer)
11690       (let (d-m-c d-m-c-u)
11691         (with-current-buffer gnus-summary-buffer
11692           (setq d-m-c (if (local-variable-p 'default-mime-charset
11693                                             gnus-summary-buffer)
11694                           default-mime-charset
11695                         t)
11696                 ;; LIMIT
11697                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11698                                               gnus-summary-buffer)
11699                             (symbol-value 'default-mime-charset-unlimited)
11700                           t)))
11701         (if (eq t d-m-c)
11702             (kill-local-variable 'default-mime-charset)
11703           (set (make-local-variable 'default-mime-charset) d-m-c))
11704         (if (eq t d-m-c-u)
11705             (kill-local-variable 'default-mime-charset-unlimited)
11706           (set (make-local-variable 'default-mime-charset-unlimited)
11707                d-m-c-u)))))
11708
11709 (defun gnus-summary-setup-default-charset ()
11710   "Setup newsgroup default charset."
11711   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11712       (progn
11713         (setq gnus-newsgroup-charset nil)
11714         (set (make-local-variable 'default-mime-charset) nil)
11715         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11716           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11717     (let ((ignored-charsets
11718            (or gnus-newsgroup-ephemeral-ignored-charsets
11719                (append
11720                 (and gnus-newsgroup-name
11721                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11722                 gnus-newsgroup-ignored-charsets)))
11723           charset)
11724       (setq gnus-newsgroup-charset
11725             (or gnus-newsgroup-ephemeral-charset
11726                 (when (and gnus-newsgroup-name
11727                            (setq charset (gnus-parameter-charset
11728                                           gnus-newsgroup-name)))
11729                   (make-local-variable 'default-mime-charset)
11730                   (setq default-mime-charset charset))
11731                 gnus-default-charset))
11732       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11733            ignored-charsets))))
11734
11735 ;;;
11736 ;;; Mime Commands
11737 ;;;
11738
11739 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11740   "Display the current article buffer fully MIME-buttonized.
11741 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11742 treated as multipart/mixed."
11743   (interactive "P")
11744   (require 'gnus-art)
11745   (let ((gnus-unbuttonized-mime-types nil)
11746         (gnus-mime-display-multipart-as-mixed show-all-parts))
11747     (gnus-summary-show-article)))
11748
11749 (defun gnus-summary-repair-multipart (article)
11750   "Add a Content-Type header to a multipart article without one."
11751   (interactive (list (gnus-summary-article-number)))
11752   (gnus-with-article article
11753     (message-narrow-to-head)
11754     (message-remove-header "Mime-Version")
11755     (goto-char (point-max))
11756     (insert "Mime-Version: 1.0\n")
11757     (widen)
11758     (when (search-forward "\n--" nil t)
11759       (let ((separator (buffer-substring (point) (point-at-eol))))
11760         (message-narrow-to-head)
11761         (message-remove-header "Content-Type")
11762         (goto-char (point-max))
11763         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11764                         separator))
11765         (widen))))
11766   (let (gnus-mark-article-hook)
11767     (gnus-summary-select-article t t nil article)))
11768
11769 (defun gnus-summary-toggle-display-buttonized ()
11770   "Toggle the buttonizing of the article buffer."
11771   (interactive)
11772   (require 'gnus-art)
11773   (if (setq gnus-inhibit-mime-unbuttonizing
11774             (not gnus-inhibit-mime-unbuttonizing))
11775       (let ((gnus-unbuttonized-mime-types nil))
11776         (gnus-summary-show-article))
11777     (gnus-summary-show-article)))
11778
11779 ;;;
11780 ;;; Intelli-mouse commmands
11781 ;;;
11782
11783 (defun gnus-wheel-summary-scroll (event)
11784   (interactive "e")
11785   (let ((amount (if (memq 'shift (event-modifiers event))
11786                     (car gnus-wheel-scroll-amount)
11787                   (cdr gnus-wheel-scroll-amount)))
11788         (direction (- (* (static-if (featurep 'xemacs)
11789                              (event-button event)
11790                            (cond ((eq 'mouse-4 (event-basic-type event))
11791                                   4)
11792                                  ((eq 'mouse-5 (event-basic-type event))
11793                                   5)))
11794                          2) 9))
11795         edge)
11796     (gnus-summary-scroll-up (* amount direction))
11797     (when (gnus-eval-in-buffer-window gnus-article-buffer
11798             (save-restriction
11799               (widen)
11800               (and (if (< 0 direction)
11801                        (gnus-article-next-page 0)
11802                      (gnus-article-prev-page 0)
11803                      (bobp))
11804                    (if (setq edge (get-text-property
11805                                    (point-min) 'gnus-wheel-edge))
11806                        (setq edge (* edge direction))
11807                      (setq edge -1))
11808                    (or (plusp edge)
11809                        (let ((buffer-read-only nil)
11810                              (inhibit-read-only t))
11811                          (put-text-property (point-min) (point-max)
11812                                             'gnus-wheel-edge direction)
11813                          nil))
11814                    (or (> edge gnus-wheel-edge-resistance)
11815                        (let ((buffer-read-only nil)
11816                              (inhibit-read-only t))
11817                          (put-text-property (point-min) (point-max)
11818                                             'gnus-wheel-edge
11819                                             (* (1+ edge) direction))
11820                          nil))
11821                    (eq last-command 'gnus-wheel-summary-scroll))))
11822       (gnus-summary-next-article nil nil (minusp direction)))))
11823
11824 (defun gnus-wheel-install ()
11825   "Enable mouse wheel support on summary window."
11826   (when gnus-use-wheel
11827     (let ((keys
11828            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11829       (dolist (key keys)
11830         (define-key gnus-summary-mode-map key
11831           'gnus-wheel-summary-scroll)))))
11832
11833 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11834
11835 ;;;
11836 ;;; Traditional PGP commmands
11837 ;;;
11838
11839 (defun gnus-summary-decrypt-article (&optional force)
11840   "Decrypt the current article in traditional PGP way.
11841 This will have permanent effect only in mail groups.
11842 If FORCE is non-nil, allow editing of articles even in read-only
11843 groups."
11844   (interactive "P")
11845   (gnus-summary-select-article t)
11846   (gnus-eval-in-buffer-window gnus-article-buffer
11847     (save-excursion
11848       (save-restriction
11849         (widen)
11850         (goto-char (point-min))
11851         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11852           (error "Not a traditional PGP message!"))
11853         (let ((armor-start (match-beginning 0)))
11854           (if (and (pgg-decrypt-region armor-start (point-max))
11855                    (or force (not (gnus-group-read-only-p))))
11856               (let ((inhibit-read-only t)
11857                     buffer-read-only)
11858                 (delete-region armor-start
11859                                (progn
11860                                  (re-search-forward "^-+END PGP" nil t)
11861                                  (beginning-of-line 2)
11862                                  (point)))
11863                 (insert-buffer-substring pgg-output-buffer))))))))
11864
11865 (defun gnus-summary-verify-article ()
11866   "Verify the current article in traditional PGP way."
11867   (interactive)
11868   (save-excursion
11869     (set-buffer gnus-original-article-buffer)
11870     (goto-char (point-min))
11871     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11872       (error "Not a traditional PGP message!"))
11873     (re-search-forward "^-+END PGP" nil t)
11874     (beginning-of-line 2)
11875     (call-interactively (function pgg-verify-region))))
11876
11877 ;;;
11878 ;;; Generic summary marking commands
11879 ;;;
11880
11881 (defvar gnus-summary-marking-alist
11882   '((read gnus-del-mark "d")
11883     (unread gnus-unread-mark "u")
11884     (ticked gnus-ticked-mark "!")
11885     (dormant gnus-dormant-mark "?")
11886     (expirable gnus-expirable-mark "e"))
11887   "An alist of names/marks/keystrokes.")
11888
11889 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11890 (defvar gnus-summary-mark-map)
11891
11892 (defun gnus-summary-make-all-marking-commands ()
11893   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11894   (dolist (elem gnus-summary-marking-alist)
11895     (apply 'gnus-summary-make-marking-command elem)))
11896
11897 (defun gnus-summary-make-marking-command (name mark keystroke)
11898   (let ((map (make-sparse-keymap)))
11899     (define-key gnus-summary-generic-mark-map keystroke map)
11900     (dolist (lway `((next "next" next nil "n")
11901                     (next-unread "next unread" next t "N")
11902                     (prev "previous" prev nil "p")
11903                     (prev-unread "previous unread" prev t "P")
11904                     (nomove "" nil nil ,keystroke)))
11905       (let ((func (gnus-summary-make-marking-command-1
11906                    mark (car lway) lway name)))
11907         (setq func (eval func))
11908         (define-key map (nth 4 lway) func)))))
11909
11910 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11911   `(defun ,(intern
11912             (format "gnus-summary-put-mark-as-%s%s"
11913                     name (if (eq way 'nomove)
11914                              ""
11915                            (concat "-" (symbol-name way)))))
11916      (n)
11917      ,(format
11918        "Mark the current article as %s%s.
11919 If N, the prefix, then repeat N times.
11920 If N is negative, move in reverse order.
11921 The difference between N and the actual number of articles marked is
11922 returned."
11923        name (car (cdr lway)))
11924      (interactive "p")
11925      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11926
11927 (defun gnus-summary-generic-mark (n mark move unread)
11928   "Mark N articles with MARK."
11929   (unless (eq major-mode 'gnus-summary-mode)
11930     (error "This command can only be used in the summary buffer"))
11931   (gnus-summary-show-thread)
11932   (let ((nummove
11933          (cond
11934           ((eq move 'next) 1)
11935           ((eq move 'prev) -1)
11936           (t 0))))
11937     (if (zerop nummove)
11938         (setq n 1)
11939       (when (< n 0)
11940         (setq n (abs n)
11941               nummove (* -1 nummove))))
11942     (while (and (> n 0)
11943                 (gnus-summary-mark-article nil mark)
11944                 (zerop (gnus-summary-next-subject nummove unread t)))
11945       (setq n (1- n)))
11946     (when (/= 0 n)
11947       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11948     (gnus-summary-recenter)
11949     (gnus-summary-position-point)
11950     (gnus-set-mode-line 'summary)
11951     n))
11952
11953 (defun gnus-summary-insert-articles (articles)
11954   (when (setq articles
11955               (gnus-sorted-difference articles
11956                                       (mapcar (lambda (h)
11957                                                 (mail-header-number h))
11958                                               gnus-newsgroup-headers)))
11959     (setq gnus-newsgroup-headers
11960           (gnus-merge 'list
11961                       gnus-newsgroup-headers
11962                       (gnus-fetch-headers articles)
11963                       'gnus-article-sort-by-number))
11964     ;; Suppress duplicates?
11965     (when gnus-suppress-duplicates
11966       (gnus-dup-suppress-articles))
11967
11968     ;; We might want to build some more threads first.
11969     (when (and gnus-fetch-old-headers
11970                (eq gnus-headers-retrieved-by 'nov))
11971       (if (eq gnus-fetch-old-headers 'invisible)
11972           (gnus-build-all-threads)
11973         (gnus-build-old-threads)))
11974     ;; Let the Gnus agent mark articles as read.
11975     (when gnus-agent
11976       (gnus-agent-get-undownloaded-list))
11977     ;; Remove list identifiers from subject
11978     (when gnus-list-identifiers
11979       (gnus-summary-remove-list-identifiers))
11980     ;; First and last article in this newsgroup.
11981     (when gnus-newsgroup-headers
11982       (setq gnus-newsgroup-begin
11983             (mail-header-number (car gnus-newsgroup-headers))
11984             gnus-newsgroup-end
11985             (mail-header-number
11986              (gnus-last-element gnus-newsgroup-headers))))
11987     (when gnus-use-scoring
11988       (gnus-possibly-score-headers))))
11989
11990 (defun gnus-summary-insert-old-articles (&optional all)
11991   "Insert all old articles in this group.
11992 If ALL is non-nil, already read articles become readable.
11993 If ALL is a number, fetch this number of articles."
11994   (interactive "P")
11995   (prog1
11996       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11997             older len)
11998         (setq older
11999               ;; Some nntp servers lie about their active range.  When
12000               ;; this happens, the active range can be in the millions.
12001               ;; Use a compressed range to avoid creating a huge list.
12002               (gnus-range-difference (list gnus-newsgroup-active) old))
12003         (setq len (gnus-range-length older))
12004         (cond
12005          ((null older) nil)
12006          ((numberp all)
12007           (if (< all len)
12008               (let ((older-range (nreverse older)))
12009                 (setq older nil)
12010
12011                 (while (> all 0)
12012                   (let* ((r (pop older-range))
12013                          (min (if (numberp r) r (car r)))
12014                          (max (if (numberp r) r (cdr r))))
12015                     (while (and (<= min max)
12016                                 (> all 0))
12017                       (push max older)
12018                       (setq all (1- all)
12019                             max (1- max))))))
12020             (setq older (gnus-uncompress-range older))))
12021          (all
12022           (setq older (gnus-uncompress-range older)))
12023          (t
12024           (when (and (numberp gnus-large-newsgroup)
12025                    (> len gnus-large-newsgroup))
12026               (let* ((cursor-in-echo-area nil)
12027                      (initial (gnus-parameter-large-newsgroup-initial
12028                                gnus-newsgroup-name))
12029                      (input
12030                       (read-string
12031                        (format
12032                         "How many articles from %s (%s %d): "
12033                         (gnus-limit-string
12034                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
12035                         (if initial "max" "default")
12036                         len)
12037                        (if initial
12038                            (cons (number-to-string initial)
12039                                  0)))))
12040                 (unless (string-match "^[ \t]*$" input)
12041                   (setq all (string-to-number input))
12042                   (if (< all len)
12043                       (let ((older-range (nreverse older)))
12044                         (setq older nil)
12045
12046                         (while (> all 0)
12047                           (let* ((r (pop older-range))
12048                                  (min (if (numberp r) r (car r)))
12049                                  (max (if (numberp r) r (cdr r))))
12050                             (while (and (<= min max)
12051                                         (> all 0))
12052                               (push max older)
12053                               (setq all (1- all)
12054                                     max (1- max))))))))))
12055           (setq older (gnus-uncompress-range older))))
12056         (if (not older)
12057             (message "No old news.")
12058           (gnus-summary-insert-articles older)
12059           (gnus-summary-limit (gnus-sorted-nunion old older))))
12060     (gnus-summary-position-point)))
12061
12062 (defun gnus-summary-insert-new-articles ()
12063   "Insert all new articles in this group."
12064   (interactive)
12065   (prog1
12066       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
12067             (old-active gnus-newsgroup-active)
12068             (nnmail-fetched-sources (list t))
12069             i new)
12070         (setq gnus-newsgroup-active
12071               (gnus-activate-group gnus-newsgroup-name 'scan))
12072         (setq i (cdr gnus-newsgroup-active))
12073         (while (> i (cdr old-active))
12074           (push i new)
12075           (decf i))
12076         (if (not new)
12077             (message "No gnus is bad news")
12078           (gnus-summary-insert-articles new)
12079           (setq gnus-newsgroup-unreads
12080                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12081           (gnus-summary-limit (gnus-sorted-nunion old new))))
12082     (gnus-summary-position-point)))
12083
12084 (gnus-summary-make-all-marking-commands)
12085
12086 (gnus-ems-redefine)
12087
12088 (provide 'gnus-sum)
12089
12090 (run-hooks 'gnus-sum-load-hook)
12091
12092 ;; Local Variables:
12093 ;; coding: iso-8859-1
12094 ;; End:
12095
12096 ;;; gnus-sum.el ends here