9afefab19c60df51134fcf9f42d26d8cc49af10e
[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 non-nil, 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 some)
88                  number
89                  (sexp :menu-tag "other" t)))
90
91 (defcustom gnus-refer-thread-limit 200
92   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
93 If t, fetch all the available old headers."
94   :group 'gnus-thread
95   :type '(choice number
96                  (sexp :menu-tag "other" t)))
97
98 (defcustom gnus-summary-make-false-root 'adopt
99   "*nil means that Gnus won't gather loose threads.
100 If the root of a thread has expired or been read in a previous
101 session, the information necessary to build a complete thread has been
102 lost.  Instead of having many small sub-threads from this original thread
103 scattered all over the summary buffer, Gnus can gather them.
104
105 If non-nil, Gnus will try to gather all loose sub-threads from an
106 original thread into one large thread.
107
108 If this variable is non-nil, it should be one of `none', `adopt',
109 `dummy' or `empty'.
110
111 If this variable is `none', Gnus will not make a false root, but just
112 present the sub-threads after another.
113 If this variable is `dummy', Gnus will create a dummy root that will
114 have all the sub-threads as children.
115 If this variable is `adopt', Gnus will make one of the \"children\"
116 the parent and mark all the step-children as such.
117 If this variable is `empty', the \"children\" are printed with empty
118 subject fields.  (Or rather, they will be printed with a string
119 given by the `gnus-summary-same-subject' variable.)"
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const none)
123                  (const dummy)
124                  (const adopt)
125                  (const empty)))
126
127 (defcustom gnus-summary-make-false-root-always nil
128   "Always make a false dummy root."
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :group 'gnus-score-default
230   :type 'integer)
231
232 (defcustom gnus-summary-default-low-score 0
233   "*Default threshold for a low scored article.
234 An article will be highlighted as low scored if its score is smaller
235 than this score."
236   :group 'gnus-score-default
237   :type 'integer)
238
239 (defcustom gnus-summary-zcore-fuzz 0
240   "*Fuzziness factor for the zcore in the summary buffer.
241 Articles with scores closer than this to `gnus-summary-default-score'
242 will not be marked."
243   :group 'gnus-summary-format
244   :type 'integer)
245
246 (defcustom gnus-simplify-subject-fuzzy-regexp nil
247   "*Strings to be removed when doing fuzzy matches.
248 This can either be a regular expression or list of regular expressions
249 that will be removed from subject strings if fuzzy subject
250 simplification is selected."
251   :group 'gnus-thread
252   :type '(repeat regexp))
253
254 (defcustom gnus-show-threads t
255   "*If non-nil, display threads in summary mode."
256   :group 'gnus-thread
257   :type 'boolean)
258
259 (defcustom gnus-thread-hide-subtree nil
260   "*If non-nil, hide all threads initially.
261 This can be a predicate specifier which says which threads to hide.
262 If threads are hidden, you have to run the command
263 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
264 to expose hidden threads."
265   :group 'gnus-thread
266   :type '(radio (sexp :format "Non-nil\n"
267                       :match (lambda (widget value)
268                                (not (or (consp value) (functionp value))))
269                       :value t)
270                 (const nil)
271                 (sexp :tag "Predicate specifier" :size 0)))
272
273 (defcustom gnus-thread-hide-killed t
274   "*If non-nil, hide killed threads automatically."
275   :group 'gnus-thread
276   :type 'boolean)
277
278 (defcustom gnus-thread-ignore-subject t
279   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
280 If nil, articles that have different subjects from their parents will
281 start separate threads."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-operation-ignore-subject t
286   "*If non-nil, subjects will be ignored when doing thread commands.
287 This affects commands like `gnus-summary-kill-thread' and
288 `gnus-summary-lower-thread'.
289
290 If this variable is nil, articles in the same thread with different
291 subjects will not be included in the operation in question.  If this
292 variable is `fuzzy', only articles that have subjects that are fuzzily
293 equal will be included."
294   :group 'gnus-thread
295   :type '(choice (const :tag "off" nil)
296                  (const fuzzy)
297                  (sexp :tag "on" t)))
298
299 (defcustom gnus-thread-indent-level 4
300   "*Number that says how much each sub-thread should be indented."
301   :group 'gnus-thread
302   :type 'integer)
303
304 (defcustom gnus-auto-extend-newsgroup t
305   "*If non-nil, extend newsgroup forward and backward when requested."
306   :group 'gnus-summary-choose
307   :type 'boolean)
308
309 (defcustom gnus-auto-select-first t
310   "*If non-nil, select the article under point.
311 Which article this is is controlled by the `gnus-auto-select-subject'
312 variable.
313
314 If you want to prevent automatic selection of articles in some
315 newsgroups, set the variable to nil in `gnus-select-group-hook'."
316   :group 'gnus-group-select
317   :type '(choice (const :tag "none" nil)
318                  (sexp :menu-tag "first" t)))
319
320 (defcustom gnus-auto-select-subject 'unread
321   "*Says what subject to place under point when entering a group.
322
323 This variable can either be the symbols `first' (place point on the
324 first subject), `unread' (place point on the subject line of the first
325 unread article), `best' (place point on the subject line of the
326 higest-scored article), `unseen' (place point on the subject line of
327 the first unseen article), 'unseen-or-unread' (place point on the subject
328 line of the first unseen article or, if all article have been seen, on the
329 subject line of the first unread article), or a function to be called to
330 place point on some subject line."
331   :group 'gnus-group-select
332   :type '(choice (const best)
333                  (const unread)
334                  (const first)
335                  (const unseen)
336                  (const unseen-or-unread)))
337
338 (defcustom gnus-dont-select-after-jump-to-other-group nil
339   "If non-nil, don't select the first unread article after entering the
340 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
341 it is depend on the value of `gnus-auto-select-first' whether to select
342 or not."
343   :group 'gnus-group-select
344   :type 'boolean)
345
346 (defcustom gnus-auto-select-next t
347   "*If non-nil, offer to go to the next group from the end of the previous.
348 If the value is t and the next newsgroup is empty, Gnus will exit
349 summary mode and go back to group mode.  If the value is neither nil
350 nor t, Gnus will select the following unread newsgroup.  In
351 particular, if the value is the symbol `quietly', the next unread
352 newsgroup will be selected without any confirmation, and if it is
353 `almost-quietly', the next group will be selected without any
354 confirmation if you are located on the last article in the group.
355 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
356 will go to the next group without confirmation."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "off" nil)
359                  (const quietly)
360                  (const almost-quietly)
361                  (const slightly-quietly)
362                  (sexp :menu-tag "on" t)))
363
364 (defcustom gnus-auto-select-same nil
365   "*If non-nil, select the next article with the same subject.
366 If there are no more articles with the same subject, go to
367 the first unread article."
368   :group 'gnus-summary-maneuvering
369   :type 'boolean)
370
371 (defcustom gnus-auto-goto-ignores 'unfetched
372   "*Says how to handle unfetched articles when maneuvering.
373
374 This variable can either be the symbols nil (maneuver to any
375 article), `undownloaded' (maneuvering while unplugged ignores articles
376 that have not been fetched), `always-undownloaded' (maneuvering always
377 ignores articles that have not been fetched), `unfetched' (maneuvering
378 ignores articles whose headers have not been fetched).
379
380 NOTE: The list of unfetched articles will always be nil when plugged
381 and, when unplugged, a subset of the undownloaded article list."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "None" nil)
384                  (const :tag "Undownloaded when unplugged" undownloaded)
385                  (const :tag "Undownloaded" always-undownloaded)
386                  (const :tag "Unfetched" unfetched)))
387
388 (defcustom gnus-summary-check-current nil
389   "*If non-nil, consider the current article when moving.
390 The \"unread\" movement commands will stay on the same line if the
391 current article is unread."
392   :group 'gnus-summary-maneuvering
393   :type 'boolean)
394
395 (defcustom gnus-auto-center-summary t
396   "*If non-nil, always center the current summary buffer.
397 In particular, if `vertical' do only vertical recentering.  If non-nil
398 and non-`vertical', do both horizontal and vertical recentering."
399   :group 'gnus-summary-maneuvering
400   :type '(choice (const :tag "none" nil)
401                  (const vertical)
402                  (integer :tag "height")
403                  (sexp :menu-tag "both" t)))
404
405 (defvar gnus-auto-center-group t
406   "*If non-nil, always center the group buffer.")
407
408 (defcustom gnus-show-all-headers nil
409   "*If non-nil, don't hide any headers."
410   :group 'gnus-article-hiding
411   :group 'gnus-article-headers
412   :type 'boolean)
413
414 (defcustom gnus-summary-ignore-duplicates nil
415   "*If non-nil, ignore articles with identical Message-ID headers."
416   :group 'gnus-summary
417   :type 'boolean)
418
419 (defcustom gnus-single-article-buffer t
420   "*If non-nil, display all articles in the same buffer.
421 If nil, each group will get its own article buffer."
422   :group 'gnus-article-various
423   :type 'boolean)
424
425 (defcustom gnus-break-pages t
426   "*If non-nil, do page breaking on articles.
427 The page delimiter is specified by the `gnus-page-delimiter'
428 variable."
429   :group 'gnus-article-various
430   :type 'boolean)
431
432 (defcustom gnus-show-mime t
433   "*If non-nil, do mime processing of articles.
434 The articles will simply be fed to the function given by
435 `gnus-article-display-method-for-mime'."
436   :group 'gnus-article-mime
437   :type 'boolean)
438
439 (defcustom gnus-move-split-methods nil
440   "*Variable used to suggest where articles are to be moved to.
441 It uses the same syntax as the `gnus-split-methods' variable.
442 However, whereas `gnus-split-methods' specifies file names as targets,
443 this variable specifies group names."
444   :group 'gnus-summary-mail
445   :type '(repeat (choice (list :value (fun) function)
446                          (cons :value ("" "") regexp (repeat string))
447                          (sexp :value nil))))
448
449 (defcustom gnus-move-group-prefix-function 'gnus-group-real-prefix
450   "Function used to compute default prefix for article move/copy/etc prompts.
451 The function should take one argument, a group name, and return a
452 string with the suggested prefix."
453   :group 'gnus-summary-mail
454   :type 'function)
455
456 (defcustom gnus-unread-mark ?\ ;;;Whitespace
457   "*Mark used for unread articles."
458   :group 'gnus-summary-marks
459   :type 'character)
460
461 (defcustom gnus-ticked-mark ?!
462   "*Mark used for ticked articles."
463   :group 'gnus-summary-marks
464   :type 'character)
465
466 (defcustom gnus-dormant-mark ??
467   "*Mark used for dormant articles."
468   :group 'gnus-summary-marks
469   :type 'character)
470
471 (defcustom gnus-del-mark ?r
472   "*Mark used for del'd articles."
473   :group 'gnus-summary-marks
474   :type 'character)
475
476 (defcustom gnus-read-mark ?R
477   "*Mark used for read articles."
478   :group 'gnus-summary-marks
479   :type 'character)
480
481 (defcustom gnus-expirable-mark ?E
482   "*Mark used for expirable articles."
483   :group 'gnus-summary-marks
484   :type 'character)
485
486 (defcustom gnus-killed-mark ?K
487   "*Mark used for killed articles."
488   :group 'gnus-summary-marks
489   :type 'character)
490
491 (defcustom gnus-spam-mark ?$
492   "*Mark used for spam articles."
493   :group 'gnus-summary-marks
494   :type 'character)
495
496 (defcustom gnus-souped-mark ?F
497   "*Mark used for souped articles."
498   :group 'gnus-summary-marks
499   :type 'character)
500
501 (defcustom gnus-kill-file-mark ?X
502   "*Mark used for articles killed by kill files."
503   :group 'gnus-summary-marks
504   :type 'character)
505
506 (defcustom gnus-low-score-mark ?Y
507   "*Mark used for articles with a low score."
508   :group 'gnus-summary-marks
509   :type 'character)
510
511 (defcustom gnus-catchup-mark ?C
512   "*Mark used for articles that are caught up."
513   :group 'gnus-summary-marks
514   :type 'character)
515
516 (defcustom gnus-replied-mark ?A
517   "*Mark used for articles that have been replied to."
518   :group 'gnus-summary-marks
519   :type 'character)
520
521 (defcustom gnus-forwarded-mark ?F
522   "*Mark used for articles that have been forwarded."
523   :group 'gnus-summary-marks
524   :type 'character)
525
526 (defcustom gnus-recent-mark ?N
527   "*Mark used for articles that are recent."
528   :group 'gnus-summary-marks
529   :type 'character)
530
531 (defcustom gnus-cached-mark ?*
532   "*Mark used for articles that are in the cache."
533   :group 'gnus-summary-marks
534   :type 'character)
535
536 (defcustom gnus-saved-mark ?S
537   "*Mark used for articles that have been saved."
538   :group 'gnus-summary-marks
539   :type 'character)
540
541 (defcustom gnus-unseen-mark ?.
542   "*Mark used for articles that haven't been seen."
543   :group 'gnus-summary-marks
544   :type 'character)
545
546 (defcustom gnus-no-mark ?\ ;;;Whitespace
547   "*Mark used for articles that have no other secondary mark."
548   :group 'gnus-summary-marks
549   :type 'character)
550
551 (defcustom gnus-ancient-mark ?O
552   "*Mark used for ancient articles."
553   :group 'gnus-summary-marks
554   :type 'character)
555
556 (defcustom gnus-sparse-mark ?Q
557   "*Mark used for sparsely reffed articles."
558   :group 'gnus-summary-marks
559   :type 'character)
560
561 (defcustom gnus-canceled-mark ?G
562   "*Mark used for canceled articles."
563   :group 'gnus-summary-marks
564   :type 'character)
565
566 (defcustom gnus-duplicate-mark ?M
567   "*Mark used for duplicate articles."
568   :group 'gnus-summary-marks
569   :type 'character)
570
571 (defcustom gnus-undownloaded-mark ?-
572   "*Mark used for articles that weren't downloaded."
573   :group 'gnus-summary-marks
574   :type 'character)
575
576 (defcustom gnus-downloaded-mark ?+
577   "*Mark used for articles that were downloaded."
578   :group 'gnus-summary-marks
579   :type 'character)
580
581 (defcustom gnus-downloadable-mark ?%
582   "*Mark used for articles that are to be downloaded."
583   :group 'gnus-summary-marks
584   :type 'character)
585
586 (defcustom gnus-unsendable-mark ?=
587   "*Mark used for articles that won't be sent."
588   :group 'gnus-summary-marks
589   :type 'character)
590
591 (defcustom gnus-score-over-mark ?+
592   "*Score mark used for articles with high scores."
593   :group 'gnus-summary-marks
594   :type 'character)
595
596 (defcustom gnus-score-below-mark ?-
597   "*Score mark used for articles with low scores."
598   :group 'gnus-summary-marks
599   :type 'character)
600
601 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
602   "*There is no thread under the article."
603   :group 'gnus-summary-marks
604   :type 'character)
605
606 (defcustom gnus-not-empty-thread-mark ?=
607   "*There is a thread under the article."
608   :group 'gnus-summary-marks
609   :type 'character)
610
611 (defcustom gnus-view-pseudo-asynchronously nil
612   "*If non-nil, Gnus will view pseudo-articles asynchronously."
613   :group 'gnus-extract-view
614   :type 'boolean)
615
616 (defcustom gnus-auto-expirable-marks
617   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
618         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
619         gnus-souped-mark gnus-duplicate-mark)
620   "*The list of marks converted into expiration if a group is auto-expirable."
621   :version "21.1"
622   :group 'gnus-summary
623   :type '(repeat character))
624
625 (defcustom gnus-inhibit-user-auto-expire t
626   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
627   :version "21.1"
628   :group 'gnus-summary
629   :type 'boolean)
630
631 (defcustom gnus-view-pseudos nil
632   "*If `automatic', pseudo-articles will be viewed automatically.
633 If `not-confirm', pseudos will be viewed automatically, and the user
634 will not be asked to confirm the command."
635   :group 'gnus-extract-view
636   :type '(choice (const :tag "off" nil)
637                  (const automatic)
638                  (const not-confirm)))
639
640 (defcustom gnus-view-pseudos-separately t
641   "*If non-nil, one pseudo-article will be created for each file to be viewed.
642 If nil, all files that use the same viewing command will be given as a
643 list of parameters to that command."
644   :group 'gnus-extract-view
645   :type 'boolean)
646
647 (defcustom gnus-insert-pseudo-articles t
648   "*If non-nil, insert pseudo-articles when decoding articles."
649   :group 'gnus-extract-view
650   :type 'boolean)
651
652 (defcustom gnus-summary-dummy-line-format
653   "   %(:                             :%) %S\n"
654   "*The format specification for the dummy roots in the summary buffer.
655 It works along the same lines as a normal formatting string,
656 with some simple extensions.
657
658 %S  The subject
659
660 General format specifiers can also be used.
661 See `(gnus)Formatting Variables'."
662   :link '(custom-manual "(gnus)Formatting Variables")
663   :group 'gnus-threading
664   :type 'string)
665
666 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
667   "*The format specification for the summary mode line.
668 It works along the same lines as a normal formatting string,
669 with some simple extensions:
670
671 %G  Group name
672 %p  Unprefixed group name
673 %A  Current article number
674 %z  Current article score
675 %V  Gnus version
676 %U  Number of unread articles in the group
677 %e  Number of unselected articles in the group
678 %Z  A string with unread/unselected article counts
679 %g  Shortish group name
680 %S  Subject of the current article
681 %u  User-defined spec
682 %s  Current score file name
683 %d  Number of dormant articles
684 %r  Number of articles that have been marked as read in this session
685 %E  Number of articles expunged by the score files"
686   :group 'gnus-summary-format
687   :type 'string)
688
689 (defcustom gnus-list-identifiers nil
690   "Regexp that matches list identifiers to be removed from subject.
691 This can also be a list of regexps."
692   :version "21.1"
693   :group 'gnus-summary-format
694   :group 'gnus-article-hiding
695   :type '(choice (const :tag "none" nil)
696                  (regexp :value ".*")
697                  (repeat :value (".*") regexp)))
698
699 (defcustom gnus-summary-mark-below 0
700   "*Mark all articles with a score below this variable as read.
701 This variable is local to each summary buffer and usually set by the
702 score file."
703   :group 'gnus-score-default
704   :type 'integer)
705
706 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
707   "*List of functions used for sorting articles in the summary buffer.
708
709 Each function takes two articles and returns non-nil if the first
710 article should be sorted before the other.  If you use more than one
711 function, the primary sort function should be the last.  You should
712 probably always include `gnus-article-sort-by-number' in the list of
713 sorting functions -- preferably first.  Also note that sorting by date
714 is often much slower than sorting by number, and the sorting order is
715 very similar.  (Sorting by date means sorting by the time the message
716 was sent, sorting by number means sorting by arrival time.)
717
718 Ready-made functions include `gnus-article-sort-by-number',
719 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
720 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
721 and `gnus-article-sort-by-score'.
722
723 When threading is turned on, the variable `gnus-thread-sort-functions'
724 controls how articles are sorted."
725   :group 'gnus-summary-sort
726   :type '(repeat (choice (function-item gnus-article-sort-by-number)
727                          (function-item gnus-article-sort-by-author)
728                          (function-item gnus-article-sort-by-subject)
729                          (function-item gnus-article-sort-by-date)
730                          (function-item gnus-article-sort-by-score)
731                          (function-item gnus-article-sort-by-random)
732                          (function :tag "other"))))
733
734 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
735   "*List of functions used for sorting threads in the summary buffer.
736 By default, threads are sorted by article number.
737
738 Each function takes two threads and returns non-nil if the first
739 thread should be sorted before the other.  If you use more than one
740 function, the primary sort function should be the last.  You should
741 probably always include `gnus-thread-sort-by-number' in the list of
742 sorting functions -- preferably first.  Also note that sorting by date
743 is often much slower than sorting by number, and the sorting order is
744 very similar.  (Sorting by date means sorting by the time the message
745 was sent, sorting by number means sorting by arrival time.)
746
747 Ready-made functions include `gnus-thread-sort-by-number',
748 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
749 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
750 `gnus-thread-sort-by-most-recent-number',
751 `gnus-thread-sort-by-most-recent-date',
752 `gnus-thread-sort-by-random', and
753 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
754
755 When threading is turned off, the variable
756 `gnus-article-sort-functions' controls how articles are sorted."
757   :group 'gnus-summary-sort
758   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
759                          (function-item gnus-thread-sort-by-author)
760                          (function-item gnus-thread-sort-by-subject)
761                          (function-item gnus-thread-sort-by-date)
762                          (function-item gnus-thread-sort-by-score)
763                          (function-item gnus-thread-sort-by-total-score)
764                          (function-item gnus-thread-sort-by-random)
765                          (function :tag "other"))))
766
767 (defcustom gnus-thread-score-function '+
768   "*Function used for calculating the total score of a thread.
769
770 The function is called with the scores of the article and each
771 subthread and should then return the score of the thread.
772
773 Some functions you can use are `+', `max', or `min'."
774   :group 'gnus-summary-sort
775   :type 'function)
776
777 (defcustom gnus-summary-expunge-below nil
778   "All articles that have a score less than this variable will be expunged.
779 This variable is local to the summary buffers."
780   :group 'gnus-score-default
781   :type '(choice (const :tag "off" nil)
782                  integer))
783
784 (defcustom gnus-thread-expunge-below nil
785   "All threads that have a total score less than this variable will be expunged.
786 See `gnus-thread-score-function' for en explanation of what a
787 \"thread score\" is.
788
789 This variable is local to the summary buffers."
790   :group 'gnus-threading
791   :group 'gnus-score-default
792   :type '(choice (const :tag "off" nil)
793                  integer))
794
795 (defcustom gnus-summary-mode-hook nil
796   "*A hook for Gnus summary mode.
797 This hook is run before any variables are set in the summary buffer."
798   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
799   :group 'gnus-summary-various
800   :type 'hook)
801
802 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
803 (when (featurep 'xemacs)
804   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
805   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
806   (add-hook 'gnus-summary-mode-hook
807             'gnus-xmas-switch-horizontal-scrollbar-off))
808
809 (defcustom gnus-summary-menu-hook nil
810   "*Hook run after the creation of the summary mode menu."
811   :group 'gnus-summary-visual
812   :type 'hook)
813
814 (defcustom gnus-summary-exit-hook nil
815   "*A hook called on exit from the summary buffer.
816 It will be called with point in the group buffer."
817   :group 'gnus-summary-exit
818   :type 'hook)
819
820 (defcustom gnus-summary-prepare-hook nil
821   "*A hook called after the summary buffer has been generated.
822 If you want to modify the summary buffer, you can use this hook."
823   :group 'gnus-summary-various
824   :type 'hook)
825
826 (defcustom gnus-summary-prepared-hook nil
827   "*A hook called as the last thing after the summary buffer has been generated."
828   :group 'gnus-summary-various
829   :type 'hook)
830
831 (defcustom gnus-summary-generate-hook nil
832   "*A hook run just before generating the summary buffer.
833 This hook is commonly used to customize threading variables and the
834 like."
835   :group 'gnus-summary-various
836   :type 'hook)
837
838 (defcustom gnus-select-group-hook nil
839   "*A hook called when a newsgroup is selected.
840
841 If you'd like to simplify subjects like the
842 `gnus-summary-next-same-subject' command does, you can use the
843 following hook:
844
845  (add-hook gnus-select-group-hook
846            (lambda ()
847              (mapcar (lambda (header)
848                        (mail-header-set-subject
849                         header
850                         (gnus-simplify-subject
851                          (mail-header-subject header) 're-only)))
852                      gnus-newsgroup-headers)))"
853   :group 'gnus-group-select
854   :type 'hook)
855
856 (defcustom gnus-select-article-hook nil
857   "*A hook called when an article is selected."
858   :group 'gnus-summary-choose
859   :options '(gnus-agent-fetch-selected-article)
860   :type 'hook)
861
862 (defcustom gnus-visual-mark-article-hook
863   (list 'gnus-highlight-selected-summary)
864   "*Hook run after selecting an article in the summary buffer.
865 It is meant to be used for highlighting the article in some way.  It
866 is not run if `gnus-visual' is nil."
867   :group 'gnus-summary-visual
868   :type 'hook)
869
870 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
871   "*A hook called before parsing the headers."
872   :group 'gnus-various
873   :type 'hook)
874
875 (defcustom gnus-exit-group-hook nil
876   "*A hook called when exiting summary mode.
877 This hook is not called from the non-updating exit commands like `Q'."
878   :group 'gnus-various
879   :type 'hook)
880
881 (defcustom gnus-summary-update-hook
882   (list 'gnus-summary-highlight-line)
883   "*A hook called when a summary line is changed.
884 The hook will not be called if `gnus-visual' is nil.
885
886 The default function `gnus-summary-highlight-line' will
887 highlight the line according to the `gnus-summary-highlight'
888 variable."
889   :group 'gnus-summary-visual
890   :type 'hook)
891
892 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
893   "*A hook called when an article is selected for the first time.
894 The hook is intended to mark an article as read (or unread)
895 automatically when it is selected."
896   :group 'gnus-summary-choose
897   :type 'hook)
898
899 (defcustom gnus-group-no-more-groups-hook nil
900   "*A hook run when returning to group mode having no more (unread) groups."
901   :group 'gnus-group-select
902   :type 'hook)
903
904 (defcustom gnus-ps-print-hook nil
905   "*A hook run before ps-printing something from Gnus."
906   :group 'gnus-summary
907   :type 'hook)
908
909 (defcustom gnus-summary-article-move-hook nil
910   "*A hook called after an article is moved, copied, respooled, or crossposted."
911   :group 'gnus-summary
912   :type 'hook)
913
914 (defcustom gnus-summary-article-delete-hook nil
915   "*A hook called after an article is deleted."
916   :group 'gnus-summary
917   :type 'hook)
918
919 (defcustom gnus-summary-article-expire-hook nil
920   "*A hook called after an article is expired."
921   :group 'gnus-summary
922   :type 'hook)
923
924 (defcustom gnus-summary-display-arrow
925   (and (fboundp 'display-graphic-p)
926        (display-graphic-p))
927   "*If non-nil, display an arrow highlighting the current article."
928   :version "21.1"
929   :group 'gnus-summary
930   :type 'boolean)
931
932 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
933   "Face used for highlighting the current article in the summary buffer."
934   :group 'gnus-summary-visual
935   :type 'face)
936
937 (defvar gnus-tmp-downloaded nil)
938
939 (defcustom gnus-summary-highlight
940   '(((eq mark gnus-canceled-mark)
941      . gnus-summary-cancelled-face)
942     ((and uncached (> score default-high))
943      . gnus-summary-high-undownloaded-face)
944     ((and uncached (< score default-low))
945      . gnus-summary-low-undownloaded-face)
946     (uncached
947      . gnus-summary-normal-undownloaded-face)
948     ((and (> score default-high)
949           (or (eq mark gnus-dormant-mark)
950               (eq mark gnus-ticked-mark)))
951      . gnus-summary-high-ticked-face)
952     ((and (< score default-low)
953           (or (eq mark gnus-dormant-mark)
954               (eq mark gnus-ticked-mark)))
955      . gnus-summary-low-ticked-face)
956     ((or (eq mark gnus-dormant-mark)
957          (eq mark gnus-ticked-mark))
958      . gnus-summary-normal-ticked-face)
959     ((and (> score default-high) (eq mark gnus-ancient-mark))
960      . gnus-summary-high-ancient-face)
961     ((and (< score default-low) (eq mark gnus-ancient-mark))
962      . gnus-summary-low-ancient-face)
963     ((eq mark gnus-ancient-mark)
964      . gnus-summary-normal-ancient-face)
965     ((and (> score default-high) (eq mark gnus-unread-mark))
966      . gnus-summary-high-unread-face)
967     ((and (< score default-low) (eq mark gnus-unread-mark))
968      . gnus-summary-low-unread-face)
969     ((eq mark gnus-unread-mark)
970      . gnus-summary-normal-unread-face)
971     ((> score default-high)
972      . gnus-summary-high-read-face)
973     ((< score default-low)
974      . gnus-summary-low-read-face)
975     (t
976      . gnus-summary-normal-read-face))
977   "*Controls the highlighting of summary buffer lines.
978
979 A list of (FORM . FACE) pairs.  When deciding how a a particular
980 summary line should be displayed, each form is evaluated.  The content
981 of the face field after the first true form is used.  You can change
982 how those summary lines are displayed, by editing the face field.
983
984 You can use the following variables in the FORM field.
985
986 score:        The article's score
987 default:      The default article score.
988 default-high: The default score for high scored articles.
989 default-low:  The default score for low scored articles.
990 below:        The score below which articles are automatically marked as read.
991 mark:         The article's mark.
992 uncached:     Non-nil if the article is uncached."
993   :group 'gnus-summary-visual
994   :type '(repeat (cons (sexp :tag "Form" nil)
995                        face)))
996
997 (defcustom gnus-alter-header-function nil
998   "Function called to allow alteration of article header structures.
999 The function is called with one parameter, the article header vector,
1000 which it may alter in any way."
1001   :type '(choice (const :tag "None" nil)
1002                  function)
1003   :group 'gnus-summary)
1004
1005 (defvar gnus-decode-encoded-word-function
1006   (mime-find-field-decoder 'From 'nov)
1007   "Variable that says which function should be used to decode a string with encoded words.")
1008
1009 (defcustom gnus-extra-headers '(To Newsgroups)
1010   "*Extra headers to parse."
1011   :version "21.1"
1012   :group 'gnus-summary
1013   :type '(repeat symbol))
1014
1015 (defcustom gnus-ignored-from-addresses
1016   (and user-mail-address (regexp-quote user-mail-address))
1017   "*Regexp of From headers that may be suppressed in favor of To headers."
1018   :version "21.1"
1019   :group 'gnus-summary
1020   :type 'regexp)
1021
1022 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1023   "List of charsets that should be ignored.
1024 When these charsets are used in the \"charset\" parameter, the
1025 default charset will be used instead."
1026   :version "21.1"
1027   :type '(repeat symbol)
1028   :group 'gnus-charset)
1029
1030 (gnus-define-group-parameter
1031  ignored-charsets
1032  :type list
1033  :function-document
1034  "Return the ignored charsets of GROUP."
1035  :variable gnus-group-ignored-charsets-alist
1036  :variable-default
1037  '(("alt\\.chinese\\.text" iso-8859-1))
1038  :variable-document
1039  "Alist of regexps (to match group names) and charsets that should be ignored.
1040 When these charsets are used in the \"charset\" parameter, the
1041 default charset will be used instead."
1042  :variable-group gnus-charset
1043  :variable-type '(repeat (cons (regexp :tag "Group")
1044                                (repeat symbol)))
1045  :parameter-type '(choice :tag "Ignored charsets"
1046                           :value nil
1047                           (repeat (symbol)))
1048  :parameter-document       "\
1049 List of charsets that should be ignored.
1050
1051 When these charsets are used in the \"charset\" parameter, the
1052 default charset will be used instead.")
1053
1054 (defcustom gnus-group-highlight-words-alist nil
1055   "Alist of group regexps and highlight regexps.
1056 This variable uses the same syntax as `gnus-emphasis-alist'."
1057   :version "21.1"
1058   :type '(repeat (cons (regexp :tag "Group")
1059                        (repeat (list (regexp :tag "Highlight regexp")
1060                                      (number :tag "Group for entire word" 0)
1061                                      (number :tag "Group for displayed part" 0)
1062                                      (symbol :tag "Face"
1063                                              gnus-emphasis-highlight-words)))))
1064   :group 'gnus-summary-visual)
1065
1066 (defcustom gnus-use-wheel nil
1067   "Use Intelli-mouse on summary movement"
1068   :type 'boolean
1069   :group 'gnus-summary-maneuvering)
1070
1071 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1072   "Amount to scroll messages by spinning the mouse wheel.
1073 This is actually a cons cell, where the first item is the amount to scroll
1074 on a normal wheel event, and the second is the amount to scroll when the
1075 wheel is moved with the shift key depressed."
1076   :type '(cons (integer :tag "Shift") integer)
1077   :group 'gnus-summary-maneuvering)
1078
1079 (defcustom gnus-wheel-edge-resistance 2
1080   "How hard it should be to change the current article
1081 by moving the mouse over the edge of the article window."
1082   :type 'integer
1083   :group 'gnus-summary-maneuvering)
1084
1085 (defcustom gnus-summary-show-article-charset-alist
1086   nil
1087   "Alist of number and charset.
1088 The article will be shown with the charset corresponding to the
1089 numbered argument.
1090 For example: ((1 . cn-gb-2312) (2 . big5))."
1091   :version "21.1"
1092   :type '(repeat (cons (number :tag "Argument" 1)
1093                        (symbol :tag "Charset")))
1094   :group 'gnus-charset)
1095
1096 (defcustom gnus-preserve-marks t
1097   "Whether marks are preserved when moving, copying and respooling messages."
1098   :version "21.1"
1099   :type 'boolean
1100   :group 'gnus-summary-marks)
1101
1102 (defcustom gnus-alter-articles-to-read-function nil
1103   "Function to be called to alter the list of articles to be selected."
1104   :type '(choice (const nil) function)
1105   :group 'gnus-summary)
1106
1107 (defcustom gnus-orphan-score nil
1108   "*All orphans get this score added.  Set in the score file."
1109   :group 'gnus-score-default
1110   :type '(choice (const nil)
1111                  integer))
1112
1113 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1114   "*A regexp to match MIME parts when saving multiple parts of a
1115 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1116 This regexp will be used by default when prompting the user for which
1117 type of files to save."
1118   :group 'gnus-summary
1119   :type 'regexp)
1120
1121 (defcustom gnus-read-all-available-headers nil
1122   "Whether Gnus should parse all headers made available to it.
1123 This is mostly relevant for slow back ends where the user may
1124 wish to widen the summary buffer to include all headers
1125 that were fetched.  Say, for nnultimate groups."
1126   :group 'gnus-summary
1127   :type '(choice boolean regexp))
1128
1129 (defcustom gnus-summary-muttprint-program "muttprint"
1130   "Command (and optional arguments) used to run Muttprint."
1131   :version "21.3"
1132   :group 'gnus-summary
1133   :type 'string)
1134
1135 (defcustom gnus-article-loose-mime nil
1136   "If non-nil, don't require MIME-Version header.
1137 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1138 supply the MIME-Version header or deliberately strip it From the mail.
1139 Set it to non-nil, Gnus will treat some articles as MIME even if
1140 the MIME-Version header is missed."
1141   :version "21.3"
1142   :type 'boolean
1143   :group 'gnus-article-mime)
1144
1145 (defcustom gnus-article-emulate-mime t
1146   "If non-nil, use MIME emulation for uuencode and the like.
1147 This means that Gnus will search message bodies for text that look
1148 like uuencoded bits, yEncoded bits, and so on, and present that using
1149 the normal Gnus MIME machinery."
1150   :type 'boolean
1151   :group 'gnus-article-mime)
1152
1153 ;;; Internal variables
1154
1155 (defvar gnus-summary-display-cache nil)
1156 (defvar gnus-article-mime-handles nil)
1157 (defvar gnus-article-decoded-p nil)
1158 (defvar gnus-article-charset nil)
1159 (defvar gnus-article-ignored-charsets nil)
1160 (defvar gnus-scores-exclude-files nil)
1161 (defvar gnus-page-broken nil)
1162
1163 (defvar gnus-original-article nil)
1164 (defvar gnus-article-internal-prepare-hook nil)
1165 (defvar gnus-newsgroup-process-stack nil)
1166
1167 (defvar gnus-thread-indent-array nil)
1168 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1169 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1170   "Function called to sort the articles within a thread after it has been gathered together.")
1171
1172 (defvar gnus-summary-save-parts-type-history nil)
1173 (defvar gnus-summary-save-parts-last-directory nil)
1174
1175 ;; Avoid highlighting in kill files.
1176 (defvar gnus-summary-inhibit-highlight nil)
1177 (defvar gnus-newsgroup-selected-overlay nil)
1178 (defvar gnus-inhibit-limiting nil)
1179 (defvar gnus-newsgroup-adaptive-score-file nil)
1180 (defvar gnus-current-score-file nil)
1181 (defvar gnus-current-move-group nil)
1182 (defvar gnus-current-copy-group nil)
1183 (defvar gnus-current-crosspost-group nil)
1184 (defvar gnus-newsgroup-display nil)
1185
1186 (defvar gnus-newsgroup-dependencies nil)
1187 (defvar gnus-newsgroup-adaptive nil)
1188 (defvar gnus-summary-display-article-function nil)
1189 (defvar gnus-summary-highlight-line-function nil
1190   "Function called after highlighting a summary line.")
1191
1192 (defvar gnus-summary-line-format-alist
1193   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1194     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1195     (?s gnus-tmp-subject-or-nil ?s)
1196     (?n gnus-tmp-name ?s)
1197     (?A (std11-address-string
1198          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1199     (?a (or (std11-full-name-string
1200              (car (mime-entity-read-field gnus-tmp-header 'From)))
1201             gnus-tmp-from) ?s)
1202     (?F gnus-tmp-from ?s)
1203     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1204     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1205     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1206     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1207     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1208     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1209     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1210     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1211     (?L gnus-tmp-lines ?s)
1212     (?O gnus-tmp-downloaded ?c)
1213     (?I gnus-tmp-indentation ?s)
1214     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1215     (?R gnus-tmp-replied ?c)
1216     (?\[ gnus-tmp-opening-bracket ?c)
1217     (?\] gnus-tmp-closing-bracket ?c)
1218     (?\> (make-string gnus-tmp-level ? ) ?s)
1219     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1220     (?i gnus-tmp-score ?d)
1221     (?z gnus-tmp-score-char ?c)
1222     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1223     (?U gnus-tmp-unread ?c)
1224     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1225         ?s)
1226     (?t (gnus-summary-number-of-articles-in-thread
1227          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1228         ?d)
1229     (?e (gnus-summary-number-of-articles-in-thread
1230          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1231         ?c)
1232     (?u gnus-tmp-user-defined ?s)
1233     (?P (gnus-pick-line-number) ?d)
1234     (?B gnus-tmp-thread-tree-header-string ?s)
1235     (user-date (gnus-user-date
1236                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1237   "An alist of format specifications that can appear in summary lines.
1238 These are paired with what variables they correspond with, along with
1239 the type of the variable (string, integer, character, etc).")
1240
1241 (defvar gnus-summary-dummy-line-format-alist
1242   `((?S gnus-tmp-subject ?s)
1243     (?N gnus-tmp-number ?d)
1244     (?u gnus-tmp-user-defined ?s)))
1245
1246 (defvar gnus-summary-mode-line-format-alist
1247   `((?G gnus-tmp-group-name ?s)
1248     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1249     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1250     (?A gnus-tmp-article-number ?d)
1251     (?Z gnus-tmp-unread-and-unselected ?s)
1252     (?V gnus-version ?s)
1253     (?U gnus-tmp-unread-and-unticked ?d)
1254     (?S gnus-tmp-subject ?s)
1255     (?e gnus-tmp-unselected ?d)
1256     (?u gnus-tmp-user-defined ?s)
1257     (?d (length gnus-newsgroup-dormant) ?d)
1258     (?t (length gnus-newsgroup-marked) ?d)
1259     (?h (length gnus-newsgroup-spam-marked) ?d)
1260     (?r (length gnus-newsgroup-reads) ?d)
1261     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1262     (?E gnus-newsgroup-expunged-tally ?d)
1263     (?s (gnus-current-score-file-nondirectory) ?s)))
1264
1265 (defvar gnus-last-search-regexp nil
1266   "Default regexp for article search command.")
1267
1268 (defvar gnus-summary-search-article-matched-data nil
1269   "Last matched data of article search command.  It is the local variable
1270 in `gnus-article-buffer' which consists of the list of start position,
1271 end position and text.")
1272
1273 (defvar gnus-last-shell-command nil
1274   "Default shell command on article.")
1275
1276 (defvar gnus-newsgroup-agentized nil
1277   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1278 (defvar gnus-newsgroup-begin nil)
1279 (defvar gnus-newsgroup-end nil)
1280 (defvar gnus-newsgroup-last-rmail nil)
1281 (defvar gnus-newsgroup-last-mail nil)
1282 (defvar gnus-newsgroup-last-folder nil)
1283 (defvar gnus-newsgroup-last-file nil)
1284 (defvar gnus-newsgroup-auto-expire nil)
1285 (defvar gnus-newsgroup-active nil)
1286
1287 (defvar gnus-newsgroup-data nil)
1288 (defvar gnus-newsgroup-data-reverse nil)
1289 (defvar gnus-newsgroup-limit nil)
1290 (defvar gnus-newsgroup-limits nil)
1291 (defvar gnus-summary-use-undownloaded-faces nil)
1292
1293 (defvar gnus-newsgroup-unreads nil
1294   "Sorted list of unread articles in the current newsgroup.")
1295
1296 (defvar gnus-newsgroup-unselected nil
1297   "Sorted list of unselected unread articles in the current newsgroup.")
1298
1299 (defvar gnus-newsgroup-reads nil
1300   "Alist of read articles and article marks in the current newsgroup.")
1301
1302 (defvar gnus-newsgroup-expunged-tally nil)
1303
1304 (defvar gnus-newsgroup-marked nil
1305   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1306
1307 (defvar gnus-newsgroup-spam-marked nil
1308   "List of ranges of articles that have been marked as spam.")
1309
1310 (defvar gnus-newsgroup-killed nil
1311   "List of ranges of articles that have been through the scoring process.")
1312
1313 (defvar gnus-newsgroup-cached nil
1314   "Sorted list of articles that come from the article cache.")
1315
1316 (defvar gnus-newsgroup-saved nil
1317   "List of articles that have been saved.")
1318
1319 (defvar gnus-newsgroup-kill-headers nil)
1320
1321 (defvar gnus-newsgroup-replied nil
1322   "List of articles that have been replied to in the current newsgroup.")
1323
1324 (defvar gnus-newsgroup-forwarded nil
1325   "List of articles that have been forwarded in the current newsgroup.")
1326
1327 (defvar gnus-newsgroup-recent nil
1328   "List of articles that have are recent in the current newsgroup.")
1329
1330 (defvar gnus-newsgroup-expirable nil
1331   "Sorted list of articles in the current newsgroup that can be expired.")
1332
1333 (defvar gnus-newsgroup-processable nil
1334   "List of articles in the current newsgroup that can be processed.")
1335
1336 (defvar gnus-newsgroup-downloadable nil
1337   "Sorted list of articles in the current newsgroup that can be processed.")
1338
1339 (defvar gnus-newsgroup-unfetched nil
1340   "Sorted list of articles in the current newsgroup whose headers have
1341 not been fetched into the agent.
1342
1343 This list will always be a subset of gnus-newsgroup-undownloaded.")
1344
1345 (defvar gnus-newsgroup-undownloaded nil
1346   "List of articles in the current newsgroup that haven't been downloaded.")
1347
1348 (defvar gnus-newsgroup-unsendable nil
1349   "List of articles in the current newsgroup that won't be sent.")
1350
1351 (defvar gnus-newsgroup-bookmarks nil
1352   "List of articles in the current newsgroup that have bookmarks.")
1353
1354 (defvar gnus-newsgroup-dormant nil
1355   "Sorted list of dormant articles in the current newsgroup.")
1356
1357 (defvar gnus-newsgroup-unseen nil
1358   "List of unseen articles in the current newsgroup.")
1359
1360 (defvar gnus-newsgroup-seen nil
1361   "Range of seen articles in the current newsgroup.")
1362
1363 (defvar gnus-newsgroup-articles nil
1364   "List of articles in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-scored nil
1367   "List of scored articles in the current newsgroup.")
1368
1369 (defvar gnus-newsgroup-incorporated nil
1370   "List of incorporated articles in the current newsgroup.")
1371
1372 (defvar gnus-newsgroup-headers nil
1373   "List of article headers in the current newsgroup.")
1374
1375 (defvar gnus-newsgroup-threads nil)
1376
1377 (defvar gnus-newsgroup-prepared nil
1378   "Whether the current group has been prepared properly.")
1379
1380 (defvar gnus-newsgroup-ancient nil
1381   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1382
1383 (defvar gnus-newsgroup-sparse nil)
1384
1385 (defvar gnus-current-article nil)
1386 (defvar gnus-article-current nil)
1387 (defvar gnus-current-headers nil)
1388 (defvar gnus-have-all-headers nil)
1389 (defvar gnus-last-article nil)
1390 (defvar gnus-newsgroup-history nil)
1391 (defvar gnus-newsgroup-charset nil)
1392 (defvar gnus-newsgroup-ephemeral-charset nil)
1393 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1394
1395 (defvar gnus-article-before-search nil)
1396
1397 (defvar gnus-summary-local-variables
1398   '(gnus-newsgroup-name
1399     gnus-newsgroup-begin gnus-newsgroup-end
1400     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1401     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1402     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1403     gnus-newsgroup-unselected gnus-newsgroup-marked
1404     gnus-newsgroup-spam-marked
1405     gnus-newsgroup-reads gnus-newsgroup-saved
1406     gnus-newsgroup-replied gnus-newsgroup-forwarded
1407     gnus-newsgroup-recent
1408     gnus-newsgroup-expirable
1409     gnus-newsgroup-processable gnus-newsgroup-killed
1410     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1411     gnus-newsgroup-unfetched
1412     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1413     gnus-newsgroup-seen gnus-newsgroup-articles
1414     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1415     gnus-newsgroup-headers gnus-newsgroup-threads
1416     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1417     gnus-current-article gnus-current-headers gnus-have-all-headers
1418     gnus-last-article gnus-article-internal-prepare-hook
1419     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1420     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1421     gnus-thread-expunge-below
1422     gnus-score-alist gnus-current-score-file
1423     (gnus-summary-expunge-below . global)
1424     (gnus-summary-mark-below . global)
1425     (gnus-orphan-score . global)
1426     gnus-newsgroup-active gnus-scores-exclude-files
1427     gnus-newsgroup-history gnus-newsgroup-ancient
1428     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1429     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1430     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1431     (gnus-newsgroup-expunged-tally . 0)
1432     gnus-cache-removable-articles gnus-newsgroup-cached
1433     gnus-newsgroup-data gnus-newsgroup-data-reverse
1434     gnus-newsgroup-limit gnus-newsgroup-limits
1435     gnus-newsgroup-charset gnus-newsgroup-display
1436     gnus-summary-use-undownloaded-faces
1437     gnus-newsgroup-incorporated)
1438   "Variables that are buffer-local to the summary buffers.")
1439
1440 (defvar gnus-newsgroup-variables nil
1441   "A list of variables that have separate values in different newsgroups.
1442 A list of newsgroup (summary buffer) local variables, or cons of
1443 variables and their default values (when the default values are not
1444 nil), that should be made global while the summary buffer is active.
1445 These variables can be used to set variables in the group parameters
1446 while still allowing them to affect operations done in other
1447 buffers. For example:
1448
1449 \(setq gnus-newsgroup-variables
1450      '(message-use-followup-to
1451        (gnus-visible-headers .
1452          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1453 ")
1454
1455 ;; Byte-compiler warning.
1456 (eval-when-compile
1457   ;; Bind features so that require will believe that gnus-sum has
1458   ;; already been loaded (avoids infinite recursion)
1459   (let ((features (cons 'gnus-sum features)))
1460     ;; Several of the declarations in gnus-sum are needed to load the
1461     ;; following files. Right now, these definitions have been
1462     ;; compiled but not defined (evaluated).  We could either do a
1463     ;; eval-and-compile about all of the declarations or evaluate the
1464     ;; source file.
1465     (if (boundp 'gnus-newsgroup-variables)
1466         nil
1467       (load "gnus-sum.el" t t t))
1468     (require 'gnus)
1469     (require 'gnus-agent)
1470     (require 'gnus-art)))
1471
1472 ;; Subject simplification.
1473
1474 (defun gnus-simplify-whitespace (str)
1475   "Remove excessive whitespace from STR."
1476   ;; Multiple spaces.
1477   (while (string-match "[ \t][ \t]+" str)
1478     (setq str (concat (substring str 0 (match-beginning 0))
1479                         " "
1480                         (substring str (match-end 0)))))
1481   ;; Leading spaces.
1482   (when (string-match "^[ \t]+" str)
1483     (setq str (substring str (match-end 0))))
1484   ;; Trailing spaces.
1485   (when (string-match "[ \t]+$" str)
1486     (setq str (substring str 0 (match-beginning 0))))
1487   str)
1488
1489 (defun gnus-simplify-all-whitespace (str)
1490   "Remove all whitespace from STR."
1491   (while (string-match "[ \t\n]+" str)
1492     (setq str (replace-match "" nil nil str)))
1493   str)
1494
1495 (defsubst gnus-simplify-subject-re (subject)
1496   "Remove \"Re:\" from subject lines."
1497   (if (string-match message-subject-re-regexp subject)
1498       (substring subject (match-end 0))
1499     subject))
1500
1501 (defun gnus-simplify-subject (subject &optional re-only)
1502   "Remove `Re:' and words in parentheses.
1503 If RE-ONLY is non-nil, strip leading `Re:'s only."
1504   (let ((case-fold-search t))           ;Ignore case.
1505     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1506     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1507       (setq subject (substring subject (match-end 0))))
1508     ;; Remove uninteresting prefixes.
1509     (when (and (not re-only)
1510                gnus-simplify-ignored-prefixes
1511                (string-match gnus-simplify-ignored-prefixes subject))
1512       (setq subject (substring subject (match-end 0))))
1513     ;; Remove words in parentheses from end.
1514     (unless re-only
1515       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1516         (setq subject (substring subject 0 (match-beginning 0)))))
1517     ;; Return subject string.
1518     subject))
1519
1520 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1521 ;; all whitespace.
1522 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1523   (goto-char (point-min))
1524   (while (re-search-forward regexp nil t)
1525     (replace-match (or newtext ""))))
1526
1527 (defun gnus-simplify-buffer-fuzzy ()
1528   "Simplify string in the buffer fuzzily.
1529 The string in the accessible portion of the current buffer is simplified.
1530 It is assumed to be a single-line subject.
1531 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1532 matter is removed.  Additional things can be deleted by setting
1533 `gnus-simplify-subject-fuzzy-regexp'."
1534   (let ((case-fold-search t)
1535         (modified-tick))
1536     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1537
1538     (while (not (eq modified-tick (buffer-modified-tick)))
1539       (setq modified-tick (buffer-modified-tick))
1540       (cond
1541        ((listp gnus-simplify-subject-fuzzy-regexp)
1542         (mapcar 'gnus-simplify-buffer-fuzzy-step
1543                 gnus-simplify-subject-fuzzy-regexp))
1544        (gnus-simplify-subject-fuzzy-regexp
1545         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1546       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1547       (gnus-simplify-buffer-fuzzy-step
1548        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1549       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1550
1551     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1552     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1553     (gnus-simplify-buffer-fuzzy-step " $")
1554     (gnus-simplify-buffer-fuzzy-step "^ +")))
1555
1556 (defun gnus-simplify-subject-fuzzy (subject)
1557   "Simplify a subject string fuzzily.
1558 See `gnus-simplify-buffer-fuzzy' for details."
1559   (save-excursion
1560     (gnus-set-work-buffer)
1561     (let ((case-fold-search t))
1562       ;; Remove uninteresting prefixes.
1563       (when (and gnus-simplify-ignored-prefixes
1564                  (string-match gnus-simplify-ignored-prefixes subject))
1565         (setq subject (substring subject (match-end 0))))
1566       (insert subject)
1567       (inline (gnus-simplify-buffer-fuzzy))
1568       (buffer-string))))
1569
1570 (defsubst gnus-simplify-subject-fully (subject)
1571   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1572   (cond
1573    (gnus-simplify-subject-functions
1574     (gnus-map-function gnus-simplify-subject-functions subject))
1575    ((null gnus-summary-gather-subject-limit)
1576     (gnus-simplify-subject-re subject))
1577    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1578     (gnus-simplify-subject-fuzzy subject))
1579    ((numberp gnus-summary-gather-subject-limit)
1580     (gnus-limit-string (gnus-simplify-subject-re subject)
1581                        gnus-summary-gather-subject-limit))
1582    (t
1583     subject)))
1584
1585 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1586   "Check whether two subjects are equal.
1587 If optional argument SIMPLE-FIRST is t, first argument is already
1588 simplified."
1589   (cond
1590    ((null simple-first)
1591     (equal (gnus-simplify-subject-fully s1)
1592            (gnus-simplify-subject-fully s2)))
1593    (t
1594     (equal s1
1595            (gnus-simplify-subject-fully s2)))))
1596
1597 (defun gnus-summary-bubble-group ()
1598   "Increase the score of the current group.
1599 This is a handy function to add to `gnus-summary-exit-hook' to
1600 increase the score of each group you read."
1601   (gnus-group-add-score gnus-newsgroup-name))
1602
1603 \f
1604 ;;;
1605 ;;; Gnus summary mode
1606 ;;;
1607
1608 (put 'gnus-summary-mode 'mode-class 'special)
1609
1610 (defvar gnus-article-commands-menu)
1611
1612 ;; Non-orthogonal keys
1613
1614 (gnus-define-keys gnus-summary-mode-map
1615   " " gnus-summary-next-page
1616   "\177" gnus-summary-prev-page
1617   [delete] gnus-summary-prev-page
1618   [backspace] gnus-summary-prev-page
1619   "\r" gnus-summary-scroll-up
1620   "\M-\r" gnus-summary-scroll-down
1621   "n" gnus-summary-next-unread-article
1622   "p" gnus-summary-prev-unread-article
1623   "N" gnus-summary-next-article
1624   "P" gnus-summary-prev-article
1625   "\M-\C-n" gnus-summary-next-same-subject
1626   "\M-\C-p" gnus-summary-prev-same-subject
1627   "\M-n" gnus-summary-next-unread-subject
1628   "\M-p" gnus-summary-prev-unread-subject
1629   "." gnus-summary-first-unread-article
1630   "," gnus-summary-best-unread-article
1631   "\M-s" gnus-summary-search-article-forward
1632   "\M-r" gnus-summary-search-article-backward
1633   "<" gnus-summary-beginning-of-article
1634   ">" gnus-summary-end-of-article
1635   "j" gnus-summary-goto-article
1636   "^" gnus-summary-refer-parent-article
1637   "\M-^" gnus-summary-refer-article
1638   "u" gnus-summary-tick-article-forward
1639   "!" gnus-summary-tick-article-forward
1640   "U" gnus-summary-tick-article-backward
1641   "d" gnus-summary-mark-as-read-forward
1642   "D" gnus-summary-mark-as-read-backward
1643   "E" gnus-summary-mark-as-expirable
1644   "\M-u" gnus-summary-clear-mark-forward
1645   "\M-U" gnus-summary-clear-mark-backward
1646   "k" gnus-summary-kill-same-subject-and-select
1647   "\C-k" gnus-summary-kill-same-subject
1648   "\M-\C-k" gnus-summary-kill-thread
1649   "\M-\C-l" gnus-summary-lower-thread
1650   "e" gnus-summary-edit-article
1651   "#" gnus-summary-mark-as-processable
1652   "\M-#" gnus-summary-unmark-as-processable
1653   "\M-\C-t" gnus-summary-toggle-threads
1654   "\M-\C-s" gnus-summary-show-thread
1655   "\M-\C-h" gnus-summary-hide-thread
1656   "\M-\C-f" gnus-summary-next-thread
1657   "\M-\C-b" gnus-summary-prev-thread
1658   [(meta down)] gnus-summary-next-thread
1659   [(meta up)] gnus-summary-prev-thread
1660   "\M-\C-u" gnus-summary-up-thread
1661   "\M-\C-d" gnus-summary-down-thread
1662   "&" gnus-summary-execute-command
1663   "c" gnus-summary-catchup-and-exit
1664   "\C-w" gnus-summary-mark-region-as-read
1665   "\C-t" gnus-summary-toggle-truncation
1666   "?" gnus-summary-mark-as-dormant
1667   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1668   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1669   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1670   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1671   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1672   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1673   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1674   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1675   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1676   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1677   "=" gnus-summary-expand-window
1678   "\C-x\C-s" gnus-summary-reselect-current-group
1679   "\M-g" gnus-summary-rescan-group
1680   "w" gnus-summary-stop-page-breaking
1681   "\C-c\C-r" gnus-summary-caesar-message
1682   "\M-t" gnus-summary-toggle-mime
1683   "f" gnus-summary-followup
1684   "F" gnus-summary-followup-with-original
1685   "C" gnus-summary-cancel-article
1686   "r" gnus-summary-reply
1687   "R" gnus-summary-reply-with-original
1688   "\C-c\C-f" gnus-summary-mail-forward
1689   "o" gnus-summary-save-article
1690   "\C-o" gnus-summary-save-article-mail
1691   "|" gnus-summary-pipe-output
1692   "\M-k" gnus-summary-edit-local-kill
1693   "\M-K" gnus-summary-edit-global-kill
1694   ;; "V" gnus-version
1695   "\C-c\C-d" gnus-summary-describe-group
1696   "q" gnus-summary-exit
1697   "Q" gnus-summary-exit-no-update
1698   "\C-c\C-i" gnus-info-find-node
1699   gnus-mouse-2 gnus-mouse-pick-article
1700   "m" gnus-summary-mail-other-window
1701   "a" gnus-summary-post-news
1702   "i" gnus-summary-news-other-window
1703   "x" gnus-summary-limit-to-unread
1704   "s" gnus-summary-isearch-article
1705   "t" gnus-summary-toggle-header
1706   "g" gnus-summary-show-article
1707   "l" gnus-summary-goto-last-article
1708   "v" gnus-summary-preview-mime-message
1709   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1710   "\C-d" gnus-summary-enter-digest-group
1711   "\M-\C-d" gnus-summary-read-document
1712   "\M-\C-e" gnus-summary-edit-parameters
1713   "\M-\C-a" gnus-summary-customize-parameters
1714   "\C-c\C-b" gnus-bug
1715   "\C-c\C-n" gnus-namazu-search
1716   "*" gnus-cache-enter-article
1717   "\M-*" gnus-cache-remove-article
1718   "\M-&" gnus-summary-universal-argument
1719   "\C-l" gnus-recenter
1720   "I" gnus-summary-increase-score
1721   "L" gnus-summary-lower-score
1722   "\M-i" gnus-symbolic-argument
1723   "h" gnus-summary-select-article-buffer
1724
1725   "V" gnus-summary-score-map
1726   "X" gnus-uu-extract-map
1727   "S" gnus-summary-send-map)
1728
1729   ;; Sort of orthogonal keymap
1730 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1731   "t" gnus-summary-tick-article-forward
1732   "!" gnus-summary-tick-article-forward
1733   "d" gnus-summary-mark-as-read-forward
1734   "r" gnus-summary-mark-as-read-forward
1735   "c" gnus-summary-clear-mark-forward
1736   " " gnus-summary-clear-mark-forward
1737   "e" gnus-summary-mark-as-expirable
1738   "x" gnus-summary-mark-as-expirable
1739   "?" gnus-summary-mark-as-dormant
1740   "b" gnus-summary-set-bookmark
1741   "B" gnus-summary-remove-bookmark
1742   "#" gnus-summary-mark-as-processable
1743   "\M-#" gnus-summary-unmark-as-processable
1744   "S" gnus-summary-limit-include-expunged
1745   "C" gnus-summary-catchup
1746   "H" gnus-summary-catchup-to-here
1747   "h" gnus-summary-catchup-from-here
1748   "\C-c" gnus-summary-catchup-all
1749   "k" gnus-summary-kill-same-subject-and-select
1750   "K" gnus-summary-kill-same-subject
1751   "P" gnus-uu-mark-map)
1752
1753 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1754   "c" gnus-summary-clear-above
1755   "u" gnus-summary-tick-above
1756   "m" gnus-summary-mark-above
1757   "k" gnus-summary-kill-below)
1758
1759 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1760   "/" gnus-summary-limit-to-subject
1761   "n" gnus-summary-limit-to-articles
1762   "w" gnus-summary-pop-limit
1763   "s" gnus-summary-limit-to-subject
1764   "a" gnus-summary-limit-to-author
1765   "u" gnus-summary-limit-to-unread
1766   "m" gnus-summary-limit-to-marks
1767   "M" gnus-summary-limit-exclude-marks
1768   "v" gnus-summary-limit-to-score
1769   "*" gnus-summary-limit-include-cached
1770   "D" gnus-summary-limit-include-dormant
1771   "T" gnus-summary-limit-include-thread
1772   "d" gnus-summary-limit-exclude-dormant
1773   "t" gnus-summary-limit-to-age
1774   "." gnus-summary-limit-to-unseen
1775   "x" gnus-summary-limit-to-extra
1776   "p" gnus-summary-limit-to-display-predicate
1777   "E" gnus-summary-limit-include-expunged
1778   "c" gnus-summary-limit-exclude-childless-dormant
1779   "C" gnus-summary-limit-mark-excluded-as-read
1780   "o" gnus-summary-insert-old-articles
1781   "N" gnus-summary-insert-new-articles
1782   "r" gnus-summary-limit-to-replied)
1783
1784 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1785   "n" gnus-summary-next-unread-article
1786   "p" gnus-summary-prev-unread-article
1787   "N" gnus-summary-next-article
1788   "P" gnus-summary-prev-article
1789   "\C-n" gnus-summary-next-same-subject
1790   "\C-p" gnus-summary-prev-same-subject
1791   "\M-n" gnus-summary-next-unread-subject
1792   "\M-p" gnus-summary-prev-unread-subject
1793   "f" gnus-summary-first-unread-article
1794   "b" gnus-summary-best-unread-article
1795   "j" gnus-summary-goto-article
1796   "g" gnus-summary-goto-subject
1797   "l" gnus-summary-goto-last-article
1798   "o" gnus-summary-pop-article)
1799
1800 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1801   "k" gnus-summary-kill-thread
1802   "l" gnus-summary-lower-thread
1803   "i" gnus-summary-raise-thread
1804   "T" gnus-summary-toggle-threads
1805   "t" gnus-summary-rethread-current
1806   "^" gnus-summary-reparent-thread
1807   "s" gnus-summary-show-thread
1808   "S" gnus-summary-show-all-threads
1809   "h" gnus-summary-hide-thread
1810   "H" gnus-summary-hide-all-threads
1811   "n" gnus-summary-next-thread
1812   "p" gnus-summary-prev-thread
1813   "u" gnus-summary-up-thread
1814   "o" gnus-summary-top-thread
1815   "d" gnus-summary-down-thread
1816   "#" gnus-uu-mark-thread
1817   "\M-#" gnus-uu-unmark-thread)
1818
1819 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1820   "g" gnus-summary-prepare
1821   "c" gnus-summary-insert-cached-articles
1822   "d" gnus-summary-insert-dormant-articles)
1823
1824 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1825   "c" gnus-summary-catchup-and-exit
1826   "C" gnus-summary-catchup-all-and-exit
1827   "E" gnus-summary-exit-no-update
1828   "J" gnus-summary-jump-to-other-group
1829   "Q" gnus-summary-exit
1830   "Z" gnus-summary-exit
1831   "n" gnus-summary-catchup-and-goto-next-group
1832   "R" gnus-summary-reselect-current-group
1833   "G" gnus-summary-rescan-group
1834   "N" gnus-summary-next-group
1835   "s" gnus-summary-save-newsrc
1836   "P" gnus-summary-prev-group)
1837
1838 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1839   " " gnus-summary-next-page
1840   "n" gnus-summary-next-page
1841   "\177" gnus-summary-prev-page
1842   [delete] gnus-summary-prev-page
1843   "p" gnus-summary-prev-page
1844   "\r" gnus-summary-scroll-up
1845   "\M-\r" gnus-summary-scroll-down
1846   "<" gnus-summary-beginning-of-article
1847   ">" gnus-summary-end-of-article
1848   "b" gnus-summary-beginning-of-article
1849   "e" gnus-summary-end-of-article
1850   "^" gnus-summary-refer-parent-article
1851   "r" gnus-summary-refer-parent-article
1852   "D" gnus-summary-enter-digest-group
1853   "R" gnus-summary-refer-references
1854   "T" gnus-summary-refer-thread
1855   "g" gnus-summary-show-article
1856   "s" gnus-summary-isearch-article
1857   "P" gnus-summary-print-article
1858   "M" gnus-mailing-list-insinuate
1859   "t" gnus-article-babel)
1860
1861 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1862   "b" gnus-article-add-buttons
1863   "B" gnus-article-add-buttons-to-head
1864   "o" gnus-article-treat-overstrike
1865   "e" gnus-article-emphasize
1866   "w" gnus-article-fill-cited-article
1867   "Q" gnus-article-fill-long-lines
1868   "C" gnus-article-capitalize-sentences
1869   "c" gnus-article-remove-cr
1870   "Z" gnus-article-decode-HZ
1871   "A" gnus-article-treat-ansi-sequences
1872   "h" gnus-article-wash-html
1873   "u" gnus-article-unsplit-urls
1874   "f" gnus-article-display-x-face
1875   "l" gnus-summary-stop-page-breaking
1876   "r" gnus-summary-caesar-message
1877   "m" gnus-summary-morse-message
1878   "t" gnus-summary-toggle-header
1879   "g" gnus-treat-smiley
1880   "v" gnus-summary-verbose-headers
1881   "m" gnus-summary-toggle-mime
1882   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1883   "p" gnus-article-verify-x-pgp-sig
1884   "d" gnus-article-treat-dumbquotes)
1885
1886 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1887   ;; mnemonic: deuglif*Y*
1888   "u" gnus-article-outlook-unwrap-lines
1889   "a" gnus-article-outlook-repair-attribution
1890   "c" gnus-article-outlook-rearrange-citation
1891   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1892
1893 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1894   "a" gnus-article-hide
1895   "h" gnus-article-hide-headers
1896   "b" gnus-article-hide-boring-headers
1897   "s" gnus-article-hide-signature
1898   "c" gnus-article-hide-citation
1899   "C" gnus-article-hide-citation-in-followups
1900   "l" gnus-article-hide-list-identifiers
1901   "B" gnus-article-strip-banner
1902   "P" gnus-article-hide-pem
1903   "\C-c" gnus-article-hide-citation-maybe)
1904
1905 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1906   "a" gnus-article-highlight
1907   "h" gnus-article-highlight-headers
1908   "c" gnus-article-highlight-citation
1909   "s" gnus-article-highlight-signature)
1910
1911 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1912   "f" gnus-article-treat-fold-headers
1913   "u" gnus-article-treat-unfold-headers
1914   "n" gnus-article-treat-fold-newsgroups)
1915
1916 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1917   "x" gnus-article-display-x-face
1918   "d" gnus-article-display-face
1919   "s" gnus-treat-smiley
1920   "D" gnus-article-remove-images
1921   "f" gnus-treat-from-picon
1922   "m" gnus-treat-mail-picon
1923   "n" gnus-treat-newsgroups-picon)
1924
1925 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1926   "z" gnus-article-date-ut
1927   "u" gnus-article-date-ut
1928   "l" gnus-article-date-local
1929   "p" gnus-article-date-english
1930   "e" gnus-article-date-lapsed
1931   "o" gnus-article-date-original
1932   "i" gnus-article-date-iso8601
1933   "s" gnus-article-date-user)
1934
1935 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1936   "t" gnus-article-remove-trailing-blank-lines
1937   "l" gnus-article-strip-leading-blank-lines
1938   "m" gnus-article-strip-multiple-blank-lines
1939   "a" gnus-article-strip-blank-lines
1940   "A" gnus-article-strip-all-blank-lines
1941   "s" gnus-article-strip-leading-space
1942   "e" gnus-article-strip-trailing-space
1943   "w" gnus-article-remove-leading-whitespace)
1944
1945 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1946   "v" gnus-version
1947   "f" gnus-summary-fetch-faq
1948   "d" gnus-summary-describe-group
1949   "h" gnus-summary-describe-briefly
1950   "i" gnus-info-find-node
1951   "c" gnus-group-fetch-charter
1952   "C" gnus-group-fetch-control)
1953
1954 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1955   "e" gnus-summary-expire-articles
1956   "\M-\C-e" gnus-summary-expire-articles-now
1957   "\177" gnus-summary-delete-article
1958   [delete] gnus-summary-delete-article
1959   [backspace] gnus-summary-delete-article
1960   "m" gnus-summary-move-article
1961   "r" gnus-summary-respool-article
1962   "w" gnus-summary-edit-article
1963   "c" gnus-summary-copy-article
1964   "B" gnus-summary-crosspost-article
1965   "q" gnus-summary-respool-query
1966   "t" gnus-summary-respool-trace
1967   "i" gnus-summary-import-article
1968   "I" gnus-summary-create-article
1969   "p" gnus-summary-article-posted-p)
1970
1971 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1972   "o" gnus-summary-save-article
1973   "m" gnus-summary-save-article-mail
1974   "F" gnus-summary-write-article-file
1975   "r" gnus-summary-save-article-rmail
1976   "f" gnus-summary-save-article-file
1977   "b" gnus-summary-save-article-body-file
1978   "h" gnus-summary-save-article-folder
1979   "v" gnus-summary-save-article-vm
1980   "p" gnus-summary-pipe-output
1981   "P" gnus-summary-muttprint
1982   "s" gnus-soup-add-article)
1983
1984 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1985   "b" gnus-summary-display-buttonized
1986   "m" gnus-summary-repair-multipart
1987   "v" gnus-article-view-part
1988   "o" gnus-article-save-part
1989   "c" gnus-article-copy-part
1990   "C" gnus-article-view-part-as-charset
1991   "e" gnus-article-view-part-externally
1992   "E" gnus-article-encrypt-body
1993   "i" gnus-article-inline-part
1994   "|" gnus-article-pipe-part)
1995
1996 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1997   "p" gnus-summary-mark-as-processable
1998   "u" gnus-summary-unmark-as-processable
1999   "U" gnus-summary-unmark-all-processable
2000   "v" gnus-uu-mark-over
2001   "s" gnus-uu-mark-series
2002   "r" gnus-uu-mark-region
2003   "g" gnus-uu-unmark-region
2004   "R" gnus-uu-mark-by-regexp
2005   "G" gnus-uu-unmark-by-regexp
2006   "t" gnus-uu-mark-thread
2007   "T" gnus-uu-unmark-thread
2008   "a" gnus-uu-mark-all
2009   "b" gnus-uu-mark-buffer
2010   "S" gnus-uu-mark-sparse
2011   "k" gnus-summary-kill-process-mark
2012   "y" gnus-summary-yank-process-mark
2013   "w" gnus-summary-save-process-mark
2014   "i" gnus-uu-invert-processable)
2015
2016 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2017   ;;"x" gnus-uu-extract-any
2018   "m" gnus-summary-save-parts
2019   "u" gnus-uu-decode-uu
2020   "U" gnus-uu-decode-uu-and-save
2021   "s" gnus-uu-decode-unshar
2022   "S" gnus-uu-decode-unshar-and-save
2023   "o" gnus-uu-decode-save
2024   "O" gnus-uu-decode-save
2025   "b" gnus-uu-decode-binhex
2026   "B" gnus-uu-decode-binhex
2027   "p" gnus-uu-decode-postscript
2028   "P" gnus-uu-decode-postscript-and-save)
2029
2030 (gnus-define-keys
2031     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2032   "u" gnus-uu-decode-uu-view
2033   "U" gnus-uu-decode-uu-and-save-view
2034   "s" gnus-uu-decode-unshar-view
2035   "S" gnus-uu-decode-unshar-and-save-view
2036   "o" gnus-uu-decode-save-view
2037   "O" gnus-uu-decode-save-view
2038   "b" gnus-uu-decode-binhex-view
2039   "B" gnus-uu-decode-binhex-view
2040   "p" gnus-uu-decode-postscript-view
2041   "P" gnus-uu-decode-postscript-and-save-view)
2042
2043 (defvar gnus-article-post-menu nil)
2044
2045 (defconst gnus-summary-menu-maxlen 20)
2046
2047 (defun gnus-summary-menu-split (menu)
2048   ;; If we have lots of elements, divide them into groups of 20
2049   ;; and make a pane (or submenu) for each one.
2050   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2051       (let ((menu menu) sublists next
2052             (i 1))
2053         (while menu
2054           ;; Pull off the next gnus-summary-menu-maxlen elements
2055           ;; and make them the next element of sublist.
2056           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2057           (if next
2058               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2059                       nil))
2060           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2061                                              (aref (car (last menu)) 0)) menu)
2062                                sublists))
2063           (setq i (1+ i))
2064           (setq menu next))
2065         (nreverse sublists))
2066     ;; Few elements--put them all in one pane.
2067     menu))
2068
2069 (defun gnus-summary-make-menu-bar ()
2070   (gnus-turn-off-edit-menu 'summary)
2071
2072   (unless (boundp 'gnus-summary-misc-menu)
2073
2074     (easy-menu-define
2075      gnus-summary-kill-menu gnus-summary-mode-map ""
2076      (cons
2077       "Score"
2078       (nconc
2079        (list
2080         ["Customize" gnus-score-customize t])
2081        (gnus-make-score-map 'increase)
2082        (gnus-make-score-map 'lower)
2083        '(("Mark"
2084           ["Kill below" gnus-summary-kill-below t]
2085           ["Mark above" gnus-summary-mark-above t]
2086           ["Tick above" gnus-summary-tick-above t]
2087           ["Clear above" gnus-summary-clear-above t])
2088          ["Current score" gnus-summary-current-score t]
2089          ["Set score" gnus-summary-set-score t]
2090          ["Switch current score file..." gnus-score-change-score-file t]
2091          ["Set mark below..." gnus-score-set-mark-below t]
2092          ["Set expunge below..." gnus-score-set-expunge-below t]
2093          ["Edit current score file" gnus-score-edit-current-scores t]
2094          ["Edit score file" gnus-score-edit-file t]
2095          ["Trace score" gnus-score-find-trace t]
2096          ["Find words" gnus-score-find-favourite-words t]
2097          ["Rescore buffer" gnus-summary-rescore t]
2098          ["Increase score..." gnus-summary-increase-score t]
2099          ["Lower score..." gnus-summary-lower-score t]))))
2100
2101     ;; Define both the Article menu in the summary buffer and the
2102     ;; equivalent Commands menu in the article buffer here for
2103     ;; consistency.
2104     (let ((innards
2105            `(("Hide"
2106               ["All" gnus-article-hide t]
2107               ["Headers" gnus-article-hide-headers t]
2108               ["Signature" gnus-article-hide-signature t]
2109               ["Citation" gnus-article-hide-citation t]
2110               ["List identifiers" gnus-article-hide-list-identifiers t]
2111               ["Banner" gnus-article-strip-banner t]
2112               ["Boring headers" gnus-article-hide-boring-headers t])
2113              ("Highlight"
2114               ["All" gnus-article-highlight t]
2115               ["Headers" gnus-article-highlight-headers t]
2116               ["Signature" gnus-article-highlight-signature t]
2117               ["Citation" gnus-article-highlight-citation t])
2118              ("Date"
2119               ["Local" gnus-article-date-local t]
2120               ["ISO8601" gnus-article-date-iso8601 t]
2121               ["UT" gnus-article-date-ut t]
2122               ["Original" gnus-article-date-original t]
2123               ["Lapsed" gnus-article-date-lapsed t]
2124               ["User-defined" gnus-article-date-user t])
2125              ("Display"
2126               ["Remove images" gnus-article-remove-images t]
2127               ["Toggle smiley" gnus-treat-smiley t]
2128               ["Show X-Face" gnus-article-display-x-face t]
2129               ["Show picons in From" gnus-treat-from-picon t]
2130               ["Show picons in mail headers" gnus-treat-mail-picon t]
2131               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2132               ("View as different encoding"
2133                ,@(gnus-summary-menu-split
2134                   (mapcar
2135                    (lambda (cs)
2136                      ;; Since easymenu under Emacs doesn't allow
2137                      ;; lambda forms for menu commands, we should
2138                      ;; provide intern'ed function symbols.
2139                      (let ((command (intern (format "\
2140 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2141                        (fset command
2142                              `(lambda ()
2143                                 (interactive)
2144                                 (let ((gnus-summary-show-article-charset-alist
2145                                        '((1 . ,cs))))
2146                                   (gnus-summary-show-article 1))))
2147                        `[,(symbol-name cs) ,command t]))
2148                    (sort (if (fboundp 'coding-system-list)
2149                              (coding-system-list)
2150                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2151                            )
2152                          'string<)))))
2153              ("Washing"
2154               ("Remove Blanks"
2155                ["Leading" gnus-article-strip-leading-blank-lines t]
2156                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2157                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2158                ["All of the above" gnus-article-strip-blank-lines t]
2159                ["All" gnus-article-strip-all-blank-lines t]
2160                ["Leading space" gnus-article-strip-leading-space t]
2161                ["Trailing space" gnus-article-strip-trailing-space t]
2162                ["Leading space in headers"
2163                 gnus-article-remove-leading-whitespace t])
2164               ["Overstrike" gnus-article-treat-overstrike t]
2165               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2166               ["Emphasis" gnus-article-emphasize t]
2167               ["Word wrap" gnus-article-fill-cited-article t]
2168               ["Fill long lines" gnus-article-fill-long-lines t]
2169               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2170               ["Remove CR" gnus-article-remove-cr t]
2171               ["Rot 13" gnus-summary-caesar-message
2172                ,@(if (featurep 'xemacs) '(t)
2173                    '(:help "\"Caesar rotate\" article by 13"))]
2174               ["Morse decode" gnus-summary-morse-message t]
2175               ["Unix pipe..." gnus-summary-pipe-message t]
2176               ["Add buttons" gnus-article-add-buttons t]
2177               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2178               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2179               ["Toggle MIME" gnus-summary-toggle-mime t]
2180               ["Verbose header" gnus-summary-verbose-headers t]
2181               ["Toggle header" gnus-summary-toggle-header t]
2182               ["Unfold headers" gnus-article-treat-unfold-headers t]
2183               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2184               ["Html" gnus-article-wash-html t]
2185               ["Unsplit URLs" gnus-article-unsplit-urls t]
2186               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2187               ["Decode HZ" gnus-article-decode-HZ t]
2188               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2189               ("(Outlook) Deuglify"
2190                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2191                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2192                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2193                ["Full (Outlook) deuglify"
2194                 gnus-article-outlook-deuglify-article t])
2195               )
2196              ("Output"
2197               ["Save in default format..." gnus-summary-save-article
2198                ,@(if (featurep 'xemacs) '(t)
2199                    '(:help "Save article using default method"))]
2200               ["Save in file..." gnus-summary-save-article-file
2201                ,@(if (featurep 'xemacs) '(t)
2202                    '(:help "Save article in file"))]
2203               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2204               ["Save in MH folder..." gnus-summary-save-article-folder t]
2205               ["Save in VM folder..." gnus-summary-save-article-vm t]
2206               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2207               ["Save body in file..." gnus-summary-save-article-body-file t]
2208               ["Pipe through a filter..." gnus-summary-pipe-output t]
2209               ["Add to SOUP packet" gnus-soup-add-article t]
2210               ["Print with Muttprint..." gnus-summary-muttprint t]
2211               ["Print" gnus-summary-print-article t])
2212              ("Backend"
2213               ["Respool article..." gnus-summary-respool-article t]
2214               ["Move article..." gnus-summary-move-article
2215                (gnus-check-backend-function
2216                 'request-move-article gnus-newsgroup-name)]
2217               ["Copy article..." gnus-summary-copy-article t]
2218               ["Crosspost article..." gnus-summary-crosspost-article
2219                (gnus-check-backend-function
2220                 'request-replace-article gnus-newsgroup-name)]
2221               ["Import file..." gnus-summary-import-article
2222                (gnus-check-backend-function
2223                 'request-accept-article gnus-newsgroup-name)]
2224               ["Create article..." gnus-summary-create-article
2225                (gnus-check-backend-function
2226                 'request-accept-article gnus-newsgroup-name)]
2227               ["Check if posted" gnus-summary-article-posted-p t]
2228               ["Edit article" gnus-summary-edit-article
2229                (not (gnus-group-read-only-p))]
2230               ["Delete article" gnus-summary-delete-article
2231                (gnus-check-backend-function
2232                 'request-expire-articles gnus-newsgroup-name)]
2233               ["Query respool" gnus-summary-respool-query t]
2234               ["Trace respool" gnus-summary-respool-trace t]
2235               ["Delete expirable articles" gnus-summary-expire-articles-now
2236                (gnus-check-backend-function
2237                 'request-expire-articles gnus-newsgroup-name)])
2238              ("Extract"
2239               ["Uudecode" gnus-uu-decode-uu
2240                ,@(if (featurep 'xemacs) '(t)
2241                    '(:help "Decode uuencoded article(s)"))]
2242               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2243               ["Unshar" gnus-uu-decode-unshar t]
2244               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2245               ["Save" gnus-uu-decode-save t]
2246               ["Binhex" gnus-uu-decode-binhex t]
2247               ["Postscript" gnus-uu-decode-postscript t])
2248              ("Cache"
2249               ["Enter article" gnus-cache-enter-article t]
2250               ["Remove article" gnus-cache-remove-article t])
2251              ["Translate" gnus-article-babel t]
2252              ["Select article buffer" gnus-summary-select-article-buffer t]
2253              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2254              ["Isearch article..." gnus-summary-isearch-article t]
2255              ["Beginning of the article" gnus-summary-beginning-of-article t]
2256              ["End of the article" gnus-summary-end-of-article t]
2257              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2258              ["Fetch referenced articles" gnus-summary-refer-references t]
2259              ["Fetch current thread" gnus-summary-refer-thread t]
2260              ["Fetch article with id..." gnus-summary-refer-article t]
2261              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2262              ["Redisplay" gnus-summary-show-article t]
2263              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2264       (easy-menu-define
2265        gnus-summary-article-menu gnus-summary-mode-map ""
2266        (cons "Article" innards))
2267
2268       (if (not (keymapp gnus-summary-article-menu))
2269           (easy-menu-define
2270            gnus-article-commands-menu gnus-article-mode-map ""
2271            (cons "Commands" innards))
2272         ;; in Emacs, don't share menu.
2273         (setq gnus-article-commands-menu
2274               (copy-keymap gnus-summary-article-menu))
2275         (define-key gnus-article-mode-map [menu-bar commands]
2276           (cons "Commands" gnus-article-commands-menu))))
2277
2278     (easy-menu-define
2279      gnus-summary-thread-menu gnus-summary-mode-map ""
2280      '("Threads"
2281        ["Find all messages in thread" gnus-summary-refer-thread t]
2282        ["Toggle threading" gnus-summary-toggle-threads t]
2283        ["Hide threads" gnus-summary-hide-all-threads t]
2284        ["Show threads" gnus-summary-show-all-threads t]
2285        ["Hide thread" gnus-summary-hide-thread t]
2286        ["Show thread" gnus-summary-show-thread t]
2287        ["Go to next thread" gnus-summary-next-thread t]
2288        ["Go to previous thread" gnus-summary-prev-thread t]
2289        ["Go down thread" gnus-summary-down-thread t]
2290        ["Go up thread" gnus-summary-up-thread t]
2291        ["Top of thread" gnus-summary-top-thread t]
2292        ["Mark thread as read" gnus-summary-kill-thread t]
2293        ["Lower thread score" gnus-summary-lower-thread t]
2294        ["Raise thread score" gnus-summary-raise-thread t]
2295        ["Rethread current" gnus-summary-rethread-current t]))
2296
2297     (easy-menu-define
2298      gnus-summary-post-menu gnus-summary-mode-map ""
2299      `("Post"
2300        ["Send a message (mail or news)" gnus-summary-post-news
2301         ,@(if (featurep 'xemacs) '(t)
2302             '(:help "Post an article"))]
2303        ["Followup" gnus-summary-followup
2304         ,@(if (featurep 'xemacs) '(t)
2305             '(:help "Post followup to this article"))]
2306        ["Followup and yank" gnus-summary-followup-with-original
2307         ,@(if (featurep 'xemacs) '(t)
2308             '(:help "Post followup to this article, quoting its contents"))]
2309        ["Supersede article" gnus-summary-supersede-article t]
2310        ["Cancel article" gnus-summary-cancel-article
2311         ,@(if (featurep 'xemacs) '(t)
2312             '(:help "Cancel an article you posted"))]
2313        ["Reply" gnus-summary-reply t]
2314        ["Reply and yank" gnus-summary-reply-with-original t]
2315        ["Wide reply" gnus-summary-wide-reply t]
2316        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2317         ,@(if (featurep 'xemacs) '(t)
2318             '(:help "Mail a reply, quoting this article"))]
2319        ["Very wide reply" gnus-summary-very-wide-reply t]
2320        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2321         ,@(if (featurep 'xemacs) '(t)
2322             '(:help "Mail a very wide reply, quoting this article"))]
2323        ["Mail forward" gnus-summary-mail-forward t]
2324        ["Post forward" gnus-summary-post-forward t]
2325        ["Digest and mail" gnus-summary-digest-mail-forward t]
2326        ["Digest and post" gnus-summary-digest-post-forward t]
2327        ["Resend message" gnus-summary-resend-message t]
2328        ["Resend message edit" gnus-summary-resend-message-edit t]
2329        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2330        ["Send a mail" gnus-summary-mail-other-window t]
2331        ["Create a local message" gnus-summary-news-other-window t]
2332        ["Uuencode and post" gnus-uu-post-news
2333         ,@(if (featurep 'xemacs) '(t)
2334             '(:help "Post a uuencoded article"))]
2335        ["Followup via news" gnus-summary-followup-to-mail t]
2336        ["Followup via news and yank"
2337         gnus-summary-followup-to-mail-with-original t]
2338        ;;("Draft"
2339        ;;["Send" gnus-summary-send-draft t]
2340        ;;["Send bounced" gnus-resend-bounced-mail t])
2341        ))
2342
2343     (cond
2344      ((not (keymapp gnus-summary-post-menu))
2345       (setq gnus-article-post-menu gnus-summary-post-menu))
2346      ((not gnus-article-post-menu)
2347       ;; Don't share post menu.
2348       (setq gnus-article-post-menu
2349             (copy-keymap gnus-summary-post-menu))))
2350     (define-key gnus-article-mode-map [menu-bar post]
2351       (cons "Post" gnus-article-post-menu))
2352
2353     (easy-menu-define
2354      gnus-summary-misc-menu gnus-summary-mode-map ""
2355      `("Gnus"
2356        ("Mark Read"
2357         ["Mark as read" gnus-summary-mark-as-read-forward t]
2358         ["Mark same subject and select"
2359          gnus-summary-kill-same-subject-and-select t]
2360         ["Mark same subject" gnus-summary-kill-same-subject t]
2361         ["Catchup" gnus-summary-catchup
2362          ,@(if (featurep 'xemacs) '(t)
2363              '(:help "Mark unread articles in this group as read"))]
2364         ["Catchup all" gnus-summary-catchup-all t]
2365         ["Catchup to here" gnus-summary-catchup-to-here t]
2366         ["Catchup from here" gnus-summary-catchup-from-here t]
2367         ["Catchup region" gnus-summary-mark-region-as-read
2368          (gnus-mark-active-p)]
2369         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2370        ("Mark Various"
2371         ["Tick" gnus-summary-tick-article-forward t]
2372         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2373         ["Remove marks" gnus-summary-clear-mark-forward t]
2374         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2375         ["Set bookmark" gnus-summary-set-bookmark t]
2376         ["Remove bookmark" gnus-summary-remove-bookmark t])
2377        ("Limit to"
2378         ["Marks..." gnus-summary-limit-to-marks t]
2379         ["Subject..." gnus-summary-limit-to-subject t]
2380         ["Author..." gnus-summary-limit-to-author t]
2381         ["Age..." gnus-summary-limit-to-age t]
2382         ["Extra..." gnus-summary-limit-to-extra t]
2383         ["Score..." gnus-summary-limit-to-score t]
2384         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2385         ["Unread" gnus-summary-limit-to-unread t]
2386         ["Unseen" gnus-summary-limit-to-unseen t]
2387         ["Replied" gnus-summary-limit-to-replied t]
2388         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2389         ["Next articles" gnus-summary-limit-to-articles t]
2390         ["Pop limit" gnus-summary-pop-limit t]
2391         ["Show dormant" gnus-summary-limit-include-dormant t]
2392         ["Hide childless dormant"
2393          gnus-summary-limit-exclude-childless-dormant t]
2394         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2395         ["Hide marked" gnus-summary-limit-exclude-marks t]
2396         ["Show expunged" gnus-summary-limit-include-expunged t])
2397        ("Process Mark"
2398         ["Set mark" gnus-summary-mark-as-processable t]
2399         ["Remove mark" gnus-summary-unmark-as-processable t]
2400         ["Remove all marks" gnus-summary-unmark-all-processable t]
2401         ["Mark above" gnus-uu-mark-over t]
2402         ["Mark series" gnus-uu-mark-series t]
2403         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2404         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2405         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2406         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2407         ["Mark all" gnus-uu-mark-all t]
2408         ["Mark buffer" gnus-uu-mark-buffer t]
2409         ["Mark sparse" gnus-uu-mark-sparse t]
2410         ["Mark thread" gnus-uu-mark-thread t]
2411         ["Unmark thread" gnus-uu-unmark-thread t]
2412         ("Process Mark Sets"
2413          ["Kill" gnus-summary-kill-process-mark t]
2414          ["Yank" gnus-summary-yank-process-mark
2415           gnus-newsgroup-process-stack]
2416          ["Save" gnus-summary-save-process-mark t]
2417          ["Run command on marked..." gnus-summary-universal-argument t]))
2418        ("Scroll article"
2419         ["Page forward" gnus-summary-next-page
2420          ,@(if (featurep 'xemacs) '(t)
2421              '(:help "Show next page of article"))]
2422         ["Page backward" gnus-summary-prev-page
2423          ,@(if (featurep 'xemacs) '(t)
2424              '(:help "Show previous page of article"))]
2425         ["Line forward" gnus-summary-scroll-up t])
2426        ("Move"
2427         ["Next unread article" gnus-summary-next-unread-article t]
2428         ["Previous unread article" gnus-summary-prev-unread-article t]
2429         ["Next article" gnus-summary-next-article t]
2430         ["Previous article" gnus-summary-prev-article t]
2431         ["Next unread subject" gnus-summary-next-unread-subject t]
2432         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2433         ["Next article same subject" gnus-summary-next-same-subject t]
2434         ["Previous article same subject" gnus-summary-prev-same-subject t]
2435         ["First unread article" gnus-summary-first-unread-article t]
2436         ["Best unread article" gnus-summary-best-unread-article t]
2437         ["Go to subject number..." gnus-summary-goto-subject t]
2438         ["Go to article number..." gnus-summary-goto-article t]
2439         ["Go to the last article" gnus-summary-goto-last-article t]
2440         ["Pop article off history" gnus-summary-pop-article t])
2441        ("Sort"
2442         ["Sort by number" gnus-summary-sort-by-number t]
2443         ["Sort by author" gnus-summary-sort-by-author t]
2444         ["Sort by subject" gnus-summary-sort-by-subject t]
2445         ["Sort by date" gnus-summary-sort-by-date t]
2446         ["Sort by score" gnus-summary-sort-by-score t]
2447         ["Sort by lines" gnus-summary-sort-by-lines t]
2448         ["Sort by characters" gnus-summary-sort-by-chars t]
2449         ["Randomize" gnus-summary-sort-by-random t]
2450         ["Original sort" gnus-summary-sort-by-original t])
2451        ("Help"
2452         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2453         ["Describe group" gnus-summary-describe-group t]
2454         ["Fetch charter" gnus-group-fetch-charter
2455          ,@(if (featurep 'xemacs) nil
2456              '(:help "Display the charter of the current group"))]
2457         ["Fetch control message" gnus-group-fetch-control
2458          ,@(if (featurep 'xemacs) nil
2459              '(:help "Display the archived control message for the current group"))]
2460         ["Read manual" gnus-info-find-node t])
2461        ("Modes"
2462         ["Pick and read" gnus-pick-mode t]
2463         ["Binary" gnus-binary-mode t])
2464        ("Regeneration"
2465         ["Regenerate" gnus-summary-prepare t]
2466         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2467         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2468         ["Toggle threading" gnus-summary-toggle-threads t])
2469        ["See old articles" gnus-summary-insert-old-articles t]
2470        ["See new articles" gnus-summary-insert-new-articles t]
2471        ["Filter articles..." gnus-summary-execute-command t]
2472        ["Run command on articles..." gnus-summary-universal-argument t]
2473        ["Search articles forward..." gnus-summary-search-article-forward t]
2474        ["Search articles backward..." gnus-summary-search-article-backward t]
2475        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2476        ["Expand window" gnus-summary-expand-window t]
2477        ["Expire expirable articles" gnus-summary-expire-articles
2478         (gnus-check-backend-function
2479          'request-expire-articles gnus-newsgroup-name)]
2480        ["Edit local kill file" gnus-summary-edit-local-kill t]
2481        ["Edit main kill file" gnus-summary-edit-global-kill t]
2482        ["Edit group parameters" gnus-summary-edit-parameters t]
2483        ["Customize group parameters" gnus-summary-customize-parameters t]
2484        ["Send a bug report" gnus-bug t]
2485        ("Exit"
2486         ["Catchup and exit" gnus-summary-catchup-and-exit
2487          ,@(if (featurep 'xemacs) '(t)
2488              '(:help "Mark unread articles in this group as read, then exit"))]
2489         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2490         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2491         ["Exit group" gnus-summary-exit
2492          ,@(if (featurep 'xemacs) '(t)
2493              '(:help "Exit current group, return to group selection mode"))]
2494         ["Exit group without updating" gnus-summary-exit-no-update t]
2495         ["Exit and goto next group" gnus-summary-next-group t]
2496         ["Exit and goto prev group" gnus-summary-prev-group t]
2497         ["Reselect group" gnus-summary-reselect-current-group t]
2498         ["Rescan group" gnus-summary-rescan-group t]
2499         ["Update dribble" gnus-summary-save-newsrc t])))
2500
2501     (gnus-run-hooks 'gnus-summary-menu-hook)))
2502
2503 (defvar gnus-summary-tool-bar-map nil)
2504
2505 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2506 (defun gnus-summary-make-tool-bar ()
2507   (if (and (fboundp 'tool-bar-add-item-from-menu)
2508            (default-value 'tool-bar-mode)
2509            (not gnus-summary-tool-bar-map))
2510       (setq gnus-summary-tool-bar-map
2511             (let ((tool-bar-map (make-sparse-keymap))
2512                   (load-path (mm-image-load-path)))
2513               (tool-bar-add-item-from-menu
2514                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-post-news "post" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-followup "followup" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-reply "reply" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2537               (tool-bar-add-item-from-menu
2538                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2539               (tool-bar-add-item-from-menu
2540                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2541               (tool-bar-add-item-from-menu
2542                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2543               tool-bar-map)))
2544   (if gnus-summary-tool-bar-map
2545       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2546
2547 (defun gnus-score-set-default (var value)
2548   "A version of set that updates the GNU Emacs menu-bar."
2549   (set var value)
2550   ;; It is the message that forces the active status to be updated.
2551   (message ""))
2552
2553 (defun gnus-make-score-map (type)
2554   "Make a summary score map of type TYPE."
2555   (if t
2556       nil
2557     (let ((headers '(("author" "from" string)
2558                      ("subject" "subject" string)
2559                      ("article body" "body" string)
2560                      ("article head" "head" string)
2561                      ("xref" "xref" string)
2562                      ("extra header" "extra" string)
2563                      ("lines" "lines" number)
2564                      ("followups to author" "followup" string)))
2565           (types '((number ("less than" <)
2566                            ("greater than" >)
2567                            ("equal" =))
2568                    (string ("substring" s)
2569                            ("exact string" e)
2570                            ("fuzzy string" f)
2571                            ("regexp" r))))
2572           (perms '(("temporary" (current-time-string))
2573                    ("permanent" nil)
2574                    ("immediate" now)))
2575           header)
2576       (list
2577        (apply
2578         'nconc
2579         (list
2580          (if (eq type 'lower)
2581              "Lower score"
2582            "Increase score"))
2583         (let (outh)
2584           (while headers
2585             (setq header (car headers))
2586             (setq outh
2587                   (cons
2588                    (apply
2589                     'nconc
2590                     (list (car header))
2591                     (let ((ts (cdr (assoc (nth 2 header) types)))
2592                           outt)
2593                       (while ts
2594                         (setq outt
2595                               (cons
2596                                (apply
2597                                 'nconc
2598                                 (list (caar ts))
2599                                 (let ((ps perms)
2600                                       outp)
2601                                   (while ps
2602                                     (setq outp
2603                                           (cons
2604                                            (vector
2605                                             (caar ps)
2606                                             (list
2607                                              'gnus-summary-score-entry
2608                                              (nth 1 header)
2609                                              (if (or (string= (nth 1 header)
2610                                                               "head")
2611                                                      (string= (nth 1 header)
2612                                                               "body"))
2613                                                  ""
2614                                                (list 'gnus-summary-header
2615                                                      (nth 1 header)))
2616                                              (list 'quote (nth 1 (car ts)))
2617                                              (list 'gnus-score-delta-default
2618                                                    nil)
2619                                              (nth 1 (car ps))
2620                                              t)
2621                                             t)
2622                                            outp))
2623                                     (setq ps (cdr ps)))
2624                                   (list (nreverse outp))))
2625                                outt))
2626                         (setq ts (cdr ts)))
2627                       (list (nreverse outt))))
2628                    outh))
2629             (setq headers (cdr headers)))
2630           (list (nreverse outh))))))))
2631
2632 \f
2633
2634 (defun gnus-summary-mode (&optional group)
2635   "Major mode for reading articles.
2636
2637 All normal editing commands are switched off.
2638 \\<gnus-summary-mode-map>
2639 Each line in this buffer represents one article.  To read an
2640 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2641 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2642 respectively.
2643
2644 You can also post articles and send mail from this buffer.  To
2645 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2646 of an article, type `\\[gnus-summary-reply]'.
2647
2648 There are approx. one gazillion commands you can execute in this
2649 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2650
2651 The following commands are available:
2652
2653 \\{gnus-summary-mode-map}"
2654   (interactive)
2655   (kill-all-local-variables)
2656   (when (gnus-visual-p 'summary-menu 'menu)
2657     (gnus-summary-make-menu-bar)
2658     (gnus-summary-make-tool-bar))
2659   (gnus-summary-make-local-variables)
2660   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2661     (gnus-summary-make-local-variables))
2662   (gnus-make-thread-indent-array)
2663   (gnus-simplify-mode-line)
2664   (setq major-mode 'gnus-summary-mode)
2665   (setq mode-name "Summary")
2666   (make-local-variable 'minor-mode-alist)
2667   (use-local-map gnus-summary-mode-map)
2668   (buffer-disable-undo)
2669   (setq buffer-read-only t)             ;Disable modification
2670   (setq truncate-lines t)
2671   (setq selective-display t)
2672   (setq selective-display-ellipses t)   ;Display `...'
2673   (gnus-summary-set-display-table)
2674   (gnus-set-default-directory)
2675   (setq gnus-newsgroup-name group)
2676   (unless (gnus-news-group-p group)
2677     (setq gnus-newsgroup-incorporated
2678           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2679   (make-local-variable 'gnus-summary-line-format)
2680   (make-local-variable 'gnus-summary-line-format-spec)
2681   (make-local-variable 'gnus-summary-dummy-line-format)
2682   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2683   (make-local-variable 'gnus-summary-mark-positions)
2684   (gnus-make-local-hook 'pre-command-hook)
2685   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2686   (gnus-run-hooks 'gnus-summary-mode-hook)
2687   (turn-on-gnus-mailing-list-mode)
2688   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2689   (gnus-update-summary-mark-positions))
2690
2691 (defun gnus-summary-make-local-variables ()
2692   "Make all the local summary buffer variables."
2693   (let (global)
2694     (dolist (local gnus-summary-local-variables)
2695       (if (consp local)
2696           (progn
2697             (if (eq (cdr local) 'global)
2698                 ;; Copy the global value of the variable.
2699                 (setq global (symbol-value (car local)))
2700               ;; Use the value from the list.
2701               (setq global (eval (cdr local))))
2702             (set (make-local-variable (car local)) global))
2703         ;; Simple nil-valued local variable.
2704         (set (make-local-variable local) nil)))))
2705
2706 (defun gnus-summary-clear-local-variables ()
2707   (let ((locals gnus-summary-local-variables))
2708     (while locals
2709       (if (consp (car locals))
2710           (and (vectorp (caar locals))
2711                (set (caar locals) nil))
2712         (and (vectorp (car locals))
2713              (set (car locals) nil)))
2714       (setq locals (cdr locals)))))
2715
2716 ;; Summary data functions.
2717
2718 (defmacro gnus-data-number (data)
2719   `(car ,data))
2720
2721 (defmacro gnus-data-set-number (data number)
2722   `(setcar ,data ,number))
2723
2724 (defmacro gnus-data-mark (data)
2725   `(nth 1 ,data))
2726
2727 (defmacro gnus-data-set-mark (data mark)
2728   `(setcar (nthcdr 1 ,data) ,mark))
2729
2730 (defmacro gnus-data-pos (data)
2731   `(nth 2 ,data))
2732
2733 (defmacro gnus-data-set-pos (data pos)
2734   `(setcar (nthcdr 2 ,data) ,pos))
2735
2736 (defmacro gnus-data-header (data)
2737   `(nth 3 ,data))
2738
2739 (defmacro gnus-data-set-header (data header)
2740   `(setcar (nthcdr 3 ,data) ,header))
2741
2742 (defmacro gnus-data-level (data)
2743   `(nth 4 ,data))
2744
2745 (defmacro gnus-data-unread-p (data)
2746   `(= (nth 1 ,data) gnus-unread-mark))
2747
2748 (defmacro gnus-data-read-p (data)
2749   `(/= (nth 1 ,data) gnus-unread-mark))
2750
2751 (defmacro gnus-data-pseudo-p (data)
2752   `(consp (nth 3 ,data)))
2753
2754 (defmacro gnus-data-find (number)
2755   `(assq ,number gnus-newsgroup-data))
2756
2757 (defmacro gnus-data-find-list (number &optional data)
2758   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2759      (memq (assq ,number bdata)
2760            bdata)))
2761
2762 (defmacro gnus-data-make (number mark pos header level)
2763   `(list ,number ,mark ,pos ,header ,level))
2764
2765 (defun gnus-data-enter (after-article number mark pos header level offset)
2766   (let ((data (gnus-data-find-list after-article)))
2767     (unless data
2768       (error "No such article: %d" after-article))
2769     (setcdr data (cons (gnus-data-make number mark pos header level)
2770                        (cdr data)))
2771     (setq gnus-newsgroup-data-reverse nil)
2772     (gnus-data-update-list (cddr data) offset)))
2773
2774 (defun gnus-data-enter-list (after-article list &optional offset)
2775   (when list
2776     (let ((data (and after-article (gnus-data-find-list after-article)))
2777           (ilist list))
2778       (if (not (or data
2779                    after-article))
2780           (let ((odata gnus-newsgroup-data))
2781             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2782             (when offset
2783               (gnus-data-update-list odata offset)))
2784         ;; Find the last element in the list to be spliced into the main
2785         ;; list.
2786         (while (cdr list)
2787           (setq list (cdr list)))
2788         (if (not data)
2789             (progn
2790               (setcdr list gnus-newsgroup-data)
2791               (setq gnus-newsgroup-data ilist)
2792               (when offset
2793                 (gnus-data-update-list (cdr list) offset)))
2794           (setcdr list (cdr data))
2795           (setcdr data ilist)
2796           (when offset
2797             (gnus-data-update-list (cdr list) offset))))
2798       (setq gnus-newsgroup-data-reverse nil))))
2799
2800 (defun gnus-data-remove (article &optional offset)
2801   (let ((data gnus-newsgroup-data))
2802     (if (= (gnus-data-number (car data)) article)
2803         (progn
2804           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2805                 gnus-newsgroup-data-reverse nil)
2806           (when offset
2807             (gnus-data-update-list gnus-newsgroup-data offset)))
2808       (while (cdr data)
2809         (when (= (gnus-data-number (cadr data)) article)
2810           (setcdr data (cddr data))
2811           (when offset
2812             (gnus-data-update-list (cdr data) offset))
2813           (setq data nil
2814                 gnus-newsgroup-data-reverse nil))
2815         (setq data (cdr data))))))
2816
2817 (defmacro gnus-data-list (backward)
2818   `(if ,backward
2819        (or gnus-newsgroup-data-reverse
2820            (setq gnus-newsgroup-data-reverse
2821                  (reverse gnus-newsgroup-data)))
2822      gnus-newsgroup-data))
2823
2824 (defun gnus-data-update-list (data offset)
2825   "Add OFFSET to the POS of all data entries in DATA."
2826   (setq gnus-newsgroup-data-reverse nil)
2827   (while data
2828     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2829     (setq data (cdr data))))
2830
2831 (defun gnus-summary-article-pseudo-p (article)
2832   "Say whether this article is a pseudo article or not."
2833   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2834
2835 (defmacro gnus-summary-article-sparse-p (article)
2836   "Say whether this article is a sparse article or not."
2837   `(memq ,article gnus-newsgroup-sparse))
2838
2839 (defmacro gnus-summary-article-ancient-p (article)
2840   "Say whether this article is a sparse article or not."
2841   `(memq ,article gnus-newsgroup-ancient))
2842
2843 (defun gnus-article-parent-p (number)
2844   "Say whether this article is a parent or not."
2845   (let ((data (gnus-data-find-list number)))
2846     (and (cdr data)                     ; There has to be an article after...
2847          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2848             (gnus-data-level (nth 1 data))))))
2849
2850 (defun gnus-article-children (number)
2851   "Return a list of all children to NUMBER."
2852   (let* ((data (gnus-data-find-list number))
2853          (level (gnus-data-level (car data)))
2854          children)
2855     (setq data (cdr data))
2856     (while (and data
2857                 (= (gnus-data-level (car data)) (1+ level)))
2858       (push (gnus-data-number (car data)) children)
2859       (setq data (cdr data)))
2860     children))
2861
2862 (defmacro gnus-summary-skip-intangible ()
2863   "If the current article is intangible, then jump to a different article."
2864   '(let ((to (get-text-property (point) 'gnus-intangible)))
2865      (and to (gnus-summary-goto-subject to))))
2866
2867 (defmacro gnus-summary-article-intangible-p ()
2868   "Say whether this article is intangible or not."
2869   '(get-text-property (point) 'gnus-intangible))
2870
2871 (defun gnus-article-read-p (article)
2872   "Say whether ARTICLE is read or not."
2873   (not (or (memq article gnus-newsgroup-marked)
2874            (memq article gnus-newsgroup-spam-marked)
2875            (memq article gnus-newsgroup-unreads)
2876            (memq article gnus-newsgroup-unselected)
2877            (memq article gnus-newsgroup-dormant))))
2878
2879 ;; Some summary mode macros.
2880
2881 (defmacro gnus-summary-article-number ()
2882   "The article number of the article on the current line.
2883 If there isn't an article number here, then we return the current
2884 article number."
2885   '(progn
2886      (gnus-summary-skip-intangible)
2887      (or (get-text-property (point) 'gnus-number)
2888          (gnus-summary-last-subject))))
2889
2890 (defmacro gnus-summary-article-header (&optional number)
2891   "Return the header of article NUMBER."
2892   `(gnus-data-header (gnus-data-find
2893                       ,(or number '(gnus-summary-article-number)))))
2894
2895 (defmacro gnus-summary-thread-level (&optional number)
2896   "Return the level of thread that starts with article NUMBER."
2897   `(if (and (eq gnus-summary-make-false-root 'dummy)
2898             (get-text-property (point) 'gnus-intangible))
2899        0
2900      (gnus-data-level (gnus-data-find
2901                        ,(or number '(gnus-summary-article-number))))))
2902
2903 (defmacro gnus-summary-article-mark (&optional number)
2904   "Return the mark of article NUMBER."
2905   `(gnus-data-mark (gnus-data-find
2906                     ,(or number '(gnus-summary-article-number)))))
2907
2908 (defmacro gnus-summary-article-pos (&optional number)
2909   "Return the position of the line of article NUMBER."
2910   `(gnus-data-pos (gnus-data-find
2911                    ,(or number '(gnus-summary-article-number)))))
2912
2913 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2914 (defmacro gnus-summary-article-subject (&optional number)
2915   "Return current subject string or nil if nothing."
2916   `(let ((headers
2917           ,(if number
2918                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2919              '(gnus-data-header (assq (gnus-summary-article-number)
2920                                       gnus-newsgroup-data)))))
2921      (and headers
2922           (vectorp headers)
2923           (mail-header-subject headers))))
2924
2925 (defmacro gnus-summary-article-score (&optional number)
2926   "Return current article score."
2927   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2928                   gnus-newsgroup-scored))
2929        gnus-summary-default-score 0))
2930
2931 (defun gnus-summary-article-children (&optional number)
2932   "Return a list of article numbers that are children of article NUMBER."
2933   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2934          (level (gnus-data-level (car data)))
2935          l children)
2936     (while (and (setq data (cdr data))
2937                 (> (setq l (gnus-data-level (car data))) level))
2938       (and (= (1+ level) l)
2939            (push (gnus-data-number (car data))
2940                  children)))
2941     (nreverse children)))
2942
2943 (defun gnus-summary-article-parent (&optional number)
2944   "Return the article number of the parent of article NUMBER."
2945   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2946                                     (gnus-data-list t)))
2947          (level (gnus-data-level (car data))))
2948     (if (zerop level)
2949         ()                              ; This is a root.
2950       ;; We search until we find an article with a level less than
2951       ;; this one.  That function has to be the parent.
2952       (while (and (setq data (cdr data))
2953                   (not (< (gnus-data-level (car data)) level))))
2954       (and data (gnus-data-number (car data))))))
2955
2956 (defun gnus-unread-mark-p (mark)
2957   "Say whether MARK is the unread mark."
2958   (= mark gnus-unread-mark))
2959
2960 (defun gnus-read-mark-p (mark)
2961   "Say whether MARK is one of the marks that mark as read.
2962 This is all marks except unread, ticked, dormant, and expirable."
2963   (not (or (= mark gnus-unread-mark)
2964            (= mark gnus-ticked-mark)
2965            (= mark gnus-spam-mark)
2966            (= mark gnus-dormant-mark)
2967            (= mark gnus-expirable-mark))))
2968
2969 (defmacro gnus-article-mark (number)
2970   "Return the MARK of article NUMBER.
2971 This macro should only be used when computing the mark the \"first\"
2972 time; i.e., when generating the summary lines.  After that,
2973 `gnus-summary-article-mark' should be used to examine the
2974 marks of articles."
2975   `(cond
2976     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2977     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2978     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2979     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2980     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2981     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2982     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2983     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2984            gnus-ancient-mark))))
2985
2986 ;; Saving hidden threads.
2987
2988 (defmacro gnus-save-hidden-threads (&rest forms)
2989   "Save hidden threads, eval FORMS, and restore the hidden threads."
2990   (let ((config (make-symbol "config")))
2991     `(let ((,config (gnus-hidden-threads-configuration)))
2992        (unwind-protect
2993            (save-excursion
2994              ,@forms)
2995          (gnus-restore-hidden-threads-configuration ,config)))))
2996 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2997 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2998
2999 (defun gnus-data-compute-positions ()
3000   "Compute the positions of all articles."
3001   (setq gnus-newsgroup-data-reverse nil)
3002   (let ((data gnus-newsgroup-data))
3003     (save-excursion
3004       (gnus-save-hidden-threads
3005         (gnus-summary-show-all-threads)
3006         (goto-char (point-min))
3007         (while data
3008           (while (get-text-property (point) 'gnus-intangible)
3009             (forward-line 1))
3010           (gnus-data-set-pos (car data) (+ (point) 3))
3011           (setq data (cdr data))
3012           (forward-line 1))))))
3013
3014 (defun gnus-hidden-threads-configuration ()
3015   "Return the current hidden threads configuration."
3016   (save-excursion
3017     (let (config)
3018       (goto-char (point-min))
3019       (while (search-forward "\r" nil t)
3020         (push (1- (point)) config))
3021       config)))
3022
3023 (defun gnus-restore-hidden-threads-configuration (config)
3024   "Restore hidden threads configuration from CONFIG."
3025   (save-excursion
3026     (let (point buffer-read-only)
3027       (while (setq point (pop config))
3028         (when (and (< point (point-max))
3029                    (goto-char point)
3030                    (eq (char-after) ?\n))
3031           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3032
3033 ;; Various summary mode internalish functions.
3034
3035 (defun gnus-mouse-pick-article (e)
3036   (interactive "e")
3037   (mouse-set-point e)
3038   (gnus-summary-next-page nil t))
3039
3040 (defun gnus-summary-set-display-table ()
3041   "Change the display table.
3042 Odd characters have a tendency to mess
3043 up nicely formatted displays - we make all possible glyphs
3044 display only a single character."
3045
3046   ;; We start from the standard display table, if any.
3047   (let ((table (or (copy-sequence standard-display-table)
3048                    (make-display-table)))
3049         (i 32))
3050     ;; Nix out all the control chars...
3051     (while (>= (setq i (1- i)) 0)
3052       (aset table i [??]))
3053     ;; ... but not newline and cr, of course.  (cr is necessary for the
3054     ;; selective display).
3055     (aset table ?\n nil)
3056     (aset table ?\r nil)
3057     ;; We keep TAB as well.
3058     (aset table ?\t nil)
3059     ;; We nix out any glyphs over 126 that are not set already.
3060     (let ((i 256))
3061       (while (>= (setq i (1- i)) 127)
3062         ;; Only modify if the entry is nil.
3063         (unless (aref table i)
3064           (aset table i [??]))))
3065     (setq buffer-display-table table)))
3066
3067 (defun gnus-summary-set-article-display-arrow (pos)
3068   "Update the overlay arrow to point to line at position POS."
3069   (when (and gnus-summary-display-arrow
3070              (boundp 'overlay-arrow-position)
3071              (boundp 'overlay-arrow-string))
3072     (save-excursion
3073       (goto-char pos)
3074       (beginning-of-line)
3075       (unless overlay-arrow-position
3076         (setq overlay-arrow-position (make-marker)))
3077       (setq overlay-arrow-string "=>"
3078             overlay-arrow-position (set-marker overlay-arrow-position
3079                                                (point)
3080                                                (current-buffer))))))
3081
3082 (defun gnus-summary-setup-buffer (group)
3083   "Initialize summary buffer."
3084   (let ((buffer (gnus-summary-buffer-name group))
3085         (dead-name (concat "*Dead Summary "
3086                            (gnus-group-decoded-name group) "*")))
3087     ;; If a dead summary buffer exists, we kill it.
3088     (when (gnus-buffer-live-p dead-name)
3089       (gnus-kill-buffer dead-name))
3090     (if (get-buffer buffer)
3091         (progn
3092           (set-buffer buffer)
3093           (setq gnus-summary-buffer (current-buffer))
3094           (not gnus-newsgroup-prepared))
3095       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3096       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3097       (gnus-summary-mode group)
3098       (when gnus-carpal
3099         (gnus-carpal-setup-buffer 'summary))
3100       (unless gnus-single-article-buffer
3101         (make-local-variable 'gnus-article-buffer)
3102         (make-local-variable 'gnus-article-current)
3103         (make-local-variable 'gnus-original-article-buffer))
3104       (setq gnus-newsgroup-name group)
3105       ;; Set any local variables in the group parameters.
3106       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3107       t)))
3108
3109 (defun gnus-set-global-variables ()
3110   "Set the global equivalents of the buffer-local variables.
3111 They are set to the latest values they had.  These reflect the summary
3112 buffer that was in action when the last article was fetched."
3113   (when (eq major-mode 'gnus-summary-mode)
3114     (setq gnus-summary-buffer (current-buffer))
3115     (let ((name gnus-newsgroup-name)
3116           (marked gnus-newsgroup-marked)
3117           (spam gnus-newsgroup-spam-marked)
3118           (unread gnus-newsgroup-unreads)
3119           (headers gnus-current-headers)
3120           (data gnus-newsgroup-data)
3121           (summary gnus-summary-buffer)
3122           (article-buffer gnus-article-buffer)
3123           (original gnus-original-article-buffer)
3124           (gac gnus-article-current)
3125           (reffed gnus-reffed-article-number)
3126           (score-file gnus-current-score-file)
3127           (default-charset gnus-newsgroup-charset)
3128           vlist)
3129       (let ((locals gnus-newsgroup-variables))
3130         (while locals
3131           (if (consp (car locals))
3132               (push (eval (caar locals)) vlist)
3133             (push (eval (car locals)) vlist))
3134           (setq locals (cdr locals)))
3135         (setq vlist (nreverse vlist)))
3136       (save-excursion
3137         (set-buffer gnus-group-buffer)
3138         (setq gnus-newsgroup-name name
3139               gnus-newsgroup-marked marked
3140               gnus-newsgroup-spam-marked spam
3141               gnus-newsgroup-unreads unread
3142               gnus-current-headers headers
3143               gnus-newsgroup-data data
3144               gnus-article-current gac
3145               gnus-summary-buffer summary
3146               gnus-article-buffer article-buffer
3147               gnus-original-article-buffer original
3148               gnus-reffed-article-number reffed
3149               gnus-current-score-file score-file
3150               gnus-newsgroup-charset default-charset)
3151         (let ((locals gnus-newsgroup-variables))
3152           (while locals
3153             (if (consp (car locals))
3154                 (set (caar locals) (pop vlist))
3155               (set (car locals) (pop vlist)))
3156             (setq locals (cdr locals))))
3157         ;; The article buffer also has local variables.
3158         (when (gnus-buffer-live-p gnus-article-buffer)
3159           (set-buffer gnus-article-buffer)
3160           (setq gnus-summary-buffer summary))))))
3161
3162 (defun gnus-summary-article-unread-p (article)
3163   "Say whether ARTICLE is unread or not."
3164   (memq article gnus-newsgroup-unreads))
3165
3166 (defun gnus-summary-first-article-p (&optional article)
3167   "Return whether ARTICLE is the first article in the buffer."
3168   (if (not (setq article (or article (gnus-summary-article-number))))
3169       nil
3170     (eq article (caar gnus-newsgroup-data))))
3171
3172 (defun gnus-summary-last-article-p (&optional article)
3173   "Return whether ARTICLE is the last article in the buffer."
3174   (if (not (setq article (or article (gnus-summary-article-number))))
3175       ;; All non-existent numbers are the last article.  :-)
3176       t
3177     (not (cdr (gnus-data-find-list article)))))
3178
3179 (defun gnus-make-thread-indent-array ()
3180   (let ((n 200))
3181     (unless (and gnus-thread-indent-array
3182                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3183       (setq gnus-thread-indent-array (make-vector 201 "")
3184             gnus-thread-indent-array-level gnus-thread-indent-level)
3185       (while (>= n 0)
3186         (aset gnus-thread-indent-array n
3187               (make-string (* n gnus-thread-indent-level) ? ))
3188         (setq n (1- n))))))
3189
3190 (defun gnus-update-summary-mark-positions ()
3191   "Compute where the summary marks are to go."
3192   (save-excursion
3193     (when (gnus-buffer-exists-p gnus-summary-buffer)
3194       (set-buffer gnus-summary-buffer))
3195     (let ((gnus-replied-mark 129)
3196           (gnus-score-below-mark 130)
3197           (gnus-score-over-mark 130)
3198           (gnus-undownloaded-mark 131)
3199           (spec gnus-summary-line-format-spec)
3200           gnus-visual pos)
3201       (save-excursion
3202         (gnus-set-work-buffer)
3203         (let ((gnus-summary-line-format-spec spec)
3204               (gnus-newsgroup-downloadable '(0)))
3205           (gnus-summary-insert-line
3206            (make-full-mail-header 0 "" "nobody"
3207                                   "05 Apr 2001 23:33:09 +0400"
3208                                   "" "" 0 0 "" nil)
3209            0 nil t 128 t nil "" nil 1)
3210           (goto-char (point-min))
3211           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3212                                              (- (point) (point-min) 1)))))
3213           (goto-char (point-min))
3214           (push (cons 'replied (and (search-forward "\201" nil t)
3215                                     (- (point) (point-min) 1)))
3216                 pos)
3217           (goto-char (point-min))
3218           (push (cons 'score (and (search-forward "\202" nil t)
3219                                   (- (point) (point-min) 1)))
3220                 pos)
3221           (goto-char (point-min))
3222           (push (cons 'download
3223                       (and (search-forward "\203" nil t)
3224                            (- (point) (point-min) 1)))
3225                 pos)))
3226       (setq gnus-summary-mark-positions pos))))
3227
3228 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3229   "Insert a dummy root in the summary buffer."
3230   (beginning-of-line)
3231   (gnus-add-text-properties
3232    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3233    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3234
3235 (defun gnus-summary-extract-address-component (from)
3236   (or (car (funcall gnus-extract-address-components from))
3237       from))
3238
3239 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3240   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3241                                 default-mime-charset)))
3242     ;; Is it really necessary to do this next part for each summary line?
3243     ;; Luckily, doesn't seem to slow things down much.
3244     (or
3245      (and gnus-ignored-from-addresses
3246           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3247           (let ((extra-headers (mail-header-extra header))
3248                 to
3249                 newsgroups)
3250             (cond
3251              ((setq to (cdr (assq 'To extra-headers)))
3252               (concat "-> "
3253                       (inline
3254                         (gnus-summary-extract-address-component
3255                          (funcall gnus-decode-encoded-word-function to)))))
3256              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3257               (concat "=> " newsgroups)))))
3258      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3259
3260 (defun gnus-summary-insert-line (gnus-tmp-header
3261                                  gnus-tmp-level gnus-tmp-current
3262                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3263                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3264                                  &optional gnus-tmp-dummy gnus-tmp-score
3265                                  gnus-tmp-process)
3266   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3267          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3268          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3269          (gnus-tmp-score-char
3270           (if (or (null gnus-summary-default-score)
3271                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3272                       gnus-summary-zcore-fuzz))
3273               ?\ ;;;Whitespace
3274             (if (< gnus-tmp-score gnus-summary-default-score)
3275                 gnus-score-below-mark gnus-score-over-mark)))
3276          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3277          (gnus-tmp-replied
3278           (cond (gnus-tmp-process gnus-process-mark)
3279                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3280                  gnus-cached-mark)
3281                 (gnus-tmp-replied gnus-replied-mark)
3282                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3283                  gnus-forwarded-mark)
3284                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3285                  gnus-saved-mark)
3286                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3287                  gnus-recent-mark)
3288                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3289                  gnus-unseen-mark)
3290                 (t gnus-no-mark)))
3291          (gnus-tmp-downloaded
3292           (cond (undownloaded
3293                  gnus-undownloaded-mark)
3294                 (gnus-newsgroup-agentized
3295                  gnus-downloaded-mark)
3296                 (t
3297                  gnus-no-mark)))
3298          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3299          (gnus-tmp-name
3300           (cond
3301            ((string-match "<[^>]+> *$" gnus-tmp-from)
3302             (let ((beg (match-beginning 0)))
3303               (or (and (string-match "^\".+\"" gnus-tmp-from)
3304                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3305                   (substring gnus-tmp-from 0 beg))))
3306            ((string-match "(.+)" gnus-tmp-from)
3307             (substring gnus-tmp-from
3308                        (1+ (match-beginning 0)) (1- (match-end 0))))
3309            (t gnus-tmp-from)))
3310          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3311          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3312          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3313          (buffer-read-only nil))
3314     (when (string= gnus-tmp-name "")
3315       (setq gnus-tmp-name gnus-tmp-from))
3316     (unless (numberp gnus-tmp-lines)
3317       (setq gnus-tmp-lines -1))
3318     (if (= gnus-tmp-lines -1)
3319         (setq gnus-tmp-lines "?")
3320       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3321     (gnus-put-text-property-excluding-characters-with-faces
3322      (point)
3323      (progn (eval gnus-summary-line-format-spec) (point))
3324      'gnus-number gnus-tmp-number)
3325     (when (gnus-visual-p 'summary-highlight 'highlight)
3326       (forward-line -1)
3327       (gnus-run-hooks 'gnus-summary-update-hook)
3328       (forward-line 1))))
3329
3330 (defun gnus-summary-update-line (&optional dont-update)
3331   "Update summary line after change."
3332   (when (and gnus-summary-default-score
3333              (not gnus-summary-inhibit-highlight))
3334     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3335            (article (gnus-summary-article-number))
3336            (score (gnus-summary-article-score article)))
3337       (unless dont-update
3338         (if (and gnus-summary-mark-below
3339                  (< (gnus-summary-article-score)
3340                     gnus-summary-mark-below))
3341             ;; This article has a low score, so we mark it as read.
3342             (when (memq article gnus-newsgroup-unreads)
3343               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3344           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3345             ;; This article was previously marked as read on account
3346             ;; of a low score, but now it has risen, so we mark it as
3347             ;; unread.
3348             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3349         (gnus-summary-update-mark
3350          (if (or (null gnus-summary-default-score)
3351                  (<= (abs (- score gnus-summary-default-score))
3352                      gnus-summary-zcore-fuzz))
3353              ?\ ;;;Whitespace
3354            (if (< score gnus-summary-default-score)
3355                gnus-score-below-mark gnus-score-over-mark))
3356          'score))
3357       ;; Do visual highlighting.
3358       (when (gnus-visual-p 'summary-highlight 'highlight)
3359         (gnus-run-hooks 'gnus-summary-update-hook)))))
3360
3361 (defvar gnus-tmp-new-adopts nil)
3362
3363 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3364   "Return the number of articles in THREAD.
3365 This may be 0 in some cases -- if none of the articles in
3366 the thread are to be displayed."
3367   (let* ((number
3368           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3369           (cond
3370            ((not (listp thread))
3371             1)
3372            ((and (consp thread) (cdr thread))
3373             (apply
3374              '+ 1 (mapcar
3375                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3376            ((null thread)
3377             1)
3378            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3379             1)
3380            (t 0))))
3381     (when (and level (zerop level) gnus-tmp-new-adopts)
3382       (incf number
3383             (apply '+ (mapcar
3384                        'gnus-summary-number-of-articles-in-thread
3385                        gnus-tmp-new-adopts))))
3386     (if char
3387         (if (> number 1) gnus-not-empty-thread-mark
3388           gnus-empty-thread-mark)
3389       number)))
3390
3391 (defsubst gnus-summary-line-message-size (head)
3392   "Return pretty-printed version of message size.
3393 This function is intended to be used in
3394 `gnus-summary-line-format-alist'."
3395   (let ((c (or (mail-header-chars head) -1)))
3396     (cond ((< c 0) "n/a")               ; chars not available
3397           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3398           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3399           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3400           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3401
3402
3403 (defun gnus-summary-set-local-parameters (group)
3404   "Go through the local params of GROUP and set all variable specs in that list."
3405   (let ((params (gnus-group-find-parameter group))
3406         (vars '(quit-config))           ; Ignore quit-config.
3407         elem)
3408     (while params
3409       (setq elem (car params)
3410             params (cdr params))
3411       (and (consp elem)                 ; Has to be a cons.
3412            (consp (cdr elem))           ; The cdr has to be a list.
3413            (symbolp (car elem))         ; Has to be a symbol in there.
3414            (not (memq (car elem) vars))
3415            (ignore-errors               ; So we set it.
3416              (push (car elem) vars)
3417              (make-local-variable (car elem))
3418              (set (car elem) (eval (nth 1 elem))))))))
3419
3420 (defun gnus-summary-read-group (group &optional show-all no-article
3421                                       kill-buffer no-display backward
3422                                       select-articles)
3423   "Start reading news in newsgroup GROUP.
3424 If SHOW-ALL is non-nil, already read articles are also listed.
3425 If NO-ARTICLE is non-nil, no article is selected initially.
3426 If NO-DISPLAY, don't generate a summary buffer."
3427   (let (result)
3428     (while (and group
3429                 (null (setq result
3430                             (let ((gnus-auto-select-next nil))
3431                               (or (gnus-summary-read-group-1
3432                                    group show-all no-article
3433                                    kill-buffer no-display
3434                                    select-articles)
3435                                   (setq show-all nil
3436                                         select-articles nil)))))
3437                 (eq gnus-auto-select-next 'quietly))
3438       (set-buffer gnus-group-buffer)
3439       ;; The entry function called above goes to the next
3440       ;; group automatically, so we go two groups back
3441       ;; if we are searching for the previous group.
3442       (when backward
3443         (gnus-group-prev-unread-group 2))
3444       (if (not (equal group (gnus-group-group-name)))
3445           (setq group (gnus-group-group-name))
3446         (setq group nil)))
3447     result))
3448
3449 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3450   "Directly jump to the other GROUP from summary buffer.
3451 If SHOW-ALL is non-nil, already read articles are also listed."
3452   (interactive
3453    (if (eq gnus-summary-buffer (current-buffer))
3454        (list (completing-read
3455               "Group: " gnus-active-hashtb nil t
3456               (when (and gnus-newsgroup-name
3457                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3458                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3459               'gnus-group-history)
3460              current-prefix-arg)
3461      (error "%s must be invoked from a gnus summary buffer." this-command)))
3462   (unless (or (zerop (length group))
3463               (and gnus-newsgroup-name
3464                    (string-equal gnus-newsgroup-name group)))
3465     (gnus-summary-exit)
3466     (gnus-summary-read-group group show-all
3467                              gnus-dont-select-after-jump-to-other-group)))
3468
3469 (defun gnus-summary-read-group-1 (group show-all no-article
3470                                         kill-buffer no-display
3471                                         &optional select-articles)
3472   ;; Killed foreign groups can't be entered.
3473   ;;  (when (and (not (gnus-group-native-p group))
3474   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3475   ;;    (error "Dead non-native groups can't be entered"))
3476   (gnus-message 5 "Retrieving newsgroup: %s..."
3477                 (gnus-group-decoded-name group))
3478   (let* ((new-group (gnus-summary-setup-buffer group))
3479          (quit-config (gnus-group-quit-config group))
3480          (did-select (and new-group (gnus-select-newsgroup
3481                                      group show-all select-articles))))
3482     (cond
3483      ;; This summary buffer exists already, so we just select it.
3484      ((not new-group)
3485       (gnus-set-global-variables)
3486       (when kill-buffer
3487         (gnus-kill-or-deaden-summary kill-buffer))
3488       (gnus-configure-windows 'summary 'force)
3489       (gnus-set-mode-line 'summary)
3490       (gnus-summary-position-point)
3491       (message "")
3492       t)
3493      ;; We couldn't select this group.
3494      ((null did-select)
3495       (when (and (eq major-mode 'gnus-summary-mode)
3496                  (not (equal (current-buffer) kill-buffer)))
3497         (kill-buffer (current-buffer))
3498         (if (not quit-config)
3499             (progn
3500               ;; Update the info -- marks might need to be removed,
3501               ;; for instance.
3502               (gnus-summary-update-info)
3503               (set-buffer gnus-group-buffer)
3504               (gnus-group-jump-to-group group)
3505               (gnus-group-next-unread-group 1))
3506           (gnus-handle-ephemeral-exit quit-config)))
3507       (let ((grpinfo (gnus-get-info group)))
3508         (if (null (gnus-info-read grpinfo))
3509             (gnus-message 3 "Group %s contains no messages"
3510                           (gnus-group-decoded-name group))
3511           (gnus-message 3 "Can't select group")))
3512       nil)
3513      ;; The user did a `C-g' while prompting for number of articles,
3514      ;; so we exit this group.
3515      ((eq did-select 'quit)
3516       (and (eq major-mode 'gnus-summary-mode)
3517            (not (equal (current-buffer) kill-buffer))
3518            (kill-buffer (current-buffer)))
3519       (when kill-buffer
3520         (gnus-kill-or-deaden-summary kill-buffer))
3521       (if (not quit-config)
3522           (progn
3523             (set-buffer gnus-group-buffer)
3524             (gnus-group-jump-to-group group)
3525             (gnus-group-next-unread-group 1)
3526             (gnus-configure-windows 'group 'force))
3527         (gnus-handle-ephemeral-exit quit-config))
3528       ;; Finally signal the quit.
3529       (signal 'quit nil))
3530      ;; The group was successfully selected.
3531      (t
3532       (gnus-set-global-variables)
3533       ;; Save the active value in effect when the group was entered.
3534       (setq gnus-newsgroup-active
3535             (gnus-copy-sequence
3536              (gnus-active gnus-newsgroup-name)))
3537       ;; You can change the summary buffer in some way with this hook.
3538       (gnus-run-hooks 'gnus-select-group-hook)
3539       (gnus-update-format-specifications
3540        nil 'summary 'summary-mode 'summary-dummy)
3541       (gnus-update-summary-mark-positions)
3542       ;; Do score processing.
3543       (when gnus-use-scoring
3544         (gnus-possibly-score-headers))
3545       ;; Check whether to fill in the gaps in the threads.
3546       (when gnus-build-sparse-threads
3547         (gnus-build-sparse-threads))
3548       ;; Find the initial limit.
3549       (if gnus-show-threads
3550           (if show-all
3551               (let ((gnus-newsgroup-dormant nil))
3552                 (gnus-summary-initial-limit show-all))
3553             (gnus-summary-initial-limit show-all))
3554         ;; When unthreaded, all articles are always shown.
3555         (setq gnus-newsgroup-limit
3556               (mapcar
3557                (lambda (header) (mail-header-number header))
3558                gnus-newsgroup-headers)))
3559       ;; Generate the summary buffer.
3560       (unless no-display
3561         (gnus-summary-prepare))
3562       (when gnus-use-trees
3563         (gnus-tree-open group)
3564         (setq gnus-summary-highlight-line-function
3565               'gnus-tree-highlight-article))
3566       ;; If the summary buffer is empty, but there are some low-scored
3567       ;; articles or some excluded dormants, we include these in the
3568       ;; buffer.
3569       (when (and (zerop (buffer-size))
3570                  (not no-display))
3571         (cond (gnus-newsgroup-dormant
3572                (gnus-summary-limit-include-dormant))
3573               ((and gnus-newsgroup-scored show-all)
3574                (gnus-summary-limit-include-expunged t))))
3575       ;; Function `gnus-apply-kill-file' must be called in this hook.
3576       (gnus-run-hooks 'gnus-apply-kill-hook)
3577       (if (and (zerop (buffer-size))
3578                (not no-display))
3579           (progn
3580             ;; This newsgroup is empty.
3581             (gnus-summary-catchup-and-exit nil t)
3582             (gnus-message 6 "No unread news")
3583             (when kill-buffer
3584               (gnus-kill-or-deaden-summary kill-buffer))
3585             ;; Return nil from this function.
3586             nil)
3587         ;; Hide conversation thread subtrees.  We cannot do this in
3588         ;; gnus-summary-prepare-hook since kill processing may not
3589         ;; work with hidden articles.
3590         (gnus-summary-maybe-hide-threads)
3591         (when kill-buffer
3592           (gnus-kill-or-deaden-summary kill-buffer))
3593         (gnus-summary-auto-select-subject)
3594         ;; Show first unread article if requested.
3595         (if (and (not no-article)
3596                  (not no-display)
3597                  gnus-newsgroup-unreads
3598                  gnus-auto-select-first)
3599             (progn
3600               (gnus-configure-windows 'summary)
3601               (let ((art (gnus-summary-article-number)))
3602                 (unless (and (not gnus-plugged)
3603                              (or (memq art gnus-newsgroup-undownloaded)
3604                                  (memq art gnus-newsgroup-downloadable)))
3605                   (gnus-summary-goto-article art))))
3606           ;; Don't select any articles.
3607           (gnus-summary-position-point)
3608           (gnus-configure-windows 'summary 'force)
3609           (gnus-set-mode-line 'summary))
3610         (when (and gnus-auto-center-group
3611                    (get-buffer-window gnus-group-buffer t))
3612           ;; Gotta use windows, because recenter does weird stuff if
3613           ;; the current buffer ain't the displayed window.
3614           (let ((owin (selected-window)))
3615             (select-window (get-buffer-window gnus-group-buffer t))
3616             (when (gnus-group-goto-group group)
3617               (recenter))
3618             (select-window owin)))
3619         ;; Mark this buffer as "prepared".
3620         (setq gnus-newsgroup-prepared t)
3621         (gnus-run-hooks 'gnus-summary-prepared-hook)
3622         (unless (gnus-ephemeral-group-p group)
3623           (gnus-group-update-group group))
3624         t)))))
3625
3626 (defun gnus-summary-auto-select-subject ()
3627   "Select the subject line on initial group entry."
3628   (goto-char (point-min))
3629   (cond
3630    ((eq gnus-auto-select-subject 'best)
3631     (gnus-summary-best-unread-subject))
3632    ((eq gnus-auto-select-subject 'unread)
3633     (gnus-summary-first-unread-subject))
3634    ((eq gnus-auto-select-subject 'unseen)
3635     (gnus-summary-first-unseen-subject))
3636    ((eq gnus-auto-select-subject 'unseen-or-unread)
3637     (gnus-summary-first-unseen-or-unread-subject))
3638    ((eq gnus-auto-select-subject 'first)
3639     ;; Do nothing.
3640     )
3641    ((functionp gnus-auto-select-subject)
3642     (funcall gnus-auto-select-subject))))
3643
3644 (defun gnus-summary-prepare ()
3645   "Generate the summary buffer."
3646   (interactive)
3647   (let ((buffer-read-only nil))
3648     (erase-buffer)
3649     (setq gnus-newsgroup-data nil
3650           gnus-newsgroup-data-reverse nil)
3651     (gnus-run-hooks 'gnus-summary-generate-hook)
3652     ;; Generate the buffer, either with threads or without.
3653     (when gnus-newsgroup-headers
3654       (gnus-summary-prepare-threads
3655        (if gnus-show-threads
3656            (gnus-sort-gathered-threads
3657             (funcall gnus-summary-thread-gathering-function
3658                      (gnus-sort-threads
3659                       (gnus-cut-threads (gnus-make-threads)))))
3660          ;; Unthreaded display.
3661          (gnus-sort-articles gnus-newsgroup-headers))))
3662     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3663     ;; Call hooks for modifying summary buffer.
3664     (goto-char (point-min))
3665     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3666
3667 (defsubst gnus-general-simplify-subject (subject)
3668   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3669   (setq subject
3670         (cond
3671          ;; Truncate the subject.
3672          (gnus-simplify-subject-functions
3673           (gnus-map-function gnus-simplify-subject-functions subject))
3674          ((numberp gnus-summary-gather-subject-limit)
3675           (setq subject (gnus-simplify-subject-re subject))
3676           (if (> (length subject) gnus-summary-gather-subject-limit)
3677               (substring subject 0 gnus-summary-gather-subject-limit)
3678             subject))
3679          ;; Fuzzily simplify it.
3680          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3681           (gnus-simplify-subject-fuzzy subject))
3682          ;; Just remove the leading "Re:".
3683          (t
3684           (gnus-simplify-subject-re subject))))
3685
3686   (if (and gnus-summary-gather-exclude-subject
3687            (string-match gnus-summary-gather-exclude-subject subject))
3688       nil                               ; This article shouldn't be gathered
3689     subject))
3690
3691 (defun gnus-summary-simplify-subject-query ()
3692   "Query where the respool algorithm would put this article."
3693   (interactive)
3694   (gnus-summary-select-article)
3695   (message "%s"
3696            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3697
3698 (defun gnus-gather-threads-by-subject (threads)
3699   "Gather threads by looking at Subject headers."
3700   (if (not gnus-summary-make-false-root)
3701       threads
3702     (let ((hashtb (gnus-make-hashtable 1024))
3703           (prev threads)
3704           (result threads)
3705           subject hthread whole-subject)
3706       (while threads
3707         (setq subject (gnus-general-simplify-subject
3708                        (setq whole-subject (mail-header-subject
3709                                             (caar threads)))))
3710         (when subject
3711           (if (setq hthread (gnus-gethash subject hashtb))
3712               (progn
3713                 ;; We enter a dummy root into the thread, if we
3714                 ;; haven't done that already.
3715                 (unless (stringp (caar hthread))
3716                   (setcar hthread (list whole-subject (car hthread))))
3717                 ;; We add this new gathered thread to this gathered
3718                 ;; thread.
3719                 (setcdr (car hthread)
3720                         (nconc (cdar hthread) (list (car threads))))
3721                 ;; Remove it from the list of threads.
3722                 (setcdr prev (cdr threads))
3723                 (setq threads prev))
3724             ;; Enter this thread into the hash table.
3725             (gnus-sethash subject
3726                           (if gnus-summary-make-false-root-always
3727                               (progn
3728                                 ;; If you want a dummy root above all
3729                                 ;; threads...
3730                                 (setcar threads (list whole-subject
3731                                                       (car threads)))
3732                                 threads)
3733                             threads)
3734                           hashtb)))
3735         (setq prev threads)
3736         (setq threads (cdr threads)))
3737       result)))
3738
3739 (defun gnus-gather-threads-by-references (threads)
3740   "Gather threads by looking at References headers."
3741   (let ((idhashtb (gnus-make-hashtable 1024))
3742         (thhashtb (gnus-make-hashtable 1024))
3743         (prev threads)
3744         (result threads)
3745         ids references id gthread gid entered ref)
3746     (while threads
3747       (when (setq references (mail-header-references (caar threads)))
3748         (setq id (mail-header-id (caar threads))
3749               ids (inline (gnus-split-references references))
3750               entered nil)
3751         (while (setq ref (pop ids))
3752           (setq ids (delete ref ids))
3753           (if (not (setq gid (gnus-gethash ref idhashtb)))
3754               (progn
3755                 (gnus-sethash ref id idhashtb)
3756                 (gnus-sethash id threads thhashtb))
3757             (setq gthread (gnus-gethash gid thhashtb))
3758             (unless entered
3759               ;; We enter a dummy root into the thread, if we
3760               ;; haven't done that already.
3761               (unless (stringp (caar gthread))
3762                 (setcar gthread (list (mail-header-subject (caar gthread))
3763                                       (car gthread))))
3764               ;; We add this new gathered thread to this gathered
3765               ;; thread.
3766               (setcdr (car gthread)
3767                       (nconc (cdar gthread) (list (car threads)))))
3768             ;; Add it into the thread hash table.
3769             (gnus-sethash id gthread thhashtb)
3770             (setq entered t)
3771             ;; Remove it from the list of threads.
3772             (setcdr prev (cdr threads))
3773             (setq threads prev))))
3774       (setq prev threads)
3775       (setq threads (cdr threads)))
3776     result))
3777
3778 (defun gnus-sort-gathered-threads (threads)
3779   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3780   (let ((result threads))
3781     (while threads
3782       (when (stringp (caar threads))
3783         (setcdr (car threads)
3784                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3785       (setq threads (cdr threads)))
3786     result))
3787
3788 (defun gnus-thread-loop-p (root thread)
3789   "Say whether ROOT is in THREAD."
3790   (let ((stack (list thread))
3791         (infloop 0)
3792         th)
3793     (while (setq thread (pop stack))
3794       (setq th (cdr thread))
3795       (while (and th
3796                   (not (eq (caar th) root)))
3797         (pop th))
3798       (if th
3799           ;; We have found a loop.
3800           (let (ref-dep)
3801             (setcdr thread (delq (car th) (cdr thread)))
3802             (if (boundp (setq ref-dep (intern "none"
3803                                               gnus-newsgroup-dependencies)))
3804                 (setcdr (symbol-value ref-dep)
3805                         (nconc (cdr (symbol-value ref-dep))
3806                                (list (car th))))
3807               (set ref-dep (list nil (car th))))
3808             (setq infloop 1
3809                   stack nil))
3810         ;; Push all the subthreads onto the stack.
3811         (push (cdr thread) stack)))
3812     infloop))
3813
3814 (defun gnus-make-threads ()
3815   "Go through the dependency hashtb and find the roots.  Return all threads."
3816   (let (threads)
3817     (while (catch 'infloop
3818              (mapatoms
3819               (lambda (refs)
3820                 ;; Deal with self-referencing References loops.
3821                 (when (and (car (symbol-value refs))
3822                            (not (zerop
3823                                  (apply
3824                                   '+
3825                                   (mapcar
3826                                    (lambda (thread)
3827                                      (gnus-thread-loop-p
3828                                       (car (symbol-value refs)) thread))
3829                                    (cdr (symbol-value refs)))))))
3830                   (setq threads nil)
3831                   (throw 'infloop t))
3832                 (unless (car (symbol-value refs))
3833                   ;; These threads do not refer back to any other articles,
3834                   ;; so they're roots.
3835                   (setq threads (append (cdr (symbol-value refs)) threads))))
3836               gnus-newsgroup-dependencies)))
3837     threads))
3838
3839 ;; Build the thread tree.
3840 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3841   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3842
3843 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3844 if it was already present.
3845
3846 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3847 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3848 Message-IDs will be renamed to a unique Message-ID before being
3849 entered.
3850
3851 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3852   (let* ((id (mail-header-id header))
3853          (id-dep (and id (intern id dependencies)))
3854          parent-id ref ref-dep ref-header replaced)
3855     ;; Enter this `header' in the `dependencies' table.
3856     (cond
3857      ((not id-dep)
3858       (setq header nil))
3859      ;; The first two cases do the normal part: enter a new `header'
3860      ;; in the `dependencies' table.
3861      ((not (boundp id-dep))
3862       (set id-dep (list header)))
3863      ((null (car (symbol-value id-dep)))
3864       (setcar (symbol-value id-dep) header))
3865
3866      ;; From here the `header' was already present in the
3867      ;; `dependencies' table.
3868      (force-new
3869       ;; Overrides an existing entry;
3870       ;; just set the header part of the entry.
3871       (setcar (symbol-value id-dep) header)
3872       (setq replaced t))
3873
3874      ;; Renames the existing `header' to a unique Message-ID.
3875      ((not gnus-summary-ignore-duplicates)
3876       ;; An article with this Message-ID has already been seen.
3877       ;; We rename the Message-ID.
3878       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3879            (list header))
3880       (mail-header-set-id header id))
3881
3882      ;; The last case ignores an existing entry, except it adds any
3883      ;; additional Xrefs (in case the two articles came from different
3884      ;; servers.
3885      ;; Also sets `header' to `nil' meaning that the `dependencies'
3886      ;; table was *not* modified.
3887      (t
3888       (mail-header-set-xref
3889        (car (symbol-value id-dep))
3890        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3891                    "")
3892                (or (mail-header-xref header) "")))
3893       (setq header nil)))
3894
3895     (when (and header (not replaced))
3896       ;; First check that we are not creating a References loop.
3897       (setq parent-id (gnus-parent-id (mail-header-references header)))
3898       (setq ref parent-id)
3899       (while (and ref
3900                   (setq ref-dep (intern-soft ref dependencies))
3901                   (boundp ref-dep)
3902                   (setq ref-header (car (symbol-value ref-dep))))
3903         (if (string= id ref)
3904             ;; Yuk!  This is a reference loop.  Make the article be a
3905             ;; root article.
3906             (progn
3907               (mail-header-set-references (car (symbol-value id-dep)) "none")
3908               (setq ref nil)
3909               (setq parent-id nil))
3910           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3911       (setq ref-dep (intern (or parent-id "none") dependencies))
3912       (if (boundp ref-dep)
3913           (setcdr (symbol-value ref-dep)
3914                   (nconc (cdr (symbol-value ref-dep))
3915                          (list (symbol-value id-dep))))
3916         (set ref-dep (list nil (symbol-value id-dep)))))
3917     header))
3918
3919 (defun gnus-extract-message-id-from-in-reply-to (string)
3920   (if (string-match "<[^>]+>" string)
3921       (substring string (match-beginning 0) (match-end 0))
3922     nil))
3923
3924 (defun gnus-build-sparse-threads ()
3925   (let ((headers gnus-newsgroup-headers)
3926         (mail-parse-charset gnus-newsgroup-charset)
3927         (gnus-summary-ignore-duplicates t)
3928         header references generation relations
3929         subject child end new-child date)
3930     ;; First we create an alist of generations/relations, where
3931     ;; generations is how much we trust the relation, and the relation
3932     ;; is parent/child.
3933     (gnus-message 7 "Making sparse threads...")
3934     (save-excursion
3935       (nnheader-set-temp-buffer " *gnus sparse threads*")
3936       (while (setq header (pop headers))
3937         (when (and (setq references (mail-header-references header))
3938                    (not (string= references "")))
3939           (insert references)
3940           (setq child (mail-header-id header)
3941                 subject (mail-header-subject header)
3942                 date (mail-header-date header)
3943                 generation 0)
3944           (while (search-backward ">" nil t)
3945             (setq end (1+ (point)))
3946             (when (search-backward "<" nil t)
3947               (setq new-child (buffer-substring (point) end))
3948               (push (list (incf generation)
3949                           child (setq child new-child)
3950                           subject date)
3951                     relations)))
3952           (when child
3953             (push (list (1+ generation) child nil subject) relations))
3954           (erase-buffer)))
3955       (kill-buffer (current-buffer)))
3956     ;; Sort over trustworthiness.
3957     (mapcar
3958      (lambda (relation)
3959        (when (gnus-dependencies-add-header
3960               (make-full-mail-header-from-decoded-header
3961                gnus-reffed-article-number
3962                (nth 3 relation) "" (or (nth 4 relation) "")
3963                (nth 1 relation)
3964                (or (nth 2 relation) "") 0 0 "")
3965               gnus-newsgroup-dependencies nil)
3966          (push gnus-reffed-article-number gnus-newsgroup-limit)
3967          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3968          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3969                gnus-newsgroup-reads)
3970          (decf gnus-reffed-article-number)))
3971      (sort relations 'car-less-than-car))
3972     (gnus-message 7 "Making sparse threads...done")))
3973
3974 (defun gnus-build-old-threads ()
3975   ;; Look at all the articles that refer back to old articles, and
3976   ;; fetch the headers for the articles that aren't there.  This will
3977   ;; build complete threads - if the roots haven't been expired by the
3978   ;; server, that is.
3979   (let ((mail-parse-charset gnus-newsgroup-charset)
3980         id heads)
3981     (mapatoms
3982      (lambda (refs)
3983        (when (not (car (symbol-value refs)))
3984          (setq heads (cdr (symbol-value refs)))
3985          (while heads
3986            (if (memq (mail-header-number (caar heads))
3987                      gnus-newsgroup-dormant)
3988                (setq heads (cdr heads))
3989              (setq id (symbol-name refs))
3990              (while (and (setq id (gnus-build-get-header id))
3991                          (not (car (gnus-id-to-thread id)))))
3992              (setq heads nil)))))
3993      gnus-newsgroup-dependencies)))
3994
3995 (defsubst gnus-remove-odd-characters (string)
3996   "Translate STRING into something that doesn't contain weird characters."
3997   (mm-subst-char-in-string
3998    ?\r ?\-
3999    (mm-subst-char-in-string ?\n ?\- string t) t))
4000
4001 ;; This function has to be called with point after the article number
4002 ;; on the beginning of the line.
4003 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
4004   (let ((eol (point-at-eol))
4005         (buffer (current-buffer))
4006         header references in-reply-to)
4007
4008     ;; overview: [num subject from date id refs chars lines misc]
4009     (unwind-protect
4010         (progn
4011           (narrow-to-region (point) eol)
4012           (unless (eobp)
4013             (forward-char))
4014
4015           (setq header
4016                 (make-full-mail-header
4017                  number                         ; number
4018                  (nnheader-nov-field)           ; subject
4019                  (nnheader-nov-field)           ; from
4020                  (nnheader-nov-field)           ; date
4021                  (nnheader-nov-read-message-id) ; id
4022                  (nnheader-nov-field)           ; refs
4023                  (nnheader-nov-read-integer)    ; chars
4024                  (nnheader-nov-read-integer)    ; lines
4025                  (unless (eobp)
4026                    (if (looking-at "Xref: ")
4027                        (goto-char (match-end 0)))
4028                    (nnheader-nov-field))        ; Xref
4029                  (nnheader-nov-parse-extra))))  ; extra
4030
4031       (widen))
4032
4033     (when (and (string= references "")
4034                (setq in-reply-to (mail-header-extra header))
4035                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4036       (mail-header-set-references
4037        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4038
4039     (when gnus-alter-header-function
4040       (funcall gnus-alter-header-function header))
4041     (gnus-dependencies-add-header header dependencies force-new)))
4042
4043 (defun gnus-build-get-header (id)
4044   "Look through the buffer of NOV lines and find the header to ID.
4045 Enter this line into the dependencies hash table, and return
4046 the id of the parent article (if any)."
4047   (let ((deps gnus-newsgroup-dependencies)
4048         found header)
4049     (prog1
4050         (save-excursion
4051           (set-buffer nntp-server-buffer)
4052           (let ((case-fold-search nil))
4053             (goto-char (point-min))
4054             (while (and (not found)
4055                         (search-forward id nil t))
4056               (beginning-of-line)
4057               (setq found (looking-at
4058                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4059                                    (regexp-quote id))))
4060               (or found (beginning-of-line 2)))
4061             (when found
4062               (beginning-of-line)
4063               (and
4064                (setq header (gnus-nov-parse-line
4065                              (read (current-buffer)) deps))
4066                (gnus-parent-id (mail-header-references header))))))
4067       (when header
4068         (let ((number (mail-header-number header)))
4069           (push number gnus-newsgroup-limit)
4070           (push header gnus-newsgroup-headers)
4071           (if (memq number gnus-newsgroup-unselected)
4072               (progn
4073                 (setq gnus-newsgroup-unreads
4074                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4075                                                number))
4076                 (setq gnus-newsgroup-unselected
4077                       (delq number gnus-newsgroup-unselected)))
4078             (push number gnus-newsgroup-ancient)))))))
4079
4080 (defun gnus-build-all-threads ()
4081   "Read all the headers."
4082   (let ((gnus-summary-ignore-duplicates t)
4083         (mail-parse-charset gnus-newsgroup-charset)
4084         (dependencies gnus-newsgroup-dependencies)
4085         header article)
4086     (save-excursion
4087       (set-buffer nntp-server-buffer)
4088       (let ((case-fold-search nil))
4089         (goto-char (point-min))
4090         (while (not (eobp))
4091           (ignore-errors
4092             (setq article (read (current-buffer))
4093                   header (gnus-nov-parse-line article dependencies)))
4094           (when header
4095             (save-excursion
4096               (set-buffer gnus-summary-buffer)
4097               (push header gnus-newsgroup-headers)
4098               (if (memq (setq article (mail-header-number header))
4099                         gnus-newsgroup-unselected)
4100                   (progn
4101                     (setq gnus-newsgroup-unreads
4102                           (gnus-add-to-sorted-list
4103                            gnus-newsgroup-unreads article))
4104                     (setq gnus-newsgroup-unselected
4105                           (delq article gnus-newsgroup-unselected)))
4106                 (push article gnus-newsgroup-ancient)))
4107             (forward-line 1)))))))
4108
4109 (defun gnus-summary-update-article-line (article header)
4110   "Update the line for ARTICLE using HEADER."
4111   (let* ((id (mail-header-id header))
4112          (thread (gnus-id-to-thread id)))
4113     (unless thread
4114       (error "Article in no thread"))
4115     ;; Update the thread.
4116     (setcar thread header)
4117     (gnus-summary-goto-subject article)
4118     (let* ((datal (gnus-data-find-list article))
4119            (data (car datal))
4120            (buffer-read-only nil)
4121            (level (gnus-summary-thread-level)))
4122       (gnus-delete-line)
4123       (let ((inserted (- (point)
4124                          (progn
4125                            (gnus-summary-insert-line
4126                             header level nil
4127                             (memq article gnus-newsgroup-undownloaded)
4128                             (gnus-article-mark article)
4129                             (memq article gnus-newsgroup-replied)
4130                             (memq article gnus-newsgroup-expirable)
4131                             ;; Only insert the Subject string when it's different
4132                             ;; from the previous Subject string.
4133                             (if (and
4134                                  gnus-show-threads
4135                                  (gnus-subject-equal
4136                                   (condition-case ()
4137                                       (mail-header-subject
4138                                        (gnus-data-header
4139                                         (cadr
4140                                          (gnus-data-find-list
4141                                           article
4142                                           (gnus-data-list t)))))
4143                                     ;; Error on the side of excessive subjects.
4144                                     (error ""))
4145                                   (mail-header-subject header)))
4146                                 ""
4147                               (mail-header-subject header))
4148                             nil (cdr (assq article gnus-newsgroup-scored))
4149                             (memq article gnus-newsgroup-processable))
4150                            (point)))))
4151         (when (cdr datal)
4152           (gnus-data-update-list
4153            (cdr datal)
4154            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4155
4156 (defun gnus-summary-update-article (article &optional iheader)
4157   "Update ARTICLE in the summary buffer."
4158   (set-buffer gnus-summary-buffer)
4159   (let* ((header (gnus-summary-article-header article))
4160          (id (mail-header-id header))
4161          (data (gnus-data-find article))
4162          (thread (gnus-id-to-thread id))
4163          (references (mail-header-references header))
4164          (parent
4165           (gnus-id-to-thread
4166            (or (gnus-parent-id
4167                 (when (and references
4168                            (not (equal "" references)))
4169                   references))
4170                "none")))
4171          (buffer-read-only nil)
4172          (old (car thread)))
4173     (when thread
4174       (unless iheader
4175         (setcar thread nil)
4176         (when parent
4177           (delq thread parent)))
4178       (if (gnus-summary-insert-subject id header)
4179           ;; Set the (possibly) new article number in the data structure.
4180           (gnus-data-set-number data (gnus-id-to-article id))
4181         (setcar thread old)
4182         nil))))
4183
4184 (defun gnus-rebuild-thread (id &optional line)
4185   "Rebuild the thread containing ID.
4186 If LINE, insert the rebuilt thread starting on line LINE."
4187   (let ((buffer-read-only nil)
4188         old-pos current thread data)
4189     (if (not gnus-show-threads)
4190         (setq thread (list (car (gnus-id-to-thread id))))
4191       ;; Get the thread this article is part of.
4192       (setq thread (gnus-remove-thread id)))
4193     (setq old-pos (point-at-bol))
4194     (setq current (save-excursion
4195                     (and (re-search-backward "[\r\n]" nil t)
4196                          (gnus-summary-article-number))))
4197     ;; If this is a gathered thread, we have to go some re-gathering.
4198     (when (stringp (car thread))
4199       (let ((subject (car thread))
4200             roots thr)
4201         (setq thread (cdr thread))
4202         (while thread
4203           (unless (memq (setq thr (gnus-id-to-thread
4204                                    (gnus-root-id
4205                                     (mail-header-id (caar thread)))))
4206                         roots)
4207             (push thr roots))
4208           (setq thread (cdr thread)))
4209         ;; We now have all (unique) roots.
4210         (if (= (length roots) 1)
4211             ;; All the loose roots are now one solid root.
4212             (setq thread (car roots))
4213           (setq thread (cons subject (gnus-sort-threads roots))))))
4214     (let (threads)
4215       ;; We then insert this thread into the summary buffer.
4216       (when line
4217         (goto-char (point-min))
4218         (forward-line (1- line)))
4219       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4220         (if gnus-show-threads
4221             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4222           (gnus-summary-prepare-unthreaded thread))
4223         (setq data (nreverse gnus-newsgroup-data))
4224         (setq threads gnus-newsgroup-threads))
4225       ;; We splice the new data into the data structure.
4226       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4227       ;;!!! then we want to insert at the beginning of the buffer.
4228       ;;!!! That happens to be true with Gnus now, but that may
4229       ;;!!! change in the future.  Perhaps.
4230       (gnus-data-enter-list
4231        (if line nil current) data (- (point) old-pos))
4232       (setq gnus-newsgroup-threads
4233             (nconc threads gnus-newsgroup-threads))
4234       (gnus-data-compute-positions))))
4235
4236 (defun gnus-number-to-header (number)
4237   "Return the header for article NUMBER."
4238   (let ((headers gnus-newsgroup-headers))
4239     (while (and headers
4240                 (not (= number (mail-header-number (car headers)))))
4241       (pop headers))
4242     (when headers
4243       (car headers))))
4244
4245 (defun gnus-parent-headers (in-headers &optional generation)
4246   "Return the headers of the GENERATIONeth parent of HEADERS."
4247   (unless generation
4248     (setq generation 1))
4249   (let ((parent t)
4250         (headers in-headers)
4251         references)
4252     (while (and parent
4253                 (not (zerop generation))
4254                 (setq references (mail-header-references headers)))
4255       (setq headers (if (and references
4256                              (setq parent (gnus-parent-id references)))
4257                         (car (gnus-id-to-thread parent))
4258                       nil))
4259       (decf generation))
4260     (and (not (eq headers in-headers))
4261          headers)))
4262
4263 (defun gnus-id-to-thread (id)
4264   "Return the (sub-)thread where ID appears."
4265   (gnus-gethash id gnus-newsgroup-dependencies))
4266
4267 (defun gnus-id-to-article (id)
4268   "Return the article number of ID."
4269   (let ((thread (gnus-id-to-thread id)))
4270     (when (and thread
4271                (car thread))
4272       (mail-header-number (car thread)))))
4273
4274 (defun gnus-id-to-header (id)
4275   "Return the article headers of ID."
4276   (car (gnus-id-to-thread id)))
4277
4278 (defun gnus-article-displayed-root-p (article)
4279   "Say whether ARTICLE is a root(ish) article."
4280   (let ((level (gnus-summary-thread-level article))
4281         (refs (mail-header-references  (gnus-summary-article-header article)))
4282         particle)
4283     (cond
4284      ((null level) nil)
4285      ((zerop level) t)
4286      ((null refs) t)
4287      ((null (gnus-parent-id refs)) t)
4288      ((and (= 1 level)
4289            (null (setq particle (gnus-id-to-article
4290                                  (gnus-parent-id refs))))
4291            (null (gnus-summary-thread-level particle)))))))
4292
4293 (defun gnus-root-id (id)
4294   "Return the id of the root of the thread where ID appears."
4295   (let (last-id prev)
4296     (while (and id (setq prev (car (gnus-id-to-thread id))))
4297       (setq last-id id
4298             id (gnus-parent-id (mail-header-references prev))))
4299     last-id))
4300
4301 (defun gnus-articles-in-thread (thread)
4302   "Return the list of articles in THREAD."
4303   (cons (mail-header-number (car thread))
4304         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4305
4306 (defun gnus-remove-thread (id &optional dont-remove)
4307   "Remove the thread that has ID in it."
4308   (let (headers thread last-id)
4309     ;; First go up in this thread until we find the root.
4310     (setq last-id (gnus-root-id id)
4311           headers (message-flatten-list (gnus-id-to-thread last-id)))
4312     ;; We have now found the real root of this thread.  It might have
4313     ;; been gathered into some loose thread, so we have to search
4314     ;; through the threads to find the thread we wanted.
4315     (let ((threads gnus-newsgroup-threads)
4316           sub)
4317       (while threads
4318         (setq sub (car threads))
4319         (if (stringp (car sub))
4320             ;; This is a gathered thread, so we look at the roots
4321             ;; below it to find whether this article is in this
4322             ;; gathered root.
4323             (progn
4324               (setq sub (cdr sub))
4325               (while sub
4326                 (when (member (caar sub) headers)
4327                   (setq thread (car threads)
4328                         threads nil
4329                         sub nil))
4330                 (setq sub (cdr sub))))
4331           ;; It's an ordinary thread, so we check it.
4332           (when (eq (car sub) (car headers))
4333             (setq thread sub
4334                   threads nil)))
4335         (setq threads (cdr threads)))
4336       ;; If this article is in no thread, then it's a root.
4337       (if thread
4338           (unless dont-remove
4339             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4340         (setq thread (gnus-id-to-thread last-id)))
4341       (when thread
4342         (prog1
4343             thread                      ; We return this thread.
4344           (unless dont-remove
4345             (if (stringp (car thread))
4346                 (progn
4347                   ;; If we use dummy roots, then we have to remove the
4348                   ;; dummy root as well.
4349                   (when (eq gnus-summary-make-false-root 'dummy)
4350                     ;; We go to the dummy root by going to
4351                     ;; the first sub-"thread", and then one line up.
4352                     (gnus-summary-goto-article
4353                      (mail-header-number (caadr thread)))
4354                     (forward-line -1)
4355                     (gnus-delete-line)
4356                     (gnus-data-compute-positions))
4357                   (setq thread (cdr thread))
4358                   (while thread
4359                     (gnus-remove-thread-1 (car thread))
4360                     (setq thread (cdr thread))))
4361               (gnus-remove-thread-1 thread))))))))
4362
4363 (defun gnus-remove-thread-1 (thread)
4364   "Remove the thread THREAD recursively."
4365   (let ((number (mail-header-number (pop thread)))
4366         d)
4367     (setq thread (reverse thread))
4368     (while thread
4369       (gnus-remove-thread-1 (pop thread)))
4370     (when (setq d (gnus-data-find number))
4371       (goto-char (gnus-data-pos d))
4372       (gnus-summary-show-thread)
4373       (gnus-data-remove
4374        number
4375        (- (point-at-bol)
4376           (prog1
4377               (1+ (point-at-eol))
4378             (gnus-delete-line)))))))
4379
4380 (defun gnus-sort-threads-1 (threads func)
4381   (sort (mapcar (lambda (thread)
4382                   (cons (car thread)
4383                         (and (cdr thread)
4384                              (gnus-sort-threads-1 (cdr thread) func))))
4385                 threads) func))
4386
4387 (defun gnus-sort-threads (threads)
4388   "Sort THREADS."
4389   (if (not gnus-thread-sort-functions)
4390       threads
4391     (gnus-message 8 "Sorting threads...")
4392     (let ((max-lisp-eval-depth 5000))
4393       (prog1 (gnus-sort-threads-1
4394          threads
4395          (gnus-make-sort-function gnus-thread-sort-functions))
4396         (gnus-message 8 "Sorting threads...done")))))
4397
4398 (defun gnus-sort-articles (articles)
4399   "Sort ARTICLES."
4400   (when gnus-article-sort-functions
4401     (gnus-message 7 "Sorting articles...")
4402     (prog1
4403         (setq gnus-newsgroup-headers
4404               (sort articles (gnus-make-sort-function
4405                               gnus-article-sort-functions)))
4406       (gnus-message 7 "Sorting articles...done"))))
4407
4408 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4409 (defmacro gnus-thread-header (thread)
4410   "Return header of first article in THREAD.
4411 Note that THREAD must never, ever be anything else than a variable -
4412 using some other form will lead to serious barfage."
4413   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4414   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4415   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4416         (vector thread) 2))
4417
4418 (defsubst gnus-article-sort-by-number (h1 h2)
4419   "Sort articles by article number."
4420   (< (mail-header-number h1)
4421      (mail-header-number h2)))
4422
4423 (defun gnus-thread-sort-by-number (h1 h2)
4424   "Sort threads by root article number."
4425   (gnus-article-sort-by-number
4426    (gnus-thread-header h1) (gnus-thread-header h2)))
4427
4428 (defsubst gnus-article-sort-by-random (h1 h2)
4429   "Sort articles by article number."
4430   (zerop (random 2)))
4431
4432 (defun gnus-thread-sort-by-random (h1 h2)
4433   "Sort threads by root article number."
4434   (gnus-article-sort-by-random
4435    (gnus-thread-header h1) (gnus-thread-header h2)))
4436
4437 (defsubst gnus-article-sort-by-lines (h1 h2)
4438   "Sort articles by article Lines header."
4439   (< (mail-header-lines h1)
4440      (mail-header-lines h2)))
4441
4442 (defun gnus-thread-sort-by-lines (h1 h2)
4443   "Sort threads by root article Lines header."
4444   (gnus-article-sort-by-lines
4445    (gnus-thread-header h1) (gnus-thread-header h2)))
4446
4447 (defsubst gnus-article-sort-by-chars (h1 h2)
4448   "Sort articles by octet length."
4449   (< (mail-header-chars h1)
4450      (mail-header-chars h2)))
4451
4452 (defun gnus-thread-sort-by-chars (h1 h2)
4453   "Sort threads by root article octet length."
4454   (gnus-article-sort-by-chars
4455    (gnus-thread-header h1) (gnus-thread-header h2)))
4456
4457 (defsubst gnus-article-sort-by-author (h1 h2)
4458   "Sort articles by root author."
4459   (string-lessp
4460    (let ((addr (car (mime-entity-read-field h1 'From))))
4461      (or (std11-full-name-string addr)
4462          (std11-address-string addr)
4463          ""))
4464    (let ((addr (car (mime-entity-read-field h2 'From))))
4465      (or (std11-full-name-string addr)
4466          (std11-address-string addr)
4467          ""))
4468    ))
4469
4470 (defun gnus-thread-sort-by-author (h1 h2)
4471   "Sort threads by root author."
4472   (gnus-article-sort-by-author
4473    (gnus-thread-header h1)  (gnus-thread-header h2)))
4474
4475 (defsubst gnus-article-sort-by-subject (h1 h2)
4476   "Sort articles by root subject."
4477   (string-lessp
4478    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4479    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4480
4481 (defun gnus-thread-sort-by-subject (h1 h2)
4482   "Sort threads by root subject."
4483   (gnus-article-sort-by-subject
4484    (gnus-thread-header h1) (gnus-thread-header h2)))
4485
4486 (defsubst gnus-article-sort-by-date (h1 h2)
4487   "Sort articles by root article date."
4488   (time-less-p
4489    (gnus-date-get-time (mail-header-date h1))
4490    (gnus-date-get-time (mail-header-date h2))))
4491
4492 (defun gnus-thread-sort-by-date (h1 h2)
4493   "Sort threads by root article date."
4494   (gnus-article-sort-by-date
4495    (gnus-thread-header h1) (gnus-thread-header h2)))
4496
4497 (defsubst gnus-article-sort-by-score (h1 h2)
4498   "Sort articles by root article score.
4499 Unscored articles will be counted as having a score of zero."
4500   (> (or (cdr (assq (mail-header-number h1)
4501                     gnus-newsgroup-scored))
4502          gnus-summary-default-score 0)
4503      (or (cdr (assq (mail-header-number h2)
4504                     gnus-newsgroup-scored))
4505          gnus-summary-default-score 0)))
4506
4507 (defun gnus-thread-sort-by-score (h1 h2)
4508   "Sort threads by root article score."
4509   (gnus-article-sort-by-score
4510    (gnus-thread-header h1) (gnus-thread-header h2)))
4511
4512 (defun gnus-thread-sort-by-total-score (h1 h2)
4513   "Sort threads by the sum of all scores in the thread.
4514 Unscored articles will be counted as having a score of zero."
4515   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4516
4517 (defun gnus-thread-total-score (thread)
4518   ;; This function find the total score of THREAD.
4519   (cond
4520    ((null thread)
4521     0)
4522    ((consp thread)
4523     (if (stringp (car thread))
4524         (apply gnus-thread-score-function 0
4525                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4526       (gnus-thread-total-score-1 thread)))
4527    (t
4528     (gnus-thread-total-score-1 (list thread)))))
4529
4530 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4531   "Sort threads such that the thread with the most recently arrived article comes first."
4532   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4533
4534 (defun gnus-thread-highest-number (thread)
4535   "Return the highest article number in THREAD."
4536   (apply 'max (mapcar (lambda (header)
4537                         (mail-header-number header))
4538                       (message-flatten-list thread))))
4539
4540 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4541   "Sort threads such that the thread with the most recently dated article comes first."
4542   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4543
4544 (defun gnus-thread-latest-date (thread)
4545   "Return the highest article date in THREAD."
4546   (let ((previous-time 0))
4547     (apply 'max
4548            (mapcar
4549             (lambda (header)
4550               (setq previous-time
4551                     (condition-case ()
4552                         (time-to-seconds (mail-header-parse-date
4553                                           (mail-header-date header)))
4554                       (error previous-time))))
4555             (sort
4556              (message-flatten-list thread)
4557              (lambda (h1 h2)
4558                (< (mail-header-number h1)
4559                   (mail-header-number h2))))))))
4560
4561 (defun gnus-thread-total-score-1 (root)
4562   ;; This function find the total score of the thread below ROOT.
4563   (setq root (car root))
4564   (apply gnus-thread-score-function
4565          (or (append
4566               (mapcar 'gnus-thread-total-score
4567                       (cdr (gnus-id-to-thread (mail-header-id root))))
4568               (when (> (mail-header-number root) 0)
4569                 (list (or (cdr (assq (mail-header-number root)
4570                                      gnus-newsgroup-scored))
4571                           gnus-summary-default-score 0))))
4572              (list gnus-summary-default-score)
4573              '(0))))
4574
4575 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4576 (defvar gnus-tmp-prev-subject nil)
4577 (defvar gnus-tmp-false-parent nil)
4578 (defvar gnus-tmp-root-expunged nil)
4579 (defvar gnus-tmp-dummy-line nil)
4580
4581 (eval-when-compile (defvar gnus-tmp-header))
4582 (defun gnus-extra-header (type &optional header)
4583   "Return the extra header of TYPE."
4584   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4585       ""))
4586
4587 (defvar gnus-tmp-thread-tree-header-string "")
4588
4589 (defcustom gnus-sum-thread-tree-root "> "
4590   "With %B spec, used for the root of a thread.
4591 If nil, use subject instead."
4592   :type '(radio (const :format "%v  " nil) (string :size 0))
4593   :group 'gnus-thread)
4594 (defcustom gnus-sum-thread-tree-false-root "> "
4595   "With %B spec, used for a false root of a thread.
4596 If nil, use subject instead."
4597   :type '(radio (const :format "%v  " nil) (string :size 0))
4598   :group 'gnus-thread)
4599 (defcustom gnus-sum-thread-tree-single-indent ""
4600   "With %B spec, used for a thread with just one message.
4601 If nil, use subject instead."
4602   :type '(radio (const :format "%v  " nil) (string :size 0))
4603   :group 'gnus-thread)
4604 (defcustom gnus-sum-thread-tree-vertical "| "
4605   "With %B spec, used for drawing a vertical line."
4606   :type 'string
4607   :group 'gnus-thread)
4608 (defcustom gnus-sum-thread-tree-indent "  "
4609   "With %B spec, used for indenting."
4610   :type 'string
4611   :group 'gnus-thread)
4612 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4613   "With %B spec, used for a leaf with brothers."
4614   :type 'string
4615   :group 'gnus-thread)
4616 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4617   "With %B spec, used for a leaf without brothers."
4618   :type 'string
4619   :group 'gnus-thread)
4620
4621 (defun gnus-summary-prepare-threads (threads)
4622   "Prepare summary buffer from THREADS and indentation LEVEL.
4623 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4624 or a straight list of headers."
4625   (gnus-message 7 "Generating summary...")
4626
4627   (setq gnus-newsgroup-threads threads)
4628   (beginning-of-line)
4629
4630   (let ((gnus-tmp-level 0)
4631         (default-score (or gnus-summary-default-score 0))
4632         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4633         (building-line-count gnus-summary-display-while-building)
4634         (building-count (integerp gnus-summary-display-while-building))
4635         thread number subject stack state gnus-tmp-gathered beg-match
4636         new-roots gnus-tmp-new-adopts thread-end simp-subject
4637         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4638         gnus-tmp-replied gnus-tmp-subject-or-nil
4639         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4640         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4641         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4642         tree-stack)
4643
4644     (setq gnus-tmp-prev-subject nil
4645           gnus-tmp-thread-tree-header-string "")
4646
4647     (if (vectorp (car threads))
4648         ;; If this is a straight (sic) list of headers, then a
4649         ;; threaded summary display isn't required, so we just create
4650         ;; an unthreaded one.
4651         (gnus-summary-prepare-unthreaded threads)
4652
4653       ;; Do the threaded display.
4654
4655       (if gnus-summary-display-while-building
4656           (switch-to-buffer (buffer-name)))
4657       (while (or threads stack gnus-tmp-new-adopts new-roots)
4658
4659         (if (and (= gnus-tmp-level 0)
4660                  (or (not stack)
4661                      (= (caar stack) 0))
4662                  (not gnus-tmp-false-parent)
4663                  (or gnus-tmp-new-adopts new-roots))
4664             (if gnus-tmp-new-adopts
4665                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4666                       thread (list (car gnus-tmp-new-adopts))
4667                       gnus-tmp-header (caar thread)
4668                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4669               (when new-roots
4670                 (setq thread (list (car new-roots))
4671                       gnus-tmp-header (caar thread)
4672                       new-roots (cdr new-roots))))
4673
4674           (if threads
4675               ;; If there are some threads, we do them before the
4676               ;; threads on the stack.
4677               (setq thread threads
4678                     gnus-tmp-header (caar thread))
4679             ;; There were no current threads, so we pop something off
4680             ;; the stack.
4681             (setq state (car stack)
4682                   gnus-tmp-level (car state)
4683                   tree-stack (cadr state)
4684                   thread (caddr state)
4685                   stack (cdr stack)
4686                   gnus-tmp-header (caar thread))))
4687
4688         (setq gnus-tmp-false-parent nil)
4689         (setq gnus-tmp-root-expunged nil)
4690         (setq thread-end nil)
4691
4692         (if (stringp gnus-tmp-header)
4693             ;; The header is a dummy root.
4694             (cond
4695              ((eq gnus-summary-make-false-root 'adopt)
4696               ;; We let the first article adopt the rest.
4697               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4698                                                (cddar thread)))
4699               (setq gnus-tmp-gathered
4700                     (nconc (mapcar
4701                             (lambda (h) (mail-header-number (car h)))
4702                             (cddar thread))
4703                            gnus-tmp-gathered))
4704               (setq thread (cons (list (caar thread)
4705                                        (cadar thread))
4706                                  (cdr thread)))
4707               (setq gnus-tmp-level -1
4708                     gnus-tmp-false-parent t))
4709              ((eq gnus-summary-make-false-root 'empty)
4710               ;; We print adopted articles with empty subject fields.
4711               (setq gnus-tmp-gathered
4712                     (nconc (mapcar
4713                             (lambda (h) (mail-header-number (car h)))
4714                             (cddar thread))
4715                            gnus-tmp-gathered))
4716               (setq gnus-tmp-level -1))
4717              ((eq gnus-summary-make-false-root 'dummy)
4718               ;; We remember that we probably want to output a dummy
4719               ;; root.
4720               (setq gnus-tmp-dummy-line gnus-tmp-header)
4721               (setq gnus-tmp-prev-subject gnus-tmp-header))
4722              (t
4723               ;; We do not make a root for the gathered
4724               ;; sub-threads at all.
4725               (setq gnus-tmp-level -1)))
4726
4727           (setq number (mail-header-number gnus-tmp-header)
4728                 subject (mail-header-subject gnus-tmp-header)
4729                 simp-subject (gnus-simplify-subject-fully subject))
4730
4731           (cond
4732            ;; If the thread has changed subject, we might want to make
4733            ;; this subthread into a root.
4734            ((and (null gnus-thread-ignore-subject)
4735                  (not (zerop gnus-tmp-level))
4736                  gnus-tmp-prev-subject
4737                  (not (string= gnus-tmp-prev-subject simp-subject)))
4738             (setq new-roots (nconc new-roots (list (car thread)))
4739                   thread-end t
4740                   gnus-tmp-header nil))
4741            ;; If the article lies outside the current limit,
4742            ;; then we do not display it.
4743            ((not (memq number gnus-newsgroup-limit))
4744             (setq gnus-tmp-gathered
4745                   (nconc (mapcar
4746                           (lambda (h) (mail-header-number (car h)))
4747                           (cdar thread))
4748                          gnus-tmp-gathered))
4749             (setq gnus-tmp-new-adopts (if (cdar thread)
4750                                           (append gnus-tmp-new-adopts
4751                                                   (cdar thread))
4752                                         gnus-tmp-new-adopts)
4753                   thread-end t
4754                   gnus-tmp-header nil)
4755             (when (zerop gnus-tmp-level)
4756               (setq gnus-tmp-root-expunged t)))
4757            ;; Perhaps this article is to be marked as read?
4758            ((and gnus-summary-mark-below
4759                  (< (or (cdr (assq number gnus-newsgroup-scored))
4760                         default-score)
4761                     gnus-summary-mark-below)
4762                  ;; Don't touch sparse articles.
4763                  (not (gnus-summary-article-sparse-p number))
4764                  (not (gnus-summary-article-ancient-p number)))
4765             (setq gnus-newsgroup-unreads
4766                   (delq number gnus-newsgroup-unreads))
4767             (if gnus-newsgroup-auto-expire
4768                 (setq gnus-newsgroup-expirable
4769                       (gnus-add-to-sorted-list
4770                        gnus-newsgroup-expirable number))
4771               (push (cons number gnus-low-score-mark)
4772                     gnus-newsgroup-reads))))
4773
4774           (when gnus-tmp-header
4775             ;; We may have an old dummy line to output before this
4776             ;; article.
4777             (when (and gnus-tmp-dummy-line
4778                        (gnus-subject-equal
4779                         gnus-tmp-dummy-line
4780                         (mail-header-subject gnus-tmp-header)))
4781               (gnus-summary-insert-dummy-line
4782                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4783               (setq gnus-tmp-dummy-line nil))
4784
4785             ;; Compute the mark.
4786             (setq gnus-tmp-unread (gnus-article-mark number))
4787
4788             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4789                                   gnus-tmp-header gnus-tmp-level)
4790                   gnus-newsgroup-data)
4791
4792             ;; Actually insert the line.
4793             (setq
4794              gnus-tmp-subject-or-nil
4795              (cond
4796               ((and gnus-thread-ignore-subject
4797                     gnus-tmp-prev-subject
4798                     (not (string= gnus-tmp-prev-subject simp-subject)))
4799                subject)
4800               ((zerop gnus-tmp-level)
4801                (if (and (eq gnus-summary-make-false-root 'empty)
4802                         (memq number gnus-tmp-gathered)
4803                         gnus-tmp-prev-subject
4804                         (string= gnus-tmp-prev-subject simp-subject))
4805                    gnus-summary-same-subject
4806                  subject))
4807               (t gnus-summary-same-subject)))
4808             (if (and (eq gnus-summary-make-false-root 'adopt)
4809                      (= gnus-tmp-level 1)
4810                      (memq number gnus-tmp-gathered))
4811                 (setq gnus-tmp-opening-bracket ?\<
4812                       gnus-tmp-closing-bracket ?\>)
4813               (setq gnus-tmp-opening-bracket ?\[
4814                     gnus-tmp-closing-bracket ?\]))
4815             (setq
4816              gnus-tmp-indentation
4817              (aref gnus-thread-indent-array gnus-tmp-level)
4818              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4819              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4820                                 gnus-summary-default-score 0)
4821              gnus-tmp-score-char
4822              (if (or (null gnus-summary-default-score)
4823                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4824                          gnus-summary-zcore-fuzz))
4825                  ?\ ;;;Whitespace
4826                (if (< gnus-tmp-score gnus-summary-default-score)
4827                    gnus-score-below-mark gnus-score-over-mark))
4828              gnus-tmp-replied
4829              (cond ((memq number gnus-newsgroup-processable)
4830                     gnus-process-mark)
4831                    ((memq number gnus-newsgroup-cached)
4832                     gnus-cached-mark)
4833                    ((memq number gnus-newsgroup-replied)
4834                     gnus-replied-mark)
4835                    ((memq number gnus-newsgroup-forwarded)
4836                     gnus-forwarded-mark)
4837                    ((memq number gnus-newsgroup-saved)
4838                     gnus-saved-mark)
4839                    ((memq number gnus-newsgroup-recent)
4840                     gnus-recent-mark)
4841                    ((memq number gnus-newsgroup-unseen)
4842                     gnus-unseen-mark)
4843                    (t gnus-no-mark))
4844              gnus-tmp-downloaded
4845              (cond ((memq number gnus-newsgroup-undownloaded)
4846                     gnus-undownloaded-mark)
4847                    (gnus-newsgroup-agentized
4848                     gnus-downloaded-mark)
4849                    (t
4850                     gnus-no-mark))
4851              gnus-tmp-from (mail-header-from gnus-tmp-header)
4852              gnus-tmp-name
4853              (cond
4854               ((string-match "<[^>]+> *$" gnus-tmp-from)
4855                (setq beg-match (match-beginning 0))
4856                (or (and (string-match "^\".+\"" gnus-tmp-from)
4857                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4858                    (substring gnus-tmp-from 0 beg-match)))
4859               ((string-match "(.+)" gnus-tmp-from)
4860                (substring gnus-tmp-from
4861                           (1+ (match-beginning 0)) (1- (match-end 0))))
4862               (t gnus-tmp-from))
4863
4864              ;; Do the %B string
4865              gnus-tmp-thread-tree-header-string
4866              (cond
4867               ((not gnus-show-threads) "")
4868               ((zerop gnus-tmp-level)
4869                (cond ((cdar thread)
4870                       (or gnus-sum-thread-tree-root subject))
4871                      (gnus-tmp-new-adopts
4872                       (or gnus-sum-thread-tree-false-root subject))
4873                      (t
4874                       (or gnus-sum-thread-tree-single-indent subject))))
4875               (t
4876                (concat (apply 'concat
4877                               (mapcar (lambda (item)
4878                                         (if (= item 1)
4879                                             gnus-sum-thread-tree-vertical
4880                                           gnus-sum-thread-tree-indent))
4881                                       (cdr (reverse tree-stack))))
4882                        (if (nth 1 thread)
4883                            gnus-sum-thread-tree-leaf-with-other
4884                          gnus-sum-thread-tree-single-leaf)))))
4885             (when (string= gnus-tmp-name "")
4886               (setq gnus-tmp-name gnus-tmp-from))
4887             (unless (numberp gnus-tmp-lines)
4888               (setq gnus-tmp-lines -1))
4889             (if (= gnus-tmp-lines -1)
4890                 (setq gnus-tmp-lines "?")
4891               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4892               (gnus-put-text-property
4893              (point)
4894              (progn (eval gnus-summary-line-format-spec) (point))
4895                'gnus-number number)
4896             (when gnus-visual-p
4897               (forward-line -1)
4898               (gnus-run-hooks 'gnus-summary-update-hook)
4899               (forward-line 1))
4900
4901             (setq gnus-tmp-prev-subject simp-subject)))
4902
4903         (when (nth 1 thread)
4904           (push (list (max 0 gnus-tmp-level)
4905                       (copy-sequence tree-stack)
4906                       (nthcdr 1 thread))
4907                 stack))
4908         (push (if (nth 1 thread) 1 0) tree-stack)
4909         (incf gnus-tmp-level)
4910         (setq threads (if thread-end nil (cdar thread)))
4911         (if gnus-summary-display-while-building
4912             (if building-count
4913                 (progn
4914                   ;; use a set frequency
4915                   (setq building-line-count (1- building-line-count))
4916                   (when (= building-line-count 0)
4917                     (sit-for 0)
4918                     (setq building-line-count
4919                           gnus-summary-display-while-building)))
4920               ;; always
4921               (sit-for 0)))
4922         (unless threads
4923           (setq gnus-tmp-level 0)))))
4924   (gnus-message 7 "Generating summary...done"))
4925
4926 (defun gnus-summary-prepare-unthreaded (headers)
4927   "Generate an unthreaded summary buffer based on HEADERS."
4928   (let (header number mark)
4929
4930     (beginning-of-line)
4931
4932     (while headers
4933       ;; We may have to root out some bad articles...
4934       (when (memq (setq number (mail-header-number
4935                                 (setq header (pop headers))))
4936                   gnus-newsgroup-limit)
4937         ;; Mark article as read when it has a low score.
4938         (when (and gnus-summary-mark-below
4939                    (< (or (cdr (assq number gnus-newsgroup-scored))
4940                           gnus-summary-default-score 0)
4941                       gnus-summary-mark-below)
4942                    (not (gnus-summary-article-ancient-p number)))
4943           (setq gnus-newsgroup-unreads
4944                 (delq number gnus-newsgroup-unreads))
4945           (if gnus-newsgroup-auto-expire
4946               (push number gnus-newsgroup-expirable)
4947             (push (cons number gnus-low-score-mark)
4948                   gnus-newsgroup-reads)))
4949
4950         (setq mark (gnus-article-mark number))
4951         (push (gnus-data-make number mark (1+ (point)) header 0)
4952               gnus-newsgroup-data)
4953         (gnus-summary-insert-line
4954          header 0 number
4955          (memq number gnus-newsgroup-undownloaded)
4956          mark (memq number gnus-newsgroup-replied)
4957          (memq number gnus-newsgroup-expirable)
4958          (mail-header-subject header) nil
4959          (cdr (assq number gnus-newsgroup-scored))
4960          (memq number gnus-newsgroup-processable))))))
4961
4962 (defun gnus-summary-remove-list-identifiers ()
4963   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4964   (let ((regexp (if (consp gnus-list-identifiers)
4965                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4966                   gnus-list-identifiers))
4967         changed subject)
4968     (when regexp
4969       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
4970       (dolist (header gnus-newsgroup-headers)
4971         (setq subject (mail-header-subject header)
4972               changed nil)
4973         (while (string-match regexp subject)
4974           (setq subject
4975                 (concat (substring subject 0 (match-beginning 1))
4976                         (substring subject (match-end 0)))
4977                 changed t))
4978         (when changed
4979           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
4980             (setq subject
4981                   (concat (substring subject 0 (match-beginning 1))
4982                           (substring subject (match-end 1)))))
4983           (mail-header-set-subject header subject))))))
4984
4985 (defun gnus-fetch-headers (articles)
4986   "Fetch headers of ARTICLES."
4987   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4988     (gnus-message 5 "Fetching headers for %s..." name)
4989     (prog1
4990         (if (eq 'nov
4991                 (setq gnus-headers-retrieved-by
4992                       (gnus-retrieve-headers
4993                        articles gnus-newsgroup-name
4994                        ;; We might want to fetch old headers, but
4995                        ;; not if there is only 1 article.
4996                        (and (or (and
4997                                  (not (eq gnus-fetch-old-headers 'some))
4998                                  (not (numberp gnus-fetch-old-headers)))
4999                                 (> (length articles) 1))
5000                             gnus-fetch-old-headers))))
5001             (gnus-get-newsgroup-headers-xover
5002              articles nil nil gnus-newsgroup-name t)
5003           (gnus-get-newsgroup-headers))
5004       (gnus-message 5 "Fetching headers for %s...done" name))))
5005
5006 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5007   "Select newsgroup GROUP.
5008 If READ-ALL is non-nil, all articles in the group are selected.
5009 If SELECT-ARTICLES, only select those articles from GROUP."
5010   (let* ((entry (gnus-group-entry group))
5011          ;;!!! Dirty hack; should be removed.
5012          (gnus-summary-ignore-duplicates
5013           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5014               t
5015             gnus-summary-ignore-duplicates))
5016          (info (nth 2 entry))
5017          articles fetched-articles cached)
5018
5019     (unless (gnus-check-server
5020              (set (make-local-variable 'gnus-current-select-method)
5021                   (gnus-find-method-for-group group)))
5022       (error "Couldn't open server"))
5023
5024     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5025         (gnus-activate-group group)     ; Or we can activate it...
5026         (progn                          ; Or we bug out.
5027           (when (equal major-mode 'gnus-summary-mode)
5028             (gnus-kill-buffer (current-buffer)))
5029           (error "Couldn't activate group %s: %s"
5030                  group (gnus-status-message group))))
5031
5032     (unless (gnus-request-group group t)
5033       (when (equal major-mode 'gnus-summary-mode)
5034         (gnus-kill-buffer (current-buffer)))
5035       (error "Couldn't request group %s: %s"
5036              group (gnus-status-message group)))
5037
5038     (when gnus-agent
5039       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5040       
5041       (setq gnus-summary-use-undownloaded-faces
5042             (gnus-agent-find-parameter
5043              group
5044              'agent-enable-undownloaded-faces)))
5045
5046     (setq gnus-newsgroup-name group
5047           gnus-newsgroup-unselected nil
5048           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5049
5050     (let ((display (gnus-group-find-parameter group 'display)))
5051       (setq gnus-newsgroup-display
5052             (cond
5053              ((not (zerop (or (car-safe read-all) 0)))
5054               ;; The user entered the group with C-u SPC/RET, let's show
5055               ;; all articles.
5056               'gnus-not-ignore)
5057              ((eq display 'all)
5058               'gnus-not-ignore)
5059              ((arrayp display)
5060               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5061              ((numberp display)
5062               ;; The following is probably the "correct" solution, but
5063               ;; it makes Gnus fetch all headers and then limit the
5064               ;; articles (which is slow), so instead we hack the
5065               ;; select-articles parameter instead. -- Simon Josefsson
5066               ;; <jas@kth.se>
5067               ;;
5068               ;; (gnus-byte-compile
5069               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5070               ;;                         display)))))
5071               (setq select-articles
5072                     (gnus-uncompress-range
5073                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5074                              (if (> tmp 0)
5075                                  tmp
5076                                1))
5077                            (cdr (gnus-active group)))))
5078               nil)
5079              (t
5080               nil))))
5081
5082     (gnus-summary-setup-default-charset)
5083
5084     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5085     (when (gnus-virtual-group-p group)
5086       (setq cached gnus-newsgroup-cached))
5087
5088     (setq gnus-newsgroup-unreads
5089           (gnus-sorted-ndifference
5090            (gnus-sorted-ndifference gnus-newsgroup-unreads
5091                                     gnus-newsgroup-marked)
5092            gnus-newsgroup-dormant))
5093
5094     (setq gnus-newsgroup-processable nil)
5095
5096     (gnus-update-read-articles group gnus-newsgroup-unreads)
5097
5098     ;; Adjust and set lists of article marks.
5099     (when info
5100       (gnus-adjust-marked-articles info))
5101     (if (setq articles select-articles)
5102         (setq gnus-newsgroup-unselected
5103               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5104       (setq articles (gnus-articles-to-read group read-all)))
5105
5106     (cond
5107      ((null articles)
5108       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5109       'quit)
5110      ((eq articles 0) nil)
5111      (t
5112       ;; Init the dependencies hash table.
5113       (setq gnus-newsgroup-dependencies
5114             (gnus-make-hashtable (length articles)))
5115       (gnus-set-global-variables)
5116       ;; Retrieve the headers and read them in.
5117
5118       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5119
5120       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5121       (when cached
5122         (setq gnus-newsgroup-cached cached))
5123
5124       ;; Suppress duplicates?
5125       (when gnus-suppress-duplicates
5126         (gnus-dup-suppress-articles))
5127
5128       ;; Set the initial limit.
5129       (setq gnus-newsgroup-limit (copy-sequence articles))
5130       ;; Remove canceled articles from the list of unread articles.
5131       (setq fetched-articles
5132             (mapcar (lambda (headers) (mail-header-number headers))
5133                     gnus-newsgroup-headers))
5134       (setq gnus-newsgroup-articles fetched-articles)
5135       (setq gnus-newsgroup-unreads
5136             (gnus-sorted-nintersection
5137              gnus-newsgroup-unreads fetched-articles))
5138       (gnus-compute-unseen-list)
5139
5140       ;; Removed marked articles that do not exist.
5141       (gnus-update-missing-marks
5142        (gnus-sorted-difference articles fetched-articles))
5143       ;; We might want to build some more threads first.
5144       (when (and gnus-fetch-old-headers
5145                  (eq gnus-headers-retrieved-by 'nov))
5146         (if (eq gnus-fetch-old-headers 'invisible)
5147             (gnus-build-all-threads)
5148           (gnus-build-old-threads)))
5149       ;; Let the Gnus agent mark articles as read.
5150       (when gnus-agent
5151         (gnus-agent-get-undownloaded-list))
5152       ;; Remove list identifiers from subject
5153       (when gnus-list-identifiers
5154         (gnus-summary-remove-list-identifiers))
5155       ;; Check whether auto-expire is to be done in this group.
5156       (setq gnus-newsgroup-auto-expire
5157             (gnus-group-auto-expirable-p group))
5158       ;; Set up the article buffer now, if necessary.
5159       (unless gnus-single-article-buffer
5160         (gnus-article-setup-buffer))
5161       ;; First and last article in this newsgroup.
5162       (when gnus-newsgroup-headers
5163         (setq gnus-newsgroup-begin
5164               (mail-header-number (car gnus-newsgroup-headers))
5165               gnus-newsgroup-end
5166               (mail-header-number
5167                (gnus-last-element gnus-newsgroup-headers))))
5168       ;; GROUP is successfully selected.
5169       (or gnus-newsgroup-headers t)))))
5170
5171 (defun gnus-compute-unseen-list ()
5172   ;; The `seen' marks are treated specially.
5173   (if (not gnus-newsgroup-seen)
5174       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5175     (setq gnus-newsgroup-unseen
5176           (gnus-inverse-list-range-intersection
5177            gnus-newsgroup-articles gnus-newsgroup-seen))))
5178
5179 (defun gnus-summary-display-make-predicate (display)
5180   (require 'gnus-agent)
5181   (when (= (length display) 1)
5182     (setq display (car display)))
5183   (unless gnus-summary-display-cache
5184     (dolist (elem (append '((unread . unread)
5185                             (read . read)
5186                             (unseen . unseen))
5187                           gnus-article-mark-lists))
5188       (push (cons (cdr elem)
5189                   (gnus-byte-compile
5190                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5191             gnus-summary-display-cache)))
5192   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5193         (gnus-category-predicate-cache gnus-summary-display-cache))
5194     (gnus-get-predicate display)))
5195
5196 ;; Uses the dynamically bound `number' variable.
5197 (eval-when-compile
5198   (defvar number))
5199 (defun gnus-article-marked-p (type &optional article)
5200   (let ((article (or article number)))
5201     (cond
5202      ((eq type 'tick)
5203       (memq article gnus-newsgroup-marked))
5204      ((eq type 'spam)
5205       (memq article gnus-newsgroup-spam-marked))
5206      ((eq type 'unsend)
5207       (memq article gnus-newsgroup-unsendable))
5208      ((eq type 'undownload)
5209       (memq article gnus-newsgroup-undownloaded))
5210      ((eq type 'download)
5211       (memq article gnus-newsgroup-downloadable))
5212      ((eq type 'unread)
5213       (memq article gnus-newsgroup-unreads))
5214      ((eq type 'read)
5215       (memq article gnus-newsgroup-reads))
5216      ((eq type 'dormant)
5217       (memq article gnus-newsgroup-dormant) )
5218      ((eq type 'expire)
5219       (memq article gnus-newsgroup-expirable))
5220      ((eq type 'reply)
5221       (memq article gnus-newsgroup-replied))
5222      ((eq type 'killed)
5223       (memq article gnus-newsgroup-killed))
5224      ((eq type 'bookmark)
5225       (assq article gnus-newsgroup-bookmarks))
5226      ((eq type 'score)
5227       (assq article gnus-newsgroup-scored))
5228      ((eq type 'save)
5229       (memq article gnus-newsgroup-saved))
5230      ((eq type 'cache)
5231       (memq article gnus-newsgroup-cached))
5232      ((eq type 'forward)
5233       (memq article gnus-newsgroup-forwarded))
5234      ((eq type 'seen)
5235       (not (memq article gnus-newsgroup-unseen)))
5236      ((eq type 'recent)
5237       (memq article gnus-newsgroup-recent))
5238      (t t))))
5239
5240 (defun gnus-articles-to-read (group &optional read-all)
5241   "Find out what articles the user wants to read."
5242   (let* ((display (gnus-group-find-parameter group 'display))
5243          (articles
5244           ;; Select all articles if `read-all' is non-nil, or if there
5245           ;; are no unread articles.
5246           (if (or read-all
5247                   (and (zerop (length gnus-newsgroup-marked))
5248                        (zerop (length gnus-newsgroup-unreads)))
5249                   ;; Fetch all if the predicate is non-nil.
5250                   gnus-newsgroup-display)
5251               ;; We want to select the headers for all the articles in
5252               ;; the group, so we select either all the active
5253               ;; articles in the group, or (if that's nil), the
5254               ;; articles in the cache.
5255               (or
5256                (gnus-uncompress-range (gnus-active group))
5257                (gnus-cache-articles-in-group group))
5258             ;; Select only the "normal" subset of articles.
5259             (gnus-sorted-nunion
5260              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5261              gnus-newsgroup-unreads)))
5262          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5263          (scored (length scored-list))
5264          (number (length articles))
5265          (marked (+ (length gnus-newsgroup-marked)
5266                     (length gnus-newsgroup-dormant)))
5267          (select
5268           (cond
5269            ((numberp read-all)
5270             read-all)
5271            ((numberp gnus-newsgroup-display)
5272             gnus-newsgroup-display)
5273            (t
5274             (condition-case ()
5275                 (cond
5276                  ((and (or (<= scored marked) (= scored number))
5277                        (numberp gnus-large-newsgroup)
5278                        (> number gnus-large-newsgroup))
5279                   (let* ((cursor-in-echo-area nil)
5280                          (initial (gnus-parameter-large-newsgroup-initial
5281                                    gnus-newsgroup-name))
5282                          (input
5283                           (read-string
5284                            (format
5285                             "How many articles from %s (%s %d): "
5286                             (gnus-limit-string
5287                              (gnus-group-decoded-name gnus-newsgroup-name)
5288                              35)
5289                             (if initial "max" "default")
5290                             number)
5291                            (if initial
5292                                (cons (number-to-string initial)
5293                                      0)))))
5294                     (if (string-match "^[ \t]*$" input) number input)))
5295                  ((and (> scored marked) (< scored number)
5296                        (> (- scored number) 20))
5297                   (let ((input
5298                          (read-string
5299                           (format "%s %s (%d scored, %d total): "
5300                                   "How many articles from"
5301                                   (gnus-group-decoded-name group)
5302                                   scored number))))
5303                     (if (string-match "^[ \t]*$" input)
5304                         number input)))
5305                  (t number))
5306               (quit
5307                (message "Quit getting the articles to read")
5308                nil))))))
5309     (setq select (if (stringp select) (string-to-number select) select))
5310     (if (or (null select) (zerop select))
5311         select
5312       (if (and (not (zerop scored)) (<= (abs select) scored))
5313           (progn
5314             (setq articles (sort scored-list '<))
5315             (setq number (length articles)))
5316         (setq articles (copy-sequence articles)))
5317
5318       (when (< (abs select) number)
5319         (if (< select 0)
5320             ;; Select the N oldest articles.
5321             (setcdr (nthcdr (1- (abs select)) articles) nil)
5322           ;; Select the N most recent articles.
5323           (setq articles (nthcdr (- number select) articles))))
5324       (setq gnus-newsgroup-unselected
5325             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5326       (when gnus-alter-articles-to-read-function
5327         (setq articles
5328               (sort
5329                (funcall gnus-alter-articles-to-read-function
5330                         gnus-newsgroup-name articles)
5331                '<)))
5332       articles)))
5333
5334 (defun gnus-killed-articles (killed articles)
5335   (let (out)
5336     (while articles
5337       (when (inline (gnus-member-of-range (car articles) killed))
5338         (push (car articles) out))
5339       (setq articles (cdr articles)))
5340     out))
5341
5342 (defun gnus-uncompress-marks (marks)
5343   "Uncompress the mark ranges in MARKS."
5344   (let ((uncompressed '(score bookmark))
5345         out)
5346     (while marks
5347       (if (memq (caar marks) uncompressed)
5348           (push (car marks) out)
5349         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5350       (setq marks (cdr marks)))
5351     out))
5352
5353 (defun gnus-article-mark-to-type (mark)
5354   "Return the type of MARK."
5355   (or (cadr (assq mark gnus-article-special-mark-lists))
5356       'list))
5357
5358 (defun gnus-article-unpropagatable-p (mark)
5359   "Return whether MARK should be propagated to back end."
5360   (memq mark gnus-article-unpropagated-mark-lists))
5361
5362 (defun gnus-adjust-marked-articles (info)
5363   "Set all article lists and remove all marks that are no longer valid."
5364   (let* ((marked-lists (gnus-info-marks info))
5365          (active (gnus-active (gnus-info-group info)))
5366          (min (car active))
5367          (max (cdr active))
5368          (types gnus-article-mark-lists)
5369          marks var articles article mark mark-type
5370          bgn end)
5371
5372     (dolist (marks marked-lists)
5373       (setq mark (car marks)
5374             mark-type (gnus-article-mark-to-type mark)
5375             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5376
5377       ;; We set the variable according to the type of the marks list,
5378       ;; and then adjust the marks to a subset of the active articles.
5379       (cond
5380        ;; Adjust "simple" lists - compressed yet unsorted
5381        ((eq mark-type 'list)
5382         ;; Simultaneously uncompress and clip to active range
5383         ;; See gnus-uncompress-range for a description of possible marks
5384         (let (l lh)
5385           (if (not (cadr marks))
5386               (set var nil)
5387             (setq articles (if (numberp (cddr marks))
5388                                (list (cdr marks))
5389                              (cdr marks))
5390                   lh (cons nil nil)
5391                   l lh)
5392
5393             (while (setq article (pop articles))
5394               (cond ((consp article)
5395                      (setq bgn (max (car article) min)
5396                            end (min (cdr article) max))
5397                      (while (<= bgn end)
5398                        (setq l (setcdr l (cons bgn nil))
5399                              bgn (1+ bgn))))
5400                     ((and (<= min article)
5401                           (>= max article))
5402                      (setq l (setcdr l (cons article nil))))))
5403             (set var (cdr lh)))))
5404        ;; Adjust assocs.
5405        ((eq mark-type 'tuple)
5406         (set var (setq articles (cdr marks)))
5407         (when (not (listp (cdr (symbol-value var))))
5408           (set var (list (symbol-value var))))
5409         (when (not (listp (cdr articles)))
5410           (setq articles (list articles)))
5411         (while articles
5412           (when (or (not (consp (setq article (pop articles))))
5413                     (< (car article) min)
5414                     (> (car article) max))
5415             (set var (delq article (symbol-value var))))))
5416        ;; Adjust ranges (sloppily).
5417        ((eq mark-type 'range)
5418         (cond
5419          ((eq mark 'seen)
5420           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5421           ;; It should be (seen (NUM1 . NUM2)).
5422           (when (numberp (cddr marks))
5423             (setcdr marks (list (cdr marks))))
5424           (setq articles (cdr marks))
5425           (while (and articles
5426                       (or (and (consp (car articles))
5427                                (> min (cdar articles)))
5428                           (and (numberp (car articles))
5429                                (> min (car articles)))))
5430             (pop articles))
5431           (set var articles))))))))
5432
5433 (defun gnus-update-missing-marks (missing)
5434   "Go through the list of MISSING articles and remove them from the mark lists."
5435   (when missing
5436     (let (var m)
5437       ;; Go through all types.
5438       (dolist (elem gnus-article-mark-lists)
5439         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5440           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5441           (when (symbol-value var)
5442             ;; This list has articles.  So we delete all missing
5443             ;; articles from it.
5444             (setq m missing)
5445             (while m
5446               (set var (delq (pop m) (symbol-value var))))))))))
5447
5448 (defun gnus-update-marks ()
5449   "Enter the various lists of marked articles into the newsgroup info list."
5450   (let ((types gnus-article-mark-lists)
5451         (info (gnus-get-info gnus-newsgroup-name))
5452         type list newmarked symbol delta-marks)
5453     (when info
5454       ;; Add all marks lists to the list of marks lists.
5455       (while (setq type (pop types))
5456         (setq list (symbol-value
5457                     (setq symbol
5458                           (intern (format "gnus-newsgroup-%s" (car type))))))
5459
5460         (when list
5461           ;; Get rid of the entries of the articles that have the
5462           ;; default score.
5463           (when (and (eq (cdr type) 'score)
5464                      gnus-save-score
5465                      list)
5466             (let* ((arts list)
5467                    (prev (cons nil list))
5468                    (all prev))
5469               (while arts
5470                 (if (or (not (consp (car arts)))
5471                         (= (cdar arts) gnus-summary-default-score))
5472                     (setcdr prev (cdr arts))
5473                   (setq prev arts))
5474                 (setq arts (cdr arts)))
5475               (setq list (cdr all)))))
5476
5477         (when (eq (cdr type) 'seen)
5478           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5479
5480         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5481           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5482
5483         (when (and (gnus-check-backend-function
5484                     'request-set-mark gnus-newsgroup-name)
5485                    (not (gnus-article-unpropagatable-p (cdr type))))
5486           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5487                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5488                  (add (gnus-remove-from-range
5489                        (gnus-copy-sequence list) old)))
5490             (when add
5491               (push (list add 'add (list (cdr type))) delta-marks))
5492             (when del
5493               (push (list del 'del (list (cdr type))) delta-marks))))
5494
5495         (when list
5496           (push (cons (cdr type) list) newmarked)))
5497
5498       (when delta-marks
5499         (unless (gnus-check-group gnus-newsgroup-name)
5500           (error "Can't open server for %s" gnus-newsgroup-name))
5501         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5502
5503       ;; Enter these new marks into the info of the group.
5504       (if (nthcdr 3 info)
5505           (setcar (nthcdr 3 info) newmarked)
5506         ;; Add the marks lists to the end of the info.
5507         (when newmarked
5508           (setcdr (nthcdr 2 info) (list newmarked))))
5509
5510       ;; Cut off the end of the info if there's nothing else there.
5511       (let ((i 5))
5512         (while (and (> i 2)
5513                     (not (nth i info)))
5514           (when (nthcdr (decf i) info)
5515             (setcdr (nthcdr i info) nil)))))))
5516
5517 (defun gnus-set-mode-line (where)
5518   "Set the mode line of the article or summary buffers.
5519 If WHERE is `summary', the summary mode line format will be used."
5520   ;; Is this mode line one we keep updated?
5521   (when (and (memq where gnus-updated-mode-lines)
5522              (symbol-value
5523               (intern (format "gnus-%s-mode-line-format-spec" where))))
5524     (let (mode-string)
5525       (save-excursion
5526         ;; We evaluate this in the summary buffer since these
5527         ;; variables are buffer-local to that buffer.
5528         (set-buffer gnus-summary-buffer)
5529         ;; We bind all these variables that are used in the `eval' form
5530         ;; below.
5531         (let* ((mformat (symbol-value
5532                          (intern
5533                           (format "gnus-%s-mode-line-format-spec" where))))
5534                (gnus-tmp-group-name (gnus-group-decoded-name
5535                                      gnus-newsgroup-name))
5536                (gnus-tmp-article-number (or gnus-current-article 0))
5537                (gnus-tmp-unread gnus-newsgroup-unreads)
5538                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5539                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5540                (gnus-tmp-unread-and-unselected
5541                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5542                             (zerop gnus-tmp-unselected))
5543                        "")
5544                       ((zerop gnus-tmp-unselected)
5545                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5546                       (t (format "{%d(+%d) more}"
5547                                  gnus-tmp-unread-and-unticked
5548                                  gnus-tmp-unselected))))
5549                (gnus-tmp-subject
5550                 (if (and gnus-current-headers
5551                          (vectorp gnus-current-headers))
5552                     (gnus-mode-string-quote
5553                      (mail-header-subject gnus-current-headers))
5554                   ""))
5555                bufname-length max-len
5556                gnus-tmp-header);; passed as argument to any user-format-funcs
5557           (setq mode-string (eval mformat))
5558           (setq bufname-length (if (string-match "%b" mode-string)
5559                                    (- (length
5560                                        (buffer-name
5561                                         (if (eq where 'summary)
5562                                             nil
5563                                           (get-buffer gnus-article-buffer))))
5564                                       2)
5565                                  0))
5566           (setq max-len (max 4 (if gnus-mode-non-string-length
5567                                    (- (window-width)
5568                                       gnus-mode-non-string-length
5569                                       bufname-length)
5570                                  (length mode-string))))
5571           ;; We might have to chop a bit of the string off...
5572           (when (> (length mode-string) max-len)
5573             (setq mode-string
5574                   (concat (gnus-truncate-string mode-string (- max-len 3))
5575                           "...")))
5576           ;; Pad the mode string a bit.
5577           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5578       ;; Update the mode line.
5579       (setq mode-line-buffer-identification
5580             (gnus-mode-line-buffer-identification (list mode-string)))
5581       (set-buffer-modified-p t))))
5582
5583 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5584   "Go through the HEADERS list and add all Xrefs to a hash table.
5585 The resulting hash table is returned, or nil if no Xrefs were found."
5586   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5587          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5588          (xref-hashtb (gnus-make-hashtable))
5589          start group entry number xrefs header)
5590     (while headers
5591       (setq header (pop headers))
5592       (when (and (setq xrefs (mail-header-xref header))
5593                  (not (memq (setq number (mail-header-number header))
5594                             unreads)))
5595         (setq start 0)
5596         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5597           (setq start (match-end 0))
5598           (setq group (if prefix
5599                           (concat prefix (substring xrefs (match-beginning 1)
5600                                                     (match-end 1)))
5601                         (substring xrefs (match-beginning 1) (match-end 1))))
5602           (setq number
5603                 (string-to-int (substring xrefs (match-beginning 2)
5604                                           (match-end 2))))
5605           (if (setq entry (gnus-gethash group xref-hashtb))
5606               (setcdr entry (cons number (cdr entry)))
5607             (gnus-sethash group (cons number nil) xref-hashtb)))))
5608     (and start xref-hashtb)))
5609
5610 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5611   "Look through all the headers and mark the Xrefs as read."
5612   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5613         name info xref-hashtb idlist method nth4)
5614     (save-excursion
5615       (set-buffer gnus-group-buffer)
5616       (when (setq xref-hashtb
5617                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5618         (mapatoms
5619          (lambda (group)
5620            (unless (string= from-newsgroup (setq name (symbol-name group)))
5621              (setq idlist (symbol-value group))
5622              ;; Dead groups are not updated.
5623              (and (prog1
5624                       (setq info (gnus-get-info name))
5625                     (when (stringp (setq nth4 (gnus-info-method info)))
5626                       (setq nth4 (gnus-server-to-method nth4))))
5627                   ;; Only do the xrefs if the group has the same
5628                   ;; select method as the group we have just read.
5629                   (or (gnus-methods-equal-p
5630                        nth4 (gnus-find-method-for-group from-newsgroup))
5631                       virtual
5632                       (equal nth4 (setq method (gnus-find-method-for-group
5633                                                 from-newsgroup)))
5634                       (and (equal (car nth4) (car method))
5635                            (equal (nth 1 nth4) (nth 1 method))))
5636                   gnus-use-cross-reference
5637                   (or (not (eq gnus-use-cross-reference t))
5638                       virtual
5639                       ;; Only do cross-references on subscribed
5640                       ;; groups, if that is what is wanted.
5641                       (<= (gnus-info-level info) gnus-level-subscribed))
5642                   (gnus-group-make-articles-read name idlist))))
5643          xref-hashtb)))))
5644
5645 (defun gnus-compute-read-articles (group articles)
5646   (let* ((entry (gnus-group-entry group))
5647          (info (nth 2 entry))
5648          (active (gnus-active group))
5649          ninfo)
5650     (when entry
5651       ;; First peel off all invalid article numbers.
5652       (when active
5653         (let ((ids articles)
5654               id first)
5655           (while (setq id (pop ids))
5656             (when (and first (> id (cdr active)))
5657               ;; We'll end up in this situation in one particular
5658               ;; obscure situation.  If you re-scan a group and get
5659               ;; a new article that is cross-posted to a different
5660               ;; group that has not been re-scanned, you might get
5661               ;; crossposted article that has a higher number than
5662               ;; Gnus believes possible.  So we re-activate this
5663               ;; group as well.  This might mean doing the
5664               ;; crossposting thingy will *increase* the number
5665               ;; of articles in some groups.  Tsk, tsk.
5666               (setq active (or (gnus-activate-group group) active)))
5667             (when (or (> id (cdr active))
5668                       (< id (car active)))
5669               (setq articles (delq id articles))))))
5670       ;; If the read list is nil, we init it.
5671       (if (and active
5672                (null (gnus-info-read info))
5673                (> (car active) 1))
5674           (setq ninfo (cons 1 (1- (car active))))
5675         (setq ninfo (gnus-info-read info)))
5676       ;; Then we add the read articles to the range.
5677       (gnus-add-to-range
5678        ninfo (setq articles (sort articles '<))))))
5679
5680 (defun gnus-group-make-articles-read (group articles)
5681   "Update the info of GROUP to say that ARTICLES are read."
5682   (let* ((num 0)
5683          (entry (gnus-group-entry group))
5684          (info (nth 2 entry))
5685          (active (gnus-active group))
5686          range)
5687     (when entry
5688       (setq range (gnus-compute-read-articles group articles))
5689       (with-current-buffer gnus-group-buffer
5690         (gnus-undo-register
5691           `(progn
5692              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5693              (gnus-info-set-read ',info ',(gnus-info-read info))
5694              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5695              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5696              (gnus-group-update-group ,group t))))
5697       ;; Add the read articles to the range.
5698       (gnus-info-set-read info range)
5699       (gnus-request-set-mark group (list (list range 'add '(read))))
5700       ;; Then we have to re-compute how many unread
5701       ;; articles there are in this group.
5702       (when active
5703         (cond
5704          ((not range)
5705           (setq num (- (1+ (cdr active)) (car active))))
5706          ((not (listp (cdr range)))
5707           (setq num (- (cdr active) (- (1+ (cdr range))
5708                                        (car range)))))
5709          (t
5710           (while range
5711             (if (numberp (car range))
5712                 (setq num (1+ num))
5713               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5714             (setq range (cdr range)))
5715           (setq num (- (cdr active) num))))
5716         ;; Update the number of unread articles.
5717         (setcar entry num)
5718         ;; Update the group buffer.
5719         (unless (gnus-ephemeral-group-p group)
5720           (gnus-group-update-group group t))))))
5721
5722 (defvar gnus-newsgroup-none-id 0)
5723
5724 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5725   (let ((cur nntp-server-buffer)
5726         (dependencies
5727          (or dependencies
5728              (save-excursion (set-buffer gnus-summary-buffer)
5729                              gnus-newsgroup-dependencies)))
5730         headers id end ref
5731         (mail-parse-charset gnus-newsgroup-charset)
5732         (mail-parse-ignored-charsets
5733          (save-excursion (condition-case nil
5734                              (set-buffer gnus-summary-buffer)
5735                            (error))
5736                          gnus-newsgroup-ignored-charsets)))
5737     (save-excursion
5738       (set-buffer nntp-server-buffer)
5739       ;; Translate all TAB characters into SPACE characters.
5740       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5741       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5742       (ietf-drums-unfold-fws)
5743       (gnus-run-hooks 'gnus-parse-headers-hook)
5744       (let ((case-fold-search t)
5745             in-reply-to header p lines chars ctype)
5746         (goto-char (point-min))
5747         ;; Search to the beginning of the next header.  Error messages
5748         ;; do not begin with 2 or 3.
5749         (while (re-search-forward "^[23][0-9]+ " nil t)
5750           (setq id nil
5751                 ref nil)
5752           ;; This implementation of this function, with nine
5753           ;; search-forwards instead of the one re-search-forward and
5754           ;; a case (which basically was the old function) is actually
5755           ;; about twice as fast, even though it looks messier.  You
5756           ;; can't have everything, I guess.  Speed and elegance
5757           ;; doesn't always go hand in hand.
5758           (setq
5759            header
5760            (make-full-mail-header
5761             ;; Number.
5762             (prog1
5763                 (read cur)
5764               (end-of-line)
5765               (setq p (point))
5766               (narrow-to-region (point)
5767                                 (or (and (search-forward "\n.\n" nil t)
5768                                          (- (point) 2))
5769                                     (point))))
5770             ;; Subject.
5771             (progn
5772               (goto-char p)
5773               (if (search-forward "\nsubject:" nil t)
5774                   (nnheader-header-value)
5775                 "(none)"))
5776             ;; From.
5777             (progn
5778               (goto-char p)
5779               (if (search-forward "\nfrom:" nil t)
5780                   (nnheader-header-value)
5781                 "(nobody)"))
5782             ;; Date.
5783             (progn
5784               (goto-char p)
5785               (if (search-forward "\ndate:" nil t)
5786                   (nnheader-header-value) ""))
5787             ;; Message-ID.
5788             (progn
5789               (goto-char p)
5790               (setq id (if (re-search-forward
5791                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5792                            ;; We do it this way to make sure the Message-ID
5793                            ;; is (somewhat) syntactically valid.
5794                            (buffer-substring (match-beginning 1)
5795                                              (match-end 1))
5796                          ;; If there was no message-id, we just fake one
5797                          ;; to make subsequent routines simpler.
5798                          (nnheader-generate-fake-message-id))))
5799             ;; References.
5800             (progn
5801               (goto-char p)
5802               (if (search-forward "\nreferences:" nil t)
5803                   (progn
5804                     (setq end (point))
5805                     (prog1
5806                         (nnheader-header-value)
5807                       (setq ref
5808                             (buffer-substring
5809                              (progn
5810                                ;; (end-of-line)
5811                                (search-backward ">" end t)
5812                                (1+ (point)))
5813                              (progn
5814                                (search-backward "<" end t)
5815                                (point))))))
5816                 ;; Get the references from the in-reply-to header if there
5817                 ;; were no references and the in-reply-to header looks
5818                 ;; promising.
5819                 (if (and (search-forward "\nin-reply-to:" nil t)
5820                          (setq in-reply-to (nnheader-header-value))
5821                          (string-match "<[^>]+>" in-reply-to))
5822                     (let (ref2)
5823                       (setq ref (substring in-reply-to (match-beginning 0)
5824                                            (match-end 0)))
5825                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5826                         (setq ref2 (substring in-reply-to (match-beginning 0)
5827                                               (match-end 0)))
5828                         (when (> (length ref2) (length ref))
5829                           (setq ref ref2)))
5830                       ref)
5831                   (setq ref nil))))
5832             ;; Chars.
5833             (progn
5834               (goto-char p)
5835               (if (search-forward "\nchars: " nil t)
5836                   (if (numberp (setq chars (ignore-errors (read cur))))
5837                       chars -1)
5838                 -1))
5839             ;; Lines.
5840             (progn
5841               (goto-char p)
5842               (if (search-forward "\nlines: " nil t)
5843                   (if (numberp (setq lines (ignore-errors (read cur))))
5844                       lines -1)
5845                 -1))
5846             ;; Xref.
5847             (progn
5848               (goto-char p)
5849               (and (search-forward "\nxref:" nil t)
5850                    (nnheader-header-value)))
5851             ;; Extra.
5852             (when gnus-extra-headers
5853               (let ((extra gnus-extra-headers)
5854                     out)
5855                 (while extra
5856                   (goto-char p)
5857                   (when (search-forward
5858                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5859                     (push (cons (car extra) (nnheader-header-value)) out))
5860                   (pop extra))
5861                 out))))
5862           (goto-char p)
5863           (if (and (search-forward "\ncontent-type: " nil t)
5864                    (setq ctype (nnheader-header-value)))
5865               (mime-entity-set-content-type-internal
5866                header (mime-parse-Content-Type ctype)))
5867           (when (equal id ref)
5868             (setq ref nil))
5869
5870           (when gnus-alter-header-function
5871             (funcall gnus-alter-header-function header)
5872             (setq id (mail-header-id header)
5873                   ref (gnus-parent-id (mail-header-references header))))
5874
5875           (when (setq header
5876                       (gnus-dependencies-add-header
5877                        header dependencies force-new))
5878             (push header headers))
5879           (goto-char (point-max))
5880           (widen))
5881         (nreverse headers)))))
5882
5883 ;; Goes through the xover lines and returns a list of vectors
5884 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5885                                                   force-new dependencies
5886                                                   group also-fetch-heads)
5887   "Parse the news overview data in the server buffer.
5888 Return a list of headers that match SEQUENCE (see
5889 `nntp-retrieve-headers')."
5890   ;; Get the Xref when the users reads the articles since most/some
5891   ;; NNTP servers do not include Xrefs when using XOVER.
5892   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5893   (let ((mail-parse-charset gnus-newsgroup-charset)
5894         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5895         (cur nntp-server-buffer)
5896         (dependencies (or dependencies gnus-newsgroup-dependencies))
5897         (allp (cond
5898                ((eq gnus-read-all-available-headers t)
5899                 t)
5900                ((stringp gnus-read-all-available-headers)
5901                 (string-match gnus-read-all-available-headers group))
5902                (t
5903                 nil)))
5904         number headers header)
5905     (save-excursion
5906       (set-buffer nntp-server-buffer)
5907       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5908       ;; Allow the user to mangle the headers before parsing them.
5909       (gnus-run-hooks 'gnus-parse-headers-hook)
5910       (goto-char (point-min))
5911       (gnus-parse-without-error
5912         (while (and (or sequence allp)
5913                     (not (eobp)))
5914           (setq number (read cur))
5915           (when (not allp)
5916             (while (and sequence
5917                         (< (car sequence) number))
5918               (setq sequence (cdr sequence))))
5919           (when (and (or allp
5920                          (and sequence
5921                               (eq number (car sequence))))
5922                      (progn
5923                        (setq sequence (cdr sequence))
5924                        (setq header (inline
5925                                       (gnus-nov-parse-line
5926                                        number dependencies force-new)))))
5927             (push header headers))
5928           (forward-line 1)))
5929       ;; A common bug in inn is that if you have posted an article and
5930       ;; then retrieves the active file, it will answer correctly --
5931       ;; the new article is included.  However, a NOV entry for the
5932       ;; article may not have been generated yet, so this may fail.
5933       ;; We work around this problem by retrieving the last few
5934       ;; headers using HEAD.
5935       (if (or (not also-fetch-heads)
5936               (not sequence))
5937           ;; We (probably) got all the headers.
5938           (nreverse headers)
5939         (let ((gnus-nov-is-evil t))
5940           (nconc
5941            (nreverse headers)
5942            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5943              (gnus-get-newsgroup-headers))))))))
5944
5945 (defun gnus-article-get-xrefs ()
5946   "Fill in the Xref value in `gnus-current-headers', if necessary.
5947 This is meant to be called in `gnus-article-internal-prepare-hook'."
5948   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5949                                  gnus-current-headers)))
5950     (or (not gnus-use-cross-reference)
5951         (not headers)
5952         (and (mail-header-xref headers)
5953              (not (string= (mail-header-xref headers) "")))
5954         (let ((case-fold-search t)
5955               xref)
5956           (save-restriction
5957             (nnheader-narrow-to-headers)
5958             (goto-char (point-min))
5959             (when (or (and (not (eobp))
5960                            (eq (downcase (char-after)) ?x)
5961                            (looking-at "Xref:"))
5962                       (search-forward "\nXref:" nil t))
5963               (goto-char (1+ (match-end 0)))
5964               (setq xref (buffer-substring (point) (point-at-eol)))
5965               (mail-header-set-xref headers xref)))))))
5966
5967 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5968   "Find article ID and insert the summary line for that article.
5969 OLD-HEADER can either be a header or a line number to insert
5970 the subject line on."
5971   (let* ((line (and (numberp old-header) old-header))
5972          (old-header (and (vectorp old-header) old-header))
5973          (header (cond ((and old-header use-old-header)
5974                         old-header)
5975                        ((and (numberp id)
5976                              (gnus-number-to-header id))
5977                         (gnus-number-to-header id))
5978                        (t
5979                         (gnus-read-header id))))
5980          (number (and (numberp id) id))
5981          d)
5982     (when header
5983       ;; Rebuild the thread that this article is part of and go to the
5984       ;; article we have fetched.
5985       (when (and (not gnus-show-threads)
5986                  old-header)
5987         (when (and number
5988                    (setq d (gnus-data-find (mail-header-number old-header))))
5989           (goto-char (gnus-data-pos d))
5990           (gnus-data-remove
5991            number
5992            (- (point-at-bol)
5993               (prog1
5994                   (1+ (point-at-eol))
5995                 (gnus-delete-line))))))
5996       (when old-header
5997         (mail-header-set-number header (mail-header-number old-header)))
5998       (setq gnus-newsgroup-sparse
5999             (delq (setq number (mail-header-number header))
6000                   gnus-newsgroup-sparse))
6001       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
6002       (push number gnus-newsgroup-limit)
6003       (gnus-rebuild-thread (mail-header-id header) line)
6004       (gnus-summary-goto-subject number nil t))
6005     (when (and (numberp number)
6006                (> number 0))
6007       ;; We have to update the boundaries even if we can't fetch the
6008       ;; article if ID is a number -- so that the next `P' or `N'
6009       ;; command will fetch the previous (or next) article even
6010       ;; if the one we tried to fetch this time has been canceled.
6011       (when (> number gnus-newsgroup-end)
6012         (setq gnus-newsgroup-end number))
6013       (when (< number gnus-newsgroup-begin)
6014         (setq gnus-newsgroup-begin number))
6015       (setq gnus-newsgroup-unselected
6016             (delq number gnus-newsgroup-unselected)))
6017     ;; Report back a success?
6018     (and header (mail-header-number header))))
6019
6020 ;;; Process/prefix in the summary buffer
6021
6022 (defun gnus-summary-work-articles (n)
6023   "Return a list of articles to be worked upon.
6024 The prefix argument, the list of process marked articles, and the
6025 current article will be taken into consideration."
6026   (save-excursion
6027     (set-buffer gnus-summary-buffer)
6028     (cond
6029      (n
6030       ;; A numerical prefix has been given.
6031       (setq n (prefix-numeric-value n))
6032       (let ((backward (< n 0))
6033             (n (abs (prefix-numeric-value n)))
6034             articles article)
6035         (save-excursion
6036           (while
6037               (and (> n 0)
6038                    (push (setq article (gnus-summary-article-number))
6039                          articles)
6040                    (if backward
6041                        (gnus-summary-find-prev nil article)
6042                      (gnus-summary-find-next nil article)))
6043             (decf n)))
6044         (nreverse articles)))
6045      ((and (gnus-region-active-p) (mark))
6046       (message "region active")
6047       ;; Work on the region between point and mark.
6048       (let ((max (max (point) (mark)))
6049             articles article)
6050         (save-excursion
6051           (goto-char (min (point) (mark)))
6052           (while
6053               (and
6054                (push (setq article (gnus-summary-article-number)) articles)
6055                (gnus-summary-find-next nil article)
6056                (< (point) max)))
6057           (nreverse articles))))
6058      (gnus-newsgroup-processable
6059       ;; There are process-marked articles present.
6060       ;; Save current state.
6061       (gnus-summary-save-process-mark)
6062       ;; Return the list.
6063       (reverse gnus-newsgroup-processable))
6064      (t
6065       ;; Just return the current article.
6066       (list (gnus-summary-article-number))))))
6067
6068 (defmacro gnus-summary-iterate (arg &rest forms)
6069   "Iterate over the process/prefixed articles and do FORMS.
6070 ARG is the interactive prefix given to the command.  FORMS will be
6071 executed with point over the summary line of the articles."
6072   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6073     `(let ((,articles (gnus-summary-work-articles ,arg)))
6074        (while ,articles
6075          (gnus-summary-goto-subject (car ,articles))
6076          ,@forms
6077          (pop ,articles)))))
6078
6079 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6080 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6081
6082 (defun gnus-summary-save-process-mark ()
6083   "Push the current set of process marked articles on the stack."
6084   (interactive)
6085   (push (copy-sequence gnus-newsgroup-processable)
6086         gnus-newsgroup-process-stack))
6087
6088 (defun gnus-summary-kill-process-mark ()
6089   "Push the current set of process marked articles on the stack and unmark."
6090   (interactive)
6091   (gnus-summary-save-process-mark)
6092   (gnus-summary-unmark-all-processable))
6093
6094 (defun gnus-summary-yank-process-mark ()
6095   "Pop the last process mark state off the stack and restore it."
6096   (interactive)
6097   (unless gnus-newsgroup-process-stack
6098     (error "Empty mark stack"))
6099   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6100
6101 (defun gnus-summary-process-mark-set (set)
6102   "Make SET into the current process marked articles."
6103   (gnus-summary-unmark-all-processable)
6104   (while set
6105     (gnus-summary-set-process-mark (pop set))))
6106
6107 ;;; Searching and stuff
6108
6109 (defun gnus-summary-search-group (&optional backward use-level)
6110   "Search for next unread newsgroup.
6111 If optional argument BACKWARD is non-nil, search backward instead."
6112   (save-excursion
6113     (set-buffer gnus-group-buffer)
6114     (when (gnus-group-search-forward
6115            backward nil (if use-level (gnus-group-group-level) nil))
6116       (gnus-group-group-name))))
6117
6118 (defun gnus-summary-best-group (&optional exclude-group)
6119   "Find the name of the best unread group.
6120 If EXCLUDE-GROUP, do not go to this group."
6121   (save-excursion
6122     (set-buffer gnus-group-buffer)
6123     (save-excursion
6124       (gnus-group-best-unread-group exclude-group))))
6125
6126 (defun gnus-summary-find-next (&optional unread article backward)
6127   (if backward
6128       (gnus-summary-find-prev unread article)
6129     (let* ((dummy (gnus-summary-article-intangible-p))
6130            (article (or article (gnus-summary-article-number)))
6131            (data (gnus-data-find-list article))
6132            result)
6133       (when (and (not dummy)
6134                  (or (not gnus-summary-check-current)
6135                      (not unread)
6136                      (not (gnus-data-unread-p (car data)))))
6137         (setq data (cdr data)))
6138       (when (setq result
6139                   (if unread
6140                       (progn
6141                         (while data
6142                           (unless (memq (gnus-data-number (car data))
6143                                         (cond
6144                                          ((eq gnus-auto-goto-ignores
6145                                               'always-undownloaded)
6146                                           gnus-newsgroup-undownloaded)
6147                                          (gnus-plugged
6148                                           nil)
6149                                          ((eq gnus-auto-goto-ignores
6150                                               'unfetched)
6151                                           gnus-newsgroup-unfetched)
6152                                          ((eq gnus-auto-goto-ignores
6153                                               'undownloaded)
6154                                           gnus-newsgroup-undownloaded)))
6155                             (when (gnus-data-unread-p (car data))
6156                               (setq result (car data)
6157                                     data nil)))
6158                           (setq data (cdr data)))
6159                         result)
6160                     (car data)))
6161         (goto-char (gnus-data-pos result))
6162         (gnus-data-number result)))))
6163
6164 (defun gnus-summary-find-prev (&optional unread article)
6165   (let* ((eobp (eobp))
6166          (article (or article (gnus-summary-article-number)))
6167          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6168          result)
6169     (when (and (not eobp)
6170                (or (not gnus-summary-check-current)
6171                    (not unread)
6172                    (not (gnus-data-unread-p (car data)))))
6173       (setq data (cdr data)))
6174     (when (setq result
6175                 (if unread
6176                     (progn
6177                       (while data
6178                         (unless (memq (gnus-data-number (car data))
6179                                       (cond
6180                                        ((eq gnus-auto-goto-ignores
6181                                             'always-undownloaded)
6182                                         gnus-newsgroup-undownloaded)
6183                                        (gnus-plugged
6184                                         nil)
6185                                        ((eq gnus-auto-goto-ignores
6186                                             'unfetched)
6187                                         gnus-newsgroup-unfetched)
6188                                        ((eq gnus-auto-goto-ignores
6189                                             'undownloaded)
6190                                         gnus-newsgroup-undownloaded)))
6191                           (when (gnus-data-unread-p (car data))
6192                             (setq result (car data)
6193                                   data nil)))
6194                         (setq data (cdr data)))
6195                       result)
6196                   (car data)))
6197       (goto-char (gnus-data-pos result))
6198       (gnus-data-number result))))
6199
6200 (defun gnus-summary-find-subject (subject &optional unread backward article)
6201   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6202          (article (or article (gnus-summary-article-number)))
6203          (articles (gnus-data-list backward))
6204          (arts (gnus-data-find-list article articles))
6205          result)
6206     (when (or (not gnus-summary-check-current)
6207               (not unread)
6208               (not (gnus-data-unread-p (car arts))))
6209       (setq arts (cdr arts)))
6210     (while arts
6211       (and (or (not unread)
6212                (gnus-data-unread-p (car arts)))
6213            (vectorp (gnus-data-header (car arts)))
6214            (gnus-subject-equal
6215             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6216            (setq result (car arts)
6217                  arts nil))
6218       (setq arts (cdr arts)))
6219     (and result
6220          (goto-char (gnus-data-pos result))
6221          (gnus-data-number result))))
6222
6223 (defun gnus-summary-search-forward (&optional unread subject backward)
6224   "Search forward for an article.
6225 If UNREAD, look for unread articles.  If SUBJECT, look for
6226 articles with that subject.  If BACKWARD, search backward instead."
6227   (cond (subject (gnus-summary-find-subject subject unread backward))
6228         (backward (gnus-summary-find-prev unread))
6229         (t (gnus-summary-find-next unread))))
6230
6231 (defun gnus-recenter (&optional n)
6232   "Center point in window and redisplay frame.
6233 Also do horizontal recentering."
6234   (interactive "P")
6235   (when (and gnus-auto-center-summary
6236              (not (eq gnus-auto-center-summary 'vertical)))
6237     (gnus-horizontal-recenter))
6238   (recenter n))
6239
6240 (defun gnus-summary-recenter ()
6241   "Center point in the summary window.
6242 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6243 displayed, no centering will be performed."
6244   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6245   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6246   (interactive)
6247   ;; The user has to want it.
6248   (when gnus-auto-center-summary
6249     (let* ((top (cond ((< (window-height) 4) 0)
6250                       ((< (window-height) 7) 1)
6251                       (t (if (numberp gnus-auto-center-summary)
6252                              gnus-auto-center-summary
6253                            2))))
6254            (height (1- (window-height)))
6255            (bottom (save-excursion (goto-char (point-max))
6256                                    (forward-line (- height))
6257                                    (point)))
6258            (window (get-buffer-window (current-buffer))))
6259       (when (get-buffer-window gnus-article-buffer)
6260         ;; Only do recentering when the article buffer is displayed,
6261         ;; Set the window start to either `bottom', which is the biggest
6262         ;; possible valid number, or the second line from the top,
6263         ;; whichever is the least.
6264         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6265           (if (> bottom top-pos)
6266               ;; Keep the second line from the top visible
6267               (set-window-start window top-pos t)
6268             ;; Try to keep the bottom line visible; if it's partially
6269             ;; obscured, either scroll one more line to make it fully
6270             ;; visible, or revert to using TOP-POS.
6271             (save-excursion
6272               (goto-char (point-max))
6273               (forward-line -1)
6274               (let ((last-line-start (point)))
6275                 (goto-char bottom)
6276                 (set-window-start window (point) t)
6277                 (when (not (pos-visible-in-window-p last-line-start window))
6278                   (forward-line 1)
6279                   (set-window-start window (min (point) top-pos) t)))))))
6280       ;; Do horizontal recentering while we're at it.
6281       (when (and (get-buffer-window (current-buffer) t)
6282                  (not (eq gnus-auto-center-summary 'vertical)))
6283         (let ((selected (selected-window)))
6284           (select-window (get-buffer-window (current-buffer) t))
6285           (gnus-summary-position-point)
6286           (gnus-horizontal-recenter)
6287           (select-window selected))))))
6288
6289 (defun gnus-summary-jump-to-group (newsgroup)
6290   "Move point to NEWSGROUP in group mode buffer."
6291   ;; Keep update point of group mode buffer if visible.
6292   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6293       (save-window-excursion
6294         ;; Take care of tree window mode.
6295         (when (get-buffer-window gnus-group-buffer)
6296           (pop-to-buffer gnus-group-buffer))
6297         (gnus-group-jump-to-group newsgroup))
6298     (save-excursion
6299       ;; Take care of tree window mode.
6300       (if (get-buffer-window gnus-group-buffer)
6301           (pop-to-buffer gnus-group-buffer)
6302         (set-buffer gnus-group-buffer))
6303       (gnus-group-jump-to-group newsgroup))))
6304
6305 ;; This function returns a list of article numbers based on the
6306 ;; difference between the ranges of read articles in this group and
6307 ;; the range of active articles.
6308 (defun gnus-list-of-unread-articles (group)
6309   (let* ((read (gnus-info-read (gnus-get-info group)))
6310          (active (or (gnus-active group) (gnus-activate-group group)))
6311          (last (cdr active))
6312          first nlast unread)
6313     ;; If none are read, then all are unread.
6314     (if (not read)
6315         (setq first (car active))
6316       ;; If the range of read articles is a single range, then the
6317       ;; first unread article is the article after the last read
6318       ;; article.  Sounds logical, doesn't it?
6319       (if (and (not (listp (cdr read)))
6320                (or (< (car read) (car active))
6321                    (progn (setq read (list read))
6322                           nil)))
6323           (setq first (max (car active) (1+ (cdr read))))
6324         ;; `read' is a list of ranges.
6325         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6326                                   (caar read)))
6327                   1)
6328           (setq first (car active)))
6329         (while read
6330           (when first
6331             (while (< first nlast)
6332               (setq unread (cons first unread)
6333                     first (1+ first))))
6334           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6335           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6336           (setq read (cdr read)))))
6337     ;; And add the last unread articles.
6338     (while (<= first last)
6339       (setq unread (cons first unread)
6340             first (1+ first)))
6341     ;; Return the list of unread articles.
6342     (delq 0 (nreverse unread))))
6343
6344 (defun gnus-list-of-read-articles (group)
6345   "Return a list of unread, unticked and non-dormant articles."
6346   (let* ((info (gnus-get-info group))
6347          (marked (gnus-info-marks info))
6348          (active (gnus-active group)))
6349     (and info active
6350          (gnus-list-range-difference
6351           (gnus-list-range-difference
6352            (gnus-sorted-complement
6353             (gnus-uncompress-range active)
6354             (gnus-list-of-unread-articles group))
6355            (cdr (assq 'dormant marked)))
6356           (cdr (assq 'tick marked))))))
6357
6358 ;; This function returns a sequence of article numbers based on the
6359 ;; difference between the ranges of read articles in this group and
6360 ;; the range of active articles.
6361 (defun gnus-sequence-of-unread-articles (group)
6362   (let* ((read (gnus-info-read (gnus-get-info group)))
6363          (active (or (gnus-active group) (gnus-activate-group group)))
6364          (last (cdr active))
6365          first nlast unread)
6366     ;; If none are read, then all are unread.
6367     (if (not read)
6368         (setq first (car active))
6369       ;; If the range of read articles is a single range, then the
6370       ;; first unread article is the article after the last read
6371       ;; article.  Sounds logical, doesn't it?
6372       (if (and (not (listp (cdr read)))
6373                (or (< (car read) (car active))
6374                    (progn (setq read (list read))
6375                           nil)))
6376           (setq first (max (car active) (1+ (cdr read))))
6377         ;; `read' is a list of ranges.
6378         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6379                                   (caar read)))
6380                   1)
6381           (setq first (car active)))
6382         (while read
6383           (when first
6384             (push (cons first nlast) unread))
6385           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6386           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6387           (setq read (cdr read)))))
6388     ;; And add the last unread articles.
6389     (cond ((< first last)
6390            (push (cons first last) unread))
6391           ((= first last)
6392            (push first unread)))
6393     ;; Return the sequence of unread articles.
6394     (delq 0 (nreverse unread))))
6395
6396 ;; Various summary commands
6397
6398 (defun gnus-summary-select-article-buffer ()
6399   "Reconfigure windows to show article buffer."
6400   (interactive)
6401   (if (not (gnus-buffer-live-p gnus-article-buffer))
6402       (error "There is no article buffer for this summary buffer")
6403     (gnus-configure-windows 'article)
6404     (select-window (get-buffer-window gnus-article-buffer))))
6405
6406 (defun gnus-summary-universal-argument (arg)
6407   "Perform any operation on all articles that are process/prefixed."
6408   (interactive "P")
6409   (let ((articles (gnus-summary-work-articles arg))
6410         func article)
6411     (if (eq
6412          (setq
6413           func
6414           (key-binding
6415            (read-key-sequence
6416             (substitute-command-keys
6417              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6418          'undefined)
6419         (gnus-error 1 "Undefined key")
6420       (save-excursion
6421         (while articles
6422           (gnus-summary-goto-subject (setq article (pop articles)))
6423           (let (gnus-newsgroup-processable)
6424             (command-execute func))
6425           (gnus-summary-remove-process-mark article)))))
6426   (gnus-summary-position-point))
6427
6428 (defun gnus-summary-toggle-truncation (&optional arg)
6429   "Toggle truncation of summary lines.
6430 With ARG, turn line truncation on if ARG is positive."
6431   (interactive "P")
6432   (setq truncate-lines
6433         (if (null arg) (not truncate-lines)
6434           (> (prefix-numeric-value arg) 0)))
6435   (redraw-display))
6436
6437 (defun gnus-summary-find-for-reselect ()
6438   "Return the number of an article to stay on across a reselect.
6439 The current article is considered, then following articles, then previous
6440 articles.  An article is sought which is not cancelled and isn't a temporary
6441 insertion from another group.  If there's no such then return a dummy 0."
6442   (let (found)
6443     (dolist (rev '(nil t))
6444       (unless found      ; don't demand the reverse list if we don't need it
6445         (let ((data (gnus-data-find-list
6446                      (gnus-summary-article-number) (gnus-data-list rev))))
6447           (while (and data (not found))
6448             (if (and (< 0 (gnus-data-number (car data)))
6449                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6450                 (setq found (gnus-data-number (car data))))
6451             (setq data (cdr data))))))
6452     (or found 0)))
6453
6454 (defun gnus-summary-reselect-current-group (&optional all rescan)
6455   "Exit and then reselect the current newsgroup.
6456 The prefix argument ALL means to select all articles."
6457   (interactive "P")
6458   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6459     (error "Ephemeral groups can't be reselected"))
6460   (let ((current-subject (gnus-summary-find-for-reselect))
6461         (group gnus-newsgroup-name))
6462     (setq gnus-newsgroup-begin nil)
6463     (gnus-summary-exit nil 'leave-hidden)
6464     ;; We have to adjust the point of group mode buffer because
6465     ;; point was moved to the next unread newsgroup by exiting.
6466     (gnus-summary-jump-to-group group)
6467     (when rescan
6468       (save-excursion
6469         (save-window-excursion
6470           ;; Don't show group contents.
6471           (set-window-start (selected-window) (point-max))
6472           (gnus-group-get-new-news-this-group 1))))
6473     (gnus-group-read-group all t)
6474     (gnus-summary-goto-subject current-subject nil t)))
6475
6476 (defun gnus-summary-rescan-group (&optional all)
6477   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6478   (interactive "P")
6479   (gnus-summary-reselect-current-group all t))
6480
6481 (defun gnus-summary-update-info (&optional non-destructive)
6482   (save-excursion
6483     (let ((group gnus-newsgroup-name))
6484       (when group
6485         (when gnus-newsgroup-kill-headers
6486           (setq gnus-newsgroup-killed
6487                 (gnus-compress-sequence
6488                  (gnus-sorted-union
6489                   (gnus-list-range-intersection
6490                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6491                   gnus-newsgroup-unreads)
6492                  t)))
6493         (unless (listp (cdr gnus-newsgroup-killed))
6494           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6495         (let ((headers gnus-newsgroup-headers))
6496           ;; Set the new ranges of read articles.
6497           (save-excursion
6498             (set-buffer gnus-group-buffer)
6499             (gnus-undo-force-boundary))
6500           (gnus-update-read-articles
6501            group (gnus-sorted-union
6502                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6503           ;; Set the current article marks.
6504           (let ((gnus-newsgroup-scored
6505                  (if (and (not gnus-save-score)
6506                           (not non-destructive))
6507                      nil
6508                    gnus-newsgroup-scored)))
6509             (save-excursion
6510               (gnus-update-marks)))
6511           ;; Do the cross-ref thing.
6512           (when gnus-use-cross-reference
6513             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6514           ;; Do not switch windows but change the buffer to work.
6515           (set-buffer gnus-group-buffer)
6516           (unless (gnus-ephemeral-group-p group)
6517             (gnus-group-update-group group)))))))
6518
6519 (defun gnus-summary-save-newsrc (&optional force)
6520   "Save the current number of read/marked articles in the dribble buffer.
6521 The dribble buffer will then be saved.
6522 If FORCE (the prefix), also save the .newsrc file(s)."
6523   (interactive "P")
6524   (gnus-summary-update-info t)
6525   (if force
6526       (gnus-save-newsrc-file)
6527     (gnus-dribble-save)))
6528
6529 (defun gnus-summary-exit (&optional temporary leave-hidden)
6530   "Exit reading current newsgroup, and then return to group selection mode.
6531 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6532   (interactive)
6533   (gnus-set-global-variables)
6534   (gnus-kill-save-kill-buffer)
6535   (gnus-async-halt-prefetch)
6536   (let* ((group gnus-newsgroup-name)
6537          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6538          (gnus-group-is-exiting-p t)
6539          (mode major-mode)
6540          (group-point nil)
6541          (buf (current-buffer)))
6542     (unless quit-config
6543       ;; Do adaptive scoring, and possibly save score files.
6544       (when gnus-newsgroup-adaptive
6545         (gnus-score-adaptive))
6546       (when gnus-use-scoring
6547         (gnus-score-save)))
6548     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6549     ;; If we have several article buffers, we kill them at exit.
6550     (unless gnus-single-article-buffer
6551       (gnus-kill-buffer gnus-original-article-buffer)
6552       (setq gnus-article-current nil))
6553     (when gnus-use-cache
6554       (gnus-cache-possibly-remove-articles)
6555       (gnus-cache-save-buffers))
6556     (gnus-async-prefetch-remove-group group)
6557     (when gnus-suppress-duplicates
6558       (gnus-dup-enter-articles))
6559     (when gnus-use-trees
6560       (gnus-tree-close group))
6561     (when gnus-use-cache
6562       (gnus-cache-write-active))
6563     ;; Remove entries for this group.
6564     (nnmail-purge-split-history (gnus-group-real-name group))
6565     ;; Make all changes in this group permanent.
6566     (unless quit-config
6567       (gnus-run-hooks 'gnus-exit-group-hook)
6568       (gnus-summary-update-info))
6569     (gnus-close-group group)
6570     ;; Make sure where we were, and go to next newsgroup.
6571     (set-buffer gnus-group-buffer)
6572     (unless quit-config
6573       (gnus-group-jump-to-group group))
6574     (gnus-run-hooks 'gnus-summary-exit-hook)
6575     (unless (or quit-config
6576                 ;; If this group has disappeared from the summary
6577                 ;; buffer, don't skip forwards.
6578                 (not (string= group (gnus-group-group-name))))
6579       (gnus-group-next-unread-group 1))
6580     (setq group-point (point))
6581     (if temporary
6582         nil                             ;Nothing to do.
6583       ;; If we have several article buffers, we kill them at exit.
6584       (unless gnus-single-article-buffer
6585         (gnus-kill-buffer gnus-article-buffer)
6586         (gnus-kill-buffer gnus-original-article-buffer)
6587         (setq gnus-article-current nil))
6588       (set-buffer buf)
6589       (if (not gnus-kill-summary-on-exit)
6590           (progn
6591             (gnus-deaden-summary)
6592             (setq mode nil))
6593         ;; We set all buffer-local variables to nil.  It is unclear why
6594         ;; this is needed, but if we don't, buffer-local variables are
6595         ;; not garbage-collected, it seems.  This would the lead to en
6596         ;; ever-growing Emacs.
6597         (gnus-summary-clear-local-variables)
6598         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6599           (gnus-summary-clear-local-variables))
6600         (when (get-buffer gnus-article-buffer)
6601           (bury-buffer gnus-article-buffer))
6602         ;; We clear the global counterparts of the buffer-local
6603         ;; variables as well, just to be on the safe side.
6604         (set-buffer gnus-group-buffer)
6605         (gnus-summary-clear-local-variables)
6606         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6607           (gnus-summary-clear-local-variables))
6608         ;; Return to group mode buffer.
6609         (when (eq mode 'gnus-summary-mode)
6610           (gnus-kill-buffer buf)))
6611       (setq gnus-current-select-method gnus-select-method)
6612       (if leave-hidden
6613           (set-buffer gnus-group-buffer)
6614         (pop-to-buffer gnus-group-buffer))
6615       (if (not quit-config)
6616           (progn
6617             (goto-char group-point)
6618             (unless leave-hidden
6619               (gnus-configure-windows 'group 'force))
6620             (unless (pos-visible-in-window-p)
6621               (forward-line (/ (static-if (featurep 'xemacs)
6622                                    (window-displayed-height)
6623                                  (1- (window-height)))
6624                                -2))
6625               (set-window-start (selected-window) (point))
6626               (goto-char group-point)))
6627         (gnus-handle-ephemeral-exit quit-config))
6628       ;; Clear the current group name.
6629       (unless quit-config
6630         (setq gnus-newsgroup-name nil)))))
6631
6632 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6633 (defun gnus-summary-exit-no-update (&optional no-questions)
6634   "Quit reading current newsgroup without updating read article info."
6635   (interactive)
6636   (let* ((group gnus-newsgroup-name)
6637          (gnus-group-is-exiting-p t)
6638          (gnus-group-is-exiting-without-update-p t)
6639          (quit-config (gnus-group-quit-config group)))
6640     (when (or no-questions
6641               gnus-expert-user
6642               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6643       (gnus-async-halt-prefetch)
6644       (run-hooks 'gnus-summary-prepare-exit-hook)
6645       ;; If we have several article buffers, we kill them at exit.
6646       (unless gnus-single-article-buffer
6647         (gnus-kill-buffer gnus-article-buffer)
6648         (gnus-kill-buffer gnus-original-article-buffer)
6649         (setq gnus-article-current nil))
6650       (if (not gnus-kill-summary-on-exit)
6651           (gnus-deaden-summary)
6652         (gnus-close-group group)
6653         (gnus-summary-clear-local-variables)
6654         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6655           (gnus-summary-clear-local-variables))
6656         (set-buffer gnus-group-buffer)
6657         (gnus-summary-clear-local-variables)
6658         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6659           (gnus-summary-clear-local-variables))
6660         (gnus-kill-buffer gnus-summary-buffer))
6661       (unless gnus-single-article-buffer
6662         (setq gnus-article-current nil))
6663       (when gnus-use-trees
6664         (gnus-tree-close group))
6665       (gnus-async-prefetch-remove-group group)
6666       (when (get-buffer gnus-article-buffer)
6667         (bury-buffer gnus-article-buffer))
6668       ;; Return to the group buffer.
6669       (gnus-configure-windows 'group 'force)
6670       ;; Clear the current group name.
6671       (setq gnus-newsgroup-name nil)
6672       (unless (gnus-ephemeral-group-p group)
6673         (gnus-group-update-group group))
6674       (when (equal (gnus-group-group-name) group)
6675         (gnus-group-next-unread-group 1))
6676       (when quit-config
6677         (gnus-handle-ephemeral-exit quit-config)))))
6678
6679 (defun gnus-handle-ephemeral-exit (quit-config)
6680   "Handle movement when leaving an ephemeral group.
6681 The state which existed when entering the ephemeral is reset."
6682   (if (not (buffer-name (car quit-config)))
6683       (gnus-configure-windows 'group 'force)
6684     (set-buffer (car quit-config))
6685     (cond ((eq major-mode 'gnus-summary-mode)
6686            (gnus-set-global-variables))
6687           ((eq major-mode 'gnus-article-mode)
6688            (save-excursion
6689              ;; The `gnus-summary-buffer' variable may point
6690              ;; to the old summary buffer when using a single
6691              ;; article buffer.
6692              (unless (gnus-buffer-live-p gnus-summary-buffer)
6693                (set-buffer gnus-group-buffer))
6694              (set-buffer gnus-summary-buffer)
6695              (gnus-set-global-variables))))
6696     (if (or (eq (cdr quit-config) 'article)
6697             (eq (cdr quit-config) 'pick))
6698         (progn
6699           ;; The current article may be from the ephemeral group
6700           ;; thus it is best that we reload this article
6701           ;;
6702           ;; If we're exiting from a large digest, this can be
6703           ;; extremely slow.  So, it's better not to reload it. -- jh.
6704           ;;(gnus-summary-show-article)
6705           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6706               (gnus-configure-windows 'pick 'force)
6707             (gnus-configure-windows (cdr quit-config) 'force)))
6708       (gnus-configure-windows (cdr quit-config) 'force))
6709     (when (eq major-mode 'gnus-summary-mode)
6710       (gnus-summary-next-subject 1 nil t)
6711       (gnus-summary-recenter)
6712       (gnus-summary-position-point))))
6713
6714 (defun gnus-summary-preview-mime-message ()
6715   "MIME decode and play this message."
6716   (interactive)
6717   (let ((gnus-break-pages nil)
6718         (gnus-show-mime t))
6719     (gnus-summary-select-article gnus-show-all-headers t))
6720   (let ((w (get-buffer-window gnus-article-buffer)))
6721     (when w
6722       (select-window (get-buffer-window gnus-article-buffer)))))
6723
6724 ;;; Dead summaries.
6725
6726 (defvar gnus-dead-summary-mode-map nil)
6727
6728 (unless gnus-dead-summary-mode-map
6729   (setq gnus-dead-summary-mode-map (make-keymap))
6730   (suppress-keymap gnus-dead-summary-mode-map)
6731   (substitute-key-definition
6732    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6733   (dolist (key '("\C-d" "\r" "\177" [delete]))
6734     (define-key gnus-dead-summary-mode-map
6735       key 'gnus-summary-wake-up-the-dead))
6736   (dolist (key '("q" "Q"))
6737     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6738
6739 (defvar gnus-dead-summary-mode nil
6740   "Minor mode for Gnus summary buffers.")
6741
6742 (defun gnus-dead-summary-mode (&optional arg)
6743   "Minor mode for Gnus summary buffers."
6744   (interactive "P")
6745   (when (eq major-mode 'gnus-summary-mode)
6746     (make-local-variable 'gnus-dead-summary-mode)
6747     (setq gnus-dead-summary-mode
6748           (if (null arg) (not gnus-dead-summary-mode)
6749             (> (prefix-numeric-value arg) 0)))
6750     (when gnus-dead-summary-mode
6751       (add-minor-mode
6752        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6753
6754 (defun gnus-deaden-summary ()
6755   "Make the current summary buffer into a dead summary buffer."
6756   ;; Kill any previous dead summary buffer.
6757   (when (and gnus-dead-summary
6758              (buffer-name gnus-dead-summary))
6759     (save-excursion
6760       (set-buffer gnus-dead-summary)
6761       (when gnus-dead-summary-mode
6762         (kill-buffer (current-buffer)))))
6763   ;; Make this the current dead summary.
6764   (setq gnus-dead-summary (current-buffer))
6765   (gnus-dead-summary-mode 1)
6766   (let ((name (buffer-name)))
6767     (when (string-match "Summary" name)
6768       (rename-buffer
6769        (concat (substring name 0 (match-beginning 0)) "Dead "
6770                (substring name (match-beginning 0)))
6771        t)
6772       (bury-buffer))))
6773
6774 (defun gnus-kill-or-deaden-summary (buffer)
6775   "Kill or deaden the summary BUFFER."
6776   (save-excursion
6777     (when (and (buffer-name buffer)
6778                (not gnus-single-article-buffer))
6779       (save-excursion
6780         (set-buffer buffer)
6781         (gnus-kill-buffer gnus-article-buffer)
6782         (gnus-kill-buffer gnus-original-article-buffer)))
6783     (cond
6784      ;; Kill the buffer.
6785      (gnus-kill-summary-on-exit
6786       (when (and gnus-use-trees
6787                  (gnus-buffer-exists-p buffer))
6788         (save-excursion
6789           (set-buffer buffer)
6790           (gnus-tree-close gnus-newsgroup-name)))
6791       (gnus-kill-buffer buffer))
6792      ;; Deaden the buffer.
6793      ((gnus-buffer-exists-p buffer)
6794       (save-excursion
6795         (set-buffer buffer)
6796         (gnus-deaden-summary))))))
6797
6798 (defun gnus-summary-wake-up-the-dead (&rest args)
6799   "Wake up the dead summary buffer."
6800   (interactive)
6801   (gnus-dead-summary-mode -1)
6802   (let ((name (buffer-name)))
6803     (when (string-match "Dead " name)
6804       (rename-buffer
6805        (concat (substring name 0 (match-beginning 0))
6806                (substring name (match-end 0)))
6807        t)))
6808   (gnus-message 3 "This dead summary is now alive again"))
6809
6810 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6811 (defun gnus-summary-fetch-faq (&optional faq-dir)
6812   "Fetch the FAQ for the current group.
6813 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6814 in."
6815   (interactive
6816    (list
6817     (when current-prefix-arg
6818       (completing-read
6819        "FAQ dir: " (and (listp gnus-group-faq-directory)
6820                         (mapcar (lambda (file) (list file))
6821                                 gnus-group-faq-directory))))))
6822   (let (gnus-faq-buffer)
6823     (when (setq gnus-faq-buffer
6824                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6825       (gnus-configure-windows 'summary-faq))))
6826
6827 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6828 (defun gnus-summary-describe-group (&optional force)
6829   "Describe the current newsgroup."
6830   (interactive "P")
6831   (gnus-group-describe-group force gnus-newsgroup-name))
6832
6833 (defun gnus-summary-describe-briefly ()
6834   "Describe summary mode commands briefly."
6835   (interactive)
6836   (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")))
6837
6838 ;; Walking around group mode buffer from summary mode.
6839
6840 (defun gnus-summary-next-group (&optional no-article target-group backward)
6841   "Exit current newsgroup and then select next unread newsgroup.
6842 If prefix argument NO-ARTICLE is non-nil, no article is selected
6843 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6844 previous group instead."
6845   (interactive "P")
6846   ;; Stop pre-fetching.
6847   (gnus-async-halt-prefetch)
6848   (let ((current-group gnus-newsgroup-name)
6849         (current-buffer (current-buffer))
6850         entered)
6851     ;; First we semi-exit this group to update Xrefs and all variables.
6852     ;; We can't do a real exit, because the window conf must remain
6853     ;; the same in case the user is prompted for info, and we don't
6854     ;; want the window conf to change before that...
6855     (gnus-summary-exit t)
6856     (while (not entered)
6857       ;; Then we find what group we are supposed to enter.
6858       (set-buffer gnus-group-buffer)
6859       (gnus-group-jump-to-group current-group)
6860       (setq target-group
6861             (or target-group
6862                 (if (eq gnus-keep-same-level 'best)
6863                     (gnus-summary-best-group gnus-newsgroup-name)
6864                   (gnus-summary-search-group backward gnus-keep-same-level))))
6865       (if (not target-group)
6866           ;; There are no further groups, so we return to the group
6867           ;; buffer.
6868           (progn
6869             (gnus-message 5 "Returning to the group buffer")
6870             (setq entered t)
6871             (when (gnus-buffer-live-p current-buffer)
6872               (set-buffer current-buffer)
6873               (gnus-summary-exit))
6874             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6875         ;; We try to enter the target group.
6876         (gnus-group-jump-to-group target-group)
6877         (let ((unreads (gnus-group-group-unread)))
6878           (if (and (or (eq t unreads)
6879                        (and unreads (not (zerop unreads))))
6880                    (gnus-summary-read-group
6881                     target-group nil no-article
6882                     (and (buffer-name current-buffer) current-buffer)
6883                     nil backward))
6884               (setq entered t)
6885             (setq current-group target-group
6886                   target-group nil)))))))
6887
6888 (defun gnus-summary-prev-group (&optional no-article)
6889   "Exit current newsgroup and then select previous unread newsgroup.
6890 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6891   (interactive "P")
6892   (gnus-summary-next-group no-article nil t))
6893
6894 ;; Walking around summary lines.
6895
6896 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6897   "Go to the first subject satisfying any non-nil constraint.
6898 If UNREAD is non-nil, the article should be unread.
6899 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6900 If UNSEEN is non-nil, the article should be unseen.
6901 Returns the article selected or nil if there are no matching articles."
6902   (interactive "P")
6903   (cond
6904    ;; Empty summary.
6905    ((null gnus-newsgroup-data)
6906     (gnus-message 3 "No articles in the group")
6907     nil)
6908    ;; Pick the first article.
6909    ((not (or unread undownloaded unseen))
6910     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6911     (gnus-data-number (car gnus-newsgroup-data)))
6912    ;; Find the first unread article.
6913    (t
6914     (let ((data gnus-newsgroup-data))
6915       (while (and data
6916                   (let ((num (gnus-data-number (car data))))
6917                     (or (memq num gnus-newsgroup-unfetched)
6918                         (not (or (and unread
6919                                       (memq num gnus-newsgroup-unreads))
6920                                  (and undownloaded
6921                                       (memq num gnus-newsgroup-undownloaded))
6922                                  (and unseen
6923                                       (memq num gnus-newsgroup-unseen)))))))
6924         (setq data (cdr data)))
6925       (prog1
6926           (if data
6927               (progn
6928                 (goto-char (gnus-data-pos (car data)))
6929                 (gnus-data-number (car data)))
6930             (gnus-message 3 "No more%s articles"
6931                           (let* ((r (when unread " unread"))
6932                                  (d (when undownloaded " undownloaded"))
6933                                  (s (when unseen " unseen"))
6934                                  (l (delq nil (list r d s))))
6935                             (cond ((= 3 (length l))
6936                                    (concat r "," d ", or" s))
6937                                   ((= 2 (length l))
6938                                    (concat (car l) ", or" (cadr l)))
6939                                   ((= 1 (length l))
6940                                    (car l))
6941                                   (t
6942                                    ""))))
6943             nil
6944             )
6945         (gnus-summary-position-point))))))
6946
6947 (defun gnus-summary-next-subject (n &optional unread dont-display)
6948   "Go to next N'th summary line.
6949 If N is negative, go to the previous N'th subject line.
6950 If UNREAD is non-nil, only unread articles are selected.
6951 The difference between N and the actual number of steps taken is
6952 returned."
6953   (interactive "p")
6954   (let ((backward (< n 0))
6955         (n (abs n)))
6956     (while (and (> n 0)
6957                 (if backward
6958                     (gnus-summary-find-prev unread)
6959                   (gnus-summary-find-next unread)))
6960       (unless (zerop (setq n (1- n)))
6961         (gnus-summary-show-thread)))
6962     (when (/= 0 n)
6963       (gnus-message 7 "No more%s articles"
6964                     (if unread " unread" "")))
6965     (unless dont-display
6966       (gnus-summary-recenter)
6967       (gnus-summary-position-point))
6968     n))
6969
6970 (defun gnus-summary-next-unread-subject (n)
6971   "Go to next N'th unread summary line."
6972   (interactive "p")
6973   (gnus-summary-next-subject n t))
6974
6975 (defun gnus-summary-prev-subject (n &optional unread)
6976   "Go to previous N'th summary line.
6977 If optional argument UNREAD is non-nil, only unread article is selected."
6978   (interactive "p")
6979   (gnus-summary-next-subject (- n) unread))
6980
6981 (defun gnus-summary-prev-unread-subject (n)
6982   "Go to previous N'th unread summary line."
6983   (interactive "p")
6984   (gnus-summary-next-subject (- n) t))
6985
6986 (defun gnus-summary-goto-subjects (articles)
6987   "Insert the subject header for ARTICLES in the current buffer."
6988   (save-excursion
6989     (dolist (article articles)
6990       (gnus-summary-goto-subject article t)))
6991   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6992   (gnus-summary-position-point))
6993  
6994 (defun gnus-summary-goto-subject (article &optional force silent)
6995   "Go the subject line of ARTICLE.
6996 If FORCE, also allow jumping to articles not currently shown."
6997   (interactive "nArticle number: ")
6998   (unless (numberp article)
6999     (error "Article %s is not a number" article))
7000   (let ((b (point))
7001         (data (gnus-data-find article)))
7002     ;; We read in the article if we have to.
7003     (and (not data)
7004          force
7005          (gnus-summary-insert-subject
7006           article
7007           (if (or (numberp force) (vectorp force)) force)
7008           t)
7009          (setq data (gnus-data-find article)))
7010     (goto-char b)
7011     (if (not data)
7012         (progn
7013           (unless silent
7014             (gnus-message 3 "Can't find article %d" article))
7015           nil)
7016       (let ((pt (gnus-data-pos data)))
7017         (goto-char pt)
7018         (gnus-summary-set-article-display-arrow pt))
7019       (gnus-summary-position-point)
7020       article)))
7021
7022 ;; Walking around summary lines with displaying articles.
7023
7024 (defun gnus-summary-expand-window (&optional arg)
7025   "Make the summary buffer take up the entire Emacs frame.
7026 Given a prefix, will force an `article' buffer configuration."
7027   (interactive "P")
7028   (if arg
7029       (gnus-configure-windows 'article 'force)
7030     (gnus-configure-windows 'summary 'force)))
7031
7032 (defun gnus-summary-display-article (article &optional all-header)
7033   "Display ARTICLE in article buffer."
7034   (when (gnus-buffer-live-p gnus-article-buffer)
7035     (with-current-buffer gnus-article-buffer
7036       (set-buffer-multibyte t)))
7037   (gnus-set-global-variables)
7038   (when (gnus-buffer-live-p gnus-article-buffer)
7039     (with-current-buffer gnus-article-buffer
7040       (setq gnus-article-charset gnus-newsgroup-charset)
7041       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7042       (set-buffer-multibyte t)))
7043   (if (null article)
7044       nil
7045     (prog1
7046         (if gnus-summary-display-article-function
7047             (funcall gnus-summary-display-article-function article all-header)
7048           (gnus-article-prepare article all-header))
7049       (with-current-buffer gnus-article-buffer
7050         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7051              nil))
7052       (gnus-run-hooks 'gnus-select-article-hook)
7053       (when (and gnus-current-article
7054                  (not (zerop gnus-current-article)))
7055         (gnus-summary-goto-subject gnus-current-article))
7056       (gnus-summary-recenter)
7057       (when (and gnus-use-trees gnus-show-threads)
7058         (gnus-possibly-generate-tree article)
7059         (gnus-highlight-selected-tree article))
7060       ;; Successfully display article.
7061       (gnus-article-set-window-start
7062        (cdr (assq article gnus-newsgroup-bookmarks))))))
7063
7064 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7065   "Select the current article.
7066 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7067 non-nil, the article will be re-fetched even if it already present in
7068 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7069 be displayed."
7070   ;; Make sure we are in the summary buffer to work around bbdb bug.
7071   (unless (eq major-mode 'gnus-summary-mode)
7072     (set-buffer gnus-summary-buffer))
7073   (let ((article (or article (gnus-summary-article-number)))
7074         (all-headers (not (not all-headers))) ;Must be T or NIL.
7075         gnus-summary-display-article-function)
7076     (and (not pseudo)
7077          (gnus-summary-article-pseudo-p article)
7078          (error "This is a pseudo-article"))
7079     (save-excursion
7080       (set-buffer gnus-summary-buffer)
7081       (if (or (and gnus-single-article-buffer
7082                    (or (null gnus-current-article)
7083                        (null gnus-article-current)
7084                        (null (get-buffer gnus-article-buffer))
7085                        (not (eq article (cdr gnus-article-current)))
7086                        (not (equal (car gnus-article-current)
7087                                    gnus-newsgroup-name))))
7088               (and (not gnus-single-article-buffer)
7089                    (or (null gnus-current-article)
7090                        (not (eq gnus-current-article article))))
7091               force)
7092           ;; The requested article is different from the current article.
7093           (progn
7094             (gnus-summary-display-article article all-headers)
7095             (gnus-article-set-window-start
7096              (cdr (assq article gnus-newsgroup-bookmarks)))
7097             article)
7098         'old))))
7099
7100 (defun gnus-summary-force-verify-and-decrypt ()
7101   "Display buttons for signed/encrypted parts and verify/decrypt them."
7102   (interactive)
7103   (let ((mm-verify-option 'known)
7104         (mm-decrypt-option 'known)
7105         (gnus-article-emulate-mime t)
7106         (gnus-buttonized-mime-types (append (list "multipart/signed"
7107                                                   "multipart/encrypted")
7108                                             gnus-buttonized-mime-types)))
7109     (gnus-summary-select-article nil 'force)))
7110
7111 (defun gnus-summary-set-current-mark (&optional current-mark)
7112   "Obsolete function."
7113   nil)
7114
7115 (defun gnus-summary-next-article (&optional unread subject backward push)
7116   "Select the next article.
7117 If UNREAD, only unread articles are selected.
7118 If SUBJECT, only articles with SUBJECT are selected.
7119 If BACKWARD, the previous article is selected instead of the next."
7120   (interactive "P")
7121   (cond
7122    ;; Is there such an article?
7123    ((and (gnus-summary-search-forward unread subject backward)
7124          (or (gnus-summary-display-article (gnus-summary-article-number))
7125              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7126     (gnus-summary-position-point))
7127    ;; If not, we try the first unread, if that is wanted.
7128    ((and subject
7129          gnus-auto-select-same
7130          (gnus-summary-first-unread-article))
7131     (gnus-summary-position-point)
7132     (gnus-message 6 "Wrapped"))
7133    ;; Try to get next/previous article not displayed in this group.
7134    ((and gnus-auto-extend-newsgroup
7135          (not unread) (not subject))
7136     (gnus-summary-goto-article
7137      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7138      nil (count-lines (point-min) (point))))
7139    ;; Go to next/previous group.
7140    (t
7141     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7142       (gnus-summary-jump-to-group gnus-newsgroup-name))
7143     (let ((cmd last-command-char)
7144           (point
7145            (save-excursion
7146              (set-buffer gnus-group-buffer)
7147              (point)))
7148           (group
7149            (if (eq gnus-keep-same-level 'best)
7150                (gnus-summary-best-group gnus-newsgroup-name)
7151              (gnus-summary-search-group backward gnus-keep-same-level))))
7152       ;; For some reason, the group window gets selected.  We change
7153       ;; it back.
7154       (select-window (get-buffer-window (current-buffer)))
7155       ;; Select next unread newsgroup automagically.
7156       (cond
7157        ((or (not gnus-auto-select-next)
7158             (not cmd))
7159         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7160        ((or (eq gnus-auto-select-next 'quietly)
7161             (and (eq gnus-auto-select-next 'slightly-quietly)
7162                  push)
7163             (and (eq gnus-auto-select-next 'almost-quietly)
7164                  (gnus-summary-last-article-p)))
7165         ;; Select quietly.
7166         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7167             (gnus-summary-exit)
7168           (gnus-message 7 "No more%s articles (%s)..."
7169                         (if unread " unread" "")
7170                         (if group (concat "selecting " group)
7171                           "exiting"))
7172           (gnus-summary-next-group nil group backward)))
7173        (t
7174         (when (gnus-key-press-event-p last-input-event)
7175           (gnus-summary-walk-group-buffer
7176            gnus-newsgroup-name cmd unread backward point))))))))
7177
7178 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7179   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7180                       (?\C-p (gnus-group-prev-unread-group 1))))
7181         (cursor-in-echo-area t)
7182         keve key group ended prompt)
7183     (save-excursion
7184       (set-buffer gnus-group-buffer)
7185       (goto-char start)
7186       (setq group
7187             (if (eq gnus-keep-same-level 'best)
7188                 (gnus-summary-best-group gnus-newsgroup-name)
7189               (gnus-summary-search-group backward gnus-keep-same-level))))
7190     (while (not ended)
7191       (setq prompt
7192             (format
7193              "No more%s articles%s " (if unread " unread" "")
7194              (if (and group
7195                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7196                  (format " (Type %s for %s [%s])"
7197                          (single-key-description cmd) group
7198                          (gnus-group-unread group))
7199                (format " (Type %s to exit %s)"
7200                        (single-key-description cmd)
7201                        gnus-newsgroup-name))))
7202       ;; Confirm auto selection.
7203       (setq key (car (setq keve (gnus-read-event-char prompt)))
7204             ended t)
7205       (cond
7206        ((assq key keystrokes)
7207         (let ((obuf (current-buffer)))
7208           (switch-to-buffer gnus-group-buffer)
7209           (when group
7210             (gnus-group-jump-to-group group))
7211           (eval (cadr (assq key keystrokes)))
7212           (setq group (gnus-group-group-name))
7213           (switch-to-buffer obuf))
7214         (setq ended nil))
7215        ((equal key cmd)
7216         (if (or (not group)
7217                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7218             (gnus-summary-exit)
7219           (gnus-summary-next-group nil group backward)))
7220        (t
7221         (push (cdr keve) unread-command-events))))))
7222
7223 (defun gnus-summary-next-unread-article ()
7224   "Select unread article after current one."
7225   (interactive)
7226   (gnus-summary-next-article
7227    (or (not (eq gnus-summary-goto-unread 'never))
7228        (gnus-summary-last-article-p (gnus-summary-article-number)))
7229    (and gnus-auto-select-same
7230         (gnus-summary-article-subject))))
7231
7232 (defun gnus-summary-prev-article (&optional unread subject)
7233   "Select the article after the current one.
7234 If UNREAD is non-nil, only unread articles are selected."
7235   (interactive "P")
7236   (gnus-summary-next-article unread subject t))
7237
7238 (defun gnus-summary-prev-unread-article ()
7239   "Select unread article before current one."
7240   (interactive)
7241   (gnus-summary-prev-article
7242    (or (not (eq gnus-summary-goto-unread 'never))
7243        (gnus-summary-first-article-p (gnus-summary-article-number)))
7244    (and gnus-auto-select-same
7245         (gnus-summary-article-subject))))
7246
7247 (defun gnus-summary-next-page (&optional lines circular stop)
7248   "Show next page of the selected article.
7249 If at the end of the current article, select the next article.
7250 LINES says how many lines should be scrolled up.
7251
7252 If CIRCULAR is non-nil, go to the start of the article instead of
7253 selecting the next article when reaching the end of the current
7254 article.
7255
7256 If STOP is non-nil, just stop when reaching the end of the message.
7257
7258 Also see the variable `gnus-article-skip-boring'."
7259   (interactive "P")
7260   (setq gnus-summary-buffer (current-buffer))
7261   (gnus-set-global-variables)
7262   (let ((article (gnus-summary-article-number))
7263         (article-window (get-buffer-window gnus-article-buffer t))
7264         endp)
7265     ;; If the buffer is empty, we have no article.
7266     (unless article
7267       (error "No article to select"))
7268     (gnus-configure-windows 'article)
7269     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7270         (if (and (eq gnus-summary-goto-unread 'never)
7271                  (not (gnus-summary-last-article-p article)))
7272             (gnus-summary-next-article)
7273           (gnus-summary-next-unread-article))
7274       (if (or (null gnus-current-article)
7275               (null gnus-article-current)
7276               (/= article (cdr gnus-article-current))
7277               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7278           ;; Selected subject is different from current article's.
7279           (gnus-summary-display-article article)
7280         (when article-window
7281           (gnus-eval-in-buffer-window gnus-article-buffer
7282             (setq endp (or (gnus-article-next-page lines)
7283                            (gnus-article-only-boring-p))))
7284           (when endp
7285             (cond (stop
7286                    (gnus-message 3 "End of message"))
7287                   (circular
7288                    (gnus-summary-beginning-of-article))
7289                   (lines
7290                    (gnus-message 3 "End of message"))
7291                   ((null lines)
7292                    (if (and (eq gnus-summary-goto-unread 'never)
7293                             (not (gnus-summary-last-article-p article)))
7294                        (gnus-summary-next-article)
7295                      (gnus-summary-next-unread-article))))))))
7296     (gnus-summary-recenter)
7297     (gnus-summary-position-point)))
7298
7299 (defun gnus-summary-prev-page (&optional lines move)
7300   "Show previous page of selected article.
7301 Argument LINES specifies lines to be scrolled down.
7302 If MOVE, move to the previous unread article if point is at
7303 the beginning of the buffer."
7304   (interactive "P")
7305   (let ((article (gnus-summary-article-number))
7306         (article-window (get-buffer-window gnus-article-buffer t))
7307         endp)
7308     (gnus-configure-windows 'article)
7309     (if (or (null gnus-current-article)
7310             (null gnus-article-current)
7311             (/= article (cdr gnus-article-current))
7312             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7313         ;; Selected subject is different from current article's.
7314         (gnus-summary-display-article article)
7315       (gnus-summary-recenter)
7316       (when article-window
7317         (gnus-eval-in-buffer-window gnus-article-buffer
7318           (setq endp (gnus-article-prev-page lines)))
7319         (when (and move endp)
7320           (cond (lines
7321                  (gnus-message 3 "Beginning of message"))
7322                 ((null lines)
7323                  (if (and (eq gnus-summary-goto-unread 'never)
7324                           (not (gnus-summary-first-article-p article)))
7325                      (gnus-summary-prev-article)
7326                    (gnus-summary-prev-unread-article))))))))
7327   (gnus-summary-position-point))
7328
7329 (defun gnus-summary-prev-page-or-article (&optional lines)
7330   "Show previous page of selected article.
7331 Argument LINES specifies lines to be scrolled down.
7332 If at the beginning of the article, go to the next article."
7333   (interactive "P")
7334   (gnus-summary-prev-page lines t))
7335
7336 (defun gnus-summary-scroll-up (lines)
7337   "Scroll up (or down) one line current article.
7338 Argument LINES specifies lines to be scrolled up (or down if negative)."
7339   (interactive "p")
7340   (gnus-configure-windows 'article)
7341   (gnus-summary-show-thread)
7342   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7343     (gnus-eval-in-buffer-window gnus-article-buffer
7344       (cond ((> lines 0)
7345              (when (gnus-article-next-page lines)
7346                (gnus-message 3 "End of message")))
7347             ((< lines 0)
7348              (gnus-article-prev-page (- lines))))))
7349   (gnus-summary-recenter)
7350   (gnus-summary-position-point))
7351
7352 (defun gnus-summary-scroll-down (lines)
7353   "Scroll down (or up) one line current article.
7354 Argument LINES specifies lines to be scrolled down (or up if negative)."
7355   (interactive "p")
7356   (gnus-summary-scroll-up (- lines)))
7357
7358 (defun gnus-summary-next-same-subject ()
7359   "Select next article which has the same subject as current one."
7360   (interactive)
7361   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7362
7363 (defun gnus-summary-prev-same-subject ()
7364   "Select previous article which has the same subject as current one."
7365   (interactive)
7366   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7367
7368 (defun gnus-summary-next-unread-same-subject ()
7369   "Select next unread article which has the same subject as current one."
7370   (interactive)
7371   (gnus-summary-next-article t (gnus-summary-article-subject)))
7372
7373 (defun gnus-summary-prev-unread-same-subject ()
7374   "Select previous unread article which has the same subject as current one."
7375   (interactive)
7376   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7377
7378 (defun gnus-summary-first-unread-article ()
7379   "Select the first unread article.
7380 Return nil if there are no unread articles."
7381   (interactive)
7382   (prog1
7383       (when (gnus-summary-first-subject t)
7384         (gnus-summary-show-thread)
7385         (gnus-summary-first-subject t)
7386         (gnus-summary-display-article (gnus-summary-article-number)))
7387     (gnus-summary-position-point)))
7388
7389 (defun gnus-summary-first-unread-subject ()
7390   "Place the point on the subject line of the first unread article.
7391 Return nil if there are no unread articles."
7392   (interactive)
7393   (prog1
7394       (when (gnus-summary-first-subject t)
7395         (gnus-summary-show-thread)
7396         (gnus-summary-first-subject t))
7397     (gnus-summary-position-point)))
7398
7399 (defun gnus-summary-first-unseen-subject ()
7400   "Place the point on the subject line of the first unseen article.
7401 Return nil if there are no unseen articles."
7402   (interactive)
7403   (prog1
7404       (when (gnus-summary-first-subject nil nil t)
7405         (gnus-summary-show-thread)
7406         (gnus-summary-first-subject nil nil t))
7407     (gnus-summary-position-point)))
7408
7409 (defun gnus-summary-first-unseen-or-unread-subject ()
7410   "Place the point on the subject line of the first unseen article or,
7411 if all article have been seen, on the subject line of the first unread
7412 article."
7413   (interactive)
7414   (prog1
7415       (unless (when (gnus-summary-first-subject nil nil t)
7416                 (gnus-summary-show-thread)
7417                 (gnus-summary-first-subject nil nil t))
7418         (when (gnus-summary-first-subject t)
7419           (gnus-summary-show-thread)
7420           (gnus-summary-first-subject t)))
7421     (gnus-summary-position-point)))
7422
7423 (defun gnus-summary-first-article ()
7424   "Select the first article.
7425 Return nil if there are no articles."
7426   (interactive)
7427   (prog1
7428       (when (gnus-summary-first-subject)
7429         (gnus-summary-show-thread)
7430         (gnus-summary-first-subject)
7431         (gnus-summary-display-article (gnus-summary-article-number)))
7432     (gnus-summary-position-point)))
7433
7434 (defun gnus-summary-best-unread-article (&optional arg)
7435   "Select the unread article with the highest score.
7436 If given a prefix argument, select the next unread article that has a
7437 score higher than the default score."
7438   (interactive "P")
7439   (let ((article (if arg
7440                      (gnus-summary-better-unread-subject)
7441                    (gnus-summary-best-unread-subject))))
7442     (if article
7443         (gnus-summary-goto-article article)
7444       (error "No unread articles"))))
7445
7446 (defun gnus-summary-best-unread-subject ()
7447   "Select the unread subject with the highest score."
7448   (interactive)
7449   (let ((best -1000000)
7450         (data gnus-newsgroup-data)
7451         article score)
7452     (while data
7453       (and (gnus-data-unread-p (car data))
7454            (> (setq score
7455                     (gnus-summary-article-score (gnus-data-number (car data))))
7456               best)
7457            (setq best score
7458                  article (gnus-data-number (car data))))
7459       (setq data (cdr data)))
7460     (when article
7461       (gnus-summary-goto-subject article))
7462     (gnus-summary-position-point)
7463     article))
7464
7465 (defun gnus-summary-better-unread-subject ()
7466   "Select the first unread subject that has a score over the default score."
7467   (interactive)
7468   (let ((data gnus-newsgroup-data)
7469         article score)
7470     (while (and (setq article (gnus-data-number (car data)))
7471                 (or (gnus-data-read-p (car data))
7472                     (not (> (gnus-summary-article-score article)
7473                             gnus-summary-default-score))))
7474       (setq data (cdr data)))
7475     (when article
7476       (gnus-summary-goto-subject article))
7477     (gnus-summary-position-point)
7478     article))
7479
7480 (defun gnus-summary-last-subject ()
7481   "Go to the last displayed subject line in the group."
7482   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7483     (when article
7484       (gnus-summary-goto-subject article))))
7485
7486 (defun gnus-summary-goto-article (article &optional all-headers force)
7487   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7488 If ALL-HEADERS is non-nil, no header lines are hidden.
7489 If FORCE, go to the article even if it isn't displayed.  If FORCE
7490 is a number, it is the line the article is to be displayed on."
7491   (interactive
7492    (list
7493     (completing-read
7494      "Article number or Message-ID: "
7495      (mapcar (lambda (number) (list (int-to-string number)))
7496              gnus-newsgroup-limit))
7497     current-prefix-arg
7498     t))
7499   (prog1
7500       (if (and (stringp article)
7501                (string-match "@\\|%40" article))
7502           (gnus-summary-refer-article article)
7503         (when (stringp article)
7504           (setq article (string-to-number article)))
7505         (if (gnus-summary-goto-subject article force)
7506             (gnus-summary-display-article article all-headers)
7507           (gnus-message 4 "Couldn't go to article %s" article) nil))
7508     (gnus-summary-position-point)))
7509
7510 (defun gnus-summary-goto-last-article ()
7511   "Go to the previously read article."
7512   (interactive)
7513   (prog1
7514       (when gnus-last-article
7515         (gnus-summary-goto-article gnus-last-article nil t))
7516     (gnus-summary-position-point)))
7517
7518 (defun gnus-summary-pop-article (number)
7519   "Pop one article off the history and go to the previous.
7520 NUMBER articles will be popped off."
7521   (interactive "p")
7522   (let (to)
7523     (setq gnus-newsgroup-history
7524           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7525     (if to
7526         (gnus-summary-goto-article (car to) nil t)
7527       (error "Article history empty")))
7528   (gnus-summary-position-point))
7529
7530 ;; Summary commands and functions for limiting the summary buffer.
7531
7532 (defun gnus-summary-limit-to-articles (n)
7533   "Limit the summary buffer to the next N articles.
7534 If not given a prefix, use the process marked articles instead."
7535   (interactive "P")
7536   (prog1
7537       (let ((articles (gnus-summary-work-articles n)))
7538         (setq gnus-newsgroup-processable nil)
7539         (gnus-summary-limit articles))
7540     (gnus-summary-position-point)))
7541
7542 (defun gnus-summary-pop-limit (&optional total)
7543   "Restore the previous limit.
7544 If given a prefix, remove all limits."
7545   (interactive "P")
7546   (when total
7547     (setq gnus-newsgroup-limits
7548           (list (mapcar (lambda (h) (mail-header-number h))
7549                         gnus-newsgroup-headers))))
7550   (unless gnus-newsgroup-limits
7551     (error "No limit to pop"))
7552   (prog1
7553       (gnus-summary-limit nil 'pop)
7554     (gnus-summary-position-point)))
7555
7556 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7557   "Limit the summary buffer to articles that have subjects that match a regexp.
7558 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7559   (interactive
7560    (list (read-string (if current-prefix-arg
7561                           "Exclude subject (regexp): "
7562                         "Limit to subject (regexp): "))
7563          nil current-prefix-arg))
7564   (unless header
7565     (setq header "subject"))
7566   (when (not (equal "" subject))
7567     (prog1
7568         (let ((articles (gnus-summary-find-matching
7569                          (or header "subject") subject 'all nil nil
7570                          not-matching)))
7571           (unless articles
7572             (error "Found no matches for \"%s\"" subject))
7573           (gnus-summary-limit articles))
7574       (gnus-summary-position-point))))
7575
7576 (defun gnus-summary-limit-to-author (from &optional not-matching)
7577   "Limit the summary buffer to articles that have authors that match a regexp.
7578 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7579   (interactive
7580    (list (read-string (if current-prefix-arg
7581                           "Exclude author (regexp): "
7582                         "Limit to author (regexp): "))
7583          current-prefix-arg))
7584   (gnus-summary-limit-to-subject from "from" not-matching))
7585
7586 (defun gnus-summary-limit-to-age (age &optional younger-p)
7587   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7588 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7589 articles that are younger than AGE days."
7590   (interactive
7591    (let ((younger current-prefix-arg)
7592          (days-got nil)
7593          days)
7594      (while (not days-got)
7595        (setq days (if younger
7596                       (read-string "Limit to articles younger than (in days, older when negative): ")
7597                     (read-string
7598                      "Limit to articles older than (in days, younger when negative): ")))
7599        (when (> (length days) 0)
7600          (setq days (read days)))
7601        (if (numberp days)
7602            (progn
7603              (setq days-got t)
7604              (when (< days 0)
7605                (setq younger (not younger))
7606                (setq days (* days -1))))
7607          (message "Please enter a number.")
7608          (sleep-for 1)))
7609      (list days younger)))
7610   (prog1
7611       (let ((data gnus-newsgroup-data)
7612             (cutoff (days-to-time age))
7613             articles d date is-younger)
7614         (while (setq d (pop data))
7615           (when (and (vectorp (gnus-data-header d))
7616                      (setq date (mail-header-date (gnus-data-header d))))
7617             (setq is-younger (time-less-p
7618                               (time-since (condition-case ()
7619                                               (date-to-time date)
7620                                             (error '(0 0))))
7621                               cutoff))
7622             (when (if younger-p
7623                       is-younger
7624                     (not is-younger))
7625               (push (gnus-data-number d) articles))))
7626         (gnus-summary-limit (nreverse articles)))
7627     (gnus-summary-position-point)))
7628
7629 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7630   "Limit the summary buffer to articles that match an 'extra' header."
7631   (interactive
7632    (let ((header
7633           (intern
7634            (gnus-completing-read-with-default
7635             (symbol-name (car gnus-extra-headers))
7636             (if current-prefix-arg
7637                 "Exclude extra header:"
7638               "Limit extra header:")
7639             (mapcar (lambda (x)
7640                       (cons (symbol-name x) x))
7641                     gnus-extra-headers)
7642             nil
7643             t))))
7644      (list header
7645            (read-string (format "%s header %s (regexp): "
7646                                 (if current-prefix-arg "Exclude" "Limit to")
7647                                 header))
7648            current-prefix-arg)))
7649   (when (not (equal "" regexp))
7650     (prog1
7651         (let ((articles (gnus-summary-find-matching
7652                          (cons 'extra header) regexp 'all nil nil
7653                          not-matching)))
7654           (unless articles
7655             (error "Found no matches for \"%s\"" regexp))
7656           (gnus-summary-limit articles))
7657       (gnus-summary-position-point))))
7658
7659 (defun gnus-summary-limit-to-display-predicate ()
7660   "Limit the summary buffer to the predicated in the `display' group parameter."
7661   (interactive)
7662   (unless gnus-newsgroup-display
7663     (error "There is no `display' group parameter"))
7664   (let (articles)
7665     (dolist (number gnus-newsgroup-articles)
7666       (when (funcall gnus-newsgroup-display)
7667         (push number articles)))
7668     (gnus-summary-limit articles))
7669   (gnus-summary-position-point))
7670
7671 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7672 (make-obsolete
7673  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7674
7675 (defun gnus-summary-limit-to-unread (&optional all)
7676   "Limit the summary buffer to articles that are not marked as read.
7677 If ALL is non-nil, limit strictly to unread articles."
7678   (interactive "P")
7679   (if all
7680       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7681     (gnus-summary-limit-to-marks
7682      ;; Concat all the marks that say that an article is read and have
7683      ;; those removed.
7684      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7685            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7686            gnus-low-score-mark gnus-expirable-mark
7687            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7688            gnus-duplicate-mark gnus-souped-mark)
7689      'reverse)))
7690
7691 (defun gnus-summary-limit-to-replied (&optional unreplied)
7692   "Limit the summary buffer to replied articles.
7693 If UNREPLIED (the prefix), limit to unreplied articles."
7694   (interactive "P")
7695   (if unreplied
7696       (gnus-summary-limit
7697        (gnus-set-difference gnus-newsgroup-articles
7698         gnus-newsgroup-replied))
7699     (gnus-summary-limit gnus-newsgroup-replied))
7700   (gnus-summary-position-point))
7701
7702 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7703 (make-obsolete 'gnus-summary-delete-marked-with
7704                'gnus-summary-limit-exclude-marks)
7705
7706 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7707   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7708 If REVERSE, limit the summary buffer to articles that are marked
7709 with MARKS.  MARKS can either be a string of marks or a list of marks.
7710 Returns how many articles were removed."
7711   (interactive "sMarks: ")
7712   (gnus-summary-limit-to-marks marks t))
7713
7714 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7715   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7716 If REVERSE (the prefix), limit the summary buffer to articles that are
7717 not marked with MARKS.  MARKS can either be a string of marks or a
7718 list of marks.
7719 Returns how many articles were removed."
7720   (interactive "sMarks: \nP")
7721   (prog1
7722       (let ((data gnus-newsgroup-data)
7723             (marks (if (listp marks) marks
7724                      (append marks nil))) ; Transform to list.
7725             articles)
7726         (while data
7727           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7728                   (memq (gnus-data-mark (car data)) marks))
7729             (push (gnus-data-number (car data)) articles))
7730           (setq data (cdr data)))
7731         (gnus-summary-limit articles))
7732     (gnus-summary-position-point)))
7733
7734 (defun gnus-summary-limit-to-score (score)
7735   "Limit to articles with score at or above SCORE."
7736   (interactive "NLimit to articles with score of at least: ")
7737   (let ((data gnus-newsgroup-data)
7738         articles)
7739     (while data
7740       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7741                 score)
7742         (push (gnus-data-number (car data)) articles))
7743       (setq data (cdr data)))
7744     (prog1
7745         (gnus-summary-limit articles)
7746       (gnus-summary-position-point))))
7747
7748 (defun gnus-summary-limit-to-unseen ()
7749   "Limit to unseen articles."
7750   (interactive)
7751   (prog1
7752       (gnus-summary-limit gnus-newsgroup-unseen)
7753     (gnus-summary-position-point)))
7754
7755 (defun gnus-summary-limit-include-thread (id)
7756   "Display all the hidden articles that is in the thread with ID in it.
7757 When called interactively, ID is the Message-ID of the current
7758 article."
7759   (interactive (list (mail-header-id (gnus-summary-article-header))))
7760   (let ((articles (gnus-articles-in-thread
7761                    (gnus-id-to-thread (gnus-root-id id)))))
7762     (prog1
7763         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7764       (gnus-summary-limit-include-matching-articles
7765        "subject"
7766        (regexp-quote (gnus-simplify-subject-re
7767                       (mail-header-subject (gnus-id-to-header id)))))
7768       (gnus-summary-position-point))))
7769
7770 (defun gnus-summary-limit-include-matching-articles (header regexp)
7771   "Display all the hidden articles that have HEADERs that match REGEXP."
7772   (interactive (list (read-string "Match on header: ")
7773                      (read-string "Regexp: ")))
7774   (let ((articles (gnus-find-matching-articles header regexp)))
7775     (prog1
7776         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7777       (gnus-summary-position-point))))
7778
7779 (defun gnus-summary-insert-dormant-articles ()
7780   "Insert all the dormant articles for this group into the current buffer."
7781   (interactive)
7782   (let ((gnus-verbose (max 6 gnus-verbose)))
7783     (if (not gnus-newsgroup-dormant)
7784         (gnus-message 3 "No cached articles for this group")
7785       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7786
7787 (defun gnus-summary-limit-include-dormant ()
7788   "Display all the hidden articles that are marked as dormant.
7789 Note that this command only works on a subset of the articles currently
7790 fetched for this group."
7791   (interactive)
7792   (unless gnus-newsgroup-dormant
7793     (error "There are no dormant articles in this group"))
7794   (prog1
7795       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7796     (gnus-summary-position-point)))
7797
7798 (defun gnus-summary-limit-exclude-dormant ()
7799   "Hide all dormant articles."
7800   (interactive)
7801   (prog1
7802       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7803     (gnus-summary-position-point)))
7804
7805 (defun gnus-summary-limit-exclude-childless-dormant ()
7806   "Hide all dormant articles that have no children."
7807   (interactive)
7808   (let ((data (gnus-data-list t))
7809         articles d children)
7810     ;; Find all articles that are either not dormant or have
7811     ;; children.
7812     (while (setq d (pop data))
7813       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7814                 (and (setq children
7815                            (gnus-article-children (gnus-data-number d)))
7816                      (let (found)
7817                        (while children
7818                          (when (memq (car children) articles)
7819                            (setq children nil
7820                                  found t))
7821                          (pop children))
7822                        found)))
7823         (push (gnus-data-number d) articles)))
7824     ;; Do the limiting.
7825     (prog1
7826         (gnus-summary-limit articles)
7827       (gnus-summary-position-point))))
7828
7829 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7830   "Mark all unread excluded articles as read.
7831 If ALL, mark even excluded ticked and dormants as read."
7832   (interactive "P")
7833   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7834   (let ((articles (gnus-sorted-ndifference
7835                    (sort
7836                     (mapcar (lambda (h) (mail-header-number h))
7837                             gnus-newsgroup-headers)
7838                     '<)
7839                    gnus-newsgroup-limit))
7840         article)
7841     (setq gnus-newsgroup-unreads
7842           (gnus-sorted-intersection gnus-newsgroup-unreads
7843                                     gnus-newsgroup-limit))
7844     (if all
7845         (setq gnus-newsgroup-dormant nil
7846               gnus-newsgroup-marked nil
7847               gnus-newsgroup-reads
7848               (nconc
7849                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7850                gnus-newsgroup-reads))
7851       (while (setq article (pop articles))
7852         (unless (or (memq article gnus-newsgroup-dormant)
7853                     (memq article gnus-newsgroup-marked))
7854           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7855
7856 (defun gnus-summary-limit (articles &optional pop)
7857   (if pop
7858       ;; We pop the previous limit off the stack and use that.
7859       (setq articles (car gnus-newsgroup-limits)
7860             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7861     ;; We use the new limit, so we push the old limit on the stack.
7862     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7863   ;; Set the limit.
7864   (setq gnus-newsgroup-limit articles)
7865   (let ((total (length gnus-newsgroup-data))
7866         (data (gnus-data-find-list (gnus-summary-article-number)))
7867         (gnus-summary-mark-below nil)   ; Inhibit this.
7868         found)
7869     ;; This will do all the work of generating the new summary buffer
7870     ;; according to the new limit.
7871     (gnus-summary-prepare)
7872     ;; Hide any threads, possibly.
7873     (gnus-summary-maybe-hide-threads)
7874     ;; Try to return to the article you were at, or one in the
7875     ;; neighborhood.
7876     (when data
7877       ;; We try to find some article after the current one.
7878       (while data
7879         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7880           (setq data nil
7881                 found t))
7882         (setq data (cdr data))))
7883     (unless found
7884       ;; If there is no data, that means that we were after the last
7885       ;; article.  The same goes when we can't find any articles
7886       ;; after the current one.
7887       (goto-char (point-max))
7888       (gnus-summary-find-prev))
7889     (gnus-set-mode-line 'summary)
7890     ;; We return how many articles were removed from the summary
7891     ;; buffer as a result of the new limit.
7892     (- total (length gnus-newsgroup-data))))
7893
7894 (defsubst gnus-invisible-cut-children (threads)
7895   (let ((num 0))
7896     (while threads
7897       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7898         (incf num))
7899       (pop threads))
7900     (< num 2)))
7901
7902 (defsubst gnus-cut-thread (thread)
7903   "Go forwards in the thread until we find an article that we want to display."
7904   (when (or (eq gnus-fetch-old-headers 'some)
7905             (eq gnus-fetch-old-headers 'invisible)
7906             (numberp gnus-fetch-old-headers)
7907             (eq gnus-build-sparse-threads 'some)
7908             (eq gnus-build-sparse-threads 'more))
7909     ;; Deal with old-fetched headers and sparse threads.
7910     (while (and
7911             thread
7912             (or
7913              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7914              (gnus-summary-article-ancient-p
7915               (mail-header-number (car thread))))
7916             (if (or (<= (length (cdr thread)) 1)
7917                     (eq gnus-fetch-old-headers 'invisible))
7918                 (setq gnus-newsgroup-limit
7919                       (delq (mail-header-number (car thread))
7920                             gnus-newsgroup-limit)
7921                       thread (cadr thread))
7922               (when (gnus-invisible-cut-children (cdr thread))
7923                 (let ((th (cdr thread)))
7924                   (while th
7925                     (if (memq (mail-header-number (caar th))
7926                               gnus-newsgroup-limit)
7927                         (setq thread (car th)
7928                               th nil)
7929                       (setq th (cdr th))))))))))
7930   thread)
7931
7932 (defun gnus-cut-threads (threads)
7933   "Cut off all uninteresting articles from the beginning of THREADS."
7934   (when (or (eq gnus-fetch-old-headers 'some)
7935             (eq gnus-fetch-old-headers 'invisible)
7936             (numberp gnus-fetch-old-headers)
7937             (eq gnus-build-sparse-threads 'some)
7938             (eq gnus-build-sparse-threads 'more))
7939     (let ((th threads))
7940       (while th
7941         (setcar th (gnus-cut-thread (car th)))
7942         (setq th (cdr th)))))
7943   ;; Remove nixed out threads.
7944   (delq nil threads))
7945
7946 (defun gnus-summary-initial-limit (&optional show-if-empty)
7947   "Figure out what the initial limit is supposed to be on group entry.
7948 This entails weeding out unwanted dormants, low-scored articles,
7949 fetch-old-headers verbiage, and so on."
7950   ;; Most groups have nothing to remove.
7951   (if (or gnus-inhibit-limiting
7952           (and (null gnus-newsgroup-dormant)
7953                (eq gnus-newsgroup-display 'gnus-not-ignore)
7954                (not (eq gnus-fetch-old-headers 'some))
7955                (not (numberp gnus-fetch-old-headers))
7956                (not (eq gnus-fetch-old-headers 'invisible))
7957                (null gnus-summary-expunge-below)
7958                (not (eq gnus-build-sparse-threads 'some))
7959                (not (eq gnus-build-sparse-threads 'more))
7960                (null gnus-thread-expunge-below)
7961                (not gnus-use-nocem)))
7962       ()                                ; Do nothing.
7963     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7964     (setq gnus-newsgroup-limit nil)
7965     (mapatoms
7966      (lambda (node)
7967        (unless (car (symbol-value node))
7968          ;; These threads have no parents -- they are roots.
7969          (let ((nodes (cdr (symbol-value node)))
7970                thread)
7971            (while nodes
7972              (if (and gnus-thread-expunge-below
7973                       (< (gnus-thread-total-score (car nodes))
7974                          gnus-thread-expunge-below))
7975                  (gnus-expunge-thread (pop nodes))
7976                (setq thread (pop nodes))
7977                (gnus-summary-limit-children thread))))))
7978      gnus-newsgroup-dependencies)
7979     ;; If this limitation resulted in an empty group, we might
7980     ;; pop the previous limit and use it instead.
7981     (when (and (not gnus-newsgroup-limit)
7982                show-if-empty)
7983       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7984     gnus-newsgroup-limit))
7985
7986 (defun gnus-summary-limit-children (thread)
7987   "Return 1 if this subthread is visible and 0 if it is not."
7988   ;; First we get the number of visible children to this thread.  This
7989   ;; is done by recursing down the thread using this function, so this
7990   ;; will really go down to a leaf article first, before slowly
7991   ;; working its way up towards the root.
7992   (when thread
7993     (let* ((max-lisp-eval-depth 5000)
7994            (children
7995            (if (cdr thread)
7996                (apply '+ (mapcar 'gnus-summary-limit-children
7997                                  (cdr thread)))
7998              0))
7999           (number (mail-header-number (car thread)))
8000           score)
8001       (if (and
8002            (not (memq number gnus-newsgroup-marked))
8003            (or
8004             ;; If this article is dormant and has absolutely no visible
8005             ;; children, then this article isn't visible.
8006             (and (memq number gnus-newsgroup-dormant)
8007                  (zerop children))
8008             ;; If this is "fetch-old-headered" and there is no
8009             ;; visible children, then we don't want this article.
8010             (and (or (eq gnus-fetch-old-headers 'some)
8011                      (numberp gnus-fetch-old-headers))
8012                  (gnus-summary-article-ancient-p number)
8013                  (zerop children))
8014             ;; If this is "fetch-old-headered" and `invisible', then
8015             ;; we don't want this article.
8016             (and (eq gnus-fetch-old-headers 'invisible)
8017                  (gnus-summary-article-ancient-p number))
8018             ;; If this is a sparsely inserted article with no children,
8019             ;; we don't want it.
8020             (and (eq gnus-build-sparse-threads 'some)
8021                  (gnus-summary-article-sparse-p number)
8022                  (zerop children))
8023             ;; If we use expunging, and this article is really
8024             ;; low-scored, then we don't want this article.
8025             (when (and gnus-summary-expunge-below
8026                        (< (setq score
8027                                 (or (cdr (assq number gnus-newsgroup-scored))
8028                                     gnus-summary-default-score))
8029                           gnus-summary-expunge-below))
8030               ;; We increase the expunge-tally here, but that has
8031               ;; nothing to do with the limits, really.
8032               (incf gnus-newsgroup-expunged-tally)
8033               ;; We also mark as read here, if that's wanted.
8034               (when (and gnus-summary-mark-below
8035                          (< score gnus-summary-mark-below))
8036                 (setq gnus-newsgroup-unreads
8037                       (delq number gnus-newsgroup-unreads))
8038                 (if gnus-newsgroup-auto-expire
8039                     (push number gnus-newsgroup-expirable)
8040                   (push (cons number gnus-low-score-mark)
8041                         gnus-newsgroup-reads)))
8042               t)
8043             ;; Do the `display' group parameter.
8044             (and gnus-newsgroup-display
8045                  (not (funcall gnus-newsgroup-display)))
8046             ;; Check NoCeM things.
8047             (when (and gnus-use-nocem
8048                        (gnus-nocem-unwanted-article-p
8049                         (mail-header-id (car thread))))
8050               (setq gnus-newsgroup-unreads
8051                     (delq number gnus-newsgroup-unreads))
8052               t)))
8053           ;; Nope, invisible article.
8054           0
8055         ;; Ok, this article is to be visible, so we add it to the limit
8056         ;; and return 1.
8057         (push number gnus-newsgroup-limit)
8058         1))))
8059
8060 (defun gnus-expunge-thread (thread)
8061   "Mark all articles in THREAD as read."
8062   (let* ((number (mail-header-number (car thread))))
8063     (incf gnus-newsgroup-expunged-tally)
8064     ;; We also mark as read here, if that's wanted.
8065     (setq gnus-newsgroup-unreads
8066           (delq number gnus-newsgroup-unreads))
8067     (if gnus-newsgroup-auto-expire
8068         (push number gnus-newsgroup-expirable)
8069       (push (cons number gnus-low-score-mark)
8070             gnus-newsgroup-reads)))
8071   ;; Go recursively through all subthreads.
8072   (mapcar 'gnus-expunge-thread (cdr thread)))
8073
8074 ;; Summary article oriented commands
8075
8076 (defun gnus-summary-refer-parent-article (n)
8077   "Refer parent article N times.
8078 If N is negative, go to ancestor -N instead.
8079 The difference between N and the number of articles fetched is returned."
8080   (interactive "p")
8081   (let ((skip 1)
8082         error header ref)
8083     (when (not (natnump n))
8084       (setq skip (abs n)
8085             n 1))
8086     (while (and (> n 0)
8087                 (not error))
8088       (setq header (gnus-summary-article-header))
8089       (if (and (eq (mail-header-number header)
8090                    (cdr gnus-article-current))
8091                (equal gnus-newsgroup-name
8092                       (car gnus-article-current)))
8093           ;; If we try to find the parent of the currently
8094           ;; displayed article, then we take a look at the actual
8095           ;; References header, since this is slightly more
8096           ;; reliable than the References field we got from the
8097           ;; server.
8098           (save-excursion
8099             (set-buffer gnus-original-article-buffer)
8100             (nnheader-narrow-to-headers)
8101             (unless (setq ref (message-fetch-field "references"))
8102               (when (setq ref (message-fetch-field "in-reply-to"))
8103                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8104             (widen))
8105         (setq ref
8106               ;; It's not the current article, so we take a bet on
8107               ;; the value we got from the server.
8108               (mail-header-references header)))
8109       (if (and ref
8110                (not (equal ref "")))
8111           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8112             (gnus-message 1 "Couldn't find parent"))
8113         (gnus-message 1 "No references in article %d"
8114                       (gnus-summary-article-number))
8115         (setq error t))
8116       (decf n))
8117     (gnus-summary-position-point)
8118     n))
8119
8120 (defun gnus-summary-refer-references ()
8121   "Fetch all articles mentioned in the References header.
8122 Return the number of articles fetched."
8123   (interactive)
8124   (let ((ref (mail-header-references (gnus-summary-article-header)))
8125         (current (gnus-summary-article-number))
8126         (n 0))
8127     (if (or (not ref)
8128             (equal ref ""))
8129         (error "No References in the current article")
8130       ;; For each Message-ID in the References header...
8131       (while (string-match "<[^>]*>" ref)
8132         (incf n)
8133         ;; ... fetch that article.
8134         (gnus-summary-refer-article
8135          (prog1 (match-string 0 ref)
8136            (setq ref (substring ref (match-end 0))))))
8137       (gnus-summary-goto-subject current)
8138       (gnus-summary-position-point)
8139       n)))
8140
8141 (defun gnus-summary-refer-thread (&optional limit)
8142   "Fetch all articles in the current thread.
8143 If LIMIT (the numerical prefix), fetch that many old headers instead
8144 of what's specified by the `gnus-refer-thread-limit' variable."
8145   (interactive "P")
8146   (let ((id (mail-header-id (gnus-summary-article-header)))
8147         (limit (if limit (prefix-numeric-value limit)
8148                  gnus-refer-thread-limit)))
8149     (unless (eq gnus-fetch-old-headers 'invisible)
8150       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8151       ;; Retrieve the headers and read them in.
8152       (if (eq (if (numberp limit)
8153                   (gnus-retrieve-headers
8154                    (list (min
8155                           (+ (mail-header-number
8156                               (gnus-summary-article-header))
8157                              limit)
8158                           gnus-newsgroup-end))
8159                    gnus-newsgroup-name (* limit 2))
8160                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8161                 ;; headers.
8162                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8163                                        gnus-newsgroup-name limit))
8164               'nov)
8165           (gnus-build-all-threads)
8166         (error "Can't fetch thread from back ends that don't support NOV"))
8167       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8168     (gnus-summary-limit-include-thread id)))
8169
8170 (defun gnus-summary-refer-article (message-id)
8171   "Fetch an article specified by MESSAGE-ID."
8172   (interactive "sMessage-ID: ")
8173   (when (and (stringp message-id)
8174              (not (zerop (length message-id))))
8175     (setq message-id (gnus-replace-in-string message-id " " ""))
8176     ;; Construct the correct Message-ID if necessary.
8177     ;; Suggested by tale@pawl.rpi.edu.
8178     (unless (string-match "^<" message-id)
8179       (setq message-id (concat "<" message-id)))
8180     (unless (string-match ">$" message-id)
8181       (setq message-id (concat message-id ">")))
8182     ;; People often post MIDs from URLs, so unhex it:
8183     (unless (string-match "@" message-id)
8184       (setq message-id (gnus-url-unhex-string message-id)))
8185     (let* ((header (gnus-id-to-header message-id))
8186            (sparse (and header
8187                         (gnus-summary-article-sparse-p
8188                          (mail-header-number header))
8189                         (memq (mail-header-number header)
8190                               gnus-newsgroup-limit)))
8191            number)
8192       (cond
8193        ;; If the article is present in the buffer we just go to it.
8194        ((and header
8195              (or (not (gnus-summary-article-sparse-p
8196                        (mail-header-number header)))
8197                  sparse))
8198         (prog1
8199             (gnus-summary-goto-article
8200              (mail-header-number header) nil t)
8201           (when sparse
8202             (gnus-summary-update-article (mail-header-number header)))))
8203        (t
8204         ;; We fetch the article.
8205         (catch 'found
8206           (dolist (gnus-override-method (gnus-refer-article-methods))
8207             (when (and (gnus-check-server gnus-override-method)
8208                        ;; Fetch the header,
8209                        (setq number (gnus-summary-insert-subject message-id)))
8210               ;; and display the article.
8211               (gnus-summary-select-article nil nil nil number)
8212               (throw 'found t)))
8213           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8214
8215 (defun gnus-refer-article-methods ()
8216   "Return a list of referable methods."
8217   (cond
8218    ;; No method, so we default to current and native.
8219    ((null gnus-refer-article-method)
8220     (list gnus-current-select-method gnus-select-method))
8221    ;; Current.
8222    ((eq 'current gnus-refer-article-method)
8223     (list gnus-current-select-method))
8224    ;; List of select methods.
8225    ((not (and (symbolp (car gnus-refer-article-method))
8226               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8227     (let (out)
8228       (dolist (method gnus-refer-article-method)
8229         (push (if (eq 'current method)
8230                   gnus-current-select-method
8231                 method)
8232               out))
8233       (nreverse out)))
8234    ;; One single select method.
8235    (t
8236     (list gnus-refer-article-method))))
8237
8238 (defun gnus-summary-edit-parameters ()
8239   "Edit the group parameters of the current group."
8240   (interactive)
8241   (gnus-group-edit-group gnus-newsgroup-name 'params))
8242
8243 (defun gnus-summary-customize-parameters ()
8244   "Customize the group parameters of the current group."
8245   (interactive)
8246   (gnus-group-customize gnus-newsgroup-name))
8247
8248 (defun gnus-summary-enter-digest-group (&optional force)
8249   "Enter an nndoc group based on the current article.
8250 If FORCE, force a digest interpretation.  If not, try
8251 to guess what the document format is."
8252   (interactive "P")
8253   (let ((conf gnus-current-window-configuration))
8254     (save-window-excursion
8255       (save-excursion
8256         (let (gnus-article-prepare-hook
8257               gnus-display-mime-function
8258               gnus-break-pages)
8259           (gnus-summary-select-article))))
8260     (setq gnus-current-window-configuration conf)
8261     (let* ((name (format "%s-%d"
8262                          (gnus-group-prefixed-name
8263                           gnus-newsgroup-name (list 'nndoc ""))
8264                          (save-excursion
8265                            (set-buffer gnus-summary-buffer)
8266                            gnus-current-article)))
8267            (ogroup gnus-newsgroup-name)
8268            (params (append (gnus-info-params (gnus-get-info ogroup))
8269                            (list (cons 'to-group ogroup))
8270                            (list (cons 'parent-group ogroup))
8271                            (list (cons 'save-article-group ogroup))))
8272            (case-fold-search t)
8273            (buf (current-buffer))
8274            dig to-address)
8275       (save-excursion
8276         (set-buffer gnus-original-article-buffer)
8277         ;; Have the digest group inherit the main mail address of
8278         ;; the parent article.
8279         (when (setq to-address (or (gnus-fetch-field "reply-to")
8280                                    (gnus-fetch-field "from")))
8281           (setq params (append
8282                         (list (cons 'to-address
8283                                     (funcall gnus-decode-encoded-word-function
8284                                              to-address))))))
8285         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8286         (insert-buffer-substring gnus-original-article-buffer)
8287         ;; Remove lines that may lead nndoc to misinterpret the
8288         ;; document type.
8289         (narrow-to-region
8290          (goto-char (point-min))
8291          (or (search-forward "\n\n" nil t) (point)))
8292         (goto-char (point-min))
8293         (delete-matching-lines "^Path:\\|^From ")
8294         (widen))
8295       (unwind-protect
8296           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8297                     (gnus-newsgroup-ephemeral-ignored-charsets
8298                      gnus-newsgroup-ignored-charsets))
8299                 (gnus-group-read-ephemeral-group
8300                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8301                               (nndoc-article-type
8302                                ,(if force 'mbox 'guess)))
8303                  t nil nil nil
8304                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8305                                                         "ADAPT")))))
8306               ;; Make all postings to this group go to the parent group.
8307               (nconc (gnus-info-params (gnus-get-info name))
8308                      params)
8309             ;; Couldn't select this doc group.
8310             (switch-to-buffer buf)
8311             (gnus-set-global-variables)
8312             (gnus-configure-windows 'summary)
8313             (gnus-message 3 "Article couldn't be entered?"))
8314         (kill-buffer dig)))))
8315
8316 (defun gnus-summary-read-document (n)
8317   "Open a new group based on the current article(s).
8318 This will allow you to read digests and other similar
8319 documents as newsgroups.
8320 Obeys the standard process/prefix convention."
8321   (interactive "P")
8322   (let* ((articles (gnus-summary-work-articles n))
8323          (ogroup gnus-newsgroup-name)
8324          (params (append (gnus-info-params (gnus-get-info ogroup))
8325                          (list (cons 'to-group ogroup))))
8326          article group egroup groups vgroup)
8327     (while (setq article (pop articles))
8328       (setq group (format "%s-%d" gnus-newsgroup-name article))
8329       (gnus-summary-remove-process-mark article)
8330       (when (gnus-summary-display-article article)
8331         (save-excursion
8332           (with-temp-buffer
8333             (insert-buffer-substring gnus-original-article-buffer)
8334             ;; Remove some headers that may lead nndoc to make
8335             ;; the wrong guess.
8336             (message-narrow-to-head)
8337             (goto-char (point-min))
8338             (delete-matching-lines "^Path:\\|^From ")
8339             (widen)
8340             (if (setq egroup
8341                       (gnus-group-read-ephemeral-group
8342                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8343                                      (nndoc-article-type guess))
8344                        t nil t))
8345                 (progn
8346                   ;; Make all postings to this group go to the parent group.
8347                   (nconc (gnus-info-params (gnus-get-info egroup))
8348                          params)
8349                   (push egroup groups))
8350               ;; Couldn't select this doc group.
8351               (gnus-error 3 "Article couldn't be entered"))))))
8352     ;; Now we have selected all the documents.
8353     (cond
8354      ((not groups)
8355       (error "None of the articles could be interpreted as documents"))
8356      ((gnus-group-read-ephemeral-group
8357        (setq vgroup (format
8358                      "nnvirtual:%s-%s" gnus-newsgroup-name
8359                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8360        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8361        t
8362        (cons (current-buffer) 'summary)))
8363      (t
8364       (error "Couldn't select virtual nndoc group")))))
8365
8366 (defun gnus-summary-isearch-article (&optional regexp-p)
8367   "Do incremental search forward on the current article.
8368 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8369   (interactive "P")
8370   (let* ((gnus-inhibit-treatment t)
8371          (old (gnus-summary-select-article)))
8372     (gnus-configure-windows 'article)
8373     (gnus-eval-in-buffer-window gnus-article-buffer
8374       (save-restriction
8375         (widen)
8376         (when (eq 'old old)
8377           (gnus-article-show-all-headers))
8378         (goto-char (point-min))
8379         (isearch-forward regexp-p)))))
8380
8381 (defun gnus-summary-search-article-forward (regexp &optional backward)
8382   "Search for an article containing REGEXP forward.
8383 If BACKWARD, search backward instead."
8384   (interactive
8385    (list (read-string
8386           (format "Search article %s (regexp%s): "
8387                   (if current-prefix-arg "backward" "forward")
8388                   (if gnus-last-search-regexp
8389                       (concat ", default " gnus-last-search-regexp)
8390                     "")))
8391          current-prefix-arg))
8392   (if (string-equal regexp "")
8393       (setq regexp (or gnus-last-search-regexp ""))
8394     (setq gnus-last-search-regexp regexp)
8395     (setq gnus-article-before-search gnus-current-article))
8396   ;; Intentionally set gnus-last-article.
8397   (setq gnus-last-article gnus-article-before-search)
8398   (let ((gnus-last-article gnus-last-article))
8399     (if (gnus-summary-search-article regexp backward)
8400         (gnus-summary-show-thread)
8401       (error "Search failed: \"%s\"" regexp))))
8402
8403 (defun gnus-summary-search-article-backward (regexp)
8404   "Search for an article containing REGEXP backward."
8405   (interactive
8406    (list (read-string
8407           (format "Search article backward (regexp%s): "
8408                   (if gnus-last-search-regexp
8409                       (concat ", default " gnus-last-search-regexp)
8410                     "")))))
8411   (gnus-summary-search-article-forward regexp 'backward))
8412
8413 (eval-when-compile
8414   (defmacro gnus-summary-search-article-position-point (regexp backward)
8415     "Dehighlight the last matched text and goto the beginning position."
8416     (` (if (and gnus-summary-search-article-matched-data
8417                 (let ((text (caddr gnus-summary-search-article-matched-data))
8418                       (inhibit-read-only t)
8419                       buffer-read-only)
8420                   (delete-region
8421                    (goto-char (car gnus-summary-search-article-matched-data))
8422                    (cadr gnus-summary-search-article-matched-data))
8423                   (insert text)
8424                   (string-match (, regexp) text)))
8425            (if (, backward) (beginning-of-line) (end-of-line))
8426          (goto-char (if (, backward) (point-max) (point-min))))))
8427
8428   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8429     "Place point where X-Face image is displayed."
8430     (if (featurep 'xemacs)
8431         (` (let ((end (if (search-forward "\n\n" nil t)
8432                           (goto-char (1- (point)))
8433                         (point-min)))
8434                  extent)
8435              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8436              (unless (and (re-search-forward "^From:" end t)
8437                           (setq extent (extent-at (point)))
8438                           (extent-begin-glyph extent))
8439                (goto-char (, opoint)))))
8440       (` (let ((end (if (search-forward "\n\n" nil t)
8441                         (goto-char (1- (point)))
8442                       (point-min)))
8443                (start (or (search-backward "\n\n" nil t) (point-min))))
8444            (goto-char
8445             (or (text-property-any start end 'x-face-image t);; x-face-e21
8446                 (text-property-any start end 'x-face-mule-bitmap-image t)
8447                 (, opoint)))))))
8448
8449   (defmacro gnus-summary-search-article-highlight-matched-text
8450     (backward treated x-face)
8451     "Highlight matched text in the function `gnus-summary-search-article'."
8452     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8453              (end (set-marker (make-marker) (match-end 0)))
8454              (inhibit-read-only t)
8455              buffer-read-only)
8456          (unless treated
8457            (let ((,@
8458                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8459                     (mapcar
8460                      (lambda (item) (setq items (delq item items)))
8461                      '(gnus-treat-buttonize
8462                        gnus-treat-fill-article
8463                        gnus-treat-fill-long-lines
8464                        gnus-treat-emphasize
8465                        gnus-treat-highlight-headers
8466                        gnus-treat-highlight-citation
8467                        gnus-treat-highlight-signature
8468                        gnus-treat-overstrike
8469                        gnus-treat-display-x-face
8470                        gnus-treat-buttonize-head
8471                        gnus-treat-decode-article-as-default-mime-charset))
8472                     (static-if (featurep 'xemacs)
8473                         items
8474                       (cons '(x-face-mule-delete-x-face-field
8475                               (quote never))
8476                             items))))
8477                  (gnus-treat-display-x-face
8478                   (when (, x-face) gnus-treat-display-x-face)))
8479              (gnus-article-prepare-mime-display)))
8480          (goto-char (if (, backward) start end))
8481          (when (, x-face)
8482            (gnus-summary-search-article-highlight-goto-x-face (point)))
8483          (setq gnus-summary-search-article-matched-data
8484                (list start end (buffer-substring start end)))
8485          (unless (eq start end);; matched text has been deleted. :-<
8486            (put-text-property start end 'face
8487                               (or (find-face 'isearch)
8488                                   'secondary-selection))))))
8489   )
8490
8491 (defun gnus-summary-search-article (regexp &optional backward)
8492   "Search for an article containing REGEXP.
8493 Optional argument BACKWARD means do search for backward.
8494 `gnus-select-article-hook' is not called during the search."
8495   ;; We have to require this here to make sure that the following
8496   ;; dynamic binding isn't shadowed by autoloading.
8497   (require 'gnus-async)
8498   (require 'gnus-art)
8499   (let ((gnus-select-article-hook nil)  ;Disable hook.
8500         (gnus-article-prepare-hook nil)
8501         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8502         (gnus-use-article-prefetch nil)
8503         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8504         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8505         (gnus-visual nil)
8506         (gnus-keep-backlog nil)
8507         (gnus-break-pages nil)
8508         (gnus-summary-display-arrow nil)
8509         (gnus-updated-mode-lines nil)
8510         (gnus-auto-center-summary nil)
8511         (sum (current-buffer))
8512         (found nil)
8513         point treated)
8514     (gnus-save-hidden-threads
8515       (static-if (featurep 'xemacs)
8516           (let ((gnus-inhibit-treatment t))
8517             (setq treated (eq 'old (gnus-summary-select-article)))
8518             (when (and treated
8519                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8520                                  (window-live-p (get-buffer-window
8521                                                  gnus-article-buffer t)))))
8522               (gnus-summary-select-article nil t)
8523               (setq treated nil)))
8524         (let ((gnus-inhibit-treatment t)
8525               (x-face-mule-delete-x-face-field 'never))
8526           (setq treated (eq 'old (gnus-summary-select-article)))
8527           (when (and treated
8528                      (not
8529                       (and (gnus-buffer-live-p gnus-article-buffer)
8530                            (window-live-p (get-buffer-window
8531                                            gnus-article-buffer t))
8532                            (or (not (string-match "^\\^X-Face:" regexp))
8533                                (with-current-buffer gnus-article-buffer
8534                                  gnus-summary-search-article-matched-data)))))
8535             (gnus-summary-select-article nil t)
8536             (setq treated nil))))
8537       (set-buffer gnus-article-buffer)
8538       (widen)
8539       (if treated
8540           (progn
8541             (gnus-article-show-all-headers)
8542             (gnus-summary-search-article-position-point regexp backward))
8543         (goto-char (if backward (point-max) (point-min))))
8544       (while (not found)
8545         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8546         (if (if backward
8547                 (re-search-backward regexp nil t)
8548               (re-search-forward regexp nil t))
8549             ;; We found the regexp.
8550             (progn
8551               (gnus-summary-search-article-highlight-matched-text
8552                backward treated (string-match "^\\^X-Face:" regexp))
8553               (setq found 'found)
8554               (forward-line
8555                (/ (- 2 (window-height
8556                         (get-buffer-window gnus-article-buffer t)))
8557                   2))
8558               (set-window-start
8559                (get-buffer-window (current-buffer))
8560                (point))
8561               (set-buffer sum)
8562               (setq point (point)))
8563           ;; We didn't find it, so we go to the next article.
8564           (set-buffer sum)
8565           (setq found 'not)
8566           (while (eq found 'not)
8567             (if (not (if backward (gnus-summary-find-prev)
8568                        (gnus-summary-find-next)))
8569                 ;; No more articles.
8570                 (setq found t)
8571               ;; Select the next article and adjust point.
8572               (unless (gnus-summary-article-sparse-p
8573                        (gnus-summary-article-number))
8574                 (setq found nil)
8575                 (let ((gnus-inhibit-treatment t))
8576                   (gnus-summary-select-article))
8577                 (setq treated nil)
8578                 (set-buffer gnus-article-buffer)
8579                 (widen)
8580                 (goto-char (if backward (point-max) (point-min))))))))
8581       (gnus-message 7 ""))
8582     ;; Return whether we found the regexp.
8583     (when (eq found 'found)
8584       (goto-char point)
8585       (gnus-summary-show-thread)
8586       (gnus-summary-goto-subject gnus-current-article)
8587       (gnus-summary-position-point)
8588       t)))
8589
8590 (defun gnus-find-matching-articles (header regexp)
8591   "Return a list of all articles that match REGEXP on HEADER.
8592 This search includes all articles in the current group that Gnus has
8593 fetched headers for, whether they are displayed or not."
8594   (let ((articles nil)
8595         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8596         (case-fold-search t))
8597     (dolist (header gnus-newsgroup-headers)
8598       (when (string-match regexp (funcall func header))
8599         (push (mail-header-number header) articles)))
8600     (nreverse articles)))
8601
8602 (defun gnus-summary-find-matching (header regexp &optional backward unread
8603                                           not-case-fold not-matching)
8604   "Return a list of all articles that match REGEXP on HEADER.
8605 The search stars on the current article and goes forwards unless
8606 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8607 If UNREAD is non-nil, only unread articles will
8608 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8609 in the comparisons. If NOT-MATCHING, return a list of all articles that
8610 not match REGEXP on HEADER."
8611   (let ((case-fold-search (not not-case-fold))
8612         articles d func)
8613     (if (consp header)
8614         (if (eq (car header) 'extra)
8615             (setq func
8616                   `(lambda (h)
8617                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8618                          "")))
8619           (error "%s is an invalid header" header))
8620       (unless (fboundp (intern (concat "mail-header-" header)))
8621         (error "%s is not a valid header" header))
8622       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8623     (dolist (d (if (eq backward 'all)
8624                    gnus-newsgroup-data
8625                  (gnus-data-find-list
8626                   (gnus-summary-article-number)
8627                   (gnus-data-list backward))))
8628       (when (and (or (not unread)       ; We want all articles...
8629                      (gnus-data-unread-p d)) ; Or just unreads.
8630                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8631                  (if not-matching
8632                      (not (string-match
8633                            regexp
8634                            (funcall func (gnus-data-header d))))
8635                    (string-match regexp
8636                                  (funcall func (gnus-data-header d)))))
8637         (push (gnus-data-number d) articles))) ; Success!
8638     (nreverse articles)))
8639
8640 (defun gnus-summary-execute-command (header regexp command &optional backward)
8641   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8642 If HEADER is an empty string (or nil), the match is done on the entire
8643 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8644   (interactive
8645    (list (let ((completion-ignore-case t))
8646            (completing-read
8647             "Header name: "
8648             (mapcar (lambda (header) (list (format "%s" header)))
8649                     (append
8650                      '("Number" "Subject" "From" "Lines" "Date"
8651                        "Message-ID" "Xref" "References" "Body")
8652                      gnus-extra-headers))
8653             nil 'require-match))
8654          (read-string "Regexp: ")
8655          (read-key-sequence "Command: ")
8656          current-prefix-arg))
8657   (when (equal header "Body")
8658     (setq header ""))
8659   ;; Hidden thread subtrees must be searched as well.
8660   (gnus-summary-show-all-threads)
8661   ;; We don't want to change current point nor window configuration.
8662   (save-excursion
8663     (save-window-excursion
8664       (let (gnus-visual
8665             gnus-treat-strip-trailing-blank-lines
8666             gnus-treat-strip-leading-blank-lines
8667             gnus-treat-strip-multiple-blank-lines
8668             gnus-treat-hide-boring-headers
8669             gnus-treat-fold-newsgroups
8670             gnus-article-prepare-hook)
8671         (gnus-message 6 "Executing %s..." (key-description command))
8672         ;; We'd like to execute COMMAND interactively so as to give arguments.
8673         (gnus-execute header regexp
8674                       `(call-interactively ',(key-binding command))
8675                       backward)
8676         (gnus-message 6 "Executing %s...done" (key-description command))))))
8677
8678 (defun gnus-summary-beginning-of-article ()
8679   "Scroll the article back to the beginning."
8680   (interactive)
8681   (gnus-summary-select-article)
8682   (gnus-configure-windows 'article)
8683   (gnus-eval-in-buffer-window gnus-article-buffer
8684     (widen)
8685     (goto-char (point-min))
8686     (when gnus-break-pages
8687       (gnus-narrow-to-page))))
8688
8689 (defun gnus-summary-end-of-article ()
8690   "Scroll to the end of the article."
8691   (interactive)
8692   (gnus-summary-select-article)
8693   (gnus-configure-windows 'article)
8694   (gnus-eval-in-buffer-window gnus-article-buffer
8695     (widen)
8696     (goto-char (point-max))
8697     (recenter -3)
8698     (when gnus-break-pages
8699       (when (re-search-backward page-delimiter nil t)
8700         (narrow-to-region (match-end 0) (point-max)))
8701       (gnus-narrow-to-page))))
8702
8703 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8704   "Truncate to LEN and quote all \"(\"'s in STRING."
8705   (gnus-replace-in-string (if (and len (> (length string) len))
8706                               (substring string 0 len)
8707                             string)
8708                           "[()]" "\\\\\\&"))
8709
8710 (defun gnus-summary-print-article (&optional filename n)
8711   "Generate and print a PostScript image of the process-marked (mail) articles.
8712
8713 If used interactively, print the current article if none are
8714 process-marked.  With prefix arg, prompt the user for the name of the
8715 file to save in.
8716
8717 When used from Lisp, accept two optional args FILENAME and N.  N means
8718 to print the next N articles.  If N is negative, print the N previous
8719 articles.  If N is nil and articles have been marked with the process
8720 mark, print these instead.
8721
8722 If the optional first argument FILENAME is nil, send the image to the
8723 printer.  If FILENAME is a string, save the PostScript image in a file with
8724 that name.  If FILENAME is a number, prompt the user for the name of the file
8725 to save in."
8726   (interactive (list (ps-print-preprint current-prefix-arg)))
8727   (dolist (article (gnus-summary-work-articles n))
8728     (gnus-summary-select-article nil nil 'pseudo article)
8729     (gnus-eval-in-buffer-window gnus-article-buffer
8730       (gnus-print-buffer))
8731     (gnus-summary-remove-process-mark article))
8732   (ps-despool filename))
8733
8734 (defun gnus-print-buffer ()
8735   (let ((buffer (generate-new-buffer " *print*")))
8736     (unwind-protect
8737         (progn
8738           (copy-to-buffer buffer (point-min) (point-max))
8739           (set-buffer buffer)
8740           (gnus-remove-text-with-property 'gnus-decoration)
8741           (when (gnus-visual-p 'article-highlight 'highlight)
8742             ;; Copy-to-buffer doesn't copy overlay.  So redo
8743             ;; highlight.
8744             (let ((gnus-article-buffer buffer))
8745               (gnus-article-highlight-citation t)
8746               (gnus-article-highlight-signature)
8747               (gnus-article-emphasize)
8748               (gnus-article-delete-invisible-text)))
8749           (let ((ps-left-header
8750                  (list
8751                   (concat "("
8752                           (gnus-summary-print-truncate-and-quote
8753                            (mail-header-subject gnus-current-headers)
8754                            66) ")")
8755                   (concat "("
8756                           (gnus-summary-print-truncate-and-quote
8757                            (mail-header-from gnus-current-headers)
8758                            45) ")")))
8759                 (ps-right-header
8760                  (list
8761                   "/pagenumberstring load"
8762                   (concat "("
8763                           (mail-header-date gnus-current-headers) ")"))))
8764             (gnus-run-hooks 'gnus-ps-print-hook)
8765             (save-excursion
8766               (if window-system
8767                   (ps-spool-buffer-with-faces)
8768                 (ps-spool-buffer)))))
8769       (kill-buffer buffer))))
8770
8771 (defun gnus-summary-show-article (&optional arg)
8772   "Force redisplaying of the current article.
8773 If ARG (the prefix) is a number, show the article with the charset
8774 defined in `gnus-summary-show-article-charset-alist', or the charset
8775 input.
8776 If ARG (the prefix) is non-nil and not a number, show the raw article
8777 without any article massaging functions being run.  Normally, the key
8778 strokes are `C-u g'."
8779   (interactive "P")
8780   (cond
8781    ((numberp arg)
8782     (gnus-summary-show-article t)
8783     (let* ((gnus-newsgroup-charset
8784             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8785                 (mm-read-coding-system
8786                  "View as charset: " ;; actually it is coding system.
8787                  (save-excursion
8788                    (set-buffer gnus-article-buffer)
8789                    (mm-detect-coding-region (point) (point-max))))))
8790            (default-mime-charset gnus-newsgroup-charset)
8791            (gnus-newsgroup-ignored-charsets 'gnus-all))
8792       (gnus-summary-select-article nil 'force)
8793       (let ((deps gnus-newsgroup-dependencies)
8794             head header lines)
8795         (save-excursion
8796           (set-buffer gnus-original-article-buffer)
8797           (save-restriction
8798             (message-narrow-to-head)
8799             (setq head (buffer-string))
8800             (goto-char (point-min))
8801             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8802               (goto-char (point-max))
8803               (widen)
8804               (setq lines (1- (count-lines (point) (point-max))))))
8805           (with-temp-buffer
8806             (insert (format "211 %d Article retrieved.\n"
8807                             (cdr gnus-article-current)))
8808             (insert head)
8809             (if lines (insert (format "Lines: %d\n" lines)))
8810             (insert ".\n")
8811             (let ((nntp-server-buffer (current-buffer)))
8812               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8813         (gnus-data-set-header
8814          (gnus-data-find (cdr gnus-article-current))
8815          header)
8816         (gnus-summary-update-article-line
8817          (cdr gnus-article-current) header)
8818         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8819           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8820    ((not arg)
8821     ;; Select the article the normal way.
8822     (gnus-summary-select-article nil 'force))
8823    (t
8824     ;; We have to require this here to make sure that the following
8825     ;; dynamic binding isn't shadowed by autoloading.
8826     (require 'gnus-async)
8827     (require 'gnus-art)
8828     ;; Bind the article treatment functions to nil.
8829     (let ((gnus-have-all-headers t)
8830           gnus-article-prepare-hook
8831           gnus-article-decode-hook
8832           gnus-break-pages
8833           gnus-show-mime
8834           (gnus-inhibit-treatment t))
8835       (gnus-summary-select-article nil 'force))))
8836   (gnus-summary-goto-subject gnus-current-article)
8837   (gnus-summary-position-point))
8838
8839 (defun gnus-summary-show-raw-article ()
8840   "Show the raw article without any article massaging functions being run."
8841   (interactive)
8842   (gnus-summary-show-article t))
8843
8844 (defun gnus-summary-verbose-headers (&optional arg)
8845   "Toggle permanent full header display.
8846 If ARG is a positive number, turn header display on.
8847 If ARG is a negative number, turn header display off."
8848   (interactive "P")
8849   (setq gnus-show-all-headers
8850         (cond ((or (not (numberp arg))
8851                    (zerop arg))
8852                (not gnus-show-all-headers))
8853               ((natnump arg)
8854                t)))
8855   (gnus-summary-show-article))
8856
8857 (defun gnus-summary-toggle-header (&optional arg)
8858   "Show the headers if they are hidden, or hide them if they are shown.
8859 If ARG is a positive number, show the entire header.
8860 If ARG is a negative number, hide the unwanted header lines."
8861   (interactive "P")
8862   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8863                      (get-buffer-window gnus-article-buffer t))))
8864     (with-current-buffer gnus-article-buffer
8865       (widen)
8866       (article-narrow-to-head)
8867       (let* ((buffer-read-only nil)
8868              (inhibit-point-motion-hooks t)
8869              (hidden (if (numberp arg)
8870                          (>= arg 0)
8871                        (gnus-article-hidden-text-p 'headers)))
8872              s e)
8873         (delete-region (point-min) (point-max))
8874         (with-current-buffer gnus-original-article-buffer
8875           (goto-char (setq s (point-min)))
8876           (setq e (if (search-forward "\n\n" nil t)
8877                       (1- (point))
8878                     (point-max))))
8879         (insert-buffer-substring gnus-original-article-buffer s e)
8880         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8881         ;; article-decode-encoded-words by default.
8882         (article-decode-encoded-words)
8883         (run-hooks 'gnus-article-decode-hook)
8884         (if hidden
8885             (let ((gnus-treat-hide-headers nil)
8886                   (gnus-treat-hide-boring-headers nil))
8887               (gnus-delete-wash-type 'headers)
8888               (gnus-treat-article 'head))
8889           (gnus-treat-article 'head))
8890         (widen)
8891         (if window
8892             (set-window-start window (goto-char (point-min))))
8893         (if gnus-break-pages
8894             (gnus-narrow-to-page)
8895           (when (gnus-visual-p 'page-marker)
8896             (let ((buffer-read-only nil))
8897               (gnus-remove-text-with-property 'gnus-prev)
8898               (gnus-remove-text-with-property 'gnus-next))))
8899         (gnus-set-mode-line 'article)))))
8900
8901 (defun gnus-summary-show-all-headers ()
8902   "Make all header lines visible."
8903   (interactive)
8904   (gnus-summary-toggle-header 1))
8905
8906 (defun gnus-summary-toggle-mime (&optional arg)
8907   "Toggle MIME processing.
8908 If ARG is a positive number, turn MIME processing on."
8909   (interactive "P")
8910   (setq gnus-show-mime
8911         (if (null arg)
8912             (not gnus-show-mime)
8913           (> (prefix-numeric-value arg) 0)))
8914   (gnus-summary-select-article t 'force))
8915
8916 (defun gnus-summary-caesar-message (&optional arg)
8917   "Caesar rotate the current article by 13.
8918 The numerical prefix specifies how many places to rotate each letter
8919 forward."
8920   (interactive "P")
8921   (gnus-summary-select-article)
8922   (let ((mail-header-separator ""))
8923     (gnus-eval-in-buffer-window gnus-article-buffer
8924       (save-restriction
8925         (widen)
8926         (let ((start (window-start))
8927               buffer-read-only)
8928           (message-caesar-buffer-body arg)
8929           (set-window-start (get-buffer-window (current-buffer)) start))))))
8930
8931 (autoload 'unmorse-region "morse"
8932   "Convert morse coded text in region to ordinary ASCII text."
8933   t)
8934
8935 (defun gnus-summary-morse-message (&optional arg)
8936   "Morse decode the current article."
8937   (interactive "P")
8938   (gnus-summary-select-article)
8939   (let ((mail-header-separator ""))
8940     (gnus-eval-in-buffer-window gnus-article-buffer
8941       (save-excursion
8942         (save-restriction
8943           (widen)
8944           (let ((pos (window-start))
8945                 buffer-read-only)
8946             (goto-char (point-min))
8947             (when (message-goto-body)
8948               (gnus-narrow-to-body))
8949             (goto-char (point-min))
8950             (while (re-search-forward "·" (point-max) t)
8951               (replace-match "."))
8952             (unmorse-region (point-min) (point-max))
8953             (widen)
8954             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8955
8956 (defun gnus-summary-stop-page-breaking ()
8957   "Stop page breaking in the current article."
8958   (interactive)
8959   (gnus-summary-select-article)
8960   (gnus-eval-in-buffer-window gnus-article-buffer
8961     (widen)
8962     (when (gnus-visual-p 'page-marker)
8963       (let ((buffer-read-only nil))
8964         (gnus-remove-text-with-property 'gnus-prev)
8965         (gnus-remove-text-with-property 'gnus-next))
8966       (setq gnus-page-broken nil))))
8967
8968 (defun gnus-summary-move-article (&optional n to-newsgroup
8969                                             select-method action)
8970   "Move the current article to a different newsgroup.
8971 If N is a positive number, move the N next articles.
8972 If N is a negative number, move the N previous articles.
8973 If N is nil and any articles have been marked with the process mark,
8974 move those articles instead.
8975 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8976 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8977 re-spool using this method.
8978
8979 When called interactively with TO-NEWSGROUP being nil, the value of
8980 the variable `gnus-move-split-methods' is used for finding a default
8981 for the target newsgroup.
8982
8983 For this function to work, both the current newsgroup and the
8984 newsgroup that you want to move to have to support the `request-move'
8985 and `request-accept' functions.
8986
8987 ACTION can be either `move' (the default), `crosspost' or `copy'."
8988   (interactive "P")
8989   (unless action
8990     (setq action 'move))
8991   ;; Check whether the source group supports the required functions.
8992   (cond ((and (eq action 'move)
8993               (not (gnus-check-backend-function
8994                     'request-move-article gnus-newsgroup-name)))
8995          (error "The current group does not support article moving"))
8996         ((and (eq action 'crosspost)
8997               (not (gnus-check-backend-function
8998                     'request-replace-article gnus-newsgroup-name)))
8999          (error "The current group does not support article editing")))
9000   (let ((articles (gnus-summary-work-articles n))
9001         (prefix (if (gnus-check-backend-function
9002                      'request-move-article gnus-newsgroup-name)
9003                     (funcall gnus-move-group-prefix-function
9004                              gnus-newsgroup-name)
9005                   ""))
9006         (names '((move "Move" "Moving")
9007                  (copy "Copy" "Copying")
9008                  (crosspost "Crosspost" "Crossposting")))
9009         (copy-buf (save-excursion
9010                     (nnheader-set-temp-buffer " *copy article*")))
9011         art-group to-method new-xref article to-groups)
9012     (unless (assq action names)
9013       (error "Unknown action %s" action))
9014     ;; Read the newsgroup name.
9015     (when (and (not to-newsgroup)
9016                (not select-method))
9017       (if (and gnus-move-split-methods
9018                (not
9019                 (and (memq gnus-current-article articles)
9020                      (gnus-buffer-live-p gnus-original-article-buffer))))
9021           ;; When `gnus-move-split-methods' is non-nil, we have to
9022           ;; select an article to give `gnus-read-move-group-name' an
9023           ;; opportunity to suggest an appropriate default.  However,
9024           ;; we needn't render or mark the article.
9025           (let ((gnus-display-mime-function nil)
9026                 (gnus-article-prepare-hook nil)
9027                 (gnus-mark-article-hook nil))
9028             (gnus-summary-select-article nil nil nil (car articles))))
9029       (setq to-newsgroup
9030             (gnus-read-move-group-name
9031              (cadr (assq action names))
9032              (symbol-value (intern (format "gnus-current-%s-group" action)))
9033              articles prefix))
9034       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9035     (setq to-method (or select-method
9036                         (gnus-server-to-method
9037                          (gnus-group-method to-newsgroup))))
9038     ;; Check the method we are to move this article to...
9039     (unless (gnus-check-backend-function
9040              'request-accept-article (car to-method))
9041       (error "%s does not support article copying" (car to-method)))
9042     (unless (gnus-check-server to-method)
9043       (error "Can't open server %s" (car to-method)))
9044     (gnus-message 6 "%s to %s: %s..."
9045                   (caddr (assq action names))
9046                   (or (car select-method) to-newsgroup) articles)
9047     (while articles
9048       (setq article (pop articles))
9049       (setq
9050        art-group
9051        (cond
9052         ;; Move the article.
9053         ((eq action 'move)
9054          ;; Remove this article from future suppression.
9055          (gnus-dup-unsuppress-article article)
9056          (gnus-request-move-article
9057           article                       ; Article to move
9058           gnus-newsgroup-name           ; From newsgroup
9059           (nth 1 (gnus-find-method-for-group
9060                   gnus-newsgroup-name)) ; Server
9061           (list 'gnus-request-accept-article
9062                 to-newsgroup (list 'quote select-method)
9063                 (not articles) t)       ; Accept form
9064           (not articles)))              ; Only save nov last time
9065         ;; Copy the article.
9066         ((eq action 'copy)
9067          (save-excursion
9068            (set-buffer copy-buf)
9069            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9070              (gnus-request-accept-article
9071               to-newsgroup select-method (not articles) t))))
9072         ;; Crosspost the article.
9073         ((eq action 'crosspost)
9074          (let ((xref (message-tokenize-header
9075                       (mail-header-xref (gnus-summary-article-header article))
9076                       " ")))
9077            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9078                                   ":" (number-to-string article)))
9079            (unless xref
9080              (setq xref (list (system-name))))
9081            (setq new-xref
9082                  (concat
9083                   (mapconcat 'identity
9084                              (delete "Xref:" (delete new-xref xref))
9085                              " ")
9086                   " " new-xref))
9087            (save-excursion
9088              (set-buffer copy-buf)
9089              ;; First put the article in the destination group.
9090              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9091              (when (consp (setq art-group
9092                                 (gnus-request-accept-article
9093                                  to-newsgroup select-method (not articles))))
9094                (setq new-xref (concat new-xref " " (car art-group)
9095                                       ":"
9096                                       (number-to-string (cdr art-group))))
9097                ;; Now we have the new Xrefs header, so we insert
9098                ;; it and replace the new article.
9099                (nnheader-replace-header "Xref" new-xref)
9100                (gnus-request-replace-article
9101                 (cdr art-group) to-newsgroup (current-buffer))
9102                art-group))))))
9103       (cond
9104        ((not art-group)
9105         (gnus-message 1 "Couldn't %s article %s: %s"
9106                       (cadr (assq action names)) article
9107                       (nnheader-get-report (car to-method))))
9108        ((eq art-group 'junk)
9109         (when (eq action 'move)
9110           (gnus-summary-mark-article article gnus-canceled-mark)
9111           (gnus-message 4 "Deleted article %s" article)
9112           ;; run the delete hook
9113           (run-hook-with-args 'gnus-summary-article-delete-hook
9114                               action
9115                               (gnus-data-header
9116                                (assoc article (gnus-data-list nil)))
9117                               gnus-newsgroup-name nil
9118                               select-method)))
9119        (t
9120         (let* ((pto-group (gnus-group-prefixed-name
9121                            (car art-group) to-method))
9122                (info (gnus-get-info pto-group))
9123                (to-group (gnus-info-group info))
9124                to-marks)
9125           ;; Update the group that has been moved to.
9126           (when (and info
9127                      (memq action '(move copy)))
9128             (unless (member to-group to-groups)
9129               (push to-group to-groups))
9130
9131             (unless (memq article gnus-newsgroup-unreads)
9132               (push 'read to-marks)
9133               (gnus-info-set-read
9134                info (gnus-add-to-range (gnus-info-read info)
9135                                        (list (cdr art-group)))))
9136
9137             ;; See whether the article is to be put in the cache.
9138             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9139                              gnus-article-mark-lists
9140                            (delete '(expirable . expire)
9141                                    (copy-sequence gnus-article-mark-lists))))
9142                   (to-article (cdr art-group)))
9143
9144               ;; Enter the article into the cache in the new group,
9145               ;; if that is required.
9146               (when gnus-use-cache
9147                 (gnus-cache-possibly-enter-article
9148                  to-group to-article
9149                  (let ((header (copy-sequence
9150                                 (gnus-summary-article-header article))))
9151                    (mail-header-set-number header to-article)
9152                    header)
9153                  (memq article gnus-newsgroup-marked)
9154                  (memq article gnus-newsgroup-dormant)
9155                  (memq article gnus-newsgroup-unreads)))
9156
9157               (when gnus-preserve-marks
9158                 ;; Copy any marks over to the new group.
9159                 (when (and (equal to-group gnus-newsgroup-name)
9160                            (not (memq article gnus-newsgroup-unreads)))
9161                   ;; Mark this article as read in this group.
9162                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9163                   (setcdr (gnus-active to-group) to-article)
9164                   (setcdr gnus-newsgroup-active to-article))
9165
9166                 (while marks
9167                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9168                     (when (memq article (symbol-value
9169                                          (intern (format "gnus-newsgroup-%s"
9170                                                          (caar marks)))))
9171                       (push (cdar marks) to-marks)
9172                       ;; If the other group is the same as this group,
9173                       ;; then we have to add the mark to the list.
9174                       (when (equal to-group gnus-newsgroup-name)
9175                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9176                              (cons to-article
9177                                    (symbol-value
9178                                     (intern (format "gnus-newsgroup-%s"
9179                                                     (caar marks)))))))
9180                       ;; Copy the marks to other group.
9181                       (gnus-add-marked-articles
9182                        to-group (cdar marks) (list to-article) info)))
9183                   (setq marks (cdr marks)))
9184
9185                 (gnus-request-set-mark
9186                  to-group (list (list (list to-article) 'add to-marks))))
9187
9188               (gnus-dribble-enter
9189                (concat "(gnus-group-set-info '"
9190                        (gnus-prin1-to-string (gnus-get-info to-group))
9191                        ")"))))
9192
9193           ;; Update the Xref header in this article to point to
9194           ;; the new crossposted article we have just created.
9195           (when (eq action 'crosspost)
9196             (save-excursion
9197               (set-buffer copy-buf)
9198               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9199               (nnheader-replace-header "Xref" new-xref)
9200               (gnus-request-replace-article
9201                article gnus-newsgroup-name (current-buffer))))
9202
9203           ;; run the move/copy/crosspost/respool hook
9204           (run-hook-with-args 'gnus-summary-article-move-hook
9205                               action
9206                               (gnus-data-header
9207                                (assoc article (gnus-data-list nil)))
9208                               gnus-newsgroup-name
9209                               to-newsgroup
9210                               select-method))
9211
9212         ;;;!!!Why is this necessary?
9213         (set-buffer gnus-summary-buffer)
9214         
9215         (gnus-summary-goto-subject article)
9216         (when (eq action 'move)
9217           (gnus-summary-mark-article article gnus-canceled-mark))))
9218       (gnus-summary-remove-process-mark article))
9219     ;; Re-activate all groups that have been moved to.
9220     (save-excursion
9221       (set-buffer gnus-group-buffer)
9222       (let ((gnus-group-marked to-groups))
9223         (gnus-group-get-new-news-this-group nil t)))
9224
9225     (gnus-kill-buffer copy-buf)
9226     (gnus-summary-position-point)
9227     (gnus-set-mode-line 'summary)))
9228
9229 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9230   "Move the current article to a different newsgroup.
9231 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9232 When called interactively, if TO-NEWSGROUP is nil, use the value of
9233 the variable `gnus-move-split-methods' for finding a default target
9234 newsgroup.
9235 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9236 re-spool using this method."
9237   (interactive "P")
9238   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9239
9240 (defun gnus-summary-crosspost-article (&optional n)
9241   "Crosspost the current article to some other group."
9242   (interactive "P")
9243   (gnus-summary-move-article n nil nil 'crosspost))
9244
9245 (defcustom gnus-summary-respool-default-method nil
9246   "Default method type for respooling an article.
9247 If nil, use to the current newsgroup method."
9248   :type 'symbol
9249   :group 'gnus-summary-mail)
9250
9251 (defcustom gnus-summary-display-while-building nil
9252   "If non-nil, show and update the summary buffer as it's being built.
9253 If the value is t, update the buffer after every line is inserted.  If
9254 the value is an integer (N), update the display every N lines."
9255   :group 'gnus-thread
9256   :type '(choice (const :tag "off" nil)
9257                  number
9258                  (const :tag "frequently" t)))
9259
9260 (defun gnus-summary-respool-article (&optional n method)
9261   "Respool the current article.
9262 The article will be squeezed through the mail spooling process again,
9263 which means that it will be put in some mail newsgroup or other
9264 depending on `nnmail-split-methods'.
9265 If N is a positive number, respool the N next articles.
9266 If N is a negative number, respool the N previous articles.
9267 If N is nil and any articles have been marked with the process mark,
9268 respool those articles instead.
9269
9270 Respooling can be done both from mail groups and \"real\" newsgroups.
9271 In the former case, the articles in question will be moved from the
9272 current group into whatever groups they are destined to.  In the
9273 latter case, they will be copied into the relevant groups."
9274   (interactive
9275    (list current-prefix-arg
9276          (let* ((methods (gnus-methods-using 'respool))
9277                 (methname
9278                  (symbol-name (or gnus-summary-respool-default-method
9279                                   (car (gnus-find-method-for-group
9280                                         gnus-newsgroup-name)))))
9281                 (method
9282                  (gnus-completing-read-with-default
9283                   methname "What backend do you want to use when respooling?"
9284                   methods nil t nil 'gnus-mail-method-history))
9285                 ms)
9286            (cond
9287             ((zerop (length (setq ms (gnus-servers-using-backend
9288                                       (intern method)))))
9289              (list (intern method) ""))
9290             ((= 1 (length ms))
9291              (car ms))
9292             (t
9293              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9294                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9295                            ms-alist))))))))
9296   (unless method
9297     (error "No method given for respooling"))
9298   (if (assoc (symbol-name
9299               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9300              (gnus-methods-using 'respool))
9301       (gnus-summary-move-article n nil method)
9302     (gnus-summary-copy-article n nil method)))
9303
9304 (defun gnus-summary-import-article (file &optional edit)
9305   "Import an arbitrary file into a mail newsgroup."
9306   (interactive "fImport file: \nP")
9307   (let ((group gnus-newsgroup-name)
9308         (now (current-time))
9309         atts lines group-art)
9310     (unless (gnus-check-backend-function 'request-accept-article group)
9311       (error "%s does not support article importing" group))
9312     (or (file-readable-p file)
9313         (not (file-regular-p file))
9314         (error "Can't read %s" file))
9315     (save-excursion
9316       (set-buffer (gnus-get-buffer-create " *import file*"))
9317       (erase-buffer)
9318       (nnheader-insert-file-contents file)
9319       (goto-char (point-min))
9320       (if (nnheader-article-p)
9321           (save-restriction
9322             (goto-char (point-min))
9323             (search-forward "\n\n" nil t)
9324             (narrow-to-region (point-min) (1- (point)))
9325             (goto-char (point-min))
9326             (unless (re-search-forward "^date:" nil t)
9327               (goto-char (point-max))
9328               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9329         ;; This doesn't look like an article, so we fudge some headers.
9330         (setq atts (file-attributes file)
9331               lines (count-lines (point-min) (point-max)))
9332         (insert "From: " (read-string "From: ") "\n"
9333                 "Subject: " (read-string "Subject: ") "\n"
9334                 "Date: " (message-make-date (nth 5 atts)) "\n"
9335                 "Message-ID: " (message-make-message-id) "\n"
9336                 "Lines: " (int-to-string lines) "\n"
9337                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9338       (setq group-art (gnus-request-accept-article group nil t))
9339       (kill-buffer (current-buffer)))
9340     (setq gnus-newsgroup-active (gnus-activate-group group))
9341     (forward-line 1)
9342     (gnus-summary-goto-article (cdr group-art) nil t)
9343     (when edit
9344       (gnus-summary-edit-article))))
9345
9346 (defun gnus-summary-create-article ()
9347   "Create an article in a mail newsgroup."
9348   (interactive)
9349   (let ((group gnus-newsgroup-name)
9350         (now (current-time))
9351         group-art)
9352     (unless (gnus-check-backend-function 'request-accept-article group)
9353       (error "%s does not support article importing" group))
9354     (save-excursion
9355       (set-buffer (gnus-get-buffer-create " *import file*"))
9356       (erase-buffer)
9357       (goto-char (point-min))
9358       ;; This doesn't look like an article, so we fudge some headers.
9359       (insert "From: " (read-string "From: ") "\n"
9360               "Subject: " (read-string "Subject: ") "\n"
9361               "Date: " (message-make-date now) "\n"
9362               "Message-ID: " (message-make-message-id) "\n")
9363       (setq group-art (gnus-request-accept-article group nil t))
9364       (kill-buffer (current-buffer)))
9365     (setq gnus-newsgroup-active (gnus-activate-group group))
9366     (forward-line 1)
9367     (gnus-summary-goto-article (cdr group-art) nil t)
9368     (gnus-summary-edit-article)))
9369
9370 (defun gnus-summary-article-posted-p ()
9371   "Say whether the current (mail) article is available from news as well.
9372 This will be the case if the article has both been mailed and posted."
9373   (interactive)
9374   (let ((id (mail-header-references (gnus-summary-article-header)))
9375         (gnus-override-method (car (gnus-refer-article-methods))))
9376     (if (gnus-request-head id "")
9377         (gnus-message 2 "The current message was found on %s"
9378                       gnus-override-method)
9379       (gnus-message 2 "The current message couldn't be found on %s"
9380                     gnus-override-method)
9381       nil)))
9382
9383 (defun gnus-summary-expire-articles (&optional now)
9384   "Expire all articles that are marked as expirable in the current group."
9385   (interactive)
9386   (when (and (not gnus-group-is-exiting-without-update-p)
9387              (gnus-check-backend-function
9388               'request-expire-articles gnus-newsgroup-name))
9389     ;; This backend supports expiry.
9390     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9391            (expirable (if total
9392                           (progn
9393                             ;; We need to update the info for
9394                             ;; this group for `gnus-list-of-read-articles'
9395                             ;; to give us the right answer.
9396                             (gnus-run-hooks 'gnus-exit-group-hook)
9397                             (gnus-summary-update-info)
9398                             (gnus-list-of-read-articles gnus-newsgroup-name))
9399                         (setq gnus-newsgroup-expirable
9400                               (sort gnus-newsgroup-expirable '<))))
9401            (expiry-wait (if now 'immediate
9402                           (gnus-group-find-parameter
9403                            gnus-newsgroup-name 'expiry-wait)))
9404            (nnmail-expiry-target
9405             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9406                 nnmail-expiry-target))
9407            es)
9408       (when expirable
9409         ;; There are expirable articles in this group, so we run them
9410         ;; through the expiry process.
9411         (gnus-message 6 "Expiring articles...")
9412         (unless (gnus-check-group gnus-newsgroup-name)
9413           (error "Can't open server for %s" gnus-newsgroup-name))
9414         ;; The list of articles that weren't expired is returned.
9415         (save-excursion
9416           (if expiry-wait
9417               (let ((nnmail-expiry-wait-function nil)
9418                     (nnmail-expiry-wait expiry-wait))
9419                 (setq es (gnus-request-expire-articles
9420                           expirable gnus-newsgroup-name)))
9421             (setq es (gnus-request-expire-articles
9422                       expirable gnus-newsgroup-name)))
9423           (unless total
9424             (setq gnus-newsgroup-expirable es))
9425           ;; We go through the old list of expirable, and mark all
9426           ;; really expired articles as nonexistent.
9427           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9428             (let ((gnus-use-cache nil))
9429               (dolist (article expirable)
9430                 (when (and (not (memq article es))
9431                            (gnus-data-find article))
9432                   (gnus-summary-mark-article article gnus-canceled-mark)
9433                   (run-hook-with-args 'gnus-summary-article-expire-hook
9434                                       'delete
9435                                       (gnus-data-header
9436                                        (assoc article (gnus-data-list nil)))
9437                                       gnus-newsgroup-name
9438                                       nil
9439                                       nil))))))
9440         (gnus-message 6 "Expiring articles...done")))))
9441
9442 (defun gnus-summary-expire-articles-now ()
9443   "Expunge all expirable articles in the current group.
9444 This means that *all* articles that are marked as expirable will be
9445 deleted forever, right now."
9446   (interactive)
9447   (or gnus-expert-user
9448       (gnus-yes-or-no-p
9449        "Are you really, really, really sure you want to delete all these messages? ")
9450       (error "Phew!"))
9451   (gnus-summary-expire-articles t))
9452
9453 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9454 (defun gnus-summary-delete-article (&optional n)
9455   "Delete the N next (mail) articles.
9456 This command actually deletes articles.  This is not a marking
9457 command.  The article will disappear forever from your life, never to
9458 return.
9459
9460 If N is negative, delete backwards.
9461 If N is nil and articles have been marked with the process mark,
9462 delete these instead.
9463
9464 If `gnus-novice-user' is non-nil you will be asked for
9465 confirmation before the articles are deleted."
9466   (interactive "P")
9467   (unless (gnus-check-backend-function 'request-expire-articles
9468                                        gnus-newsgroup-name)
9469     (error "The current newsgroup does not support article deletion"))
9470   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9471     (error "Couldn't open server"))
9472   ;; Compute the list of articles to delete.
9473   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9474         (nnmail-expiry-target 'delete)
9475         not-deleted)
9476     (if (and gnus-novice-user
9477              (not (gnus-yes-or-no-p
9478                    (format "Do you really want to delete %s forever? "
9479                            (if (> (length articles) 1)
9480                                (format "these %s articles" (length articles))
9481                              "this article")))))
9482         ()
9483       ;; Delete the articles.
9484       (setq not-deleted (gnus-request-expire-articles
9485                          articles gnus-newsgroup-name 'force))
9486       (while articles
9487         (gnus-summary-remove-process-mark (car articles))
9488         ;; The backend might not have been able to delete the article
9489         ;; after all.
9490         (unless (memq (car articles) not-deleted)
9491           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9492         (let* ((article (car articles))
9493                (id (mail-header-id (gnus-data-header
9494                                     (assoc article (gnus-data-list nil))))))
9495           (run-hook-with-args 'gnus-summary-article-delete-hook
9496                               'delete id gnus-newsgroup-name nil
9497                               nil))
9498         (setq articles (cdr articles)))
9499       (when not-deleted
9500         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9501     (gnus-summary-position-point)
9502     (gnus-set-mode-line 'summary)
9503     not-deleted))
9504
9505 (defun gnus-summary-edit-article (&optional force)
9506   "Edit the current article.
9507 This will have permanent effect only in mail groups.
9508 If FORCE is non-nil, allow editing of articles even in read-only
9509 groups."
9510   (interactive "P")
9511   (save-excursion
9512     (set-buffer gnus-summary-buffer)
9513     (let ((mail-parse-charset gnus-newsgroup-charset)
9514           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9515       (gnus-set-global-variables)
9516       (when (and (not force)
9517                  (gnus-group-read-only-p))
9518         (error "The current newsgroup does not support article editing"))
9519       (gnus-summary-show-article t)
9520       (gnus-article-edit-article
9521        'ignore
9522        `(lambda (no-highlight)
9523           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9524                 (message-options message-options)
9525                 (message-options-set-recipient)
9526                 (mail-parse-ignored-charsets
9527                  ',gnus-newsgroup-ignored-charsets))
9528             (gnus-summary-edit-article-done
9529              ,(or (mail-header-references gnus-current-headers) "")
9530              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9531
9532 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9533
9534 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9535                                                  no-highlight)
9536   "Make edits to the current article permanent."
9537   (interactive)
9538   (save-excursion
9539     ;; The buffer restriction contains the entire article if it exists.
9540     (when (article-goto-body)
9541       (let ((lines (count-lines (point) (point-max)))
9542             (length (- (point-max) (point)))
9543             (case-fold-search t)
9544             (body (copy-marker (point))))
9545         (goto-char (point-min))
9546         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9547           (delete-region (match-beginning 1) (match-end 1))
9548           (insert (number-to-string length)))
9549         (goto-char (point-min))
9550         (when (re-search-forward
9551                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9552           (delete-region (match-beginning 1) (match-end 1))
9553           (insert (number-to-string length)))
9554         (goto-char (point-min))
9555         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9556           (delete-region (match-beginning 1) (match-end 1))
9557           (insert (number-to-string lines))))))
9558   ;; Replace the article.
9559   (let ((buf (current-buffer)))
9560     (with-temp-buffer
9561       (insert-buffer-substring buf)
9562
9563       (if (and (not read-only)
9564                (not (gnus-request-replace-article
9565                      (cdr gnus-article-current) (car gnus-article-current)
9566                      (current-buffer) t)))
9567           (error "Couldn't replace article")
9568         ;; Update the summary buffer.
9569         (if (and references
9570                  (equal (message-tokenize-header references " ")
9571                         (message-tokenize-header
9572                          (or (message-fetch-field "references") "") " ")))
9573             ;; We only have to update this line.
9574             (save-excursion
9575               (save-restriction
9576                 (message-narrow-to-head)
9577                 (let ((head (buffer-string))
9578                       header)
9579                   (with-temp-buffer
9580                     (insert (format "211 %d Article retrieved.\n"
9581                                     (cdr gnus-article-current)))
9582                     (insert head)
9583                     (insert ".\n")
9584                     (let ((nntp-server-buffer (current-buffer)))
9585                       (setq header (car (gnus-get-newsgroup-headers
9586                                          nil t))))
9587                     (save-excursion
9588                       (set-buffer gnus-summary-buffer)
9589                       (gnus-data-set-header
9590                        (gnus-data-find (cdr gnus-article-current))
9591                        header)
9592                       (gnus-summary-update-article-line
9593                        (cdr gnus-article-current) header)
9594                       (if (gnus-summary-goto-subject
9595                            (cdr gnus-article-current) nil t)
9596                           (gnus-summary-update-secondary-mark
9597                            (cdr gnus-article-current))))))))
9598           ;; Update threads.
9599           (set-buffer (or buffer gnus-summary-buffer))
9600           (gnus-summary-update-article (cdr gnus-article-current))
9601           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9602               (gnus-summary-update-secondary-mark
9603                (cdr gnus-article-current))))
9604         ;; Prettify the article buffer again.
9605         (unless no-highlight
9606           (save-excursion
9607             (set-buffer gnus-article-buffer)
9608             ;;;!!! Fix this -- article should be rehighlighted.
9609             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9610             (set-buffer gnus-original-article-buffer)
9611             (gnus-request-article
9612              (cdr gnus-article-current)
9613              (car gnus-article-current) (current-buffer))))
9614         ;; Prettify the summary buffer line.
9615         (when (gnus-visual-p 'summary-highlight 'highlight)
9616           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9617
9618 (defun gnus-summary-edit-wash (key)
9619   "Perform editing command KEY in the article buffer."
9620   (interactive
9621    (list
9622     (progn
9623       (message "%s" (concat (this-command-keys) "- "))
9624       (read-char))))
9625   (message "")
9626   (gnus-summary-edit-article)
9627   (execute-kbd-macro (concat (this-command-keys) key))
9628   (gnus-article-edit-done))
9629
9630 ;;; Respooling
9631
9632 (defun gnus-summary-respool-query (&optional silent trace)
9633   "Query where the respool algorithm would put this article."
9634   (interactive)
9635   (let (gnus-mark-article-hook)
9636     (gnus-summary-select-article)
9637     (save-excursion
9638       (set-buffer gnus-original-article-buffer)
9639       (let ((groups (nnmail-article-group 'identity trace)))
9640         (unless silent
9641           (if groups
9642               (message "This message would go to %s"
9643                        (mapconcat 'car groups ", "))
9644             (message "This message would go to no groups"))
9645           groups)))))
9646
9647 (defun gnus-summary-respool-trace ()
9648   "Trace where the respool algorithm would put this article.
9649 Display a buffer showing all fancy splitting patterns which matched."
9650   (interactive)
9651   (gnus-summary-respool-query nil t))
9652
9653 ;; Summary marking commands.
9654
9655 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9656   "Mark articles which has the same subject as read, and then select the next.
9657 If UNMARK is positive, remove any kind of mark.
9658 If UNMARK is negative, tick articles."
9659   (interactive "P")
9660   (when unmark
9661     (setq unmark (prefix-numeric-value unmark)))
9662   (let ((count
9663          (gnus-summary-mark-same-subject
9664           (gnus-summary-article-subject) unmark)))
9665     ;; Select next unread article.  If auto-select-same mode, should
9666     ;; select the first unread article.
9667     (gnus-summary-next-article t (and gnus-auto-select-same
9668                                       (gnus-summary-article-subject)))
9669     (gnus-message 7 "%d article%s marked as %s"
9670                   count (if (= count 1) " is" "s are")
9671                   (if unmark "unread" "read"))))
9672
9673 (defun gnus-summary-kill-same-subject (&optional unmark)
9674   "Mark articles which has the same subject as read.
9675 If UNMARK is positive, remove any kind of mark.
9676 If UNMARK is negative, tick articles."
9677   (interactive "P")
9678   (when unmark
9679     (setq unmark (prefix-numeric-value unmark)))
9680   (let ((count
9681          (gnus-summary-mark-same-subject
9682           (gnus-summary-article-subject) unmark)))
9683     ;; If marked as read, go to next unread subject.
9684     (when (null unmark)
9685       ;; Go to next unread subject.
9686       (gnus-summary-next-subject 1 t))
9687     (gnus-message 7 "%d articles are marked as %s"
9688                   count (if unmark "unread" "read"))))
9689
9690 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9691   "Mark articles with same SUBJECT as read, and return marked number.
9692 If optional argument UNMARK is positive, remove any kinds of marks.
9693 If optional argument UNMARK is negative, mark articles as unread instead."
9694   (let ((count 1))
9695     (save-excursion
9696       (cond
9697        ((null unmark)                   ; Mark as read.
9698         (while (and
9699                 (progn
9700                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9701                   (gnus-summary-show-thread) t)
9702                 (gnus-summary-find-subject subject))
9703           (setq count (1+ count))))
9704        ((> unmark 0)                    ; Tick.
9705         (while (and
9706                 (progn
9707                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9708                   (gnus-summary-show-thread) t)
9709                 (gnus-summary-find-subject subject))
9710           (setq count (1+ count))))
9711        (t                               ; Mark as unread.
9712         (while (and
9713                 (progn
9714                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9715                   (gnus-summary-show-thread) t)
9716                 (gnus-summary-find-subject subject))
9717           (setq count (1+ count)))))
9718       (gnus-set-mode-line 'summary)
9719       ;; Return the number of marked articles.
9720       count)))
9721
9722 (defun gnus-summary-mark-as-processable (n &optional unmark)
9723   "Set the process mark on the next N articles.
9724 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9725 the process mark instead.  The difference between N and the actual
9726 number of articles marked is returned."
9727   (interactive "P")
9728   (if (and (null n) (gnus-region-active-p))
9729       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9730     (setq n (prefix-numeric-value n))
9731     (let ((backward (< n 0))
9732           (n (abs n)))
9733       (while (and
9734               (> n 0)
9735               (if unmark
9736                   (gnus-summary-remove-process-mark
9737                    (gnus-summary-article-number))
9738                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9739               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9740         (setq n (1- n)))
9741       (when (/= 0 n)
9742         (gnus-message 7 "No more articles"))
9743       (gnus-summary-recenter)
9744       (gnus-summary-position-point)
9745       n)))
9746
9747 (defun gnus-summary-unmark-as-processable (n)
9748   "Remove the process mark from the next N articles.
9749 If N is negative, unmark backward instead.  The difference between N and
9750 the actual number of articles unmarked is returned."
9751   (interactive "P")
9752   (gnus-summary-mark-as-processable n t))
9753
9754 (defun gnus-summary-unmark-all-processable ()
9755   "Remove the process mark from all articles."
9756   (interactive)
9757   (save-excursion
9758     (while gnus-newsgroup-processable
9759       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9760   (gnus-summary-position-point))
9761
9762 (defun gnus-summary-add-mark (article type)
9763   "Mark ARTICLE with a mark of TYPE."
9764   (let ((vtype (car (assq type gnus-article-mark-lists)))
9765         var)
9766     (if (not vtype)
9767         (error "No such mark type: %s" type)
9768       (setq var (intern (format "gnus-newsgroup-%s" type)))
9769       (set var (cons article (symbol-value var)))
9770       (if (memq type '(processable cached replied forwarded recent saved))
9771           (gnus-summary-update-secondary-mark article)
9772         ;;; !!! This is bogus.  We should find out what primary
9773         ;;; !!! mark we want to set.
9774         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9775
9776 (defun gnus-summary-mark-as-expirable (n)
9777   "Mark N articles forward as expirable.
9778 If N is negative, mark backward instead.  The difference between N and
9779 the actual number of articles marked is returned."
9780   (interactive "p")
9781   (gnus-summary-mark-forward n gnus-expirable-mark))
9782
9783 (defun gnus-summary-mark-as-spam (n)
9784   "Mark N articles forward as spam.
9785 If N is negative, mark backward instead.  The difference between N and
9786 the actual number of articles marked is returned."
9787   (interactive "p")
9788   (gnus-summary-mark-forward n gnus-spam-mark))
9789
9790 (defun gnus-summary-mark-article-as-replied (article)
9791   "Mark ARTICLE as replied to and update the summary line.
9792 ARTICLE can also be a list of articles."
9793   (interactive (list (gnus-summary-article-number)))
9794   (let ((articles (if (listp article) article (list article))))
9795     (dolist (article articles)
9796       (unless (numberp article)
9797         (error "%s is not a number" article))
9798       (push article gnus-newsgroup-replied)
9799       (let ((buffer-read-only nil))
9800         (when (gnus-summary-goto-subject article nil t)
9801           (gnus-summary-update-secondary-mark article))))))
9802
9803 (defun gnus-summary-mark-article-as-forwarded (article)
9804   "Mark ARTICLE as forwarded and update the summary line.
9805 ARTICLE can also be a list of articles."
9806   (let ((articles (if (listp article) article (list article))))
9807     (dolist (article articles)
9808       (push article gnus-newsgroup-forwarded)
9809       (let ((buffer-read-only nil))
9810         (when (gnus-summary-goto-subject article nil t)
9811           (gnus-summary-update-secondary-mark article))))))
9812
9813 (defun gnus-summary-set-bookmark (article)
9814   "Set a bookmark in current article."
9815   (interactive (list (gnus-summary-article-number)))
9816   (when (or (not (get-buffer gnus-article-buffer))
9817             (not gnus-current-article)
9818             (not gnus-article-current)
9819             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9820     (error "No current article selected"))
9821   ;; Remove old bookmark, if one exists.
9822   (gnus-pull article gnus-newsgroup-bookmarks)
9823   ;; Set the new bookmark, which is on the form
9824   ;; (article-number . line-number-in-body).
9825   (push
9826    (cons article
9827          (save-excursion
9828            (set-buffer gnus-article-buffer)
9829            (count-lines
9830             (min (point)
9831                  (save-excursion
9832                    (article-goto-body)
9833                    (point)))
9834             (point))))
9835    gnus-newsgroup-bookmarks)
9836   (gnus-message 6 "A bookmark has been added to the current article."))
9837
9838 (defun gnus-summary-remove-bookmark (article)
9839   "Remove the bookmark from the current article."
9840   (interactive (list (gnus-summary-article-number)))
9841   ;; Remove old bookmark, if one exists.
9842   (if (not (assq article gnus-newsgroup-bookmarks))
9843       (gnus-message 6 "No bookmark in current article.")
9844     (gnus-pull article gnus-newsgroup-bookmarks)
9845     (gnus-message 6 "Removed bookmark.")))
9846
9847 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9848 (defun gnus-summary-mark-as-dormant (n)
9849   "Mark N articles forward as dormant.
9850 If N is negative, mark backward instead.  The difference between N and
9851 the actual number of articles marked is returned."
9852   (interactive "p")
9853   (gnus-summary-mark-forward n gnus-dormant-mark))
9854
9855 (defun gnus-summary-set-process-mark (article)
9856   "Set the process mark on ARTICLE and update the summary line."
9857   (setq gnus-newsgroup-processable
9858         (cons article
9859               (delq article gnus-newsgroup-processable)))
9860   (when (gnus-summary-goto-subject article)
9861     (gnus-summary-show-thread)
9862     (gnus-summary-goto-subject article)
9863     (gnus-summary-update-secondary-mark article)))
9864
9865 (defun gnus-summary-remove-process-mark (article)
9866   "Remove the process mark from ARTICLE and update the summary line."
9867   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9868   (when (gnus-summary-goto-subject article)
9869     (gnus-summary-show-thread)
9870     (gnus-summary-goto-subject article)
9871     (gnus-summary-update-secondary-mark article)))
9872
9873 (defun gnus-summary-set-saved-mark (article)
9874   "Set the process mark on ARTICLE and update the summary line."
9875   (push article gnus-newsgroup-saved)
9876   (when (gnus-summary-goto-subject article)
9877     (gnus-summary-update-secondary-mark article)))
9878
9879 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9880   "Mark N articles as read forwards.
9881 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9882 The difference between N and the actual number of articles marked is
9883 returned.
9884 If NO-EXPIRE, auto-expiry will be inhibited."
9885   (interactive "p")
9886   (gnus-summary-show-thread)
9887   (let ((backward (< n 0))
9888         (gnus-summary-goto-unread
9889          (and gnus-summary-goto-unread
9890               (not (eq gnus-summary-goto-unread 'never))
9891               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9892                                     gnus-ticked-mark gnus-dormant-mark)))))
9893         (n (abs n))
9894         (mark (or mark gnus-del-mark)))
9895     (while (and (> n 0)
9896                 (gnus-summary-mark-article nil mark no-expire)
9897                 (zerop (gnus-summary-next-subject
9898                         (if backward -1 1)
9899                         (and gnus-summary-goto-unread
9900                              (not (eq gnus-summary-goto-unread 'never)))
9901                         t)))
9902       (setq n (1- n)))
9903     (when (/= 0 n)
9904       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9905     (gnus-summary-recenter)
9906     (gnus-summary-position-point)
9907     (gnus-set-mode-line 'summary)
9908     n))
9909
9910 (defun gnus-summary-mark-article-as-read (mark)
9911   "Mark the current article quickly as read with MARK."
9912   (let ((article (gnus-summary-article-number)))
9913     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9914     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9915     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9916     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9917     (push (cons article mark) gnus-newsgroup-reads)
9918     ;; Possibly remove from cache, if that is used.
9919     (when gnus-use-cache
9920       (gnus-cache-enter-remove-article article))
9921     ;; Allow the backend to change the mark.
9922     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9923     ;; Check for auto-expiry.
9924     (when (and gnus-newsgroup-auto-expire
9925                (memq mark gnus-auto-expirable-marks))
9926       (setq mark gnus-expirable-mark)
9927       ;; Let the backend know about the mark change.
9928       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9929       (push article gnus-newsgroup-expirable))
9930     ;; Set the mark in the buffer.
9931     (gnus-summary-update-mark mark 'unread)
9932     t))
9933
9934 (defun gnus-summary-mark-article-as-unread (mark)
9935   "Mark the current article quickly as unread with MARK."
9936   (let* ((article (gnus-summary-article-number))
9937          (old-mark (gnus-summary-article-mark article)))
9938     ;; Allow the backend to change the mark.
9939     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9940     (if (eq mark old-mark)
9941         t
9942       (if (<= article 0)
9943           (progn
9944             (gnus-error 1 "Can't mark negative article numbers")
9945             nil)
9946         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9947         (setq gnus-newsgroup-spam-marked
9948               (delq article gnus-newsgroup-spam-marked))
9949         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9950         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9951         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9952         (cond ((= mark gnus-ticked-mark)
9953                (setq gnus-newsgroup-marked
9954                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9955                                               article)))
9956               ((= mark gnus-spam-mark)
9957                (setq gnus-newsgroup-spam-marked
9958                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9959                                               article)))
9960               ((= mark gnus-dormant-mark)
9961                (setq gnus-newsgroup-dormant
9962                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9963                                               article)))
9964               (t
9965                (setq gnus-newsgroup-unreads
9966                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9967                                               article))))
9968         (gnus-pull article gnus-newsgroup-reads)
9969
9970         ;; See whether the article is to be put in the cache.
9971         (and gnus-use-cache
9972              (vectorp (gnus-summary-article-header article))
9973              (save-excursion
9974                (gnus-cache-possibly-enter-article
9975                 gnus-newsgroup-name article
9976                 (gnus-summary-article-header article)
9977                 (= mark gnus-ticked-mark)
9978                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9979
9980         ;; Fix the mark.
9981         (gnus-summary-update-mark mark 'unread)
9982         t))))
9983
9984 (defun gnus-summary-mark-article (&optional article mark no-expire)
9985   "Mark ARTICLE with MARK.  MARK can be any character.
9986 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9987 `??' (dormant) and `?E' (expirable).
9988 If MARK is nil, then the default character `?r' is used.
9989 If ARTICLE is nil, then the article on the current line will be
9990 marked.
9991 If NO-EXPIRE, auto-expiry will be inhibited."
9992   ;; The mark might be a string.
9993   (when (stringp mark)
9994     (setq mark (aref mark 0)))
9995   ;; If no mark is given, then we check auto-expiring.
9996   (when (null mark)
9997     (setq mark gnus-del-mark))
9998   (when (and (not no-expire)
9999              gnus-newsgroup-auto-expire
10000              (memq mark gnus-auto-expirable-marks))
10001     (setq mark gnus-expirable-mark))
10002   (let ((article (or article (gnus-summary-article-number)))
10003         (old-mark (gnus-summary-article-mark article)))
10004     ;; Allow the backend to change the mark.
10005     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10006     (if (eq mark old-mark)
10007         t
10008       (unless article
10009         (error "No article on current line"))
10010       (if (not (if (or (= mark gnus-unread-mark)
10011                        (= mark gnus-ticked-mark)
10012                        (= mark gnus-spam-mark)
10013                        (= mark gnus-dormant-mark))
10014                    (gnus-mark-article-as-unread article mark)
10015                  (gnus-mark-article-as-read article mark)))
10016           t
10017         ;; See whether the article is to be put in the cache.
10018         (and gnus-use-cache
10019              (not (= mark gnus-canceled-mark))
10020              (vectorp (gnus-summary-article-header article))
10021              (save-excursion
10022                (gnus-cache-possibly-enter-article
10023                 gnus-newsgroup-name article
10024                 (gnus-summary-article-header article)
10025                 (= mark gnus-ticked-mark)
10026                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10027
10028         (when (gnus-summary-goto-subject article nil t)
10029           (let ((buffer-read-only nil))
10030             (gnus-summary-show-thread)
10031             ;; Fix the mark.
10032             (gnus-summary-update-mark mark 'unread)
10033             t))))))
10034
10035 (defun gnus-summary-update-secondary-mark (article)
10036   "Update the secondary (read, process, cache) mark."
10037   (gnus-summary-update-mark
10038    (cond ((memq article gnus-newsgroup-processable)
10039           gnus-process-mark)
10040          ((memq article gnus-newsgroup-cached)
10041           gnus-cached-mark)
10042          ((memq article gnus-newsgroup-replied)
10043           gnus-replied-mark)
10044          ((memq article gnus-newsgroup-forwarded)
10045           gnus-forwarded-mark)
10046          ((memq article gnus-newsgroup-saved)
10047           gnus-saved-mark)
10048          ((memq article gnus-newsgroup-recent)
10049           gnus-recent-mark)
10050          ((memq article gnus-newsgroup-unseen)
10051           gnus-unseen-mark)
10052          (t gnus-no-mark))
10053    'replied)
10054   (when (gnus-visual-p 'summary-highlight 'highlight)
10055     (gnus-run-hooks 'gnus-summary-update-hook))
10056   t)
10057
10058 (defun gnus-summary-update-download-mark (article)
10059   "Update the download mark."
10060   (gnus-summary-update-mark
10061    (cond ((memq article gnus-newsgroup-undownloaded)
10062           gnus-undownloaded-mark)
10063          (gnus-newsgroup-agentized
10064           gnus-downloaded-mark)
10065          (t
10066           gnus-no-mark))
10067    'download)
10068   (gnus-summary-update-line t)
10069   t)
10070
10071 (defun gnus-summary-update-mark (mark type)
10072   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10073         (buffer-read-only nil))
10074     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10075     (when forward
10076       (when (looking-at "\r")
10077         (incf forward))
10078       (when (<= (+ forward (point)) (point-max))
10079         ;; Go to the right position on the line.
10080         (goto-char (+ forward (point)))
10081         ;; Replace the old mark with the new mark.
10082         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10083         ;; Optionally update the marks by some user rule.
10084         (when (eq type 'unread)
10085           (gnus-data-set-mark
10086            (gnus-data-find (gnus-summary-article-number)) mark)
10087           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10088
10089 (defun gnus-mark-article-as-read (article &optional mark)
10090   "Enter ARTICLE in the pertinent lists and remove it from others."
10091   ;; Make the article expirable.
10092   (let ((mark (or mark gnus-del-mark)))
10093     (setq gnus-newsgroup-expirable
10094           (if (= mark gnus-expirable-mark)
10095               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10096             (delq article gnus-newsgroup-expirable)))
10097     ;; Remove from unread and marked lists.
10098     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10099     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10100     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10101     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10102     (push (cons article mark) gnus-newsgroup-reads)
10103     ;; Possibly remove from cache, if that is used.
10104     (when gnus-use-cache
10105       (gnus-cache-enter-remove-article article))
10106     t))
10107
10108 (defun gnus-mark-article-as-unread (article &optional mark)
10109   "Enter ARTICLE in the pertinent lists and remove it from others."
10110   (let ((mark (or mark gnus-ticked-mark)))
10111     (if (<= article 0)
10112         (progn
10113           (gnus-error 1 "Can't mark negative article numbers")
10114           nil)
10115       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10116             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10117             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10118             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10119             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10120
10121       ;; Unsuppress duplicates?
10122       (when gnus-suppress-duplicates
10123         (gnus-dup-unsuppress-article article))
10124
10125       (cond ((= mark gnus-ticked-mark)
10126              (setq gnus-newsgroup-marked
10127                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10128             ((= mark gnus-spam-mark)
10129              (setq gnus-newsgroup-spam-marked
10130                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10131                                             article)))
10132             ((= mark gnus-dormant-mark)
10133              (setq gnus-newsgroup-dormant
10134                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10135             (t
10136              (setq gnus-newsgroup-unreads
10137                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10138       (gnus-pull article gnus-newsgroup-reads)
10139       t)))
10140
10141 (defalias 'gnus-summary-mark-as-unread-forward
10142   'gnus-summary-tick-article-forward)
10143 (make-obsolete 'gnus-summary-mark-as-unread-forward
10144                'gnus-summary-tick-article-forward)
10145 (defun gnus-summary-tick-article-forward (n)
10146   "Tick N articles forwards.
10147 If N is negative, tick backwards instead.
10148 The difference between N and the number of articles ticked is returned."
10149   (interactive "p")
10150   (gnus-summary-mark-forward n gnus-ticked-mark))
10151
10152 (defalias 'gnus-summary-mark-as-unread-backward
10153   'gnus-summary-tick-article-backward)
10154 (make-obsolete 'gnus-summary-mark-as-unread-backward
10155                'gnus-summary-tick-article-backward)
10156 (defun gnus-summary-tick-article-backward (n)
10157   "Tick N articles backwards.
10158 The difference between N and the number of articles ticked is returned."
10159   (interactive "p")
10160   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10161
10162 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10163 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10164 (defun gnus-summary-tick-article (&optional article clear-mark)
10165   "Mark current article as unread.
10166 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10167 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10168   (interactive)
10169   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10170                                        gnus-ticked-mark)))
10171
10172 (defun gnus-summary-mark-as-read-forward (n)
10173   "Mark N articles as read forwards.
10174 If N is negative, mark backwards instead.
10175 The difference between N and the actual number of articles marked is
10176 returned."
10177   (interactive "p")
10178   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10179
10180 (defun gnus-summary-mark-as-read-backward (n)
10181   "Mark the N articles as read backwards.
10182 The difference between N and the actual number of articles marked is
10183 returned."
10184   (interactive "p")
10185   (gnus-summary-mark-forward
10186    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10187
10188 (defun gnus-summary-mark-as-read (&optional article mark)
10189   "Mark current article as read.
10190 ARTICLE specifies the article to be marked as read.
10191 MARK specifies a string to be inserted at the beginning of the line."
10192   (gnus-summary-mark-article article mark))
10193
10194 (defun gnus-summary-clear-mark-forward (n)
10195   "Clear marks from N articles forward.
10196 If N is negative, clear backward instead.
10197 The difference between N and the number of marks cleared is returned."
10198   (interactive "p")
10199   (gnus-summary-mark-forward n gnus-unread-mark))
10200
10201 (defun gnus-summary-clear-mark-backward (n)
10202   "Clear marks from N articles backward.
10203 The difference between N and the number of marks cleared is returned."
10204   (interactive "p")
10205   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10206
10207 (defun gnus-summary-mark-unread-as-read ()
10208   "Intended to be used by `gnus-summary-mark-article-hook'."
10209   (when (memq gnus-current-article gnus-newsgroup-unreads)
10210     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10211
10212 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10213   "Intended to be used by `gnus-summary-mark-article-hook'."
10214   (let ((mark (gnus-summary-article-mark)))
10215     (when (or (gnus-unread-mark-p mark)
10216               (gnus-read-mark-p mark))
10217       (gnus-summary-mark-article gnus-current-article
10218                                  (or new-mark gnus-read-mark)))))
10219
10220 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10221   "Intended to be used by `gnus-summary-mark-article-hook'."
10222   (let ((mark (gnus-summary-article-mark)))
10223     (when (or (gnus-unread-mark-p mark)
10224               (gnus-read-mark-p mark))
10225       (gnus-summary-mark-article (gnus-summary-article-number)
10226                                  (or new-mark gnus-read-mark)))))
10227
10228 (defun gnus-summary-mark-unread-as-ticked ()
10229   "Intended to be used by `gnus-summary-mark-article-hook'."
10230   (when (memq gnus-current-article gnus-newsgroup-unreads)
10231     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10232
10233 (defun gnus-summary-mark-region-as-read (point mark all)
10234   "Mark all unread articles between point and mark as read.
10235 If given a prefix, mark all articles between point and mark as read,
10236 even ticked and dormant ones."
10237   (interactive "r\nP")
10238   (save-excursion
10239     (let (article)
10240       (goto-char point)
10241       (beginning-of-line)
10242       (while (and
10243               (< (point) mark)
10244               (progn
10245                 (when (or all
10246                           (memq (setq article (gnus-summary-article-number))
10247                                 gnus-newsgroup-unreads))
10248                   (gnus-summary-mark-article article gnus-del-mark))
10249                 t)
10250               (gnus-summary-find-next))))))
10251
10252 (defun gnus-summary-mark-below (score mark)
10253   "Mark articles with score less than SCORE with MARK."
10254   (interactive "P\ncMark: ")
10255   (setq score (if score
10256                   (prefix-numeric-value score)
10257                 (or gnus-summary-default-score 0)))
10258   (save-excursion
10259     (set-buffer gnus-summary-buffer)
10260     (goto-char (point-min))
10261     (while
10262         (progn
10263           (and (< (gnus-summary-article-score) score)
10264                (gnus-summary-mark-article nil mark))
10265           (gnus-summary-find-next)))))
10266
10267 (defun gnus-summary-kill-below (&optional score)
10268   "Mark articles with score below SCORE as read."
10269   (interactive "P")
10270   (gnus-summary-mark-below score gnus-killed-mark))
10271
10272 (defun gnus-summary-clear-above (&optional score)
10273   "Clear all marks from articles with score above SCORE."
10274   (interactive "P")
10275   (gnus-summary-mark-above score gnus-unread-mark))
10276
10277 (defun gnus-summary-tick-above (&optional score)
10278   "Tick all articles with score above SCORE."
10279   (interactive "P")
10280   (gnus-summary-mark-above score gnus-ticked-mark))
10281
10282 (defun gnus-summary-mark-above (score mark)
10283   "Mark articles with score over SCORE with MARK."
10284   (interactive "P\ncMark: ")
10285   (setq score (if score
10286                   (prefix-numeric-value score)
10287                 (or gnus-summary-default-score 0)))
10288   (save-excursion
10289     (set-buffer gnus-summary-buffer)
10290     (goto-char (point-min))
10291     (while (and (progn
10292                   (when (> (gnus-summary-article-score) score)
10293                     (gnus-summary-mark-article nil mark))
10294                   t)
10295                 (gnus-summary-find-next)))))
10296
10297 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10298 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10299 (defun gnus-summary-limit-include-expunged (&optional no-error)
10300   "Display all the hidden articles that were expunged for low scores."
10301   (interactive)
10302   (let ((buffer-read-only nil))
10303     (let ((scored gnus-newsgroup-scored)
10304           headers h)
10305       (while scored
10306         (unless (gnus-summary-article-header (caar scored))
10307           (and (setq h (gnus-number-to-header (caar scored)))
10308                (< (cdar scored) gnus-summary-expunge-below)
10309                (push h headers)))
10310         (setq scored (cdr scored)))
10311       (if (not headers)
10312           (when (not no-error)
10313             (error "No expunged articles hidden"))
10314         (goto-char (point-min))
10315         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10316         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10317         (mapcar (lambda (x) (push (mail-header-number x)
10318                                   gnus-newsgroup-limit))
10319                 headers)
10320         (gnus-summary-prepare-unthreaded (nreverse headers))
10321         (goto-char (point-min))
10322         (gnus-summary-position-point)
10323         t))))
10324
10325 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10326   "Mark all unread articles in this newsgroup as read.
10327 If prefix argument ALL is non-nil, ticked and dormant articles will
10328 also be marked as read.
10329 If QUIETLY is non-nil, no questions will be asked.
10330
10331 If TO-HERE is non-nil, it should be a point in the buffer.  All
10332 articles before (after, if REVERSE is set) this point will be marked
10333 as read.
10334
10335 Note that this function will only catch up the unread article
10336 in the current summary buffer limitation.
10337
10338 The number of articles marked as read is returned."
10339   (interactive "P")
10340   (prog1
10341       (save-excursion
10342         (when (or quietly
10343                   (not gnus-interactive-catchup) ;Without confirmation?
10344                   gnus-expert-user
10345                   (gnus-y-or-n-p
10346                    (if all
10347                        "Mark absolutely all articles as read? "
10348                      "Mark all unread articles as read? ")))
10349           (if (and not-mark
10350                    (not gnus-newsgroup-adaptive)
10351                    (not gnus-newsgroup-auto-expire)
10352                    (not gnus-suppress-duplicates)
10353                    (or (not gnus-use-cache)
10354                        (eq gnus-use-cache 'passive)))
10355               (progn
10356                 (when all
10357                   (setq gnus-newsgroup-marked nil
10358                         gnus-newsgroup-spam-marked nil
10359                         gnus-newsgroup-dormant nil))
10360                 (setq gnus-newsgroup-unreads
10361                       (gnus-sorted-nunion
10362                        (gnus-intersection gnus-newsgroup-unreads
10363                                           gnus-newsgroup-downloadable)
10364                        gnus-newsgroup-unfetched)))
10365             ;; We actually mark all articles as canceled, which we
10366             ;; have to do when using auto-expiry or adaptive scoring.
10367             (gnus-summary-show-all-threads)
10368             (if (and to-here reverse)
10369                 (progn
10370                   (goto-char to-here)
10371                   (gnus-summary-mark-current-read-and-unread-as-read
10372                    gnus-catchup-mark)
10373                   (while (gnus-summary-find-next (not all))
10374                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10375               (when (gnus-summary-first-subject (not all))
10376                 (while (and
10377                         (if to-here (< (point) to-here) t)
10378                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10379                         (gnus-summary-find-next (not all))))))
10380             (gnus-set-mode-line 'summary))
10381           t))
10382     (gnus-summary-position-point)))
10383
10384 (defun gnus-summary-catchup-to-here (&optional all)
10385   "Mark all unticked articles before the current one as read.
10386 If ALL is non-nil, also mark ticked and dormant articles as read."
10387   (interactive "P")
10388   (save-excursion
10389     (gnus-save-hidden-threads
10390       (let ((beg (point)))
10391         ;; We check that there are unread articles.
10392         (when (or all (gnus-summary-find-prev))
10393           (gnus-summary-catchup all t beg)))))
10394   (gnus-summary-position-point))
10395
10396 (defun gnus-summary-catchup-from-here (&optional all)
10397   "Mark all unticked articles after (and including) the current one as read.
10398 If ALL is non-nil, also mark ticked and dormant articles as read."
10399   (interactive "P")
10400   (save-excursion
10401     (gnus-save-hidden-threads
10402       (let ((beg (point)))
10403         ;; We check that there are unread articles.
10404         (when (or all (gnus-summary-find-next))
10405           (gnus-summary-catchup all t beg nil t)))))
10406   (gnus-summary-position-point))
10407
10408 (defun gnus-summary-catchup-all (&optional quietly)
10409   "Mark all articles in this newsgroup as read.
10410 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10411 instead, which marks only unread articles as read."
10412   (interactive "P")
10413   (gnus-summary-catchup t quietly))
10414
10415 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10416   "Mark all unread articles in this group as read, then exit.
10417 If prefix argument ALL is non-nil, all articles are marked as read.
10418 If QUIETLY is non-nil, no questions will be asked."
10419   (interactive "P")
10420   (when (gnus-summary-catchup all quietly nil 'fast)
10421     ;; Select next newsgroup or exit.
10422     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10423              (eq gnus-auto-select-next 'quietly))
10424         (gnus-summary-next-group nil)
10425       (gnus-summary-exit))))
10426
10427 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10428   "Mark all articles in this newsgroup as read, and then exit.
10429 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10430 instead, which marks only unread articles as read."
10431   (interactive "P")
10432   (gnus-summary-catchup-and-exit t quietly))
10433
10434 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10435   "Mark all articles in this group as read and select the next group.
10436 If given a prefix, mark all articles, unread as well as ticked, as
10437 read."
10438   (interactive "P")
10439   (save-excursion
10440     (gnus-summary-catchup all))
10441   (gnus-summary-next-group))
10442
10443 ;;;
10444 ;;; with article
10445 ;;;
10446
10447 (defmacro gnus-with-article (article &rest forms)
10448   "Select ARTICLE and perform FORMS in the original article buffer.
10449 Then replace the article with the result."
10450   `(progn
10451      ;; We don't want the article to be marked as read.
10452      (let (gnus-mark-article-hook)
10453        (gnus-summary-select-article t t nil ,article))
10454      (set-buffer gnus-original-article-buffer)
10455      ,@forms
10456      (if (not (gnus-check-backend-function
10457                'request-replace-article (car gnus-article-current)))
10458          (gnus-message 5 "Read-only group; not replacing")
10459        (unless (gnus-request-replace-article
10460                 ,article (car gnus-article-current)
10461                 (current-buffer) t)
10462          (error "Couldn't replace article")))
10463      ;; The cache and backlog have to be flushed somewhat.
10464      (when gnus-keep-backlog
10465        (gnus-backlog-remove-article
10466         (car gnus-article-current) (cdr gnus-article-current)))
10467      (when gnus-use-cache
10468        (gnus-cache-update-article
10469         (car gnus-article-current) (cdr gnus-article-current)))))
10470
10471 (put 'gnus-with-article 'lisp-indent-function 1)
10472 (put 'gnus-with-article 'edebug-form-spec '(form body))
10473
10474 ;; Thread-based commands.
10475
10476 (defun gnus-summary-articles-in-thread (&optional article)
10477   "Return a list of all articles in the current thread.
10478 If ARTICLE is non-nil, return all articles in the thread that starts
10479 with that article."
10480   (let* ((article (or article (gnus-summary-article-number)))
10481          (data (gnus-data-find-list article))
10482          (top-level (gnus-data-level (car data)))
10483          (top-subject
10484           (cond ((null gnus-thread-operation-ignore-subject)
10485                  (gnus-simplify-subject-re
10486                   (mail-header-subject (gnus-data-header (car data)))))
10487                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10488                  (gnus-simplify-subject-fuzzy
10489                   (mail-header-subject (gnus-data-header (car data)))))
10490                 (t nil)))
10491          (end-point (save-excursion
10492                       (if (gnus-summary-go-to-next-thread)
10493                           (point) (point-max))))
10494          articles)
10495     (while (and data
10496                 (< (gnus-data-pos (car data)) end-point))
10497       (when (or (not top-subject)
10498                 (string= top-subject
10499                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10500                              (gnus-simplify-subject-fuzzy
10501                               (mail-header-subject
10502                                (gnus-data-header (car data))))
10503                            (gnus-simplify-subject-re
10504                             (mail-header-subject
10505                              (gnus-data-header (car data)))))))
10506         (push (gnus-data-number (car data)) articles))
10507       (unless (and (setq data (cdr data))
10508                    (> (gnus-data-level (car data)) top-level))
10509         (setq data nil)))
10510     ;; Return the list of articles.
10511     (nreverse articles)))
10512
10513 (defun gnus-summary-rethread-current ()
10514   "Rethread the thread the current article is part of."
10515   (interactive)
10516   (let* ((gnus-show-threads t)
10517          (article (gnus-summary-article-number))
10518          (id (mail-header-id (gnus-summary-article-header)))
10519          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10520     (unless id
10521       (error "No article on the current line"))
10522     (gnus-rebuild-thread id)
10523     (gnus-summary-goto-subject article)))
10524
10525 (defun gnus-summary-reparent-thread ()
10526   "Make the current article child of the marked (or previous) article.
10527
10528 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10529 is non-nil or the Subject: of both articles are the same."
10530   (interactive)
10531   (unless (not (gnus-group-read-only-p))
10532     (error "The current newsgroup does not support article editing"))
10533   (unless (<= (length gnus-newsgroup-processable) 1)
10534     (error "No more than one article may be marked"))
10535   (save-window-excursion
10536     (let ((gnus-article-buffer " *reparent*")
10537           (current-article (gnus-summary-article-number))
10538           ;; First grab the marked article, otherwise one line up.
10539           (parent-article (if (not (null gnus-newsgroup-processable))
10540                               (car gnus-newsgroup-processable)
10541                             (save-excursion
10542                               (if (eq (forward-line -1) 0)
10543                                   (gnus-summary-article-number)
10544                                 (error "Beginning of summary buffer"))))))
10545       (unless (not (eq current-article parent-article))
10546         (error "An article may not be self-referential"))
10547       (let ((message-id (mail-header-id
10548                          (gnus-summary-article-header parent-article))))
10549         (unless (and message-id (not (equal message-id "")))
10550           (error "No message-id in desired parent"))
10551         (gnus-with-article current-article
10552           (save-restriction
10553             (goto-char (point-min))
10554             (message-narrow-to-head)
10555             (if (re-search-forward "^References: " nil t)
10556                 (progn
10557                   (re-search-forward "^[^ \t]" nil t)
10558                   (forward-line -1)
10559                   (end-of-line)
10560                   (insert " " message-id))
10561               (insert "References: " message-id "\n"))))
10562         (set-buffer gnus-summary-buffer)
10563         (gnus-summary-unmark-all-processable)
10564         (gnus-summary-update-article current-article)
10565         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10566             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10567         (gnus-summary-rethread-current)
10568         (gnus-message 3 "Article %d is now the child of article %d"
10569                       current-article parent-article)))))
10570
10571 (defun gnus-summary-toggle-threads (&optional arg)
10572   "Toggle showing conversation threads.
10573 If ARG is positive number, turn showing conversation threads on."
10574   (interactive "P")
10575   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10576     (setq gnus-show-threads
10577           (if (null arg) (not gnus-show-threads)
10578             (> (prefix-numeric-value arg) 0)))
10579     (gnus-summary-prepare)
10580     (gnus-summary-goto-subject current)
10581     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10582     (gnus-summary-position-point)))
10583
10584 (defun gnus-summary-show-all-threads ()
10585   "Show all threads."
10586   (interactive)
10587   (save-excursion
10588     (let ((buffer-read-only nil))
10589       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10590   (gnus-summary-position-point))
10591
10592 (defun gnus-summary-show-thread ()
10593   "Show thread subtrees.
10594 Returns nil if no thread was there to be shown."
10595   (interactive)
10596   (let ((buffer-read-only nil)
10597         (orig (point))
10598         (end (point-at-eol))
10599         ;; Leave point at bol
10600         (beg (progn (beginning-of-line) (point))))
10601     (prog1
10602         ;; Any hidden lines here?
10603         (search-forward "\r" end t)
10604       (subst-char-in-region beg end ?\^M ?\n t)
10605       (goto-char orig)
10606       (gnus-summary-position-point))))
10607
10608 (defun gnus-summary-maybe-hide-threads ()
10609   "If requested, hide the threads that should be hidden."
10610   (when (and gnus-show-threads
10611              gnus-thread-hide-subtree)
10612     (gnus-summary-hide-all-threads
10613      (if (or (consp gnus-thread-hide-subtree)
10614              (functionp gnus-thread-hide-subtree))
10615          (gnus-make-predicate gnus-thread-hide-subtree)
10616        nil))))
10617
10618 ;;; Hiding predicates.
10619
10620 (defun gnus-article-unread-p (header)
10621   (memq (mail-header-number header) gnus-newsgroup-unreads))
10622
10623 (defun gnus-article-unseen-p (header)
10624   (memq (mail-header-number header) gnus-newsgroup-unseen))
10625
10626 (defun gnus-map-articles (predicate articles)
10627   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10628   (apply 'gnus-or (mapcar predicate
10629                           (mapcar 'gnus-summary-article-header articles))))
10630
10631 (defun gnus-summary-hide-all-threads (&optional predicate)
10632   "Hide all thread subtrees.
10633 If PREDICATE is supplied, threads that satisfy this predicate
10634 will not be hidden."
10635   (interactive)
10636   (save-excursion
10637     (goto-char (point-min))
10638     (let ((end nil))
10639       (while (not end)
10640         (when (or (not predicate)
10641                   (gnus-map-articles
10642                    predicate (gnus-summary-article-children)))
10643             (gnus-summary-hide-thread))
10644         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10645   (gnus-summary-position-point))
10646
10647 (defun gnus-summary-hide-thread ()
10648   "Hide thread subtrees.
10649 If PREDICATE is supplied, threads that satisfy this predicate
10650 will not be hidden.
10651 Returns nil if no threads were there to be hidden."
10652   (interactive)
10653   (let ((buffer-read-only nil)
10654         (start (point))
10655         (article (gnus-summary-article-number)))
10656     (goto-char start)
10657     ;; Go forward until either the buffer ends or the subthread
10658     ;; ends.
10659     (when (and (not (eobp))
10660                (or (zerop (gnus-summary-next-thread 1 t))
10661                    (goto-char (point-max))))
10662       (prog1
10663           (if (and (> (point) start)
10664                    (search-backward "\n" start t))
10665               (progn
10666                 (subst-char-in-region start (point) ?\n ?\^M)
10667                 (gnus-summary-goto-subject article))
10668             (goto-char start)
10669             nil)))))
10670
10671 (defun gnus-summary-go-to-next-thread (&optional previous)
10672   "Go to the same level (or less) next thread.
10673 If PREVIOUS is non-nil, go to previous thread instead.
10674 Return the article number moved to, or nil if moving was impossible."
10675   (let ((level (gnus-summary-thread-level))
10676         (way (if previous -1 1))
10677         (beg (point)))
10678     (forward-line way)
10679     (while (and (not (eobp))
10680                 (< level (gnus-summary-thread-level)))
10681       (forward-line way))
10682     (if (eobp)
10683         (progn
10684           (goto-char beg)
10685           nil)
10686       (setq beg (point))
10687       (prog1
10688           (gnus-summary-article-number)
10689         (goto-char beg)))))
10690
10691 (defun gnus-summary-next-thread (n &optional silent)
10692   "Go to the same level next N'th thread.
10693 If N is negative, search backward instead.
10694 Returns the difference between N and the number of skips actually
10695 done.
10696
10697 If SILENT, don't output messages."
10698   (interactive "p")
10699   (let ((backward (< n 0))
10700         (n (abs n)))
10701     (while (and (> n 0)
10702                 (gnus-summary-go-to-next-thread backward))
10703       (decf n))
10704     (unless silent
10705       (gnus-summary-position-point))
10706     (when (and (not silent) (/= 0 n))
10707       (gnus-message 7 "No more threads"))
10708     n))
10709
10710 (defun gnus-summary-prev-thread (n)
10711   "Go to the same level previous N'th thread.
10712 Returns the difference between N and the number of skips actually
10713 done."
10714   (interactive "p")
10715   (gnus-summary-next-thread (- n)))
10716
10717 (defun gnus-summary-go-down-thread ()
10718   "Go down one level in the current thread."
10719   (let ((children (gnus-summary-article-children)))
10720     (when children
10721       (gnus-summary-goto-subject (car children)))))
10722
10723 (defun gnus-summary-go-up-thread ()
10724   "Go up one level in the current thread."
10725   (let ((parent (gnus-summary-article-parent)))
10726     (when parent
10727       (gnus-summary-goto-subject parent))))
10728
10729 (defun gnus-summary-down-thread (n)
10730   "Go down thread N steps.
10731 If N is negative, go up instead.
10732 Returns the difference between N and how many steps down that were
10733 taken."
10734   (interactive "p")
10735   (let ((up (< n 0))
10736         (n (abs n)))
10737     (while (and (> n 0)
10738                 (if up (gnus-summary-go-up-thread)
10739                   (gnus-summary-go-down-thread)))
10740       (setq n (1- n)))
10741     (gnus-summary-position-point)
10742     (when (/= 0 n)
10743       (gnus-message 7 "Can't go further"))
10744     n))
10745
10746 (defun gnus-summary-up-thread (n)
10747   "Go up thread N steps.
10748 If N is negative, go down instead.
10749 Returns the difference between N and how many steps down that were
10750 taken."
10751   (interactive "p")
10752   (gnus-summary-down-thread (- n)))
10753
10754 (defun gnus-summary-top-thread ()
10755   "Go to the top of the thread."
10756   (interactive)
10757   (while (gnus-summary-go-up-thread))
10758   (gnus-summary-article-number))
10759
10760 (defun gnus-summary-kill-thread (&optional unmark)
10761   "Mark articles under current thread as read.
10762 If the prefix argument is positive, remove any kinds of marks.
10763 If the prefix argument is negative, tick articles instead."
10764   (interactive "P")
10765   (when unmark
10766     (setq unmark (prefix-numeric-value unmark)))
10767   (let ((articles (gnus-summary-articles-in-thread)))
10768     (save-excursion
10769       ;; Expand the thread.
10770       (gnus-summary-show-thread)
10771       ;; Mark all the articles.
10772       (while articles
10773         (gnus-summary-goto-subject (car articles))
10774         (cond ((null unmark)
10775                (gnus-summary-mark-article-as-read gnus-killed-mark))
10776               ((> unmark 0)
10777                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10778               (t
10779                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10780         (setq articles (cdr articles))))
10781     ;; Hide killed subtrees.
10782     (and (null unmark)
10783          gnus-thread-hide-killed
10784          (gnus-summary-hide-thread))
10785     ;; If marked as read, go to next unread subject.
10786     (when (null unmark)
10787       ;; Go to next unread subject.
10788       (gnus-summary-next-subject 1 t)))
10789   (gnus-set-mode-line 'summary))
10790
10791 ;; Summary sorting commands
10792
10793 (defun gnus-summary-sort-by-number (&optional reverse)
10794   "Sort the summary buffer by article number.
10795 Argument REVERSE means reverse order."
10796   (interactive "P")
10797   (gnus-summary-sort 'number reverse))
10798
10799 (defun gnus-summary-sort-by-random (&optional reverse)
10800   "Randomize the order in the summary buffer.
10801 Argument REVERSE means to randomize in reverse order."
10802   (interactive "P")
10803   (gnus-summary-sort 'random reverse))
10804
10805 (defun gnus-summary-sort-by-author (&optional reverse)
10806   "Sort the summary buffer by author name alphabetically.
10807 If `case-fold-search' is non-nil, case of letters is ignored.
10808 Argument REVERSE means reverse order."
10809   (interactive "P")
10810   (gnus-summary-sort 'author reverse))
10811
10812 (defun gnus-summary-sort-by-subject (&optional reverse)
10813   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10814 If `case-fold-search' is non-nil, case of letters is ignored.
10815 Argument REVERSE means reverse order."
10816   (interactive "P")
10817   (gnus-summary-sort 'subject reverse))
10818
10819 (defun gnus-summary-sort-by-date (&optional reverse)
10820   "Sort the summary buffer by date.
10821 Argument REVERSE means reverse order."
10822   (interactive "P")
10823   (gnus-summary-sort 'date reverse))
10824
10825 (defun gnus-summary-sort-by-score (&optional reverse)
10826   "Sort the summary buffer by score.
10827 Argument REVERSE means reverse order."
10828   (interactive "P")
10829   (gnus-summary-sort 'score reverse))
10830
10831 (defun gnus-summary-sort-by-lines (&optional reverse)
10832   "Sort the summary buffer by the number of lines.
10833 Argument REVERSE means reverse order."
10834   (interactive "P")
10835   (gnus-summary-sort 'lines reverse))
10836
10837 (defun gnus-summary-sort-by-chars (&optional reverse)
10838   "Sort the summary buffer by article length.
10839 Argument REVERSE means reverse order."
10840   (interactive "P")
10841   (gnus-summary-sort 'chars reverse))
10842
10843 (defun gnus-summary-sort-by-original (&optional reverse)
10844   "Sort the summary buffer using the default sorting method.
10845 Argument REVERSE means reverse order."
10846   (interactive "P")
10847   (let* ((buffer-read-only)
10848          (gnus-summary-prepare-hook nil))
10849     ;; We do the sorting by regenerating the threads.
10850     (gnus-summary-prepare)
10851     ;; Hide subthreads if needed.
10852     (gnus-summary-maybe-hide-threads)))
10853
10854 (defun gnus-summary-sort (predicate reverse)
10855   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10856   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10857          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10858          (gnus-thread-sort-functions
10859           (if (not reverse)
10860               thread
10861             `(lambda (t1 t2)
10862                (,thread t2 t1))))
10863          (gnus-sort-gathered-threads-function
10864           gnus-thread-sort-functions)
10865          (gnus-article-sort-functions
10866           (if (not reverse)
10867               article
10868             `(lambda (t1 t2)
10869                (,article t2 t1))))
10870          (buffer-read-only)
10871          (gnus-summary-prepare-hook nil))
10872     ;; We do the sorting by regenerating the threads.
10873     (gnus-summary-prepare)
10874     ;; Hide subthreads if needed.
10875     (gnus-summary-maybe-hide-threads)))
10876
10877 ;; Summary saving commands.
10878
10879 (defun gnus-summary-save-article (&optional n not-saved)
10880   "Save the current article using the default saver function.
10881 If N is a positive number, save the N next articles.
10882 If N is a negative number, save the N previous articles.
10883 If N is nil and any articles have been marked with the process mark,
10884 save those articles instead.
10885 The variable `gnus-default-article-saver' specifies the saver function."
10886   (interactive "P")
10887   (let* ((articles (gnus-summary-work-articles n))
10888          (save-buffer (save-excursion
10889                         (nnheader-set-temp-buffer " *Gnus Save*")))
10890          (num (length articles))
10891          header file)
10892     (dolist (article articles)
10893       (setq header (gnus-summary-article-header article))
10894       (if (not (vectorp header))
10895           ;; This is a pseudo-article.
10896           (if (assq 'name header)
10897               (gnus-copy-file (cdr (assq 'name header)))
10898             (gnus-message 1 "Article %d is unsaveable" article))
10899         ;; This is a real article.
10900         (save-window-excursion
10901           (let ((gnus-display-mime-function nil)
10902                 (gnus-article-prepare-hook nil))
10903             (gnus-summary-select-article t nil nil article)))
10904         (save-excursion
10905           (set-buffer save-buffer)
10906           (erase-buffer)
10907           (insert-buffer-substring gnus-original-article-buffer))
10908         (setq file (gnus-article-save save-buffer file num))
10909         (gnus-summary-remove-process-mark article)
10910         (unless not-saved
10911           (gnus-summary-set-saved-mark article))))
10912     (gnus-kill-buffer save-buffer)
10913     (gnus-summary-position-point)
10914     (gnus-set-mode-line 'summary)
10915     n))
10916
10917 (defun gnus-summary-pipe-output (&optional arg headers)
10918   "Pipe the current article to a subprocess.
10919 If N is a positive number, pipe the N next articles.
10920 If N is a negative number, pipe the N previous articles.
10921 If N is nil and any articles have been marked with the process mark,
10922 pipe those articles instead.
10923 If HEADERS (the symbolic prefix), include the headers, too."
10924   (interactive (gnus-interactive "P\ny"))
10925   (require 'gnus-art)
10926   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10927         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10928     (gnus-summary-save-article arg t))
10929   (let ((buffer (get-buffer "*Shell Command Output*")))
10930     (when (and buffer
10931                (not (zerop (buffer-size buffer))))
10932       (gnus-configure-windows 'pipe))))
10933
10934 (defun gnus-summary-save-article-mail (&optional arg)
10935   "Append the current article to an mail file.
10936 If N is a positive number, save the N next articles.
10937 If N is a negative number, save the N previous articles.
10938 If N is nil and any articles have been marked with the process mark,
10939 save those articles instead."
10940   (interactive "P")
10941   (require 'gnus-art)
10942   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10943     (gnus-summary-save-article arg)))
10944
10945 (defun gnus-summary-save-article-rmail (&optional arg)
10946   "Append the current article to an rmail file.
10947 If N is a positive number, save the N next articles.
10948 If N is a negative number, save the N previous articles.
10949 If N is nil and any articles have been marked with the process mark,
10950 save those articles instead."
10951   (interactive "P")
10952   (require 'gnus-art)
10953   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10954     (gnus-summary-save-article arg)))
10955
10956 (defun gnus-summary-save-article-file (&optional arg)
10957   "Append the current article to a file.
10958 If N is a positive number, save the N next articles.
10959 If N is a negative number, save the N previous articles.
10960 If N is nil and any articles have been marked with the process mark,
10961 save those articles instead."
10962   (interactive "P")
10963   (require 'gnus-art)
10964   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10965     (gnus-summary-save-article arg)))
10966
10967 (defun gnus-summary-write-article-file (&optional arg)
10968   "Write the current article to a file, deleting the previous file.
10969 If N is a positive number, save the N next articles.
10970 If N is a negative number, save the N previous articles.
10971 If N is nil and any articles have been marked with the process mark,
10972 save those articles instead."
10973   (interactive "P")
10974   (require 'gnus-art)
10975   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10976     (gnus-summary-save-article arg)))
10977
10978 (defun gnus-summary-save-article-body-file (&optional arg)
10979   "Append the current article body to a file.
10980 If N is a positive number, save the N next articles.
10981 If N is a negative number, save the N previous articles.
10982 If N is nil and any articles have been marked with the process mark,
10983 save those articles instead."
10984   (interactive "P")
10985   (require 'gnus-art)
10986   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10987     (gnus-summary-save-article arg)))
10988
10989 (defun gnus-summary-muttprint (&optional arg)
10990   "Print the current article using Muttprint.
10991 If N is a positive number, save the N next articles.
10992 If N is a negative number, save the N previous articles.
10993 If N is nil and any articles have been marked with the process mark,
10994 save those articles instead."
10995   (interactive "P")
10996   (require 'gnus-art)
10997   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10998     (gnus-summary-save-article arg t)))
10999
11000 (defun gnus-summary-pipe-message (program)
11001   "Pipe the current article through PROGRAM."
11002   (interactive "sProgram: ")
11003   (gnus-summary-select-article)
11004   (let ((mail-header-separator ""))
11005     (gnus-eval-in-buffer-window gnus-article-buffer
11006       (save-restriction
11007         (widen)
11008         (let ((start (window-start))
11009               buffer-read-only)
11010           (message-pipe-buffer-body program)
11011           (set-window-start (get-buffer-window (current-buffer)) start))))))
11012
11013 (defun gnus-get-split-value (methods)
11014   "Return a value based on the split METHODS."
11015   (let (split-name method result match)
11016     (when methods
11017       (save-excursion
11018         (set-buffer gnus-original-article-buffer)
11019         (save-restriction
11020           (nnheader-narrow-to-headers)
11021           (while (and methods (not split-name))
11022             (goto-char (point-min))
11023             (setq method (pop methods))
11024             (setq match (car method))
11025             (when (cond
11026                    ((stringp match)
11027                     ;; Regular expression.
11028                     (ignore-errors
11029                       (re-search-forward match nil t)))
11030                    ((functionp match)
11031                     ;; Function.
11032                     (save-restriction
11033                       (widen)
11034                       (setq result (funcall match gnus-newsgroup-name))))
11035                    ((consp match)
11036                     ;; Form.
11037                     (save-restriction
11038                       (widen)
11039                       (setq result (eval match)))))
11040               (setq split-name (cdr method))
11041               (cond ((stringp result)
11042                      (push (expand-file-name
11043                             result gnus-article-save-directory)
11044                            split-name))
11045                     ((consp result)
11046                      (setq split-name (append result split-name)))))))))
11047     (nreverse split-name)))
11048
11049 (defun gnus-valid-move-group-p (group)
11050   (and (boundp group)
11051        (symbol-name group)
11052        (symbol-value group)
11053        (gnus-get-function (gnus-find-method-for-group
11054                            (symbol-name group)) 'request-accept-article t)))
11055
11056 (defun gnus-read-move-group-name (prompt default articles prefix)
11057   "Read a group name."
11058   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11059          (minibuffer-confirm-incomplete nil) ; XEmacs
11060          (prom
11061           (format "%s %s to:"
11062                   prompt
11063                   (if (> (length articles) 1)
11064                       (format "these %d articles" (length articles))
11065                     "this article")))
11066          (to-newsgroup
11067           (cond
11068            ((null split-name)
11069             (gnus-completing-read-with-default
11070              default prom
11071              gnus-active-hashtb
11072              'gnus-valid-move-group-p
11073              nil prefix
11074              'gnus-group-history))
11075            ((= 1 (length split-name))
11076             (gnus-completing-read-with-default
11077              (car split-name) prom
11078              gnus-active-hashtb
11079              'gnus-valid-move-group-p
11080              nil nil
11081              'gnus-group-history))
11082            (t
11083             (gnus-completing-read-with-default
11084              nil prom
11085              (mapcar (lambda (el) (list el))
11086                      (nreverse split-name))
11087              nil nil nil
11088              'gnus-group-history))))
11089          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11090     (when to-newsgroup
11091       (if (or (string= to-newsgroup "")
11092               (string= to-newsgroup prefix))
11093           (setq to-newsgroup default))
11094       (unless to-newsgroup
11095         (error "No group name entered"))
11096       (or (gnus-active to-newsgroup)
11097           (gnus-activate-group to-newsgroup nil nil to-method)
11098           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11099                                      to-newsgroup))
11100               (or (and (gnus-request-create-group to-newsgroup to-method)
11101                        (gnus-activate-group
11102                         to-newsgroup nil nil to-method)
11103                        (gnus-subscribe-group to-newsgroup))
11104                   (error "Couldn't create group %s" to-newsgroup)))
11105           (error "No such group: %s" to-newsgroup)))
11106     to-newsgroup))
11107
11108 (defun gnus-summary-save-parts (type dir n &optional reverse)
11109   "Save parts matching TYPE to DIR.
11110 If REVERSE, save parts that do not match TYPE."
11111   (interactive
11112    (list (read-string "Save parts of type: "
11113                       (or (car gnus-summary-save-parts-type-history)
11114                           gnus-summary-save-parts-default-mime)
11115                       'gnus-summary-save-parts-type-history)
11116          (setq gnus-summary-save-parts-last-directory
11117                (read-file-name "Save to directory: "
11118                                gnus-summary-save-parts-last-directory
11119                                nil t))
11120          current-prefix-arg))
11121   (gnus-summary-iterate n
11122     (let ((gnus-display-mime-function nil)
11123           (gnus-inhibit-treatment t))
11124       (gnus-summary-select-article))
11125     (save-excursion
11126       (set-buffer gnus-article-buffer)
11127       (let ((handles (or gnus-article-mime-handles
11128                          (mm-dissect-buffer nil gnus-article-loose-mime)
11129                          (and gnus-article-emulate-mime
11130                               (mm-uu-dissect)))))
11131         (when handles
11132           (gnus-summary-save-parts-1 type dir handles reverse)
11133           (unless gnus-article-mime-handles ;; Don't destroy this case.
11134             (mm-destroy-parts handles)))))))
11135
11136 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11137   (if (stringp (car handle))
11138       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11139               (cdr handle))
11140     (when (if reverse
11141               (not (string-match type (mm-handle-media-type handle)))
11142             (string-match type (mm-handle-media-type handle)))
11143       (let ((file (expand-file-name
11144                    (gnus-map-function
11145                     mm-file-name-rewrite-functions
11146                     (file-name-nondirectory
11147                      (or
11148                       (mail-content-type-get
11149                        (mm-handle-disposition handle) 'filename)
11150                       (mail-content-type-get
11151                        (mm-handle-type handle) 'name)
11152                       (concat gnus-newsgroup-name
11153                               "." (number-to-string
11154                                    (cdr gnus-article-current))))))
11155                    dir)))
11156         (unless (file-exists-p file)
11157           (mm-save-part-to-file handle file))))))
11158
11159 ;; Summary extract commands
11160
11161 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11162   (let ((buffer-read-only nil)
11163         (article (gnus-summary-article-number))
11164         after-article b e)
11165     (unless (gnus-summary-goto-subject article)
11166       (error "No such article: %d" article))
11167     (gnus-summary-position-point)
11168     ;; If all commands are to be bunched up on one line, we collect
11169     ;; them here.
11170     (unless gnus-view-pseudos-separately
11171       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11172             files action)
11173         (while ps
11174           (setq action (cdr (assq 'action (car ps))))
11175           (setq files (list (cdr (assq 'name (car ps)))))
11176           (while (and ps (cdr ps)
11177                       (string= (or action "1")
11178                                (or (cdr (assq 'action (cadr ps))) "2")))
11179             (push (cdr (assq 'name (cadr ps))) files)
11180             (setcdr ps (cddr ps)))
11181           (when files
11182             (when (not (string-match "%s" action))
11183               (push " " files))
11184             (push " " files)
11185             (when (assq 'execute (car ps))
11186               (setcdr (assq 'execute (car ps))
11187                       (funcall (if (string-match "%s" action)
11188                                    'format 'concat)
11189                                action
11190                                (mapconcat
11191                                 (lambda (f)
11192                                   (if (equal f " ")
11193                                       f
11194                                     (shell-quote-argument f)))
11195                                 files " ")))))
11196           (setq ps (cdr ps)))))
11197     (if (and gnus-view-pseudos (not not-view))
11198         (while pslist
11199           (when (assq 'execute (car pslist))
11200             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11201                                   (eq gnus-view-pseudos 'not-confirm)))
11202           (setq pslist (cdr pslist)))
11203       (save-excursion
11204         (while pslist
11205           (setq after-article (or (cdr (assq 'article (car pslist)))
11206                                   (gnus-summary-article-number)))
11207           (gnus-summary-goto-subject after-article)
11208           (forward-line 1)
11209           (setq b (point))
11210           (insert "    " (file-name-nondirectory
11211                           (cdr (assq 'name (car pslist))))
11212                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11213           (setq e (point))
11214           (forward-line -1)             ; back to `b'
11215           (gnus-add-text-properties
11216            b (1- e) (list 'gnus-number gnus-reffed-article-number
11217                           gnus-mouse-face-prop gnus-mouse-face))
11218           (gnus-data-enter
11219            after-article gnus-reffed-article-number
11220            gnus-unread-mark b (car pslist) 0 (- e b))
11221           (setq gnus-newsgroup-unreads
11222                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11223                                          gnus-reffed-article-number))
11224           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11225           (setq pslist (cdr pslist)))))))
11226
11227 (defun gnus-pseudos< (p1 p2)
11228   (let ((c1 (cdr (assq 'action p1)))
11229         (c2 (cdr (assq 'action p2))))
11230     (and c1 c2 (string< c1 c2))))
11231
11232 (defun gnus-request-pseudo-article (props)
11233   (cond ((assq 'execute props)
11234          (gnus-execute-command (cdr (assq 'execute props)))))
11235   (let ((gnus-current-article (gnus-summary-article-number)))
11236     (gnus-run-hooks 'gnus-mark-article-hook)))
11237
11238 (defun gnus-execute-command (command &optional automatic)
11239   (save-excursion
11240     (gnus-article-setup-buffer)
11241     (set-buffer gnus-article-buffer)
11242     (setq buffer-read-only nil)
11243     (let ((command (if automatic command
11244                      (read-string "Command: " (cons command 0)))))
11245       (erase-buffer)
11246       (insert "$ " command "\n\n")
11247       (if gnus-view-pseudo-asynchronously
11248           (start-process "gnus-execute" (current-buffer) shell-file-name
11249                          shell-command-switch command)
11250         (call-process shell-file-name nil t nil
11251                       shell-command-switch command)))))
11252
11253 ;; Summary kill commands.
11254
11255 (defun gnus-summary-edit-global-kill (article)
11256   "Edit the \"global\" kill file."
11257   (interactive (list (gnus-summary-article-number)))
11258   (gnus-group-edit-global-kill article))
11259
11260 (defun gnus-summary-edit-local-kill ()
11261   "Edit a local kill file applied to the current newsgroup."
11262   (interactive)
11263   (setq gnus-current-headers (gnus-summary-article-header))
11264   (gnus-group-edit-local-kill
11265    (gnus-summary-article-number) gnus-newsgroup-name))
11266
11267 ;;; Header reading.
11268
11269 (defun gnus-read-header (id &optional header)
11270   "Read the headers of article ID and enter them into the Gnus system."
11271   (let ((group gnus-newsgroup-name)
11272         (gnus-override-method
11273          (or
11274           gnus-override-method
11275           (and (gnus-news-group-p gnus-newsgroup-name)
11276                (car (gnus-refer-article-methods)))))
11277         where)
11278     ;; First we check to see whether the header in question is already
11279     ;; fetched.
11280     (if (stringp id)
11281         ;; This is a Message-ID.
11282         (setq header (or header (gnus-id-to-header id)))
11283       ;; This is an article number.
11284       (setq header (or header (gnus-summary-article-header id))))
11285     (if (and header
11286              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11287         ;; We have found the header.
11288         header
11289       ;; If this is a sparse article, we have to nix out its
11290       ;; previous entry in the thread hashtb.
11291       (when (and header
11292                  (gnus-summary-article-sparse-p (mail-header-number header)))
11293         (let* ((parent (gnus-parent-id (mail-header-references header)))
11294                (thread (and parent (gnus-id-to-thread parent))))
11295           (when thread
11296             (delq (assq header thread) thread))))
11297       ;; We have to really fetch the header to this article.
11298       (save-excursion
11299         (set-buffer nntp-server-buffer)
11300         (when (setq where (gnus-request-head id group))
11301           (nnheader-fold-continuation-lines)
11302           (goto-char (point-max))
11303           (insert ".\n")
11304           (goto-char (point-min))
11305           (insert "211 ")
11306           (princ (cond
11307                   ((numberp id) id)
11308                   ((cdr where) (cdr where))
11309                   (header (mail-header-number header))
11310                   (t gnus-reffed-article-number))
11311                  (current-buffer))
11312           (insert " Article retrieved.\n"))
11313         (if (or (not where)
11314                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11315             ()                          ; Malformed head.
11316           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11317             (when (and (stringp id)
11318                        (not (string= (gnus-group-real-name group)
11319                                      (car where))))
11320               ;; If we fetched by Message-ID and the article came
11321               ;; from a different group, we fudge some bogus article
11322               ;; numbers for this article.
11323               (mail-header-set-number header gnus-reffed-article-number))
11324             (save-excursion
11325               (set-buffer gnus-summary-buffer)
11326               (decf gnus-reffed-article-number)
11327               (gnus-remove-header (mail-header-number header))
11328               (push header gnus-newsgroup-headers)
11329               (setq gnus-current-headers header)
11330               (push (mail-header-number header) gnus-newsgroup-limit)))
11331           header)))))
11332
11333 (defun gnus-remove-header (number)
11334   "Remove header NUMBER from `gnus-newsgroup-headers'."
11335   (if (and gnus-newsgroup-headers
11336            (= number (mail-header-number (car gnus-newsgroup-headers))))
11337       (pop gnus-newsgroup-headers)
11338     (let ((headers gnus-newsgroup-headers))
11339       (while (and (cdr headers)
11340                   (not (= number (mail-header-number (cadr headers)))))
11341         (pop headers))
11342       (when (cdr headers)
11343         (setcdr headers (cddr headers))))))
11344
11345 ;;;
11346 ;;; summary highlights
11347 ;;;
11348
11349 (defun gnus-highlight-selected-summary ()
11350   "Highlight selected article in summary buffer."
11351   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11352   (when gnus-summary-selected-face
11353     (save-excursion
11354       (let* ((beg (point-at-bol))
11355              (end (point-at-eol))
11356              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11357              (from (if (get-text-property beg gnus-mouse-face-prop)
11358                        beg
11359                      (or (next-single-property-change
11360                           beg gnus-mouse-face-prop nil end)
11361                          beg)))
11362              (to
11363               (if (= from end)
11364                   (- from 2)
11365                 (or (next-single-property-change
11366                      from gnus-mouse-face-prop nil end)
11367                     end))))
11368         ;; If no mouse-face prop on line we will have to = from = end,
11369         ;; so we highlight the entire line instead.
11370         (when (= (+ to 2) from)
11371           (setq from beg)
11372           (setq to end))
11373         (if gnus-newsgroup-selected-overlay
11374             ;; Move old overlay.
11375             (gnus-move-overlay
11376              gnus-newsgroup-selected-overlay from to (current-buffer))
11377           ;; Create new overlay.
11378           (gnus-overlay-put
11379            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11380            'face gnus-summary-selected-face))))))
11381
11382 (defvar gnus-summary-highlight-line-cached nil)
11383 (defvar gnus-summary-highlight-line-trigger nil)
11384
11385 (defun gnus-summary-highlight-line-0 ()
11386   (if (and (eq gnus-summary-highlight-line-trigger
11387                gnus-summary-highlight)
11388            gnus-summary-highlight-line-cached)
11389       gnus-summary-highlight-line-cached
11390     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11391           gnus-summary-highlight-line-cached
11392           (let* ((cond (list 'cond))
11393                  (c cond)
11394                  (list gnus-summary-highlight))
11395             (while list
11396               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11397                               nil))
11398               (setq c (cdr c)
11399                     list (cdr list)))
11400             (gnus-byte-compile (list 'lambda nil cond))))))
11401
11402 (defun gnus-summary-highlight-line ()
11403   "Highlight current line according to `gnus-summary-highlight'."
11404   (let* ((beg (point-at-bol))
11405          (article (or (gnus-summary-article-number) gnus-current-article))
11406          (score (or (cdr (assq article
11407                                gnus-newsgroup-scored))
11408                     gnus-summary-default-score 0))
11409          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11410          (inhibit-read-only t)
11411          (default gnus-summary-default-score)
11412          (default-high gnus-summary-default-high-score)
11413          (default-low gnus-summary-default-low-score)
11414          (uncached (and gnus-summary-use-undownloaded-faces
11415                         (memq article gnus-newsgroup-undownloaded))))
11416     (let ((face (funcall (gnus-summary-highlight-line-0))))
11417       (unless (eq face (get-text-property beg 'face))
11418         (gnus-put-text-property-excluding-characters-with-faces
11419          beg (point-at-eol) 'face
11420          (setq face (if (boundp face) (symbol-value face) face)))
11421         (when gnus-summary-highlight-line-function
11422           (funcall gnus-summary-highlight-line-function article face))))))
11423
11424 (defun gnus-update-read-articles (group unread &optional compute)
11425   "Update the list of read articles in GROUP.
11426 UNREAD is a sorted list."
11427   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11428         (info (gnus-get-info group))
11429         (prev 1)
11430         read)
11431     (if (or (not info) (not active))
11432         ;; There is no info on this group if it was, in fact,
11433         ;; killed.  Gnus stores no information on killed groups, so
11434         ;; there's nothing to be done.
11435         ;; One could store the information somewhere temporarily,
11436         ;; perhaps...  Hmmm...
11437         ()
11438       ;; Remove any negative articles numbers.
11439       (while (and unread (< (car unread) 0))
11440         (setq unread (cdr unread)))
11441       ;; Remove any expired article numbers
11442       (while (and unread (< (car unread) (car active)))
11443         (setq unread (cdr unread)))
11444       ;; Compute the ranges of read articles by looking at the list of
11445       ;; unread articles.
11446       (while unread
11447         (when (/= (car unread) prev)
11448           (push (if (= prev (1- (car unread))) prev
11449                   (cons prev (1- (car unread))))
11450                 read))
11451         (setq prev (1+ (car unread)))
11452         (setq unread (cdr unread)))
11453       (when (<= prev (cdr active))
11454         (push (cons prev (cdr active)) read))
11455       (setq read (if (> (length read) 1) (nreverse read) read))
11456       (if compute
11457           read
11458         (save-excursion
11459           (let (setmarkundo)
11460             ;; Propagate the read marks to the backend.
11461             (when (gnus-check-backend-function 'request-set-mark group)
11462               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11463                     (add (gnus-remove-from-range read (gnus-info-read info))))
11464                 (when (or add del)
11465                   (unless (gnus-check-group group)
11466                     (error "Can't open server for %s" group))
11467                   (gnus-request-set-mark
11468                    group (delq nil (list (if add (list add 'add '(read)))
11469                                          (if del (list del 'del '(read))))))
11470                   (setq setmarkundo
11471                         `(gnus-request-set-mark
11472                           ,group
11473                           ',(delq nil (list
11474                                        (if del (list del 'add '(read)))
11475                                        (if add (list add 'del '(read))))))))))
11476             (set-buffer gnus-group-buffer)
11477             (gnus-undo-register
11478               `(progn
11479                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11480                  (gnus-info-set-read ',info ',(gnus-info-read info))
11481                  (gnus-get-unread-articles-in-group ',info
11482                                                     (gnus-active ,group))
11483                  (gnus-group-update-group ,group t)
11484                  ,setmarkundo))))
11485         ;; Enter this list into the group info.
11486         (gnus-info-set-read info read)
11487         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11488         (gnus-get-unread-articles-in-group info (gnus-active group))
11489         t))))
11490
11491 (defun gnus-offer-save-summaries ()
11492   "Offer to save all active summary buffers."
11493   (let (buffers)
11494     ;; Go through all buffers and find all summaries.
11495     (dolist (buffer (buffer-list))
11496       (when (and (setq buffer (buffer-name buffer))
11497                  (string-match "Summary" buffer)
11498                  (save-excursion
11499                    (set-buffer buffer)
11500                    ;; We check that this is, indeed, a summary buffer.
11501                    (and (eq major-mode 'gnus-summary-mode)
11502                         ;; Also make sure this isn't bogus.
11503                         gnus-newsgroup-prepared
11504                         ;; Also make sure that this isn't a
11505                         ;; dead summary buffer.
11506                         (not gnus-dead-summary-mode))))
11507         (push buffer buffers)))
11508     ;; Go through all these summary buffers and offer to save them.
11509     (when buffers
11510       (save-excursion
11511         (map-y-or-n-p
11512          "Update summary buffer %s? "
11513          (lambda (buf)
11514            (switch-to-buffer buf)
11515            (gnus-summary-exit))
11516          buffers)))))
11517
11518
11519 ;;; @ for mime-partial
11520 ;;;
11521
11522 (defun gnus-request-partial-message ()
11523   (save-excursion
11524     (let ((number (gnus-summary-article-number))
11525           (group gnus-newsgroup-name)
11526           (mother gnus-article-buffer))
11527       (set-buffer (get-buffer-create " *Partial Article*"))
11528       (erase-buffer)
11529       (setq mime-preview-buffer mother)
11530       (gnus-request-article-this-buffer number group)
11531       (mime-parse-buffer)
11532       )))
11533
11534 (autoload 'mime-combine-message/partial-pieces-automatically
11535   "mime-partial"
11536   "Internal method to combine message/partial messages automatically.")
11537
11538 (mime-add-condition
11539  'action '((type . message)(subtype . partial)
11540            (major-mode . gnus-original-article-mode)
11541            (method . mime-combine-message/partial-pieces-automatically)
11542            (summary-buffer-exp . gnus-summary-buffer)
11543            (request-partial-message-method . gnus-request-partial-message)
11544            ))
11545
11546
11547 ;;; @ for message/rfc822
11548 ;;;
11549
11550 (defun gnus-mime-extract-message/rfc822 (entity situation)
11551   "Burst a forwarded article."
11552   (save-excursion
11553     (set-buffer gnus-summary-buffer)
11554     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11555                                    gnus-newsgroup-name 'gnus-group-history))
11556            (gnus-group-marked (list group))
11557            article info)
11558       (with-temp-buffer
11559         (mime-insert-entity-content entity)
11560         (setq article (gnus-request-accept-article group)))
11561       (when (and (consp article)
11562                  (numberp (setq article (cdr article))))
11563         (setq info (gnus-get-info group))
11564         (gnus-info-set-read info
11565                             (gnus-remove-from-range (gnus-info-read info)
11566                                                     (list article)))
11567         (when (string-equal group gnus-newsgroup-name)
11568           (forward-line 1)
11569           (let (gnus-show-threads)
11570             (gnus-summary-goto-subject article t))
11571           (gnus-summary-clear-mark-forward 1))
11572         (set-buffer gnus-group-buffer)
11573         (gnus-group-get-new-news-this-group nil t)))))
11574
11575 (mime-add-condition
11576  'action '((type . message)(subtype . rfc822)
11577            (major-mode . gnus-original-article-mode)
11578            (method . gnus-mime-extract-message/rfc822)
11579            (mode . "extract")
11580            ))
11581
11582 (mime-add-condition
11583  'action '((type . message)(subtype . news)
11584            (major-mode . gnus-original-article-mode)
11585            (method . gnus-mime-extract-message/rfc822)
11586            (mode . "extract")
11587            ))
11588
11589 (defun gnus-mime-extract-multipart (entity situation)
11590   (let ((children (mime-entity-children entity))
11591         mime-acting-situation-to-override
11592         f)
11593     (while children
11594       (mime-play-entity (car children)
11595                         (cons (assq 'mode situation)
11596                               mime-acting-situation-to-override))
11597       (setq children (cdr children)))
11598     (if (setq f (cdr (assq 'after-method
11599                            mime-acting-situation-to-override)))
11600         (eval f)
11601       )))
11602
11603 (mime-add-condition
11604  'action '((type . multipart)
11605            (method . gnus-mime-extract-multipart)
11606            (mode . "extract")
11607            )
11608  'with-default)
11609
11610
11611 ;;; @ end
11612 ;;;
11613
11614 (defun gnus-summary-inherit-default-charset ()
11615   "Import `default-mime-charset' from summary buffer.
11616 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11617 of FLIM is used."
11618   (if (buffer-live-p gnus-summary-buffer)
11619       (let (d-m-c d-m-c-u)
11620         (with-current-buffer gnus-summary-buffer
11621           (setq d-m-c (if (local-variable-p 'default-mime-charset
11622                                             gnus-summary-buffer)
11623                           default-mime-charset
11624                         t)
11625                 ;; LIMIT
11626                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11627                                               gnus-summary-buffer)
11628                             (symbol-value 'default-mime-charset-unlimited)
11629                           t)))
11630         (if (eq t d-m-c)
11631             (kill-local-variable 'default-mime-charset)
11632           (set (make-local-variable 'default-mime-charset) d-m-c))
11633         (if (eq t d-m-c-u)
11634             (kill-local-variable 'default-mime-charset-unlimited)
11635           (set (make-local-variable 'default-mime-charset-unlimited)
11636                d-m-c-u)))))
11637
11638 (defun gnus-summary-setup-default-charset ()
11639   "Setup newsgroup default charset."
11640   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11641       (progn
11642         (setq gnus-newsgroup-charset nil)
11643         (set (make-local-variable 'default-mime-charset) nil)
11644         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11645           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11646     (let ((ignored-charsets
11647            (or gnus-newsgroup-ephemeral-ignored-charsets
11648                (append
11649                 (and gnus-newsgroup-name
11650                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11651                 gnus-newsgroup-ignored-charsets)))
11652           charset)
11653       (setq gnus-newsgroup-charset
11654             (or gnus-newsgroup-ephemeral-charset
11655                 (when (and gnus-newsgroup-name
11656                            (setq charset (gnus-parameter-charset
11657                                           gnus-newsgroup-name)))
11658                   (make-local-variable 'default-mime-charset)
11659                   (setq default-mime-charset charset))
11660                 gnus-default-charset))
11661       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11662            ignored-charsets))))
11663
11664 ;;;
11665 ;;; Mime Commands
11666 ;;;
11667
11668 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11669   "Display the current article buffer fully MIME-buttonized.
11670 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11671 treated as multipart/mixed."
11672   (interactive "P")
11673   (require 'gnus-art)
11674   (let ((gnus-unbuttonized-mime-types nil)
11675         (gnus-mime-display-multipart-as-mixed show-all-parts))
11676     (gnus-summary-show-article)))
11677
11678 (defun gnus-summary-repair-multipart (article)
11679   "Add a Content-Type header to a multipart article without one."
11680   (interactive (list (gnus-summary-article-number)))
11681   (gnus-with-article article
11682     (message-narrow-to-head)
11683     (message-remove-header "Mime-Version")
11684     (goto-char (point-max))
11685     (insert "Mime-Version: 1.0\n")
11686     (widen)
11687     (when (search-forward "\n--" nil t)
11688       (let ((separator (buffer-substring (point) (point-at-eol))))
11689         (message-narrow-to-head)
11690         (message-remove-header "Content-Type")
11691         (goto-char (point-max))
11692         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11693                         separator))
11694         (widen))))
11695   (let (gnus-mark-article-hook)
11696     (gnus-summary-select-article t t nil article)))
11697
11698 (defun gnus-summary-toggle-display-buttonized ()
11699   "Toggle the buttonizing of the article buffer."
11700   (interactive)
11701   (require 'gnus-art)
11702   (if (setq gnus-inhibit-mime-unbuttonizing
11703             (not gnus-inhibit-mime-unbuttonizing))
11704       (let ((gnus-unbuttonized-mime-types nil))
11705         (gnus-summary-show-article))
11706     (gnus-summary-show-article)))
11707
11708 ;;;
11709 ;;; Intelli-mouse commmands
11710 ;;;
11711
11712 (defun gnus-wheel-summary-scroll (event)
11713   (interactive "e")
11714   (let ((amount (if (memq 'shift (event-modifiers event))
11715                     (car gnus-wheel-scroll-amount)
11716                   (cdr gnus-wheel-scroll-amount)))
11717         (direction (- (* (static-if (featurep 'xemacs)
11718                              (event-button event)
11719                            (cond ((eq 'mouse-4 (event-basic-type event))
11720                                   4)
11721                                  ((eq 'mouse-5 (event-basic-type event))
11722                                   5)))
11723                          2) 9))
11724         edge)
11725     (gnus-summary-scroll-up (* amount direction))
11726     (when (gnus-eval-in-buffer-window gnus-article-buffer
11727             (save-restriction
11728               (widen)
11729               (and (if (< 0 direction)
11730                        (gnus-article-next-page 0)
11731                      (gnus-article-prev-page 0)
11732                      (bobp))
11733                    (if (setq edge (get-text-property
11734                                    (point-min) 'gnus-wheel-edge))
11735                        (setq edge (* edge direction))
11736                      (setq edge -1))
11737                    (or (plusp edge)
11738                        (let ((buffer-read-only nil)
11739                              (inhibit-read-only t))
11740                          (put-text-property (point-min) (point-max)
11741                                             'gnus-wheel-edge direction)
11742                          nil))
11743                    (or (> edge gnus-wheel-edge-resistance)
11744                        (let ((buffer-read-only nil)
11745                              (inhibit-read-only t))
11746                          (put-text-property (point-min) (point-max)
11747                                             'gnus-wheel-edge
11748                                             (* (1+ edge) direction))
11749                          nil))
11750                    (eq last-command 'gnus-wheel-summary-scroll))))
11751       (gnus-summary-next-article nil nil (minusp direction)))))
11752
11753 (defun gnus-wheel-install ()
11754   "Enable mouse wheel support on summary window."
11755   (when gnus-use-wheel
11756     (let ((keys
11757            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11758       (dolist (key keys)
11759         (define-key gnus-summary-mode-map key
11760           'gnus-wheel-summary-scroll)))))
11761
11762 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11763
11764 ;;;
11765 ;;; Traditional PGP commmands
11766 ;;;
11767
11768 (defun gnus-summary-decrypt-article (&optional force)
11769   "Decrypt the current article in traditional PGP way.
11770 This will have permanent effect only in mail groups.
11771 If FORCE is non-nil, allow editing of articles even in read-only
11772 groups."
11773   (interactive "P")
11774   (gnus-summary-select-article t)
11775   (gnus-eval-in-buffer-window gnus-article-buffer
11776     (save-excursion
11777       (save-restriction
11778         (widen)
11779         (goto-char (point-min))
11780         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11781           (error "Not a traditional PGP message!"))
11782         (let ((armor-start (match-beginning 0)))
11783           (if (and (pgg-decrypt-region armor-start (point-max))
11784                    (or force (not (gnus-group-read-only-p))))
11785               (let ((inhibit-read-only t)
11786                     buffer-read-only)
11787                 (delete-region armor-start
11788                                (progn
11789                                  (re-search-forward "^-+END PGP" nil t)
11790                                  (beginning-of-line 2)
11791                                  (point)))
11792                 (insert-buffer-substring pgg-output-buffer))))))))
11793
11794 (defun gnus-summary-verify-article ()
11795   "Verify the current article in traditional PGP way."
11796   (interactive)
11797   (save-excursion
11798     (set-buffer gnus-original-article-buffer)
11799     (goto-char (point-min))
11800     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11801       (error "Not a traditional PGP message!"))
11802     (re-search-forward "^-+END PGP" nil t)
11803     (beginning-of-line 2)
11804     (call-interactively (function pgg-verify-region))))
11805
11806 ;;;
11807 ;;; Generic summary marking commands
11808 ;;;
11809
11810 (defvar gnus-summary-marking-alist
11811   '((read gnus-del-mark "d")
11812     (unread gnus-unread-mark "u")
11813     (ticked gnus-ticked-mark "!")
11814     (dormant gnus-dormant-mark "?")
11815     (expirable gnus-expirable-mark "e"))
11816   "An alist of names/marks/keystrokes.")
11817
11818 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11819 (defvar gnus-summary-mark-map)
11820
11821 (defun gnus-summary-make-all-marking-commands ()
11822   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11823   (dolist (elem gnus-summary-marking-alist)
11824     (apply 'gnus-summary-make-marking-command elem)))
11825
11826 (defun gnus-summary-make-marking-command (name mark keystroke)
11827   (let ((map (make-sparse-keymap)))
11828     (define-key gnus-summary-generic-mark-map keystroke map)
11829     (dolist (lway `((next "next" next nil "n")
11830                     (next-unread "next unread" next t "N")
11831                     (prev "previous" prev nil "p")
11832                     (prev-unread "previous unread" prev t "P")
11833                     (nomove "" nil nil ,keystroke)))
11834       (let ((func (gnus-summary-make-marking-command-1
11835                    mark (car lway) lway name)))
11836         (setq func (eval func))
11837         (define-key map (nth 4 lway) func)))))
11838
11839 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11840   `(defun ,(intern
11841             (format "gnus-summary-put-mark-as-%s%s"
11842                     name (if (eq way 'nomove)
11843                              ""
11844                            (concat "-" (symbol-name way)))))
11845      (n)
11846      ,(format
11847        "Mark the current article as %s%s.
11848 If N, the prefix, then repeat N times.
11849 If N is negative, move in reverse order.
11850 The difference between N and the actual number of articles marked is
11851 returned."
11852        name (car (cdr lway)))
11853      (interactive "p")
11854      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11855
11856 (defun gnus-summary-generic-mark (n mark move unread)
11857   "Mark N articles with MARK."
11858   (unless (eq major-mode 'gnus-summary-mode)
11859     (error "This command can only be used in the summary buffer"))
11860   (gnus-summary-show-thread)
11861   (let ((nummove
11862          (cond
11863           ((eq move 'next) 1)
11864           ((eq move 'prev) -1)
11865           (t 0))))
11866     (if (zerop nummove)
11867         (setq n 1)
11868       (when (< n 0)
11869         (setq n (abs n)
11870               nummove (* -1 nummove))))
11871     (while (and (> n 0)
11872                 (gnus-summary-mark-article nil mark)
11873                 (zerop (gnus-summary-next-subject nummove unread t)))
11874       (setq n (1- n)))
11875     (when (/= 0 n)
11876       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11877     (gnus-summary-recenter)
11878     (gnus-summary-position-point)
11879     (gnus-set-mode-line 'summary)
11880     n))
11881
11882 (defun gnus-summary-insert-articles (articles)
11883   (when (setq articles
11884               (gnus-sorted-difference articles
11885                                       (mapcar (lambda (h)
11886                                                 (mail-header-number h))
11887                                               gnus-newsgroup-headers)))
11888     (setq gnus-newsgroup-headers
11889           (gnus-merge 'list
11890                       gnus-newsgroup-headers
11891                       (gnus-fetch-headers articles)
11892                       'gnus-article-sort-by-number))
11893     ;; Suppress duplicates?
11894     (when gnus-suppress-duplicates
11895       (gnus-dup-suppress-articles))
11896
11897     ;; We might want to build some more threads first.
11898     (when (and gnus-fetch-old-headers
11899                (eq gnus-headers-retrieved-by 'nov))
11900       (if (eq gnus-fetch-old-headers 'invisible)
11901           (gnus-build-all-threads)
11902         (gnus-build-old-threads)))
11903     ;; Let the Gnus agent mark articles as read.
11904     (when gnus-agent
11905       (gnus-agent-get-undownloaded-list))
11906     ;; Remove list identifiers from subject
11907     (when gnus-list-identifiers
11908       (gnus-summary-remove-list-identifiers))
11909     ;; First and last article in this newsgroup.
11910     (when gnus-newsgroup-headers
11911       (setq gnus-newsgroup-begin
11912             (mail-header-number (car gnus-newsgroup-headers))
11913             gnus-newsgroup-end
11914             (mail-header-number
11915              (gnus-last-element gnus-newsgroup-headers))))
11916     (when gnus-use-scoring
11917       (gnus-possibly-score-headers))))
11918
11919 (defun gnus-summary-insert-old-articles (&optional all)
11920   "Insert all old articles in this group.
11921 If ALL is non-nil, already read articles become readable.
11922 If ALL is a number, fetch this number of articles."
11923   (interactive "P")
11924   (prog1
11925       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11926             older len)
11927         (setq older
11928               ;; Some nntp servers lie about their active range.  When
11929               ;; this happens, the active range can be in the millions.
11930               ;; Use a compressed range to avoid creating a huge list.
11931               (gnus-range-difference (list gnus-newsgroup-active) old))
11932         (setq len (gnus-range-length older))
11933         (cond
11934          ((null older) nil)
11935          ((numberp all)
11936           (if (< all len)
11937               (let ((older-range (nreverse older)))
11938                 (setq older nil)
11939
11940                 (while (> all 0)
11941                   (let* ((r (pop older-range))
11942                          (min (if (numberp r) r (car r)))
11943                          (max (if (numberp r) r (cdr r))))
11944                     (while (and (<= min max)
11945                                 (> all 0))
11946                       (push max older)
11947                       (setq all (1- all)
11948                             max (1- max))))))
11949             (setq older (gnus-uncompress-range older))))
11950          (all
11951           (setq older (gnus-uncompress-range older)))
11952          (t
11953           (when (and (numberp gnus-large-newsgroup)
11954                    (> len gnus-large-newsgroup))
11955               (let* ((cursor-in-echo-area nil)
11956                      (initial (gnus-parameter-large-newsgroup-initial
11957                                gnus-newsgroup-name))
11958                      (input
11959                       (read-string
11960                        (format
11961                         "How many articles from %s (%s %d): "
11962                         (gnus-limit-string
11963                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11964                         (if initial "max" "default")
11965                         len)
11966                        (if initial
11967                            (cons (number-to-string initial)
11968                                  0)))))
11969                 (unless (string-match "^[ \t]*$" input)
11970                   (setq all (string-to-number input))
11971                   (if (< all len)
11972                       (let ((older-range (nreverse older)))
11973                         (setq older nil)
11974
11975                         (while (> all 0)
11976                           (let* ((r (pop older-range))
11977                                  (min (if (numberp r) r (car r)))
11978                                  (max (if (numberp r) r (cdr r))))
11979                             (while (and (<= min max)
11980                                         (> all 0))
11981                               (push max older)
11982                               (setq all (1- all)
11983                                     max (1- max))))))))))
11984           (setq older (gnus-uncompress-range older))))
11985         (if (not older)
11986             (message "No old news.")
11987           (gnus-summary-insert-articles older)
11988           (gnus-summary-limit (gnus-sorted-nunion old older))))
11989     (gnus-summary-position-point)))
11990
11991 (defun gnus-summary-insert-new-articles ()
11992   "Insert all new articles in this group."
11993   (interactive)
11994   (prog1
11995       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11996             (old-active gnus-newsgroup-active)
11997             (nnmail-fetched-sources (list t))
11998             i new)
11999         (setq gnus-newsgroup-active
12000               (gnus-activate-group gnus-newsgroup-name 'scan))
12001         (setq i (cdr gnus-newsgroup-active))
12002         (while (> i (cdr old-active))
12003           (push i new)
12004           (decf i))
12005         (if (not new)
12006             (message "No gnus is bad news")
12007           (gnus-summary-insert-articles new)
12008           (setq gnus-newsgroup-unreads
12009                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12010           (gnus-summary-limit (gnus-sorted-nunion old new))))
12011     (gnus-summary-position-point)))
12012
12013 (gnus-summary-make-all-marking-commands)
12014
12015 (gnus-ems-redefine)
12016
12017 (provide 'gnus-sum)
12018
12019 (run-hooks 'gnus-sum-load-hook)
12020
12021 ;; Local Variables:
12022 ;; coding: iso-8859-1
12023 ;; End:
12024
12025 ;;; gnus-sum.el ends here